author | Claus Gittinger <cg@exept.de> |
Tue, 19 Aug 2003 12:01:11 +0200 | |
changeset 1292 | cf61a7dfeca6 |
parent 1285 | 0b70d88e4a74 |
child 1293 | 4504cf3db7d0 |
permissions | -rw-r--r-- |
884 | 1 |
" |
2 |
COPYRIGHT (c) 1996 eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
" |
|
12 |
||
13 |
||
14 |
||
981 | 15 |
"{ Package: 'stx:libbasic2' }" |
16 |
||
363 | 17 |
Object subclass:#UnitConverter |
18 |
instanceVariableNames:'' |
|
516 | 19 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 20 |
poolDictionaries:'' |
21 |
category:'Magnitude-General' |
|
22 |
! |
|
23 |
||
24 |
!UnitConverter class methodsFor:'documentation'! |
|
25 |
||
884 | 26 |
copyright |
27 |
" |
|
28 |
COPYRIGHT (c) 1996 eXept Software AG |
|
29 |
All Rights Reserved |
|
30 |
||
31 |
This software is furnished under a license and may be used |
|
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
||
39 |
||
40 |
! |
|
41 |
||
363 | 42 |
documentation |
43 |
" |
|
44 |
In order to collect various unit conversions into one central |
|
45 |
manageable (and especially: browsable) place, all previously |
|
564 | 46 |
spread knowledge about metric (and other) conversions has been |
363 | 47 |
brought into this class. |
48 |
||
49 |
This class is purely functional, abstract and has no instances; |
|
50 |
all queries are via class protocol. |
|
51 |
Choosing the Magnitudes category as its home is arbitrary. |
|
52 |
||
515 | 53 |
The supported units are setup in the classes initialize method; |
364 | 54 |
supported are: |
886 | 55 |
meter - inch - feet - foot - yard |
56 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
57 |
liter - gallon - barrel - floz |
|
58 |
celsius - fahrenheit |
|
364 | 59 |
and many others. |
60 |
||
515 | 61 |
The converter does a recursive search for a conversion; |
62 |
thus, if there is a conversion from #inch to #millimeter and another |
|
63 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
64 |
is found automatically. |
|
65 |
||
886 | 66 |
No Warranty: |
67 |
The numbers and conversion factors were obtained from the Unix |
|
68 |
units command. Please check before using it - there might be |
|
69 |
typos or wrong conversions in the setup code. |
|
70 |
||
71 |
||
363 | 72 |
[author:] |
886 | 73 |
Claus Gittinger |
363 | 74 |
|
75 |
see also: |
|
886 | 76 |
examples |
77 |
/usr/share/lib/unittab (if present on your system) |
|
363 | 78 |
" |
79 |
! |
|
80 |
||
81 |
examples |
|
82 |
" |
|
83 |
ever wanted to know, how many floz's are there in a european |
|
84 |
Coce bottle ? |
|
982 | 85 |
[exBegin] |
86 |
Transcript showCR: |
|
87 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
88 |
[exEnd] |
|
89 |
||
90 |
[exBegin] |
|
91 |
Transcript showCR: |
|
92 |
(UnitConverter convert:43.5 from:#oz to:#gram) |
|
93 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
94 |
|
363 | 95 |
or, how many square-meters an acre is: |
982 | 96 |
[exBegin] |
97 |
Transcript showCR: |
|
98 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
|
99 |
[exEnd] |
|
363 | 100 |
|
516 | 101 |
|
515 | 102 |
or europeans might want to know, what those |
103 |
fahrenheit numbers mean in an US weather report ;-): |
|
982 | 104 |
[exBegin] |
105 |
Transcript showCR:( |
|
106 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
107 |
asFixedPoint:1) |
|
108 |
[exEnd] |
|
515 | 109 |
|
516 | 110 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
111 |
how fast do I drive ? :-) |
982 | 112 |
[exBegin] |
113 |
Transcript showCR:( |
|
114 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
115 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
116 |
how fast does Chris drive ? :-) |
982 | 117 |
[exBegin] |
118 |
Transcript showCR:( |
|
119 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
120 |
[exEnd] |
|
516 | 121 |
|
122 |
||
123 |
calories or joule ? |
|
982 | 124 |
[exBegin] |
125 |
Transcript showCR:( |
|
126 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
127 |
[exEnd] |
|
128 |
[exBegin] |
|
129 |
Transcript showCR:( |
|
130 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
131 |
[exEnd] |
|
516 | 132 |
|
133 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
134 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
135 |
|
982 | 136 |
[exBegin] |
137 |
Transcript showCR:( |
|
138 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
|
139 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
140 |
thats the same: |
982 | 141 |
[exBegin] |
142 |
Transcript showCR:( |
|
143 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
|
144 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
145 |
a days travel ... |
982 | 146 |
[exBegin] |
147 |
Transcript showCR:( |
|
148 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
|
149 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
150 |
a year travel ... |
982 | 151 |
[exBegin] |
152 |
Transcript showCR:( |
|
153 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
|
154 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
155 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
156 |
|
363 | 157 |
real estate buyers might want to know, how many acres |
158 |
a german ar is: |
|
982 | 159 |
[exBegin] |
160 |
Transcript showCR: |
|
161 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
162 |
[exEnd] |
|
516 | 163 |
- or how many square-feet are there in your living room: |
982 | 164 |
[exBegin] |
165 |
Transcript showCR: |
|
166 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
167 |
[exEnd] |
|
1031 | 168 |
- or how about: |
169 |
[exBegin] |
|
170 |
Transcript showCR: |
|
171 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
172 |
[exEnd] |
|
173 |
||
516 | 174 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
175 |
how many tea spoons are there in a cubic meter ? |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
176 |
(roughly, since a teaspoon is not a standard unit) |
982 | 177 |
[exBegin] |
178 |
Transcript showCR: |
|
179 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
180 |
[exEnd] |
|
365 | 181 |
how wide is a US page in inches: |
982 | 182 |
[exBegin] |
183 |
Transcript showCR: |
|
184 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
185 |
[exEnd] |
|
516 | 186 |
- in millimeter: |
982 | 187 |
[exBegin] |
188 |
Transcript showCR: |
|
189 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
190 |
[exEnd] |
|
516 | 191 |
|
192 |
||
365 | 193 |
the height of a US page in inches: |
982 | 194 |
[exBegin] |
195 |
Transcript showCR: |
|
196 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
197 |
[exEnd] |
|
516 | 198 |
- in millimeter: |
982 | 199 |
[exBegin] |
200 |
Transcript showCR: |
|
201 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
202 |
[exEnd] |
|
516 | 203 |
|
204 |
||
365 | 205 |
the same for european A4 standard page: |
982 | 206 |
[exBegin] |
207 |
Transcript showCR: |
|
208 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
209 |
[exEnd] |
|
210 |
[exBegin] |
|
211 |
Transcript showCR: |
|
212 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
213 |
[exEnd] |
|
363 | 214 |
" |
215 |
! ! |
|
216 |
||
217 |
!UnitConverter class methodsFor:'initialization'! |
|
218 |
||
550 | 219 |
initializeConversions |
363 | 220 |
"initialize common conversions" |
221 |
||
1135 | 222 |
self initializeScaleFactors. |
223 |
||
363 | 224 |
Conversions := IdentityDictionary new. |
225 |
Aliases := IdentityDictionary new. |
|
516 | 226 |
Constants := IdentityDictionary new. |
363 | 227 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
228 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
229 |
|
556 | 230 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
231 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
232 |
|
363 | 233 |
"/ -------------- length ------------- |
234 |
||
235 |
Aliases at:#km put:#kilometer. |
|
236 |
Aliases at:#m put:#meter. |
|
237 |
Aliases at:#dm put:#decimeter. |
|
238 |
Aliases at:#cm put:#centimeter. |
|
239 |
Aliases at:#mm put:#millimeter. |
|
240 |
Aliases at:#micron put:#micrometer. |
|
241 |
Aliases at:#nm put:#nanometer. |
|
242 |
Aliases at:#angstrom put:#decinanometer. |
|
243 |
||
244 |
"/ US |
|
245 |
self addConversion:12 from:#foot to:#inch. |
|
246 |
self addConversion:3 from:#yard to:#foot. |
|
247 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 248 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 249 |
|
250 |
Aliases at:#feet put:#foot. |
|
251 |
Aliases at:#ft put:#foot. |
|
252 |
Aliases at:#yd put:#yard. |
|
253 |
Aliases at:#mi put:#mile. |
|
254 |
||
255 |
"/ inch to millimeter |
|
256 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
257 |
||
365 | 258 |
"/ nautic |
259 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
260 |
||
556 | 261 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
262 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
263 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
264 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 265 |
|
516 | 266 |
"/ ---------- time ------------- |
267 |
||
268 |
self addConversion:60 from:#min to:#s. |
|
269 |
self addConversion:60 from:#hr to:#min. |
|
270 |
self addConversion:24 from:#dy to:#hr. |
|
271 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
272 |
||
273 |
||
385 | 274 |
"/ ---------- printing ------------- |
1135 | 275 |
self initializePrintValues. |
365 | 276 |
|
1135 | 277 |
"/ ---------------- area ------------------- |
278 |
||
279 |
"/ US |
|
280 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
281 |
||
282 |
self addConversion:100 from:#are to:#'meter^2'. |
|
283 |
self addConversion:100 from:#hectare to:#are. |
|
284 |
||
285 |
"/ german area - add your countries, and return to me ... |
|
286 |
Aliases at:#'german-ar' put:#are. |
|
287 |
Aliases at:#'german-hektar' put:#hectare. |
|
288 |
||
289 |
||
290 |
"/ ---------------- liquid ---------------- |
|
291 |
||
292 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
293 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
294 |
"/ thats also 1/4th of a good wine ;-) |
|
295 |
self addConversion:(1/2) from:#pint to:#quart. |
|
296 |
self addConversion:(1/16) from:#floz to:#pint. |
|
297 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
298 |
||
299 |
self addConversion:42 from:#barrel to:#gallon. |
|
300 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
301 |
||
302 |
Aliases at:#cc put:#'cm^3'. |
|
303 |
Aliases at:#liter put:#kilocc. |
|
304 |
Aliases at:#ml put:#milliliter. |
|
305 |
Aliases at:#gal put:#gallon. |
|
306 |
Aliases at:#qt put:#quart. |
|
307 |
||
308 |
||
309 |
"/ ---------------- mass ---------------- |
|
310 |
||
311 |
self addConversion:28.35 from:#ounce to:#gram. |
|
312 |
self addConversion:453.59237 from:#lb to:#gram. |
|
313 |
self addConversion:205 from:#carat to:#milligram. |
|
314 |
||
315 |
Aliases at:#oz put:#ounce. |
|
316 |
Aliases at:#lbs put:#lb. |
|
317 |
||
318 |
Aliases at:#gm put:#gram. |
|
319 |
Aliases at:#kg put:#kilogram. |
|
320 |
||
321 |
"/ well, a pint of beer is not always: |
|
322 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
323 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
324 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
325 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
326 |
||
327 |
"/ ---------------- energy ---------------- |
|
328 |
||
329 |
Aliases at:#cal put:#calorie. |
|
330 |
Aliases at:#nt put:#newton. |
|
331 |
Aliases at:#joule put:#'nt*m'. |
|
332 |
||
333 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
334 |
||
335 |
||
336 |
"/ ---------------- cooking ---------------- |
|
337 |
||
338 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
339 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
340 |
||
341 |
||
342 |
"/ ---------------- temperature ---------------- |
|
343 |
||
344 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
|
345 |
self addConversion:[:f | f - 32 / 1.8] from:#fahrenheit to:#celsius. |
|
346 |
||
347 |
" |
|
348 |
Conversions := nil. |
|
349 |
UnitConverter initializeConversions |
|
350 |
" |
|
351 |
||
352 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
353 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
354 |
! |
|
355 |
||
356 |
initializePrintValues |
|
363 | 357 |
"/ inch to (roughly) a typesetter point |
358 |
self addConversion:(1/72) from:#point to:#inch. |
|
359 |
||
360 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
361 |
self addConversion:(1/20) from:#twip to:#point. |
|
362 |
||
365 | 363 |
"/ US paper |
364 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
365 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
366 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
367 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
368 |
||
385 | 369 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
370 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
371 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
372 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
373 |
||
374 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
375 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
376 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
377 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
378 |
||
365 | 379 |
"/ European paper |
380 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
381 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
382 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
383 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
384 |
||
385 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
386 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
387 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
388 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
389 |
||
390 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
391 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
392 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
393 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
394 |
||
395 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
396 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
397 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
398 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
399 |
||
400 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
401 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
402 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
403 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
404 |
||
405 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
406 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
407 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
408 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
409 |
||
385 | 410 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
411 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
412 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
413 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
414 |
||
415 |
||
365 | 416 |
Aliases at:#letterW put:#'letter-page-width'. |
417 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 418 |
Aliases at:#legalW put:#'legal-page-width'. |
419 |
Aliases at:#legalH put:#'legal-page-height'. |
|
420 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
421 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 422 |
Aliases at:#a1W put:#'a1-page-width'. |
423 |
Aliases at:#a1H put:#'a1-page-height'. |
|
424 |
Aliases at:#a2W put:#'a2-page-width'. |
|
425 |
Aliases at:#a2H put:#'a2-page-height'. |
|
426 |
Aliases at:#a3W put:#'a3-page-width'. |
|
427 |
Aliases at:#a3H put:#'a3-page-height'. |
|
428 |
Aliases at:#a4W put:#'a4-page-width'. |
|
429 |
Aliases at:#a4H put:#'a4-page-height'. |
|
430 |
Aliases at:#a5W put:#'a5-page-width'. |
|
431 |
Aliases at:#a5H put:#'a5-page-height'. |
|
432 |
Aliases at:#a6W put:#'a6-page-width'. |
|
433 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 434 |
Aliases at:#b5W put:#'b5-page-width'. |
435 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 436 |
|
437 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
438 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 439 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
440 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
441 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
442 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 443 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
444 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
445 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
446 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
447 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
448 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
449 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
450 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
451 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
452 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
453 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
454 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 455 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
456 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 457 |
! |
363 | 458 |
|
1135 | 459 |
initializeScaleFactors |
460 |
Scaling := IdentityDictionary new. |
|
363 | 461 |
|
1285 | 462 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
463 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
464 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
465 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
466 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
467 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
468 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 469 |
Scaling at:#myria put:10000. |
1285 | 470 |
Scaling at:#kilo put:1000. "/ 10^3 |
471 |
Scaling at:#hecto put:100. "/ 10^2 |
|
472 |
Scaling at:#deca put:10. "/ 10^1 |
|
1135 | 473 |
Scaling at:#deci put:(1/10). |
474 |
Scaling at:#centi put:(1/100). |
|
475 |
Scaling at:#milli put:(1/1000). |
|
476 |
Scaling at:#micro put:(1/1000000). |
|
477 |
Scaling at:#nano put:(1/1000000000). |
|
478 |
Scaling at:#pico put:(1/1000000000000). |
|
479 |
Scaling at:#femto put:(1/1000000000000000). |
|
480 |
Scaling at:#atto put:(1/1000000000000000000). |
|
1285 | 481 |
Scaling at:#zepto put:(1/1000000000000000000000). |
482 |
Scaling at:#yocto put:(1/1000000000000000000000000). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
483 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
484 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
485 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
486 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
487 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
488 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
489 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
490 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
491 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
492 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
493 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
494 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
495 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
496 |
"Created: 6.8.1997 / 16:53:19 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
497 |
"Modified: 6.8.1997 / 16:58:11 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
498 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
499 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
500 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
501 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
502 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
503 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
504 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
505 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
506 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 507 |
setOfUnits add:srcUnit. |
508 |
conversionInfo keysDo:[:targetUnit | |
|
509 |
setOfUnits add:targetUnit |
|
510 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
511 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
512 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
513 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
514 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
515 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
516 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
517 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
518 |
"Created: 6.8.1997 / 16:57:08 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
519 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 520 |
! ! |
521 |
||
522 |
!UnitConverter class methodsFor:'conversions'! |
|
523 |
||
524 |
convert:howMany from:sourceUnit to:destUnit |
|
525 |
"given a value in sourceUnit (symbolic), try to convert it |
|
526 |
to destUnit (symbolic); return nil, if the conversion is |
|
527 |
unknown." |
|
528 |
||
516 | 529 |
|u conversions alias rslt sU dU const val unit |
530 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
531 |
sF1 sF2 dF1 dF2 |
|
532 |
s d| |
|
363 | 533 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
534 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
535 |
|
550 | 536 |
Conversions isNil ifTrue:[ |
1031 | 537 |
self initializeConversions |
550 | 538 |
]. |
539 |
||
363 | 540 |
"/ somehow, recursion must end ... |
541 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 542 |
^ howMany |
363 | 543 |
]. |
544 |
||
369 | 545 |
sourceUnit isSymbol ifFalse:[ |
1031 | 546 |
s := sourceUnit withoutSeparators. |
547 |
sU := s asSymbolIfInterned. |
|
548 |
sU notNil ifTrue:[ |
|
549 |
^ self convert:howMany from:sU to:destUnit |
|
550 |
]. |
|
551 |
(s startsWith:$() ifTrue:[ |
|
552 |
(s endsWith:$)) ifTrue:[ |
|
553 |
s := s copyFrom:2 to:(s size - 1). |
|
554 |
^ self convert:howMany from:s to:destUnit |
|
555 |
] |
|
556 |
]. |
|
369 | 557 |
]. |
558 |
destUnit isSymbol ifFalse:[ |
|
1031 | 559 |
d := destUnit withoutSeparators. |
560 |
dU := d asSymbolIfInterned. |
|
561 |
dU notNil ifTrue:[ |
|
562 |
^ self convert:howMany from:sourceUnit to:dU |
|
563 |
]. |
|
564 |
(d startsWith:$() ifTrue:[ |
|
565 |
(d endsWith:$)) ifTrue:[ |
|
566 |
d := d copyFrom:2 to:(d size - 1). |
|
567 |
^ self convert:howMany from:sourceUnit to:d |
|
568 |
] |
|
569 |
]. |
|
369 | 570 |
]. |
571 |
||
363 | 572 |
"/ first, get rid of scalers ... |
573 |
||
574 |
u := self unscaled:sourceUnit. |
|
575 |
u notNil ifTrue:[ |
|
1031 | 576 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 577 |
]. |
578 |
||
579 |
u := self unscaled:destUnit. |
|
580 |
u notNil ifTrue:[ |
|
1031 | 581 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 582 |
]. |
583 |
||
584 |
"/ and of aliases ... |
|
585 |
||
586 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
587 |
alias notNil ifTrue:[ |
|
1031 | 588 |
^ self convert:howMany from:alias to:destUnit |
363 | 589 |
]. |
590 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
591 |
alias notNil ifTrue:[ |
|
1031 | 592 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 593 |
]. |
594 |
||
516 | 595 |
"/ any constants ? |
596 |
||
597 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 598 |
const := Constants at:sourceUnit. |
599 |
val := const at:1. |
|
600 |
unit := const at:2. |
|
601 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 602 |
]. |
603 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
604 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
605 |
"/ need a full expression parser (tree) for full power. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
606 |
"/ I leave that as an excercise to you ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
607 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
608 |
|
363 | 609 |
"/ working with squares or cubics ? |
610 |
||
516 | 611 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
1031 | 612 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
613 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
614 |
(sU notNil and:[dU notNil]) ifTrue:[ |
|
615 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
616 |
]. |
|
617 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 618 |
]. |
516 | 619 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
1031 | 620 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
621 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
622 |
(sU notNil and:[dU notNil]) ifTrue:[ |
|
623 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
624 |
]. |
|
625 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 626 |
]. |
627 |
||
628 |
"/ working with fractions ? |
|
629 |
||
630 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 631 |
"/ look for a constant conversion factor |
632 |
i := sourceUnit indexOf:$/. |
|
633 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
634 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
635 |
i := destUnit indexOf:$/. |
|
636 |
duNumerator := destUnit copyTo:(i - 1). |
|
637 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 638 |
|
1031 | 639 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
640 |
uN notNil ifTrue:[ |
|
641 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
642 |
uD notNil ifTrue:[ |
|
643 |
^ uN / uD |
|
644 |
] |
|
645 |
]. |
|
516 | 646 |
]. |
647 |
||
648 |
"/ working with products ? |
|
649 |
||
650 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 651 |
i := sourceUnit indexOf:$*. |
652 |
sF1 := sourceUnit copyTo:(i - 1). |
|
653 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
654 |
i := destUnit indexOf:$*. |
|
655 |
dF1 := destUnit copyTo:(i - 1). |
|
656 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 657 |
|
1031 | 658 |
u := self convert:howMany from:sF1 to:dF1. |
659 |
u notNil ifTrue:[ |
|
660 |
u := self convert:u from:sF2 to:dF2. |
|
661 |
u notNil ifTrue:[ |
|
662 |
^ u |
|
663 |
] |
|
664 |
]. |
|
363 | 665 |
]. |
666 |
||
667 |
"/ the real work comes here ... |
|
668 |
||
669 |
"/ is there a direct conversion in the dataBase ? |
|
670 |
||
671 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
672 |
rslt notNil ifTrue:[^ rslt]. |
|
673 |
||
674 |
"/ try inverse conversion ... |
|
675 |
||
676 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
677 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
678 |
||
516 | 679 |
"/ here's the deep recursion ... |
363 | 680 |
|
681 |
||
682 |
"/ try indirect conversion from source |
|
683 |
||
684 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
685 |
conversions notNil ifTrue:[ |
|
1031 | 686 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
687 |
|factor2| |
|
363 | 688 |
|
1031 | 689 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
690 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
691 |
]. |
|
363 | 692 |
]. |
693 |
||
694 |
"/ try indirect conversion from dest |
|
695 |
||
696 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
697 |
conversions notNil ifTrue:[ |
|
1031 | 698 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
699 |
|factor2| |
|
363 | 700 |
|
1031 | 701 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
702 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
703 |
]. |
|
363 | 704 |
]. |
705 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
706 |
"/ if working with a product, try each component |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
707 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
708 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 709 |
i := sourceUnit indexOf:$*. |
710 |
sF1 := sourceUnit copyTo:(i - 1). |
|
711 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
712 |
|
1031 | 713 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
714 |
|
1031 | 715 |
self units do:[:aUnit | |
716 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
717 |
|
1031 | 718 |
pref := sF1 , '*'. |
719 |
(aUnit startsWith:pref) ifTrue:[ |
|
720 |
"/ ok; want a/b -> x |
|
721 |
"/ found a/c -> any |
|
722 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
723 |
|
1031 | 724 |
iUnit := aUnit copyFrom:pref size + 1. |
725 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
726 |
factor2 notNil ifTrue:[ |
|
727 |
"/ good ... |
|
728 |
rslt := self convert:(factor2 * howMany) |
|
729 |
from:aUnit to:destUnit. |
|
730 |
rslt notNil ifTrue:[^ rslt]. |
|
731 |
] |
|
732 |
] |
|
733 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
734 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
735 |
|
516 | 736 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 737 |
|
738 |
"direct - how many meters are there in two inches: |
|
739 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 740 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 741 |
|
742 |
reverse - how many inches are there in one meter |
|
743 |
UnitConverter convert:1 from:#meter to:#inch |
|
744 |
||
745 |
with alias: |
|
746 |
UnitConverter convert:1 from:#inch to:#m |
|
747 |
UnitConverter convert:1 from:#inch to:#mm |
|
748 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
749 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
750 |
UnitConverter convert:1 from:#mm to:#km |
|
751 |
UnitConverter convert:1 from:#km to:#foot |
|
752 |
||
753 |
indirect: |
|
754 |
UnitConverter convert:1 from:#mm to:#twip |
|
755 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 756 |
UnitConverter convert:1 from:'letterH' to:#point |
757 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 758 |
|
759 |
UnitConverter convert:5 from:#barrel to:#liter |
|
760 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
761 |
||
762 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 763 |
" |
764 |
||
765 |
"Created: 31.5.1996 / 16:23:38 / cg" |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
766 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 767 |
! |
768 |
||
1292 | 769 |
fileSizeFromString:fileSizeString |
770 |
"given a fileSize string (i.e. a number followed by optional Kb, Mb or Gb as required. |
|
771 |
Provided here since this is so common..." |
|
772 |
||
773 |
|numericValue s scaleChar scale| |
|
774 |
||
775 |
fileSizeString isEmpty ifTrue:[ |
|
776 |
^ 0 |
|
777 |
]. |
|
778 |
||
779 |
scale := 1. |
|
780 |
s := fileSizeString readStream. |
|
781 |
numericValue := Number readFrom:s onError:[0]. |
|
782 |
s skipSeparators. |
|
783 |
s atEnd ifFalse:[ |
|
784 |
scaleChar := s next asLowercase. |
|
785 |
scaleChar = $k ifTrue:[ |
|
786 |
scale := 1024 |
|
787 |
] ifFalse:[ |
|
788 |
scaleChar = $m ifTrue:[ |
|
789 |
scale := 1024 * 1024 |
|
790 |
] ifFalse:[ |
|
791 |
scaleChar = $g ifTrue:[ |
|
792 |
scale := 1024 * 1024 * 1024 |
|
793 |
] ifFalse:[ |
|
794 |
scaleChar = $t ifTrue:[ |
|
795 |
scale := 1024 * 1024 * 1024 * 1024 |
|
796 |
] |
|
797 |
] |
|
798 |
] |
|
799 |
]. |
|
800 |
]. |
|
801 |
^ (numericValue*scale) asInteger |
|
802 |
||
803 |
" |
|
804 |
self fileSizeFromString:'1234' |
|
805 |
self fileSizeFromString:'10 kB' |
|
806 |
self fileSizeFromString:'0.6 kB' |
|
807 |
self fileSizeFromString:'1.8 MB' |
|
808 |
self fileSizeFromString:'0.5 GB' |
|
809 |
self fileSizeFromString:'0.5 TB' |
|
810 |
" |
|
811 |
! |
|
812 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
813 |
fileSizeStringFor:nBytes |
1143 | 814 |
"return a useful string for a size-in-bytes; returns Kb, Mb or Gb as required. |
815 |
Provided here since this is so common..." |
|
816 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
817 |
|unitString n| |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
818 |
|
1148 | 819 |
nBytes < (1000 * 1024 * 1024) ifTrue:[ |
820 |
nBytes < (1000 * 1024) ifTrue:[ |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
821 |
nBytes < 1024 ifTrue:[ |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
822 |
n := nBytes. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
823 |
unitString := ' '. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
824 |
] ifFalse:[ |
1161 | 825 |
n := (nBytes / 1024) asFixedPoint:1. |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
826 |
unitString := ' K' |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
827 |
] |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
828 |
] ifFalse:[ |
1161 | 829 |
n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
830 |
unitString := ' M' |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
831 |
]. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
832 |
] ifFalse:[ |
1161 | 833 |
n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
834 |
unitString := ' G' |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
835 |
]. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
836 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
837 |
^ n printString , unitString. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
838 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
839 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
840 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
841 |
self fileSizeStringFor:100 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
842 |
self fileSizeStringFor:1000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
843 |
self fileSizeStringFor:10000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
844 |
self fileSizeStringFor:100000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
845 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
846 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
847 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
848 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
849 |
self fileSizeStringFor:10000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
850 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
851 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
852 |
|
363 | 853 |
inchToMillimeter:inches |
364 | 854 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 855 |
|
856 |
^ inches * 25.4 |
|
857 |
||
858 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 859 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 860 |
! |
861 |
||
862 |
millimeterToInch:mm |
|
364 | 863 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 864 |
|
865 |
^ mm / 25.4 |
|
866 |
||
867 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 868 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
363 | 869 |
! ! |
870 |
||
516 | 871 |
!UnitConverter class methodsFor:'missing conversion'! |
872 |
||
873 |
noConversionFrom:sourceUnit to:destUnit |
|
874 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
875 |
^ nil |
|
876 |
||
877 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
878 |
! ! |
|
879 |
||
363 | 880 |
!UnitConverter class methodsFor:'private'! |
881 |
||
882 |
addConversion:factor from:sourceMetric to:destMetric |
|
883 |
"add a conversion" |
|
884 |
||
885 |
|conversion| |
|
886 |
||
887 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
888 |
conversion isNil ifTrue:[ |
|
601 | 889 |
conversion := IdentityDictionary new. |
890 |
Conversions at:sourceMetric put:conversion |
|
363 | 891 |
]. |
892 |
conversion at:destMetric put:factor. |
|
893 |
^ conversion |
|
894 |
||
895 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 896 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 897 |
! |
898 |
||
899 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
900 |
"given a value in sourceMetric (symbolic), try to convert it |
|
901 |
to destMetric (symbolic); |
|
902 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
903 |
||
904 |
|conversion factor| |
|
905 |
||
550 | 906 |
Conversions isNil ifTrue:[ |
601 | 907 |
self initializeConversions |
550 | 908 |
]. |
909 |
||
363 | 910 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
911 |
conversion isNil ifTrue:[^ nil]. |
|
912 |
||
913 |
factor := conversion at:destMetric ifAbsent:nil. |
|
914 |
factor isNil ifTrue:[^ nil]. |
|
915 |
||
364 | 916 |
factor isNumber ifTrue:[ |
601 | 917 |
^ howMany * factor |
364 | 918 |
]. |
515 | 919 |
^ factor value:howMany |
363 | 920 |
|
921 |
" |
|
922 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
923 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 924 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 925 |
" |
926 |
||
927 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 928 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 929 |
! |
930 |
||
931 |
unscaled:what |
|
932 |
"given a unit, return the base and a factor as assoc, |
|
933 |
or nil if not found" |
|
934 |
||
550 | 935 |
Conversions isNil ifTrue:[ |
601 | 936 |
self initializeConversions |
550 | 937 |
]. |
938 |
||
363 | 939 |
Scaling keysAndValuesDo:[:name :factor | |
601 | 940 |
|rest| |
363 | 941 |
|
601 | 942 |
(what startsWith:name) ifTrue:[ |
943 |
rest := what copyFrom:(name size+1). |
|
944 |
rest := rest asSymbolIfInterned. |
|
945 |
rest notNil ifTrue:[ |
|
946 |
^ rest -> factor. |
|
947 |
] |
|
948 |
]. |
|
363 | 949 |
]. |
950 |
^ nil |
|
951 |
||
952 |
" |
|
953 |
UnitConverter unscaled:#millimeter |
|
954 |
UnitConverter unscaled:#nanometer |
|
955 |
UnitConverter unscaled:#kilometer |
|
956 |
UnitConverter unscaled:#fuzzymeter |
|
957 |
" |
|
958 |
||
550 | 959 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 960 |
! ! |
961 |
||
962 |
!UnitConverter class methodsFor:'documentation'! |
|
963 |
||
964 |
version |
|
1292 | 965 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.28 2003-08-19 10:01:11 cg Exp $' |
363 | 966 |
! ! |