author | Claus Gittinger <cg@exept.de> |
Thu, 09 Dec 1999 10:13:06 +0100 | |
changeset 3097 | b2cca70d5d55 |
parent 3082 | 0f6435aaa66a |
child 3101 | 38cc69894380 |
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 |
|
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
14 |
instanceVariableNames:'superPack 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 |
" |
|
1942 | 38 |
This class supports easy customization of smalltalk code (i.e. internationalization |
39 |
and viewStyle adaption). |
|
145 | 40 |
ResourcePacks are class specific, meaning that every subclass of View |
1942 | 41 |
and ApplicationModel has an instance of ResourcePack (instVar called 'resources') |
42 |
which is created when the first instance of the view/app is created, |
|
43 |
and cached in a class-instVar (so the file is only read once). |
|
54 | 44 |
|
50 | 45 |
The resourcePack consists of a mapping from strings to values, which are |
46 |
then used in labels, buttons, menus etc. |
|
47 |
The resourcePack initializes itself from a file found in 'resources/<className>.rs', |
|
48 |
where 'className' is built by the usual abbreviation mechanism (see abbrev-files). |
|
1942 | 49 |
|
50 | 50 |
Conditional mappings are possible, by including lines as: |
2314 | 51 |
#if <expression> |
52 |
#endif |
|
50 | 53 |
in the resourcefile. Example: |
54 |
file 'foo.rs': |
|
2314 | 55 |
#if Language == #german |
56 |
'abort' 'Abbruch' |
|
57 |
#endif |
|
58 |
#if Language == #french |
|
59 |
'abort' 'canceller' |
|
60 |
#endif |
|
145 | 61 |
|
62 |
the corresponding resource-strings are accessed (from methods within the class) |
|
63 |
using: |
|
2314 | 64 |
resources string:'abort' |
145 | 65 |
|
54 | 66 |
returning the mapped string (i.e. 'Abbruch' if the global Language is set |
67 |
to #german).. |
|
145 | 68 |
|
1942 | 69 |
If no corresponding entry is found in the resources, the key is returned; |
70 |
alternatively, use: |
|
2314 | 71 |
resources string:'foo' default:'bar' |
1942 | 72 |
which returns 'bar', if no resource definition for 'foo' is found. |
73 |
||
54 | 74 |
Translations can also include arguments, such as: |
2314 | 75 |
resources string:'really delete %1' with:fileName |
145 | 76 |
|
77 |
This scheme has the advantage, that you can write your programs using your |
|
78 |
native language strings. Later, when new languages are to be supported, |
|
79 |
simply create a resource file for the class and add translations for |
|
80 |
all those strings. (find the keys by looking at users of resource or senders |
|
1942 | 81 |
of 'string:*'). |
82 |
Notice, that the grammar of different languages may imply a reordering, |
|
83 |
so the above string becomes the german 'wollen Sie %1 wirklich löschen'; |
|
84 |
so using percent-placeholders is much better than simple concatenations of |
|
85 |
arguments to the question. |
|
145 | 86 |
|
87 |
More languages can be added later without any change in the code, |
|
88 |
recompilation or the like. |
|
89 |
Even by people without access to the source code i.e. which only have the |
|
90 |
applications binary. |
|
91 |
||
92 |
Also, unsupported languages' strings are simply left unchanged - if you |
|
93 |
write your application in (say) english, and only plan to use it in english, |
|
94 |
no additional work is required (i.e you dont need a resource file then). |
|
95 |
Strings for unknown languages will come in english |
|
96 |
(which is better than nothing or empty button labels ;-) |
|
97 |
||
98 |
Summary: |
|
2314 | 99 |
in subclasses of View and ApplicationModel, |
100 |
instead of writing: |
|
145 | 101 |
|
2314 | 102 |
... |
103 |
b := Button label:'press me' |
|
104 |
... |
|
145 | 105 |
|
2314 | 106 |
always write: |
612 | 107 |
|
2314 | 108 |
... |
109 |
b := Button label:(resources string:'press me') |
|
110 |
... |
|
145 | 111 |
|
2314 | 112 |
if your class is not a subclass of one of the above, AND you need |
113 |
resource translations, you wont inherit the resources variable |
|
114 |
(which is automatically initialized). |
|
115 |
In this case, you have to ask the ResourcePack class explicitely for |
|
116 |
a corresponding package: |
|
145 | 117 |
|
2314 | 118 |
ResourcePack for:aClassName |
145 | 119 |
|
2314 | 120 |
as an example, see how the Date class gets the national names of |
121 |
week & monthnames. |
|
145 | 122 |
|
612 | 123 |
[author:] |
2314 | 124 |
Claus Gittinger |
145 | 125 |
" |
126 |
! |
|
127 |
||
128 |
examples |
|
129 |
" |
|
130 |
normally, resources are found in files named after their classes sourcefile |
|
131 |
For example, the FileBrowsers resources are found in 'FBrowser.rs'. |
|
132 |
For the examples below, we process resources from a constant string; |
|
2314 | 133 |
this is NOT representative. |
134 |
[exBegin] |
|
135 |
|stream res| |
|
145 | 136 |
|
2314 | 137 |
stream := ReadStream on:' |
145 | 138 |
foo ''the translation for foo'' |
139 |
#if Language == #german |
|
140 |
bar ''die deutsche uebersetzung von bar'' |
|
141 |
baz ''baz hat den Wert %1'' |
|
142 |
#endif |
|
143 |
#if Language == #french |
|
144 |
bar ''bar en francaise'' |
|
145 |
baz ''%1, c''''est baz'' |
|
146 |
#endif |
|
147 |
||
148 |
'. |
|
149 |
||
2314 | 150 |
res := ResourcePack new readFromResourceStream:stream in:nil. |
145 | 151 |
|
2314 | 152 |
Transcript showCR:'baz is translated to: ' , (res string:'baz' with:'1234'). |
153 |
Transcript showCR:'bar is translated to: ' , (res string:'bar'). |
|
154 |
Transcript showCR:'foo is translated to: ' , (res string:'foo'). |
|
155 |
Transcript showCR:'fooBar is translated to: ' , (res string:'fooBar'). |
|
156 |
[exEnd] |
|
145 | 157 |
set the Language to french: |
2314 | 158 |
[exBegin] |
159 |
Language := #french |
|
160 |
[exEnd] |
|
145 | 161 |
and repeat the above. |
620 | 162 |
back to english: |
2314 | 163 |
[exBegin] |
164 |
Language := #english |
|
165 |
[exEnd] |
|
620 | 166 |
back to german: |
2314 | 167 |
[exBegin] |
168 |
Language := #german |
|
169 |
[exEnd] |
|
46 | 170 |
" |
171 |
! ! |
|
5 | 172 |
|
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
173 |
!ResourcePack class methodsFor:'initialization'! |
0 | 174 |
|
72 | 175 |
flushCachedResourcePacks |
24 | 176 |
"forget all cached resources - needed after a style change" |
177 |
||
178 |
Packs := nil. |
|
179 |
self initialize |
|
180 |
||
72 | 181 |
"ResourcePack flushCachedResourcePacks" |
244 | 182 |
! |
183 |
||
184 |
initialize |
|
185 |
Packs isNil ifTrue:[ |
|
186 |
Packs := WeakArray new:30 |
|
187 |
]. |
|
188 |
||
189 |
"ResourcePack initialize" |
|
190 |
! ! |
|
191 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
192 |
!ResourcePack class methodsFor:'instance creation'! |
244 | 193 |
|
194 |
for:aClass |
|
195 |
"get the full resource definitions for aClass (i.e. with super packs). |
|
196 |
Also leave the resulting pack in the cache for faster access next time." |
|
197 |
||
3049
d5f2b3f66e37
load resources from package directory, if present
Claus Gittinger <cg@exept.de>
parents:
3047
diff
changeset
|
198 |
|nm pack superPack pkg prj prjDir baseName path| |
244 | 199 |
|
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
200 |
nm := aClass resourcePackName. |
244 | 201 |
pack := self searchCacheFor:nm. |
202 |
pack notNil ifTrue:[^ pack]. |
|
203 |
||
3049
d5f2b3f66e37
load resources from package directory, if present
Claus Gittinger <cg@exept.de>
parents:
3047
diff
changeset
|
204 |
baseName := (Smalltalk fileNameForClass:nm) , '.rs'. |
3046 | 205 |
|
3049
d5f2b3f66e37
load resources from package directory, if present
Claus Gittinger <cg@exept.de>
parents:
3047
diff
changeset
|
206 |
"/ search in the classes package directory first ... |
d5f2b3f66e37
load resources from package directory, if present
Claus Gittinger <cg@exept.de>
parents:
3047
diff
changeset
|
207 |
prjDir := Smalltalk projectDirectoryForClass:aClass. |
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
208 |
|
2971 | 209 |
(prjDir notNil |
210 |
and:[(prjDir := prjDir asFilename) exists |
|
211 |
and:[(prjDir := prjDir construct:'resources') exists |
|
212 |
and:[(prjDir construct:baseName) exists]]]) ifTrue:[ |
|
213 |
pack := self fromFile:(prjDir constructString:baseName) directory:nil. |
|
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
214 |
] ifFalse:[ |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
215 |
pack := self fromFile:baseName. |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
216 |
]. |
244 | 217 |
aClass == Object ifFalse:[ |
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
218 |
pack superPack:(self for:(aClass superclass)). |
244 | 219 |
]. |
220 |
pack packsClassName:nm. |
|
221 |
self addToCache:pack. |
|
222 |
^ pack |
|
223 |
||
224 |
" |
|
3046 | 225 |
ResourcePack for:TextView |
244 | 226 |
ResourcePack for:CodeView |
3046 | 227 |
ResourcePack for:Workspace |
3049
d5f2b3f66e37
load resources from package directory, if present
Claus Gittinger <cg@exept.de>
parents:
3047
diff
changeset
|
228 |
ResourcePack for:View |
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
229 |
ResourcePack for:ErrorLogger |
244 | 230 |
Workspace classResources |
231 |
" |
|
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
232 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
233 |
"Modified: / 29.1.1998 / 22:42:53 / cg" |
244 | 234 |
! |
235 |
||
236 |
fromFile:aFileName |
|
237 |
"get the resource definitions from a file in the default directory. |
|
238 |
Uncached low-level entry." |
|
239 |
||
240 |
^ self fromFile:aFileName directory:'resources' |
|
241 |
||
242 |
" |
|
243 |
ResourcePack fromFile:'SBrowser.rs' |
|
244 |
ResourcePack fromFile:'FBrowser.rs' |
|
245 |
ResourcePack fromFile:'Smalltalk.rs' |
|
246 |
" |
|
247 |
! |
|
248 |
||
249 |
fromFile:aFileName directory:dirName |
|
250 |
"get the resource definitions from a file in a directory. |
|
251 |
Uncached low-level entry." |
|
252 |
||
253 |
|newPack| |
|
254 |
||
255 |
newPack := self new. |
|
256 |
newPack readFromFile:aFileName directory:dirName. |
|
257 |
^ newPack |
|
0 | 258 |
! ! |
259 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
260 |
!ResourcePack class methodsFor:'private'! |
54 | 261 |
|
262 |
addToCache:aPack |
|
263 |
|idx| |
|
0 | 264 |
|
54 | 265 |
idx := Packs identityIndexOf:nil. |
266 |
idx == 0 ifTrue:[ |
|
1078
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
267 |
idx := Packs identityIndexOf:0 |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
268 |
]. |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
269 |
idx == 0 ifTrue:[ |
72 | 270 |
" |
271 |
throw away oldest |
|
272 |
" |
|
273 |
idx := Packs size. |
|
274 |
Packs replaceFrom:1 to:idx-1 with:Packs startingAt:2. |
|
54 | 275 |
]. |
276 |
Packs at:idx put:aPack |
|
277 |
! |
|
0 | 278 |
|
54 | 279 |
searchCacheFor:aClassname |
998 | 280 |
|sz "{ Class: SmallInteger }" | |
0 | 281 |
|
24 | 282 |
Packs isNil ifTrue:[ |
72 | 283 |
self initialize. |
284 |
^ nil |
|
0 | 285 |
]. |
54 | 286 |
|
287 |
sz := Packs size. |
|
288 |
1 to:sz do:[:idx | |
|
72 | 289 |
|aPack| |
54 | 290 |
|
72 | 291 |
aPack := Packs at:idx. |
1078
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
292 |
(aPack notNil |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
293 |
and:[aPack ~~ 0]) ifTrue:[ |
72 | 294 |
aClassname = aPack packsClassName ifTrue:[ |
295 |
" |
|
296 |
bring to end for LRU |
|
297 |
" |
|
298 |
Packs replaceFrom:idx to:sz-1 with:Packs startingAt:idx+1. |
|
299 |
Packs at:sz put:aPack. |
|
300 |
^ aPack |
|
301 |
] |
|
302 |
] |
|
0 | 303 |
]. |
54 | 304 |
^ nil |
123 | 305 |
|
306 |
" |
|
307 |
ResourcePack searchCacheFor:'TextView' |
|
308 |
" |
|
54 | 309 |
! ! |
310 |
||
0 | 311 |
!ResourcePack methodsFor:'accessing'! |
312 |
||
244 | 313 |
array:anArray |
314 |
"translate a collection of strings" |
|
0 | 315 |
|
244 | 316 |
^ anArray collect:[:r | self at:r default:r] |
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
317 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
318 |
" |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
319 |
Launcher classResources array:#('file' 'classes') |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
320 |
" |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
321 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
322 |
"Modified: / 29.1.1998 / 22:44:22 / cg" |
0 | 323 |
! |
324 |
||
2 | 325 |
at:aKey |
123 | 326 |
"translate a string" |
327 |
||
72 | 328 |
^ self at:aKey ifAbsent:aKey |
0 | 329 |
! |
330 |
||
331 |
at:aKey default:default |
|
123 | 332 |
"translate a string" |
333 |
||
72 | 334 |
^ self at:aKey ifAbsent:default |
0 | 335 |
! |
336 |
||
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
337 |
at:aKey ifAbsent:defaultValue |
123 | 338 |
"translate a string" |
339 |
||
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
340 |
|val| |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
341 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
342 |
aKey notNil ifTrue:[ |
2314 | 343 |
val := super at:aKey ifAbsent:nil. |
344 |
val notNil ifTrue:[^ val]. |
|
345 |
superPack notNil ifTrue:[ |
|
346 |
^ superPack at:aKey ifAbsent:defaultValue |
|
347 |
]. |
|
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
348 |
]. |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
349 |
^ defaultValue value |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
350 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
351 |
"Modified: / 29.1.1998 / 22:38:15 / cg" |
2 | 352 |
! |
353 |
||
354 |
name:aKey default:default |
|
2461
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
355 |
"translate a string. |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
356 |
Obsolete - use #string:default:" |
123 | 357 |
|
72 | 358 |
^ self at:aKey ifAbsent:default |
2 | 359 |
! |
360 |
||
0 | 361 |
string:s |
3097 | 362 |
"translate (retrieve) a string - if not present, return s" |
123 | 363 |
|
72 | 364 |
^ self at:s ifAbsent:s |
3097 | 365 |
|
366 |
" |
|
367 |
NewLauncher classResources |
|
368 |
string:'LICENCEFILE' |
|
369 |
" |
|
0 | 370 |
! |
371 |
||
2461
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
372 |
string:s default:defaultString |
3097 | 373 |
"translate (retrieve) a string - if not present, return defaultString" |
2461
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
374 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
375 |
^ self at:s ifAbsent:defaultString |
3097 | 376 |
|
377 |
" |
|
378 |
NewLauncher classResources |
|
379 |
string:'fooBar' default:'Hello world' |
|
380 |
" |
|
381 |
||
2461
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
382 |
! |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
383 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
384 |
string:s default:defaultString with:arg |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
385 |
"translate and expand arg" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
386 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
387 |
^ self string:s default:defaultString withArgs:(Array with:arg) |
3097 | 388 |
|
389 |
" |
|
390 |
NewLauncher classResources |
|
391 |
string:'%1 fooBar' default:'Hello %1' with:'foo' |
|
392 |
" |
|
393 |
||
2461
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
394 |
! |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
395 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
396 |
string:s default:defaultString with:arg1 with:arg2 |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
397 |
"translate and expand args" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
398 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
399 |
^ self string:s default:defaultString withArgs:(Array with:arg1 with:arg2) |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
400 |
! |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
401 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
402 |
string:s default:defaultString with:arg1 with:arg2 with:arg3 |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
403 |
"translate and expand args" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
404 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
405 |
^ self string:s default:defaultString withArgs:(Array with:arg1 with:arg2 with:arg3) |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
406 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
407 |
"Created: 9.12.1995 / 19:08:50 / cg" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
408 |
! |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
409 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
410 |
string:s default:defaultString with:arg1 with:arg2 with:arg3 with:arg4 |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
411 |
"translate and expand args" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
412 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
413 |
^ self string:s default:defaultString withArgs:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
414 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
415 |
"Created: 9.9.1996 / 18:52:14 / cg" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
416 |
! |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
417 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
418 |
string:s default:defaultString withArgs:argArray |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
419 |
"translate and expand args" |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
420 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
421 |
|template| |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
422 |
|
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
423 |
template := self at:s ifAbsent:defaultString. |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
424 |
^ template expandPlaceholdersWith:argArray |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
425 |
! |
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
426 |
|
0 | 427 |
string:s with:arg |
123 | 428 |
"translate and expand arg" |
429 |
||
0 | 430 |
^ self string:s withArgs:(Array with:arg) |
431 |
! |
|
432 |
||
433 |
string:s with:arg1 with:arg2 |
|
123 | 434 |
"translate and expand args" |
435 |
||
0 | 436 |
^ self string:s withArgs:(Array with:arg1 with:arg2) |
437 |
! |
|
438 |
||
292
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
439 |
string:s with:arg1 with:arg2 with:arg3 |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
440 |
"translate and expand args" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
441 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
442 |
^ 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
|
443 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
444 |
"Created: 9.12.1995 / 19:08:50 / cg" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
445 |
! |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
446 |
|
1042 | 447 |
string:s with:arg1 with:arg2 with:arg3 with:arg4 |
448 |
"translate and expand args" |
|
449 |
||
450 |
^ self string:s withArgs:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
451 |
||
452 |
"Created: 9.9.1996 / 18:52:14 / cg" |
|
453 |
! |
|
454 |
||
0 | 455 |
string:s withArgs:argArray |
123 | 456 |
"translate and expand args" |
457 |
||
135 | 458 |
|template| |
0 | 459 |
|
2461
3c212ae910ec
added #string:default:with* methods.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
460 |
template := self at:s ifAbsent:s. |
135 | 461 |
^ template expandPlaceholdersWith:argArray |
0 | 462 |
! ! |
463 |
||
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
464 |
!ResourcePack methodsFor:'accessing - internels'! |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
465 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
466 |
name:aKey |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
467 |
"translate a string" |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
468 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
469 |
^ self at:aKey ifAbsent:aKey |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
470 |
! |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
471 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
472 |
packsClassName |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
473 |
^ packsClassName |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
474 |
! |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
475 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
476 |
packsClassName:aString |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
477 |
packsClassName := aString |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
478 |
! |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
479 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
480 |
superPack:anotherResourcePack |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
481 |
superPack := anotherResourcePack |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
482 |
|
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
483 |
"Created: / 29.1.1998 / 22:43:09 / cg" |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
484 |
! ! |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
485 |
|
0 | 486 |
!ResourcePack methodsFor:'file reading'! |
487 |
||
684 | 488 |
fileReadFailed |
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
489 |
"return true, if the pack does not really contain |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
490 |
valid translations, since the fileRead failed. |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
491 |
However, all inherited translations are still available |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
492 |
through the receiver" |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
493 |
|
687 | 494 |
^ fileReadFailed ~~ false |
684 | 495 |
|
2033
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
496 |
"Created: / 14.5.1996 / 10:19:26 / cg" |
dad8f0ee02f4
do not merge superPacks data;
Claus Gittinger <cg@exept.de>
parents:
1956
diff
changeset
|
497 |
"Modified: / 29.1.1998 / 22:46:38 / cg" |
684 | 498 |
! |
499 |
||
1894
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
500 |
nonexistingFileRead |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
501 |
"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
|
502 |
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
|
503 |
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
|
504 |
|
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
505 |
^ nil |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
506 |
|
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
507 |
"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
|
508 |
! |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
509 |
|
2291
eaac1c5f2a61
show fileName if a line has error.
Claus Gittinger <cg@exept.de>
parents:
2153
diff
changeset
|
510 |
processLine:aLine encoding:encoding file:fileName |
145 | 511 |
"process a single valid line (i.e. #ifdef & #include has already been processed)" |
512 |
||
2314 | 513 |
|lineString name stream l rest macroName value |
514 |
conditional hasError| |
|
144 | 515 |
|
145 | 516 |
lineString := aLine withoutSeparators. |
144 | 517 |
name := nil. |
518 |
(lineString at:1) == $' ifTrue:[ |
|
2608 | 519 |
stream := ReadStream on:lineString. |
520 |
stream signalAtEnd:false. |
|
521 |
name := String |
|
522 |
readFrom:stream |
|
523 |
onError:[('ResourcePack [warning]: invalid line <' |
|
524 |
,lineString |
|
525 |
,'> in file:' |
|
526 |
, fileName |
|
527 |
) errorPrintCR. nil]. |
|
158 | 528 |
"/ OLD: l := stream position |
2608 | 529 |
l := stream position + 1. |
144 | 530 |
|
531 |
"/ l := lineString indexOf:$' startingAt:2. |
|
532 |
"/ l ~~ 0 ifTrue:[ |
|
533 |
"/ name := (lineString copyFrom:2 to:l-1). |
|
534 |
"/ l := l + 1 |
|
535 |
"/ ] |
|
536 |
] ifFalse:[ |
|
2608 | 537 |
l := lineString indexOfSeparatorStartingAt:1. |
538 |
l ~~ 0 ifTrue:[ |
|
539 |
name := lineString copyFrom:1 to:l-1. |
|
540 |
] |
|
144 | 541 |
]. |
542 |
name notNil ifTrue:[ |
|
2608 | 543 |
hasError := false. |
2153 | 544 |
|
2608 | 545 |
rest := (lineString copyFrom:l) withoutSeparators. |
546 |
" |
|
547 |
skip <type> if present |
|
548 |
" |
|
549 |
(rest startsWith:$<) ifTrue:[ |
|
550 |
l := lineString indexOf:$> startingAt:l. |
|
551 |
rest := (lineString copyFrom:l+1) withoutSeparators. |
|
552 |
]. |
|
2153 | 553 |
|
2608 | 554 |
conditional := false. |
555 |
(rest startsWith:$?) ifTrue:[ |
|
556 |
rest := (rest copyFrom:2) withoutSeparators. |
|
557 |
conditional := true. |
|
558 |
]. |
|
2153 | 559 |
|
2608 | 560 |
(rest startsWith:$=) ifTrue:[ |
561 |
rest := rest copyFrom:2. |
|
562 |
stream := ReadStream on:rest. |
|
563 |
macroName := stream nextAlphaNumericWord. |
|
564 |
[stream peek == $.] whileTrue:[ |
|
565 |
stream next. |
|
566 |
stream peek notNil ifTrue:[ |
|
567 |
macroName := macroName , '.' , (stream nextAlphaNumericWord) |
|
568 |
] |
|
569 |
]. |
|
570 |
rest := stream upToEnd. |
|
571 |
value := self at:macroName ifAbsent:nil. |
|
572 |
(value isNil) ifTrue:[ |
|
573 |
hasError := true. |
|
3003 | 574 |
('ResourcePack [warning]: bad (nil-valued) macro: ' , macroName) errorPrintCR. |
575 |
('ResourcePack [warning]: in line: ' , lineString) errorPrintCR. |
|
576 |
('ResourcePack [warning]: in file: ' , fileName) errorPrintCR. |
|
2608 | 577 |
]. |
578 |
value isBlock ifTrue:[ |
|
579 |
value := value value |
|
580 |
]. |
|
581 |
value := Compiler evaluate:('self ' , rest) |
|
582 |
receiver:value |
|
583 |
notifying:nil |
|
584 |
compile:false. |
|
585 |
(value == #Error) ifTrue:[ |
|
586 |
hasError := true. |
|
587 |
('ResourcePack [warning]: error in line: "self ' , rest , '"') errorPrintCR. |
|
3003 | 588 |
('ResourcePack [warning]: in file: ' , fileName) errorPrintCR. |
2608 | 589 |
] |
590 |
] ifFalse:[ |
|
591 |
value := Compiler evaluate:rest compile:false. |
|
592 |
(value == #Error) ifTrue:[ |
|
593 |
hasError := true. |
|
594 |
('ResourcePack [warning]: error in line: "' , rest , '"') errorPrintCR. |
|
3003 | 595 |
('ResourcePack [warning]: in file: ' , fileName) errorPrintCR. |
2608 | 596 |
] ifFalse:[ |
597 |
encoding notNil ifTrue:[ |
|
598 |
value isString ifTrue:[ |
|
599 |
value := value decodeFrom:encoding |
|
600 |
] |
|
601 |
] |
|
602 |
] |
|
603 |
]. |
|
604 |
hasError ifFalse:[ |
|
605 |
(conditional not |
|
606 |
or:[(self includesKey:name) not]) ifTrue:[ |
|
607 |
self at:name put:value. |
|
608 |
] |
|
609 |
] |
|
144 | 610 |
] |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
611 |
|
2291
eaac1c5f2a61
show fileName if a line has error.
Claus Gittinger <cg@exept.de>
parents:
2153
diff
changeset
|
612 |
"Created: / 30.8.1998 / 12:35:37 / cg" |
2608 | 613 |
"Modified: / 28.4.1999 / 22:23:14 / cg" |
144 | 614 |
! |
615 |
||
244 | 616 |
readFromFile:fileName directory:dirName |
617 |
"read definitions from a file in a directory" |
|
618 |
||
619 |
|inStream ok| |
|
620 |
||
686 | 621 |
fileReadFailed := false. |
244 | 622 |
dirName = 'resources' ifTrue:[ |
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
623 |
inStream := Smalltalk resourceFileStreamFor:fileName |
244 | 624 |
] ifFalse:[ |
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
625 |
dirName isNil ifTrue:[ |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
626 |
inStream := Smalltalk systemFileStreamFor:fileName. |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
627 |
] ifFalse:[ |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
628 |
inStream := Smalltalk systemFileStreamFor:(dirName , '/' , fileName). |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
629 |
] |
244 | 630 |
]. |
631 |
inStream isNil ifTrue:[ |
|
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
632 |
" |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
633 |
an empty pack |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
634 |
" |
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
635 |
^ self nonexistingFileRead |
244 | 636 |
]. |
2996 | 637 |
|
244 | 638 |
ok := self readFromResourceStream:inStream in:dirName. |
639 |
inStream close. |
|
686 | 640 |
|
244 | 641 |
ok ifFalse:[ |
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
642 |
fileReadFailed := true. |
684 | 643 |
|
2970
928387584c9c
for non-base system classes (i.e. package not stx:),
Claus Gittinger <cg@exept.de>
parents:
2608
diff
changeset
|
644 |
('ResourcePack [warning]: ''' , fileName , ''' contains error(s) - data may be incomplete.') errorPrintCR. |
244 | 645 |
]. |
646 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
647 |
"Modified: 10.1.1997 / 18:05:17 / cg" |
244 | 648 |
! |
649 |
||
72 | 650 |
readFromResourceStream:inStream in:dirName |
145 | 651 |
"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
|
652 |
specify where #include files are searched for. |
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
653 |
Return true, if the style sheet could be read without errors, false otherwise." |
145 | 654 |
|
3082
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
655 |
|lineString l lNo rest value ifLevel skipping first ok encoding pack| |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
656 |
|
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
657 |
encoding := nil. |
0 | 658 |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
659 |
ok := true. |
0 | 660 |
ifLevel := 0. |
661 |
skipping := false. |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
662 |
lNo := 0. |
0 | 663 |
[inStream atEnd] whileFalse:[ |
2459
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
664 |
lineString := inStream nextLine. lNo := lNo + 1. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
665 |
[lineString notNil and:[lineString endsWith:$\]] whileTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
666 |
lineString := lineString copyWithoutLast:1. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
667 |
l := inStream nextLine. lNo := lNo + 1. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
668 |
l notNil ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
669 |
lineString := lineString , l. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
670 |
]. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
671 |
]. |
177 | 672 |
|
2459
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
673 |
(lineString size > 0) ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
674 |
first := lineString at:1. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
675 |
"/ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
676 |
"/ for your convenience: treat ; AND "/ as comment-line |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
677 |
"/ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
678 |
((first == $;) or:[lineString startsWith:'"/']) ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
679 |
first == $# ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
680 |
lineString := (lineString copyFrom:2) withoutSpaces. |
72 | 681 |
|
2459
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
682 |
(lineString startsWith:'if') ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
683 |
skipping ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
684 |
rest := lineString copyFrom:3. |
2996 | 685 |
ErrorSignal handle:[:ex | |
686 |
('ResourcePack [warning]: ''' , ex errorString , '''') errorPrintCR. |
|
687 |
('ResourcePack [info]: file: ' , inStream pathName printString , ' line: ' , lNo printString , ': ''' , lineString , '''') errorPrintCR. |
|
688 |
value := nil. |
|
689 |
] do:[ |
|
690 |
value := Compiler evaluate:rest compile:false. |
|
3082
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
691 |
]. |
2459
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
692 |
(value == #Error) ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
693 |
('ResourcePack [warning]: error in resource:' , lineString) errorPrintCR. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
694 |
]. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
695 |
(value == false) ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
696 |
skipping := true |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
697 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
698 |
]. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
699 |
ifLevel := ifLevel + 1 |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
700 |
] ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
701 |
(lineString startsWith:'endif') ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
702 |
ifLevel := ifLevel - 1. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
703 |
ifLevel == 0 ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
704 |
skipping := false |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
705 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
706 |
] ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
707 |
(lineString startsWith:'else') ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
708 |
skipping := skipping not |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
709 |
] ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
710 |
skipping ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
711 |
(lineString startsWith:'include') ifTrue:[ |
3082
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
712 |
(lineString startsWith:'includeResourcesFor') ifTrue:[ |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
713 |
"/ include those resources ... |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
714 |
rest := lineString copyFrom:('includeResourcesFor' size + 1). |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
715 |
value := Compiler evaluate:rest compile:false. |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
716 |
value isBehavior ifTrue:[ |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
717 |
pack := self class for:value. |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
718 |
pack notNil ifTrue:[ |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
719 |
self declareAllFrom:pack. |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
720 |
] |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
721 |
] |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
722 |
] ifFalse:[ |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
723 |
rest := lineString copyFrom:('include' size + 1). |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
724 |
value := Compiler evaluate:rest compile:false. |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
725 |
value isString ifFalse:[ |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
726 |
('ResourcePack [warning]: bad include filename: ' , value printString) errorPrintCR. |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
727 |
('ResourcePack [info]: line ' , lNo printString , ': ''' , lineString , '''') infoPrintCR. |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
728 |
] ifTrue:[ |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
729 |
self readFromFile:value directory:dirName |
0f6435aaa66a
new directive: #includeResourcesFor
Claus Gittinger <cg@exept.de>
parents:
3049
diff
changeset
|
730 |
] |
2459
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
731 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
732 |
] ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
733 |
(lineString startsWith:'encoding') ifTrue:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
734 |
rest := lineString copyFrom:9. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
735 |
encoding := rest withoutSeparators asSymbol |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
736 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
737 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
738 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
739 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
740 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
741 |
]. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
742 |
] ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
743 |
skipping ifFalse:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
744 |
ErrorSignal handle:[:ex | |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
745 |
('ResourcePack [warning]: ''' , ex errorString , '''') errorPrintCR. |
2996 | 746 |
('ResourcePack [info]: file: ' , inStream pathName printString , ' line: ' , lNo printString , ': ''' , lineString , '''') errorPrintCR. |
2459
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
747 |
ok := false. |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
748 |
] do:[ |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
749 |
self |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
750 |
processLine:lineString |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
751 |
encoding:encoding |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
752 |
file:(inStream isFileStream |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
753 |
ifTrue:[inStream pathName] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
754 |
ifFalse:['---']) |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
755 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
756 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
757 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
758 |
] |
55f57cb8551b
(notNil and:[isEmpty not]) -> (size > 0)
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
759 |
] |
0 | 760 |
]. |
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
761 |
^ ok |
177 | 762 |
|
2291
eaac1c5f2a61
show fileName if a line has error.
Claus Gittinger <cg@exept.de>
parents:
2153
diff
changeset
|
763 |
"Modified: / 31.8.1995 / 02:33:45 / claus" |
eaac1c5f2a61
show fileName if a line has error.
Claus Gittinger <cg@exept.de>
parents:
2153
diff
changeset
|
764 |
"Modified: / 30.8.1998 / 12:35:24 / cg" |
0 | 765 |
! ! |
766 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
767 |
!ResourcePack class methodsFor:'documentation'! |
251 | 768 |
|
769 |
version |
|
3097 | 770 |
^ '$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.60 1999-12-09 09:13:06 cg Exp $' |
251 | 771 |
! ! |
0 | 772 |
ResourcePack initialize! |