author | Claus Gittinger <cg@exept.de> |
Sat, 06 Sep 1997 19:32:01 +0200 | |
changeset 1894 | 9982e96dff75 |
parent 1803 | 1d27b31ed478 |
child 1936 | 6943714d3ab2 |
permissions | -rw-r--r-- |
5 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
72 | 3 |
All Rights Reserved |
5 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
0 | 12 |
|
13 |
Dictionary subclass:#ResourcePack |
|
684 | 14 |
instanceVariableNames:'elements dependents packsClassName fileReadFailed' |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
15 |
classVariableNames:'Packs' |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
16 |
poolDictionaries:'' |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
17 |
category:'Interface-Support' |
0 | 18 |
! |
19 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
20 |
!ResourcePack class methodsFor:'documentation'! |
46 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
72 | 25 |
All Rights Reserved |
5 | 26 |
|
46 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
5 | 35 |
|
46 | 36 |
documentation |
37 |
" |
|
38 |
to allow easy customization of smalltalk code (i.e. internationalization) |
|
50 | 39 |
(replaces the obsolete Resource class). |
145 | 40 |
ResourcePacks are class specific, meaning that every subclass of View |
41 |
and ApplicationModel has an instance of ResourcePack (called resources) |
|
42 |
which is created when the first instance of the view is created. |
|
54 | 43 |
|
50 | 44 |
The resourcePack consists of a mapping from strings to values, which are |
45 |
then used in labels, buttons, menus etc. |
|
46 |
The resourcePack initializes itself from a file found in 'resources/<className>.rs', |
|
47 |
where 'className' is built by the usual abbreviation mechanism (see abbrev-files). |
|
48 |
Conditional mappings are possible, by including lines as: |
|
612 | 49 |
#if <expression> |
50 |
#endif |
|
50 | 51 |
in the resourcefile. Example: |
52 |
file 'foo.rs': |
|
612 | 53 |
#if Language == #german |
54 |
'abort' 'Abbruch' |
|
55 |
#endif |
|
56 |
#if Language == #french |
|
57 |
'abort' 'canceller' |
|
58 |
#endif |
|
145 | 59 |
|
60 |
the corresponding resource-strings are accessed (from methods within the class) |
|
61 |
using: |
|
62 |
||
612 | 63 |
resource string:'abort' |
145 | 64 |
|
54 | 65 |
returning the mapped string (i.e. 'Abbruch' if the global Language is set |
66 |
to #german).. |
|
145 | 67 |
|
54 | 68 |
If no corresponding entry is found in the resources, the key is returned. |
69 |
Translations can also include arguments, such as: |
|
612 | 70 |
resources string:'really delete %1' with:fileName |
145 | 71 |
|
72 |
This scheme has the advantage, that you can write your programs using your |
|
73 |
native language strings. Later, when new languages are to be supported, |
|
74 |
simply create a resource file for the class and add translations for |
|
75 |
all those strings. (find the keys by looking at users of resource or senders |
|
76 |
of 'string:*'. |
|
77 |
||
78 |
More languages can be added later without any change in the code, |
|
79 |
recompilation or the like. |
|
80 |
Even by people without access to the source code i.e. which only have the |
|
81 |
applications binary. |
|
82 |
||
83 |
Also, unsupported languages' strings are simply left unchanged - if you |
|
84 |
write your application in (say) english, and only plan to use it in english, |
|
85 |
no additional work is required (i.e you dont need a resource file then). |
|
86 |
Strings for unknown languages will come in english |
|
87 |
(which is better than nothing or empty button labels ;-) |
|
88 |
||
89 |
Summary: |
|
612 | 90 |
in subclasses of View and ApplicationModel, |
91 |
instead of writing: |
|
145 | 92 |
|
612 | 93 |
... |
94 |
b := Button label:'press me' |
|
95 |
... |
|
145 | 96 |
|
612 | 97 |
always write: |
98 |
||
99 |
... |
|
100 |
b := Button label:(resources string:'press me') |
|
101 |
... |
|
145 | 102 |
|
612 | 103 |
if your class is not a subclass of one of the above, AND you need |
104 |
resource translations, you wont inherit the resources variable |
|
105 |
(which is automatically initialized). |
|
106 |
In this case, you have to ask the ResourcePack class explicitely for |
|
107 |
a corresponding package: |
|
145 | 108 |
|
612 | 109 |
ResourcePack for:aClassName |
145 | 110 |
|
612 | 111 |
as an example, see how the Date class gets the national names of |
112 |
week & monthnames. |
|
145 | 113 |
|
612 | 114 |
[author:] |
115 |
Claus Gittinger |
|
145 | 116 |
" |
117 |
! |
|
118 |
||
119 |
examples |
|
120 |
" |
|
121 |
normally, resources are found in files named after their classes sourcefile |
|
122 |
For example, the FileBrowsers resources are found in 'FBrowser.rs'. |
|
123 |
For the examples below, we process resources from a constant string; |
|
620 | 124 |
this is NOT representative. |
125 |
[exBegin] |
|
126 |
|stream res| |
|
145 | 127 |
|
620 | 128 |
stream := ReadStream on:' |
145 | 129 |
foo ''the translation for foo'' |
130 |
#if Language == #german |
|
131 |
bar ''die deutsche uebersetzung von bar'' |
|
132 |
baz ''baz hat den Wert %1'' |
|
133 |
#endif |
|
134 |
#if Language == #french |
|
135 |
bar ''bar en francaise'' |
|
136 |
baz ''%1, c''''est baz'' |
|
137 |
#endif |
|
138 |
||
139 |
'. |
|
140 |
||
620 | 141 |
res := ResourcePack new readFromResourceStream:stream in:nil. |
145 | 142 |
|
697 | 143 |
Transcript showCR:'baz is translated to: ' , (res string:'baz' with:'1234'). |
144 |
Transcript showCR:'bar is translated to: ' , (res string:'bar'). |
|
145 |
Transcript showCR:'foo is translated to: ' , (res string:'foo'). |
|
146 |
Transcript showCR:'fooBar is translated to: ' , (res string:'fooBar'). |
|
620 | 147 |
[exEnd] |
145 | 148 |
set the Language to french: |
620 | 149 |
[exBegin] |
150 |
Language := #french |
|
151 |
[exEnd] |
|
145 | 152 |
and repeat the above. |
620 | 153 |
back to english: |
154 |
[exBegin] |
|
155 |
Language := #english |
|
156 |
[exEnd] |
|
157 |
back to german: |
|
158 |
[exBegin] |
|
159 |
Language := #german |
|
160 |
[exEnd] |
|
46 | 161 |
" |
162 |
! ! |
|
5 | 163 |
|
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
164 |
!ResourcePack class methodsFor:'initialization'! |
0 | 165 |
|
72 | 166 |
flushCachedResourcePacks |
24 | 167 |
"forget all cached resources - needed after a style change" |
168 |
||
169 |
Packs := nil. |
|
170 |
self initialize |
|
171 |
||
72 | 172 |
"ResourcePack flushCachedResourcePacks" |
244 | 173 |
! |
174 |
||
175 |
initialize |
|
176 |
Packs isNil ifTrue:[ |
|
177 |
Packs := WeakArray new:30 |
|
178 |
]. |
|
179 |
||
180 |
"ResourcePack initialize" |
|
181 |
! ! |
|
182 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
183 |
!ResourcePack class methodsFor:'instance creation'! |
244 | 184 |
|
185 |
for:aClass |
|
186 |
"get the full resource definitions for aClass (i.e. with super packs). |
|
187 |
Also leave the resulting pack in the cache for faster access next time." |
|
188 |
||
189 |
|nm pack superPack| |
|
190 |
||
191 |
nm := aClass name. |
|
192 |
pack := self searchCacheFor:nm. |
|
193 |
pack notNil ifTrue:[^ pack]. |
|
194 |
||
195 |
pack := self fromFile:(Smalltalk fileNameForClass:nm) , '.rs'. |
|
196 |
aClass == Object ifFalse:[ |
|
197 |
superPack := self for:(aClass superclass). |
|
198 |
pack := pack merge:superPack |
|
199 |
]. |
|
200 |
pack packsClassName:nm. |
|
201 |
self addToCache:pack. |
|
202 |
^ pack |
|
203 |
||
204 |
" |
|
205 |
ResourcePack for:TextView |
|
206 |
ResourcePack for:CodeView |
|
207 |
ResourcePack for:Workspace |
|
208 |
Workspace classResources |
|
209 |
" |
|
210 |
! |
|
211 |
||
212 |
fromFile:aFileName |
|
213 |
"get the resource definitions from a file in the default directory. |
|
214 |
Uncached low-level entry." |
|
215 |
||
216 |
^ self fromFile:aFileName directory:'resources' |
|
217 |
||
218 |
" |
|
219 |
ResourcePack fromFile:'SBrowser.rs' |
|
220 |
ResourcePack fromFile:'FBrowser.rs' |
|
221 |
ResourcePack fromFile:'Smalltalk.rs' |
|
222 |
" |
|
223 |
! |
|
224 |
||
225 |
fromFile:aFileName directory:dirName |
|
226 |
"get the resource definitions from a file in a directory. |
|
227 |
Uncached low-level entry." |
|
228 |
||
229 |
|newPack| |
|
230 |
||
231 |
newPack := self new. |
|
232 |
newPack readFromFile:aFileName directory:dirName. |
|
233 |
^ newPack |
|
0 | 234 |
! ! |
235 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
236 |
!ResourcePack class methodsFor:'private'! |
54 | 237 |
|
238 |
addToCache:aPack |
|
239 |
|idx| |
|
0 | 240 |
|
54 | 241 |
idx := Packs identityIndexOf:nil. |
242 |
idx == 0 ifTrue:[ |
|
1078
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
243 |
idx := Packs identityIndexOf:0 |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
244 |
]. |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
245 |
idx == 0 ifTrue:[ |
72 | 246 |
" |
247 |
throw away oldest |
|
248 |
" |
|
249 |
idx := Packs size. |
|
250 |
Packs replaceFrom:1 to:idx-1 with:Packs startingAt:2. |
|
54 | 251 |
]. |
252 |
Packs at:idx put:aPack |
|
253 |
! |
|
0 | 254 |
|
54 | 255 |
searchCacheFor:aClassname |
998 | 256 |
|sz "{ Class: SmallInteger }" | |
0 | 257 |
|
24 | 258 |
Packs isNil ifTrue:[ |
72 | 259 |
self initialize. |
260 |
^ nil |
|
0 | 261 |
]. |
54 | 262 |
|
263 |
sz := Packs size. |
|
264 |
1 to:sz do:[:idx | |
|
72 | 265 |
|aPack| |
54 | 266 |
|
72 | 267 |
aPack := Packs at:idx. |
1078
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
268 |
(aPack notNil |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
269 |
and:[aPack ~~ 0]) ifTrue:[ |
72 | 270 |
aClassname = aPack packsClassName ifTrue:[ |
271 |
" |
|
272 |
bring to end for LRU |
|
273 |
" |
|
274 |
Packs replaceFrom:idx to:sz-1 with:Packs startingAt:idx+1. |
|
275 |
Packs at:sz put:aPack. |
|
276 |
^ aPack |
|
277 |
] |
|
278 |
] |
|
0 | 279 |
]. |
54 | 280 |
^ nil |
123 | 281 |
|
282 |
" |
|
283 |
ResourcePack searchCacheFor:'TextView' |
|
284 |
" |
|
54 | 285 |
! ! |
286 |
||
0 | 287 |
!ResourcePack methodsFor:'accessing'! |
288 |
||
244 | 289 |
array:anArray |
290 |
"translate a collection of strings" |
|
0 | 291 |
|
244 | 292 |
^ anArray collect:[:r | self at:r default:r] |
0 | 293 |
! |
294 |
||
2 | 295 |
at:aKey |
123 | 296 |
"translate a string" |
297 |
||
72 | 298 |
^ self at:aKey ifAbsent:aKey |
0 | 299 |
! |
300 |
||
301 |
at:aKey default:default |
|
123 | 302 |
"translate a string" |
303 |
||
72 | 304 |
^ self at:aKey ifAbsent:default |
0 | 305 |
! |
306 |
||
244 | 307 |
dependents |
308 |
^ dependents |
|
309 |
! |
|
310 |
||
311 |
dependents:aCollection |
|
312 |
dependents := aCollection |
|
313 |
! |
|
314 |
||
2 | 315 |
name:aKey |
123 | 316 |
"translate a string" |
317 |
||
72 | 318 |
^ self at:aKey ifAbsent:aKey |
2 | 319 |
! |
320 |
||
321 |
name:aKey default:default |
|
123 | 322 |
"translate a string" |
323 |
||
72 | 324 |
^ self at:aKey ifAbsent:default |
2 | 325 |
! |
326 |
||
244 | 327 |
packsClassName |
328 |
^ packsClassName |
|
329 |
! |
|
123 | 330 |
|
244 | 331 |
packsClassName:aString |
332 |
packsClassName := aString |
|
0 | 333 |
! |
334 |
||
335 |
string:s |
|
123 | 336 |
"translate a string" |
337 |
||
72 | 338 |
^ self at:s ifAbsent:s |
0 | 339 |
! |
340 |
||
341 |
string:s with:arg |
|
123 | 342 |
"translate and expand arg" |
343 |
||
0 | 344 |
^ self string:s withArgs:(Array with:arg) |
345 |
! |
|
346 |
||
347 |
string:s with:arg1 with:arg2 |
|
123 | 348 |
"translate and expand args" |
349 |
||
0 | 350 |
^ self string:s withArgs:(Array with:arg1 with:arg2) |
351 |
! |
|
352 |
||
292
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
353 |
string:s with:arg1 with:arg2 with:arg3 |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
354 |
"translate and expand args" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
355 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
356 |
^ self string:s withArgs:(Array with:arg1 with:arg2 with:arg3) |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
357 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
358 |
"Created: 9.12.1995 / 19:08:50 / cg" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
359 |
! |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
360 |
|
1042 | 361 |
string:s with:arg1 with:arg2 with:arg3 with:arg4 |
362 |
"translate and expand args" |
|
363 |
||
364 |
^ self string:s withArgs:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
365 |
||
366 |
"Created: 9.9.1996 / 18:52:14 / cg" |
|
367 |
! |
|
368 |
||
0 | 369 |
string:s withArgs:argArray |
123 | 370 |
"translate and expand args" |
371 |
||
135 | 372 |
|template| |
0 | 373 |
|
374 |
template := self at:s ifAbsent:[s]. |
|
135 | 375 |
^ template expandPlaceholdersWith:argArray |
0 | 376 |
! ! |
377 |
||
378 |
!ResourcePack methodsFor:'file reading'! |
|
379 |
||
684 | 380 |
fileReadFailed |
687 | 381 |
^ fileReadFailed ~~ false |
684 | 382 |
|
383 |
"Created: 14.5.1996 / 10:19:26 / cg" |
|
687 | 384 |
"Modified: 14.5.1996 / 12:00:59 / cg" |
684 | 385 |
! |
386 |
||
1894
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
387 |
nonexistingFileRead |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
388 |
"asked to read definitions for a non-existing file. |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
389 |
Here, this is legal and ignored (typically using inherited resources). |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
390 |
However, subclasses (such as styleSheet) may flag it as an error." |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
391 |
|
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
392 |
^ nil |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
393 |
|
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
394 |
"Created: 6.9.1997 / 11:38:53 / cg" |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
395 |
! |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
396 |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
397 |
processLine:aLine encoding:encoding |
145 | 398 |
"process a single valid line (i.e. #ifdef & #include has already been processed)" |
399 |
||
144 | 400 |
|lineString name stream l rest macroName value| |
401 |
||
145 | 402 |
lineString := aLine withoutSeparators. |
144 | 403 |
name := nil. |
404 |
(lineString at:1) == $' ifTrue:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
405 |
stream := ReadStream on:lineString. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
406 |
name := String |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
407 |
readFrom:stream |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
408 |
onError:[('ResourcePack [warning]: invalid line <',lineString,'>') errorPrintCR. nil]. |
158 | 409 |
"/ OLD: l := stream position |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
410 |
l := stream position + 1. |
144 | 411 |
|
412 |
"/ l := lineString indexOf:$' startingAt:2. |
|
413 |
"/ l ~~ 0 ifTrue:[ |
|
414 |
"/ name := (lineString copyFrom:2 to:l-1). |
|
415 |
"/ l := l + 1 |
|
416 |
"/ ] |
|
417 |
] ifFalse:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
418 |
l := lineString indexOfSeparatorStartingAt:1. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
419 |
l ~~ 0 ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
420 |
name := lineString copyFrom:1 to:l-1. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
421 |
] |
144 | 422 |
]. |
423 |
name notNil ifTrue:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
424 |
rest := (lineString copyFrom:l) withoutSeparators. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
425 |
" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
426 |
skip <type> if present |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
427 |
" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
428 |
(rest startsWith:$<) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
429 |
l := lineString indexOf:$> startingAt:l. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
430 |
rest := (lineString copyFrom:l+1) withoutSeparators. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
431 |
]. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
432 |
(rest startsWith:$=) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
433 |
rest := rest copyFrom:2. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
434 |
stream := ReadStream on:rest. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
435 |
macroName := stream nextAlphaNumericWord. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
436 |
rest := stream upToEnd. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
437 |
value := self at:macroName. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
438 |
value := Compiler evaluate:('self ' , rest) |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
439 |
receiver:value |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
440 |
notifying:nil |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
441 |
compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
442 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
443 |
value := Compiler evaluate:rest compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
444 |
(value == #Error) ifTrue:[ |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
445 |
Transcript show:('ResourcePack: error in resource:' , name). |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
446 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
447 |
encoding notNil ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
448 |
value isString ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
449 |
value := value decodeFrom:encoding |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
450 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
451 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
452 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
453 |
]. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
454 |
self at:name put:value. |
144 | 455 |
] |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
456 |
|
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
457 |
"Created: 26.2.1996 / 19:17:07 / cg" |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
458 |
"Modified: 10.1.1997 / 18:05:06 / cg" |
144 | 459 |
! |
460 |
||
244 | 461 |
readFromFile:fileName directory:dirName |
462 |
"read definitions from a file in a directory" |
|
463 |
||
464 |
|inStream ok| |
|
465 |
||
686 | 466 |
fileReadFailed := false. |
244 | 467 |
dirName = 'resources' ifTrue:[ |
684 | 468 |
inStream := Smalltalk resourceFileStreamFor:fileName |
244 | 469 |
] ifFalse:[ |
684 | 470 |
inStream := Smalltalk systemFileStreamFor:(dirName , '/' , fileName). |
244 | 471 |
]. |
472 |
inStream isNil ifTrue:[ |
|
684 | 473 |
" |
474 |
an empty pack |
|
475 |
" |
|
1894
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
476 |
^ self nonexistingFileRead |
244 | 477 |
]. |
478 |
ok := self readFromResourceStream:inStream in:dirName. |
|
479 |
inStream close. |
|
686 | 480 |
|
244 | 481 |
ok ifFalse:[ |
684 | 482 |
fileReadFailed := true. |
483 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
484 |
('ResourcePack [warning]: ''' , fileName , ''' contains error(s) - data may be incomplete.') errorPrintCR. |
244 | 485 |
]. |
486 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
487 |
"Modified: 10.1.1997 / 18:05:17 / cg" |
244 | 488 |
! |
489 |
||
72 | 490 |
readFromResourceStream:inStream in:dirName |
145 | 491 |
"read definitions from a stream. THe dirName argument is required to |
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
492 |
specify where #include files are searched for. |
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
493 |
Return true, if the style sheet could be read without errors, false otherwise." |
145 | 494 |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
495 |
|lineString l lNo rest value ifLevel skipping first ok encoding| |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
496 |
|
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
497 |
encoding := nil. |
0 | 498 |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
499 |
ok := true. |
0 | 500 |
ifLevel := 0. |
501 |
skipping := false. |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
502 |
lNo := 0. |
0 | 503 |
[inStream atEnd] whileFalse:[ |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
504 |
lineString := inStream nextLine. lNo := lNo + 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
505 |
[lineString notNil and:[lineString endsWith:$\]] whileTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
506 |
lineString := lineString copyWithoutLast:1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
507 |
l := inStream nextLine. lNo := lNo + 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
508 |
l notNil ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
509 |
lineString := lineString , l. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
510 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
511 |
]. |
177 | 512 |
|
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
513 |
(lineString notNil and:[lineString isEmpty not]) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
514 |
first := lineString at:1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
515 |
"/ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
516 |
"/ for your convenience: treat ; AND "/ as comment-line |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
517 |
"/ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
518 |
((first == $;) or:[lineString startsWith:'"/']) ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
519 |
first == $# ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
520 |
lineString := (lineString copyFrom:2) withoutSpaces. |
72 | 521 |
|
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
522 |
(lineString startsWith:'if') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
523 |
skipping ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
524 |
rest := lineString copyFrom:3. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
525 |
value := Compiler evaluate:rest compile:false. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
526 |
(value == #Error) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
527 |
Transcript show:('error in resource:' , lineString). |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
528 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
529 |
(value == false) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
530 |
skipping := true |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
531 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
532 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
533 |
ifLevel := ifLevel + 1 |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
534 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
535 |
(lineString startsWith:'endif') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
536 |
ifLevel := ifLevel - 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
537 |
ifLevel == 0 ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
538 |
skipping := false |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
539 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
540 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
541 |
(lineString startsWith:'else') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
542 |
skipping := skipping not |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
543 |
] ifFalse:[ |
444
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
544 |
skipping ifFalse:[ |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
545 |
(lineString startsWith:'include') ifTrue:[ |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
546 |
rest := lineString copyFrom:8. |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
547 |
value := Compiler evaluate:rest compile:false. |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
548 |
self readFromFile:value directory:dirName |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
549 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
550 |
(lineString startsWith:'encoding') ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
551 |
rest := lineString copyFrom:9. |
1803 | 552 |
encoding := rest withoutSeparators asSymbol |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
553 |
] |
444
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
554 |
] |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
555 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
556 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
557 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
558 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
559 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
560 |
skipping ifFalse:[ |
426 | 561 |
ErrorSignal handle:[:ex | |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
562 |
('ResourcePack [warning]: ''' , ex errorString , '''') errorPrintCR. |
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
563 |
('ResourcePack [info]: line ' , lNo printString , ': ''' , lineString , '''') infoPrintCR. |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
564 |
ok := false. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
565 |
] do:[ |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
566 |
self processLine:lineString encoding:encoding |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
567 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
568 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
569 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
570 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
571 |
] |
0 | 572 |
]. |
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
573 |
^ ok |
177 | 574 |
|
575 |
"Modified: 31.8.1995 / 02:33:45 / claus" |
|
1803 | 576 |
"Modified: 1.7.1997 / 00:59:22 / cg" |
0 | 577 |
! ! |
578 |
||
244 | 579 |
!ResourcePack methodsFor:'merging'! |
580 |
||
581 |
merge:anotherPack |
|
582 |
anotherPack keysAndValuesDo:[:key :value | |
|
583 |
(self includesKey:key) ifFalse:[ |
|
584 |
self at:key put:value |
|
585 |
] |
|
586 |
] |
|
587 |
! ! |
|
588 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
589 |
!ResourcePack class methodsFor:'documentation'! |
251 | 590 |
|
591 |
version |
|
1894
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
592 |
^ '$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.40 1997-09-06 17:31:58 cg Exp $' |
251 | 593 |
! ! |
0 | 594 |
ResourcePack initialize! |