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