author | Claus Gittinger <cg@exept.de> |
Tue, 14 May 1996 11:57:34 +0200 | |
changeset 686 | 152d8437af35 |
parent 685 | 69001780dfcc |
child 687 | d2cf3cd18f65 |
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 |
||
46 | 20 |
!ResourcePack class methodsFor:'documentation'! |
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 |
|
620 | 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'). |
|
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 |
|
0 | 164 |
!ResourcePack class methodsFor:'initialization'! |
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 |
||
183 |
!ResourcePack class methodsFor:'instance creation'! |
|
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 |
||
54 | 236 |
!ResourcePack class methodsFor:'private'! |
237 |
||
238 |
addToCache:aPack |
|
239 |
|idx| |
|
0 | 240 |
|
54 | 241 |
idx := Packs identityIndexOf:nil. |
242 |
idx == 0 ifTrue:[ |
|
72 | 243 |
" |
244 |
throw away oldest |
|
245 |
" |
|
246 |
idx := Packs size. |
|
247 |
Packs replaceFrom:1 to:idx-1 with:Packs startingAt:2. |
|
54 | 248 |
]. |
249 |
Packs at:idx put:aPack |
|
250 |
! |
|
0 | 251 |
|
54 | 252 |
searchCacheFor:aClassname |
253 |
|sz| |
|
0 | 254 |
|
24 | 255 |
Packs isNil ifTrue:[ |
72 | 256 |
self initialize. |
257 |
^ nil |
|
0 | 258 |
]. |
54 | 259 |
|
260 |
sz := Packs size. |
|
261 |
1 to:sz do:[:idx | |
|
72 | 262 |
|aPack| |
54 | 263 |
|
72 | 264 |
aPack := Packs at:idx. |
265 |
aPack notNil ifTrue:[ |
|
266 |
aClassname = aPack packsClassName ifTrue:[ |
|
267 |
" |
|
268 |
bring to end for LRU |
|
269 |
" |
|
270 |
Packs replaceFrom:idx to:sz-1 with:Packs startingAt:idx+1. |
|
271 |
Packs at:sz put:aPack. |
|
272 |
^ aPack |
|
273 |
] |
|
274 |
] |
|
0 | 275 |
]. |
54 | 276 |
^ nil |
123 | 277 |
|
278 |
" |
|
279 |
ResourcePack searchCacheFor:'TextView' |
|
280 |
" |
|
54 | 281 |
! ! |
282 |
||
0 | 283 |
!ResourcePack methodsFor:'accessing'! |
284 |
||
244 | 285 |
array:anArray |
286 |
"translate a collection of strings" |
|
0 | 287 |
|
244 | 288 |
^ anArray collect:[:r | self at:r default:r] |
0 | 289 |
! |
290 |
||
2 | 291 |
at:aKey |
123 | 292 |
"translate a string" |
293 |
||
72 | 294 |
^ self at:aKey ifAbsent:aKey |
0 | 295 |
! |
296 |
||
297 |
at:aKey default:default |
|
123 | 298 |
"translate a string" |
299 |
||
72 | 300 |
^ self at:aKey ifAbsent:default |
0 | 301 |
! |
302 |
||
244 | 303 |
dependents |
304 |
^ dependents |
|
305 |
! |
|
306 |
||
307 |
dependents:aCollection |
|
308 |
dependents := aCollection |
|
309 |
! |
|
310 |
||
2 | 311 |
name:aKey |
123 | 312 |
"translate a string" |
313 |
||
72 | 314 |
^ self at:aKey ifAbsent:aKey |
2 | 315 |
! |
316 |
||
317 |
name:aKey default:default |
|
123 | 318 |
"translate a string" |
319 |
||
72 | 320 |
^ self at:aKey ifAbsent:default |
2 | 321 |
! |
322 |
||
244 | 323 |
packsClassName |
324 |
^ packsClassName |
|
325 |
! |
|
123 | 326 |
|
244 | 327 |
packsClassName:aString |
328 |
packsClassName := aString |
|
0 | 329 |
! |
330 |
||
331 |
string:s |
|
123 | 332 |
"translate a string" |
333 |
||
72 | 334 |
^ self at:s ifAbsent:s |
0 | 335 |
! |
336 |
||
337 |
string:s with:arg |
|
123 | 338 |
"translate and expand arg" |
339 |
||
0 | 340 |
^ self string:s withArgs:(Array with:arg) |
341 |
! |
|
342 |
||
343 |
string:s with:arg1 with:arg2 |
|
123 | 344 |
"translate and expand args" |
345 |
||
0 | 346 |
^ self string:s withArgs:(Array with:arg1 with:arg2) |
347 |
! |
|
348 |
||
292
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
349 |
string:s with:arg1 with:arg2 with:arg3 |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
350 |
"translate and expand args" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
351 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
352 |
^ 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
|
353 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
354 |
"Created: 9.12.1995 / 19:08:50 / cg" |
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 |
|
0 | 357 |
string:s withArgs:argArray |
123 | 358 |
"translate and expand args" |
359 |
||
135 | 360 |
|template| |
0 | 361 |
|
362 |
template := self at:s ifAbsent:[s]. |
|
135 | 363 |
^ template expandPlaceholdersWith:argArray |
0 | 364 |
! ! |
365 |
||
366 |
!ResourcePack methodsFor:'file reading'! |
|
367 |
||
684 | 368 |
fileReadFailed |
685 | 369 |
^ fileReadFailed ~~ true |
684 | 370 |
|
371 |
"Created: 14.5.1996 / 10:19:26 / cg" |
|
685 | 372 |
"Modified: 14.5.1996 / 11:53:11 / cg" |
684 | 373 |
! |
374 |
||
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
375 |
processLine:aLine encoding:encoding |
145 | 376 |
"process a single valid line (i.e. #ifdef & #include has already been processed)" |
377 |
||
144 | 378 |
|lineString name stream l rest macroName value| |
379 |
||
145 | 380 |
lineString := aLine withoutSeparators. |
144 | 381 |
name := nil. |
382 |
(lineString at:1) == $' ifTrue:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
383 |
stream := ReadStream on:lineString. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
384 |
name := String |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
385 |
readFrom:stream |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
386 |
onError:[('RESOURCEPACK: invalid line <',lineString,'>') errorPrintNL. nil]. |
158 | 387 |
"/ OLD: l := stream position |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
388 |
l := stream position + 1. |
144 | 389 |
|
390 |
"/ l := lineString indexOf:$' startingAt:2. |
|
391 |
"/ l ~~ 0 ifTrue:[ |
|
392 |
"/ name := (lineString copyFrom:2 to:l-1). |
|
393 |
"/ l := l + 1 |
|
394 |
"/ ] |
|
395 |
] ifFalse:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
396 |
l := lineString indexOfSeparatorStartingAt:1. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
397 |
l ~~ 0 ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
398 |
name := lineString copyFrom:1 to:l-1. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
399 |
] |
144 | 400 |
]. |
401 |
name notNil ifTrue:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
402 |
rest := (lineString copyFrom:l) withoutSeparators. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
403 |
" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
404 |
skip <type> if present |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
405 |
" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
406 |
(rest startsWith:$<) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
407 |
l := lineString indexOf:$> startingAt:l. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
408 |
rest := (lineString copyFrom:l+1) withoutSeparators. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
409 |
]. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
410 |
(rest startsWith:$=) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
411 |
rest := rest copyFrom:2. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
412 |
stream := ReadStream on:rest. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
413 |
macroName := stream nextAlphaNumericWord. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
414 |
rest := stream upToEnd. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
415 |
value := self at:macroName. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
416 |
value := Compiler evaluate:('self ' , rest) |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
417 |
receiver:value |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
418 |
notifying:nil |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
419 |
compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
420 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
421 |
value := Compiler evaluate:rest compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
422 |
(value == #Error) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
423 |
Transcript show:('error in resource:' , name). |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
424 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
425 |
encoding notNil ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
426 |
value isString ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
427 |
value := value decodeFrom:encoding |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
428 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
429 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
430 |
] |
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 |
self at:name put:value. |
144 | 433 |
] |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
434 |
|
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
435 |
"Created: 26.2.1996 / 19:17:07 / cg" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
436 |
"Modified: 26.2.1996 / 19:18:07 / cg" |
144 | 437 |
! |
438 |
||
244 | 439 |
readFromFile:fileName directory:dirName |
440 |
"read definitions from a file in a directory" |
|
441 |
||
442 |
|inStream ok| |
|
443 |
||
686 | 444 |
fileReadFailed := false. |
244 | 445 |
dirName = 'resources' ifTrue:[ |
684 | 446 |
inStream := Smalltalk resourceFileStreamFor:fileName |
244 | 447 |
] ifFalse:[ |
684 | 448 |
inStream := Smalltalk systemFileStreamFor:(dirName , '/' , fileName). |
244 | 449 |
]. |
450 |
inStream isNil ifTrue:[ |
|
684 | 451 |
" |
452 |
an empty pack |
|
453 |
" |
|
454 |
^ nil |
|
244 | 455 |
]. |
456 |
ok := self readFromResourceStream:inStream in:dirName. |
|
457 |
inStream close. |
|
686 | 458 |
|
244 | 459 |
ok ifFalse:[ |
684 | 460 |
fileReadFailed := true. |
461 |
||
462 |
('RESOURCEPACK: ''' , fileName , ''' contains error(s) - data may be incomplete.') errorPrintNL. |
|
244 | 463 |
]. |
464 |
||
686 | 465 |
"Modified: 14.5.1996 / 11:57:22 / cg" |
244 | 466 |
! |
467 |
||
72 | 468 |
readFromResourceStream:inStream in:dirName |
145 | 469 |
"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
|
470 |
specify where #include files are searched for. |
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
471 |
Return true, if the style sheet could be read without errors, false otherwise." |
145 | 472 |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
473 |
|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
|
474 |
|
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
475 |
encoding := nil. |
0 | 476 |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
477 |
ok := true. |
0 | 478 |
ifLevel := 0. |
479 |
skipping := false. |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
480 |
lNo := 0. |
0 | 481 |
[inStream atEnd] whileFalse:[ |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
482 |
lineString := inStream nextLine. lNo := lNo + 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
483 |
[lineString notNil and:[lineString endsWith:$\]] whileTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
484 |
lineString := lineString copyWithoutLast:1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
485 |
l := inStream nextLine. lNo := lNo + 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
486 |
l notNil ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
487 |
lineString := lineString , l. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
488 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
489 |
]. |
177 | 490 |
|
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
491 |
(lineString notNil and:[lineString isEmpty not]) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
492 |
first := lineString at:1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
493 |
"/ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
494 |
"/ for your convenience: treat ; AND "/ as comment-line |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
495 |
"/ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
496 |
((first == $;) or:[lineString startsWith:'"/']) ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
497 |
first == $# ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
498 |
lineString := (lineString copyFrom:2) withoutSpaces. |
72 | 499 |
|
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
500 |
(lineString startsWith:'if') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
501 |
skipping ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
502 |
rest := lineString copyFrom:3. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
503 |
value := Compiler evaluate:rest compile:false. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
504 |
(value == #Error) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
505 |
Transcript show:('error in resource:' , lineString). |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
506 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
507 |
(value == false) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
508 |
skipping := true |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
509 |
] |
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 |
ifLevel := ifLevel + 1 |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
512 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
513 |
(lineString startsWith:'endif') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
514 |
ifLevel := ifLevel - 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
515 |
ifLevel == 0 ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
516 |
skipping := false |
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 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
519 |
(lineString startsWith:'else') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
520 |
skipping := skipping not |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
521 |
] ifFalse:[ |
444
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
522 |
skipping ifFalse:[ |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
523 |
(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
|
524 |
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
|
525 |
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
|
526 |
self readFromFile:value directory:dirName |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
527 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
528 |
(lineString startsWith:'encoding') ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
529 |
rest := lineString copyFrom:9. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
530 |
encoding := Compiler evaluate:rest compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
531 |
] |
444
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
532 |
] |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
533 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
534 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
535 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
536 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
537 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
538 |
skipping ifFalse:[ |
426 | 539 |
ErrorSignal handle:[:ex | |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
540 |
('RESOURCEPACK: error: ''' , ex errorString , '''') errorPrintNL. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
541 |
('RESOURCEPACK: line ' , lNo printString , ': ''' , lineString , '''') errorPrintNL. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
542 |
ok := false. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
543 |
] do:[ |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
544 |
self processLine:lineString encoding:encoding |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
545 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
546 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
547 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
548 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
549 |
] |
0 | 550 |
]. |
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
551 |
^ ok |
177 | 552 |
|
553 |
"Modified: 31.8.1995 / 02:33:45 / claus" |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
554 |
"Modified: 26.2.1996 / 19:22:36 / cg" |
0 | 555 |
! ! |
556 |
||
244 | 557 |
!ResourcePack methodsFor:'merging'! |
558 |
||
559 |
merge:anotherPack |
|
560 |
anotherPack keysAndValuesDo:[:key :value | |
|
561 |
(self includesKey:key) ifFalse:[ |
|
562 |
self at:key put:value |
|
563 |
] |
|
564 |
] |
|
565 |
! ! |
|
566 |
||
251 | 567 |
!ResourcePack class methodsFor:'documentation'! |
568 |
||
569 |
version |
|
686 | 570 |
^ '$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.32 1996-05-14 09:57:34 cg Exp $' |
251 | 571 |
! ! |
0 | 572 |
ResourcePack initialize! |