author | Claus Gittinger <cg@exept.de> |
Fri, 17 Nov 2017 10:33:27 +0100 | |
changeset 4535 | 774a428df2cb |
parent 4515 | 34fbe1d0b79f |
child 4557 | bd89a041f1db |
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 |
" |
|
981 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3486 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
363 | 16 |
Object subclass:#UnitConverter |
17 |
instanceVariableNames:'' |
|
516 | 18 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-General' |
|
21 |
! |
|
22 |
||
23 |
!UnitConverter class methodsFor:'documentation'! |
|
24 |
||
884 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1996 eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
||
39 |
! |
|
40 |
||
363 | 41 |
documentation |
42 |
" |
|
43 |
In order to collect various unit conversions into one central |
|
44 |
manageable (and especially: browsable) place, all previously |
|
564 | 45 |
spread knowledge about metric (and other) conversions has been |
363 | 46 |
brought into this class. |
47 |
||
48 |
This class is purely functional, abstract and has no instances; |
|
49 |
all queries are via class protocol. |
|
50 |
Choosing the Magnitudes category as its home is arbitrary. |
|
51 |
||
515 | 52 |
The supported units are setup in the classes initialize method; |
364 | 53 |
supported are: |
886 | 54 |
meter - inch - feet - foot - yard |
55 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
56 |
liter - gallon - barrel - floz |
|
57 |
celsius - fahrenheit |
|
364 | 58 |
and many others. |
59 |
||
515 | 60 |
The converter does a recursive search for a conversion; |
61 |
thus, if there is a conversion from #inch to #millimeter and another |
|
62 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
63 |
is found automatically. |
|
64 |
||
886 | 65 |
No Warranty: |
66 |
The numbers and conversion factors were obtained from the Unix |
|
67 |
units command. Please check before using it - there might be |
|
68 |
typos or wrong conversions in the setup code. |
|
69 |
||
70 |
||
1323 | 71 |
Notice: |
72 |
This class is *much* older than the younger and more object oriented |
|
73 |
Measurements package. Other than solving similar problems, these two packages |
|
74 |
are not related. |
|
75 |
||
76 |
||
363 | 77 |
[author:] |
886 | 78 |
Claus Gittinger |
363 | 79 |
|
80 |
see also: |
|
886 | 81 |
examples |
82 |
/usr/share/lib/unittab (if present on your system) |
|
1323 | 83 |
Measurement Unit |
363 | 84 |
" |
85 |
! |
|
86 |
||
87 |
examples |
|
88 |
" |
|
89 |
ever wanted to know, how many floz's are there in a european |
|
90 |
Coce bottle ? |
|
982 | 91 |
[exBegin] |
92 |
Transcript showCR: |
|
93 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
94 |
[exEnd] |
|
95 |
||
96 |
[exBegin] |
|
97 |
Transcript showCR: |
|
98 |
(UnitConverter convert:43.5 from:#oz to:#gram) |
|
99 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
100 |
|
363 | 101 |
or, how many square-meters an acre is: |
982 | 102 |
[exBegin] |
103 |
Transcript showCR: |
|
104 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
|
105 |
[exEnd] |
|
363 | 106 |
|
516 | 107 |
|
515 | 108 |
or europeans might want to know, what those |
109 |
fahrenheit numbers mean in an US weather report ;-): |
|
982 | 110 |
[exBegin] |
111 |
Transcript showCR:( |
|
112 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
113 |
asFixedPoint:1) |
|
114 |
[exEnd] |
|
515 | 115 |
|
516 | 116 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
117 |
how fast do I drive ? :-) |
982 | 118 |
[exBegin] |
119 |
Transcript showCR:( |
|
120 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
121 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
122 |
how fast does Chris drive ? :-) |
982 | 123 |
[exBegin] |
124 |
Transcript showCR:( |
|
125 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
126 |
[exEnd] |
|
516 | 127 |
|
128 |
||
129 |
calories or joule ? |
|
982 | 130 |
[exBegin] |
131 |
Transcript showCR:( |
|
132 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
133 |
[exEnd] |
|
134 |
[exBegin] |
|
135 |
Transcript showCR:( |
|
136 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
137 |
[exEnd] |
|
516 | 138 |
|
139 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
140 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
141 |
|
982 | 142 |
[exBegin] |
143 |
Transcript showCR:( |
|
144 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
|
145 |
[exEnd] |
|
4092 | 146 |
that's the same: |
982 | 147 |
[exBegin] |
148 |
Transcript showCR:( |
|
149 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
|
150 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
151 |
a days travel ... |
982 | 152 |
[exBegin] |
153 |
Transcript showCR:( |
|
154 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
|
155 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
156 |
a year travel ... |
982 | 157 |
[exBegin] |
158 |
Transcript showCR:( |
|
159 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
|
160 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
161 |
|
2320 | 162 |
wonna race on the autobahn ? |
163 |
[exBegin] |
|
164 |
Transcript showCR:( |
|
165 |
UnitConverter convert:200 from:#'km/h' to:#'mph') |
|
166 |
[exEnd] |
|
2547 | 167 |
[exBegin] |
168 |
Transcript showCR:( |
|
169 |
UnitConverter convert:13 from:#'m/s' to:#'km/h') |
|
170 |
[exEnd] |
|
2320 | 171 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
172 |
|
363 | 173 |
real estate buyers might want to know, how many acres |
174 |
a german ar is: |
|
982 | 175 |
[exBegin] |
176 |
Transcript showCR: |
|
177 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
178 |
[exEnd] |
|
516 | 179 |
- or how many square-feet are there in your living room: |
982 | 180 |
[exBegin] |
181 |
Transcript showCR: |
|
182 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
183 |
[exEnd] |
|
1031 | 184 |
- or how about: |
185 |
[exBegin] |
|
186 |
Transcript showCR: |
|
187 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
188 |
[exEnd] |
|
189 |
||
516 | 190 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
191 |
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
|
192 |
(roughly, since a teaspoon is not a standard unit) |
982 | 193 |
[exBegin] |
194 |
Transcript showCR: |
|
195 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
196 |
[exEnd] |
|
365 | 197 |
how wide is a US page in inches: |
982 | 198 |
[exBegin] |
199 |
Transcript showCR: |
|
200 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
201 |
[exEnd] |
|
516 | 202 |
- in millimeter: |
982 | 203 |
[exBegin] |
204 |
Transcript showCR: |
|
205 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
206 |
[exEnd] |
|
516 | 207 |
|
208 |
||
365 | 209 |
the height of a US page in inches: |
982 | 210 |
[exBegin] |
211 |
Transcript showCR: |
|
212 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
213 |
[exEnd] |
|
516 | 214 |
- in millimeter: |
982 | 215 |
[exBegin] |
216 |
Transcript showCR: |
|
217 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
218 |
[exEnd] |
|
516 | 219 |
|
220 |
||
365 | 221 |
the same for european A4 standard page: |
982 | 222 |
[exBegin] |
223 |
Transcript showCR: |
|
224 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
225 |
[exEnd] |
|
226 |
[exBegin] |
|
227 |
Transcript showCR: |
|
228 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
229 |
[exEnd] |
|
2443 | 230 |
|
231 |
the mass of a proton: |
|
232 |
[exBegin] |
|
233 |
Transcript showCR: |
|
234 |
(UnitConverter convert:1 from:#'proton' to:#eV) |
|
235 |
[exEnd] |
|
236 |
the energy of a single proton in the LHC (as of 2010) |
|
237 |
[exBegin] |
|
238 |
Transcript showCR: |
|
239 |
(UnitConverter convert:7 from:#'TeV' to:#joule) |
|
240 |
[exEnd] |
|
363 | 241 |
" |
242 |
! ! |
|
243 |
||
244 |
!UnitConverter class methodsFor:'initialization'! |
|
245 |
||
550 | 246 |
initializeConversions |
363 | 247 |
"initialize common conversions" |
248 |
||
1135 | 249 |
self initializeScaleFactors. |
250 |
||
363 | 251 |
Conversions := IdentityDictionary new. |
252 |
Aliases := IdentityDictionary new. |
|
516 | 253 |
Constants := IdentityDictionary new. |
363 | 254 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
255 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
256 |
|
556 | 257 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
2443 | 258 |
Constants at:#soundspeed put:#(343 #'m/s'). "/ 20degC - AIR |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
259 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
260 |
|
363 | 261 |
"/ -------------- length ------------- |
262 |
||
263 |
Aliases at:#km put:#kilometer. |
|
264 |
Aliases at:#m put:#meter. |
|
265 |
Aliases at:#dm put:#decimeter. |
|
266 |
Aliases at:#cm put:#centimeter. |
|
267 |
Aliases at:#mm put:#millimeter. |
|
268 |
Aliases at:#micron put:#micrometer. |
|
269 |
Aliases at:#nm put:#nanometer. |
|
270 |
Aliases at:#angstrom put:#decinanometer. |
|
271 |
||
272 |
"/ US |
|
273 |
self addConversion:12 from:#foot to:#inch. |
|
274 |
self addConversion:3 from:#yard to:#foot. |
|
275 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 276 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 277 |
|
2320 | 278 |
Aliases at:#feet put:#foot. |
279 |
Aliases at:#ft put:#foot. |
|
280 |
Aliases at:#yd put:#yard. |
|
281 |
Aliases at:#mi put:#mile. |
|
282 |
Aliases at:#mph put:#'mile/hr'. |
|
363 | 283 |
|
284 |
"/ inch to millimeter |
|
285 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
286 |
||
365 | 287 |
"/ nautic |
288 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
289 |
||
556 | 290 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
291 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
292 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
293 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 294 |
|
516 | 295 |
"/ ---------- time ------------- |
296 |
||
297 |
self addConversion:60 from:#min to:#s. |
|
298 |
self addConversion:60 from:#hr to:#min. |
|
299 |
self addConversion:24 from:#dy to:#hr. |
|
300 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
301 |
||
2320 | 302 |
Aliases at:#h put:#'hr'. |
303 |
Aliases at:#d put:#'dy'. |
|
516 | 304 |
|
385 | 305 |
"/ ---------- printing ------------- |
1135 | 306 |
self initializePrintValues. |
365 | 307 |
|
1135 | 308 |
"/ ---------------- area ------------------- |
309 |
||
310 |
"/ US |
|
311 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
312 |
||
313 |
self addConversion:100 from:#are to:#'meter^2'. |
|
314 |
self addConversion:100 from:#hectare to:#are. |
|
315 |
||
3701 | 316 |
"/ german area - add your countries, and return to me ... |
1135 | 317 |
Aliases at:#'german-ar' put:#are. |
318 |
Aliases at:#'german-hektar' put:#hectare. |
|
319 |
||
320 |
||
321 |
"/ ---------------- liquid ---------------- |
|
322 |
||
323 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
324 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
4092 | 325 |
"/ that's also 1/4th of a good wine ;-) |
1135 | 326 |
self addConversion:(1/2) from:#pint to:#quart. |
327 |
self addConversion:(1/16) from:#floz to:#pint. |
|
328 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
329 |
||
330 |
self addConversion:42 from:#barrel to:#gallon. |
|
331 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
332 |
||
333 |
Aliases at:#cc put:#'cm^3'. |
|
334 |
Aliases at:#liter put:#kilocc. |
|
335 |
Aliases at:#ml put:#milliliter. |
|
336 |
Aliases at:#gal put:#gallon. |
|
337 |
Aliases at:#qt put:#quart. |
|
338 |
||
2320 | 339 |
"/ well, a pint of beer is not always: |
340 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
341 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
342 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
343 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
1135 | 344 |
|
345 |
"/ ---------------- mass ---------------- |
|
346 |
||
347 |
self addConversion:28.35 from:#ounce to:#gram. |
|
348 |
self addConversion:453.59237 from:#lb to:#gram. |
|
349 |
self addConversion:205 from:#carat to:#milligram. |
|
350 |
||
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
351 |
self addConversion:0.938272013 from:#proton to:#GeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
352 |
self addConversion:0.510998910 from:#electron to:#MeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
353 |
self addConversion:0.939565346 from:#neutron to:#GeV. |
2443 | 354 |
|
1135 | 355 |
Aliases at:#oz put:#ounce. |
356 |
Aliases at:#lbs put:#lb. |
|
357 |
||
358 |
Aliases at:#gm put:#gram. |
|
359 |
Aliases at:#kg put:#kilogram. |
|
360 |
||
361 |
"/ ---------------- energy ---------------- |
|
362 |
||
363 |
Aliases at:#cal put:#calorie. |
|
364 |
Aliases at:#nt put:#newton. |
|
365 |
Aliases at:#joule put:#'nt*m'. |
|
2320 | 366 |
Aliases at:#J put:#'joule'. |
367 |
Aliases at:#N put:#'newton'. |
|
2443 | 368 |
Aliases at:#eV put:#'electronvolt'. |
369 |
Aliases at:#MeV put:#'megaeV'. |
|
370 |
Aliases at:#GeV put:#'gigaeV'. |
|
371 |
Aliases at:#TeV put:#'teraeV'. |
|
2320 | 372 |
|
373 |
Aliases at:#watt put:#'J/s'. |
|
1135 | 374 |
|
375 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
2443 | 376 |
self addConversion:1.60217653E-19 from:#electronvolt to:#joule. |
1135 | 377 |
|
378 |
||
379 |
"/ ---------------- cooking ---------------- |
|
380 |
||
381 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
382 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
383 |
||
384 |
||
385 |
"/ ---------------- temperature ---------------- |
|
386 |
||
4514 | 387 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
2443 | 388 |
self addConversion:[:f | (f - 32) / 1.8] from:#fahrenheit to:#celsius. |
4514 | 389 |
self addConversion:[:d | d + 273.15] from:#celsius to:#kelvin. |
390 |
self addConversion:[:k | k - 273.15] from:#kelvin to:#celsius. |
|
1135 | 391 |
|
3701 | 392 |
"/ ---------------- nature ---------------- |
393 |
Constants at:#planckLength put:#(1.616e-35 #'m'). |
|
394 |
Constants at:#planckMass put:#(2.176-8 #'Kg'). |
|
395 |
Constants at:#planckTime put:#(5.391e-44 #'s'). |
|
396 |
||
1135 | 397 |
" |
398 |
Conversions := nil. |
|
399 |
UnitConverter initializeConversions |
|
400 |
" |
|
401 |
||
4514 | 402 |
"Created: / 22-07-1997 / 13:56:40 / cg" |
403 |
"Modified: / 18-09-2017 / 08:38:14 / cg" |
|
1135 | 404 |
! |
405 |
||
406 |
initializePrintValues |
|
363 | 407 |
"/ inch to (roughly) a typesetter point |
408 |
self addConversion:(1/72) from:#point to:#inch. |
|
409 |
||
410 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
411 |
self addConversion:(1/20) from:#twip to:#point. |
|
412 |
||
365 | 413 |
"/ US paper |
414 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
415 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
416 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
417 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
418 |
||
385 | 419 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
420 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
421 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
422 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
423 |
||
424 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
425 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
426 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
427 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
428 |
||
365 | 429 |
"/ European paper |
430 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
431 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
432 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
433 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
434 |
||
435 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
436 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
437 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
438 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
439 |
||
440 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
441 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
442 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
443 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
444 |
||
445 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
446 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
447 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
448 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
449 |
||
450 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
451 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
452 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
453 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
454 |
||
455 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
456 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
457 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
458 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
459 |
||
385 | 460 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
461 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
462 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
463 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
464 |
||
465 |
||
365 | 466 |
Aliases at:#letterW put:#'letter-page-width'. |
467 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 468 |
Aliases at:#legalW put:#'legal-page-width'. |
469 |
Aliases at:#legalH put:#'legal-page-height'. |
|
470 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
471 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 472 |
Aliases at:#a1W put:#'a1-page-width'. |
473 |
Aliases at:#a1H put:#'a1-page-height'. |
|
474 |
Aliases at:#a2W put:#'a2-page-width'. |
|
475 |
Aliases at:#a2H put:#'a2-page-height'. |
|
476 |
Aliases at:#a3W put:#'a3-page-width'. |
|
477 |
Aliases at:#a3H put:#'a3-page-height'. |
|
478 |
Aliases at:#a4W put:#'a4-page-width'. |
|
479 |
Aliases at:#a4H put:#'a4-page-height'. |
|
480 |
Aliases at:#a5W put:#'a5-page-width'. |
|
481 |
Aliases at:#a5H put:#'a5-page-height'. |
|
482 |
Aliases at:#a6W put:#'a6-page-width'. |
|
483 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 484 |
Aliases at:#b5W put:#'b5-page-width'. |
485 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 486 |
|
487 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
488 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 489 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
490 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
491 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
492 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 493 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
494 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
495 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
496 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
497 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
498 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
499 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
500 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
501 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
502 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
503 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
504 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 505 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
506 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 507 |
! |
363 | 508 |
|
1135 | 509 |
initializeScaleFactors |
510 |
Scaling := IdentityDictionary new. |
|
363 | 511 |
|
1285 | 512 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
513 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
514 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
515 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
516 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
517 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
518 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 519 |
Scaling at:#myria put:10000. |
1285 | 520 |
Scaling at:#kilo put:1000. "/ 10^3 |
521 |
Scaling at:#hecto put:100. "/ 10^2 |
|
522 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 523 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 524 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 525 |
Scaling at:#centi put:(1/100). |
1323 | 526 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
527 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
528 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
529 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
530 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
531 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
532 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
533 |
Scaling at:#yocto put:(1/1000000000000000000000000)."/ 10^-24 |
|
4535 | 534 |
|
535 |
Scaling at:#kibi put:1024. "/ 2^10 |
|
536 |
Scaling at:#mebi put:1048576. "/ 2^20 |
|
537 |
Scaling at:#gibi put:1073741824. "/ 2^30 |
|
538 |
Scaling at:#tebi put:1099511627776. "/ 2^40 |
|
539 |
Scaling at:#pebi put:1125899906842624. "/ 2^50 |
|
540 |
Scaling at:#exbi put:1152921504606846976. "/ 2^60 |
|
541 |
Scaling at:#zebi put:1180591620717411303424. "/ 2^70 |
|
542 |
Scaling at:#yobi put:1208925819614629174706176. "/ 2^80 |
|
543 |
||
544 |
"Modified: / 17-11-2017 / 10:29:22 / cg" |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
545 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
546 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
547 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
548 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
549 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
550 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
551 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
552 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
553 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
554 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
555 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
556 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
557 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
558 |
"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
|
559 |
"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
|
560 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
561 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
562 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
563 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
564 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
565 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
566 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
567 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
568 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 569 |
setOfUnits add:srcUnit. |
570 |
conversionInfo keysDo:[:targetUnit | |
|
571 |
setOfUnits add:targetUnit |
|
572 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
573 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
574 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
575 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
576 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
577 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
578 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
579 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
580 |
"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
|
581 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 582 |
! ! |
583 |
||
584 |
!UnitConverter class methodsFor:'conversions'! |
|
585 |
||
586 |
convert:howMany from:sourceUnit to:destUnit |
|
587 |
"given a value in sourceUnit (symbolic), try to convert it |
|
588 |
to destUnit (symbolic); return nil, if the conversion is |
|
589 |
unknown." |
|
590 |
||
4514 | 591 |
^ self convert:howMany from:sourceUnit to:destUnit pairsAlreadyTried:(OrderedCollection new) |
592 |
||
593 |
"direct - how many meters are there in two inches: |
|
594 |
UnitConverter convert:2 from:#inch to:#meter |
|
595 |
||
596 |
reverse - how many inches are there in one meter |
|
597 |
UnitConverter convert:1 from:#meter to:#inch |
|
598 |
||
599 |
with alias: |
|
600 |
UnitConverter convert:1 from:#inch to:#m |
|
601 |
UnitConverter convert:1 from:#inch to:#mm |
|
602 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
603 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
604 |
UnitConverter convert:1 from:#mm to:#km |
|
605 |
UnitConverter convert:1 from:#km to:#foot |
|
606 |
||
607 |
indirect: |
|
608 |
UnitConverter convert:1 from:#mm to:#twip |
|
609 |
UnitConverter convert:1 from:#inch to:#twip |
|
610 |
UnitConverter convert:1 from:'letterH' to:#point |
|
611 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
612 |
||
613 |
UnitConverter convert:5 from:#barrel to:#liter |
|
614 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
615 |
||
616 |
UnitConverter convert:1 from:#liter to:#floz |
|
617 |
UnitConverter convert:1 from:#floz to:#liter |
|
618 |
UnitConverter convert:1 from:#oz to:#gram |
|
619 |
UnitConverter convert:1 from:#ounce to:#gram |
|
620 |
||
621 |
UnitConverter convert:37 from:#celsius to:#fahrenheit -> 98.6 |
|
622 |
UnitConverter convert:37 from:#celsius to:#kelvin -> 310.15 |
|
623 |
UnitConverter convert:0 from:#celsius to:#kelvin -> 273.15 |
|
624 |
UnitConverter convert:0 from:#kelvin to:#celsius -> -273.15 |
|
625 |
UnitConverter convert:0 from:#kelvin to:#fahrenheit -> -459.67 |
|
626 |
UnitConverter convert:0 from:#fahrenheit to:#celsius -> -17.7777777777778 |
|
627 |
UnitConverter convert:0 from:#fahrenheit to:#kelvin -> 255.372222222222 |
|
628 |
||
629 |
" |
|
630 |
||
631 |
"Created: / 31-05-1996 / 16:23:38 / cg" |
|
632 |
"Modified (comment): / 18-09-2017 / 09:02:09 / cg" |
|
633 |
! |
|
634 |
||
635 |
convert:howMany from:sourceUnit to:destUnit pairsAlreadyTried:pairsAlready |
|
636 |
"given a value in sourceUnit (symbolic), try to convert it |
|
637 |
to destUnit (symbolic); return nil, if the conversion is |
|
638 |
unknown." |
|
639 |
||
516 | 640 |
|u conversions alias rslt sU dU const val unit |
641 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
642 |
sF1 sF2 dF1 dF2 |
|
643 |
s d| |
|
363 | 644 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
645 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
646 |
|
550 | 647 |
Conversions isNil ifTrue:[ |
1031 | 648 |
self initializeConversions |
550 | 649 |
]. |
650 |
||
363 | 651 |
"/ somehow, recursion must end ... |
652 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 653 |
^ howMany |
363 | 654 |
]. |
4514 | 655 |
thisContext isReallyRecursive ifTrue:[ |
656 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
657 |
]. |
|
658 |
(pairsAlready includes:(sourceUnit->destUnit)) ifTrue:[ |
|
659 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
660 |
]. |
|
661 |
||
369 | 662 |
sourceUnit isSymbol ifFalse:[ |
1031 | 663 |
s := sourceUnit withoutSeparators. |
664 |
sU := s asSymbolIfInterned. |
|
665 |
sU notNil ifTrue:[ |
|
4514 | 666 |
^ self convert:howMany from:sU to:destUnit pairsAlreadyTried:pairsAlready |
1031 | 667 |
]. |
668 |
(s startsWith:$() ifTrue:[ |
|
669 |
(s endsWith:$)) ifTrue:[ |
|
670 |
s := s copyFrom:2 to:(s size - 1). |
|
4514 | 671 |
^ self convert:howMany from:s to:destUnit pairsAlreadyTried:pairsAlready |
1031 | 672 |
] |
673 |
]. |
|
369 | 674 |
]. |
675 |
destUnit isSymbol ifFalse:[ |
|
1031 | 676 |
d := destUnit withoutSeparators. |
677 |
dU := d asSymbolIfInterned. |
|
678 |
dU notNil ifTrue:[ |
|
4514 | 679 |
^ self convert:howMany from:sourceUnit to:dU pairsAlreadyTried:pairsAlready |
1031 | 680 |
]. |
681 |
(d startsWith:$() ifTrue:[ |
|
682 |
(d endsWith:$)) ifTrue:[ |
|
683 |
d := d copyFrom:2 to:(d size - 1). |
|
4514 | 684 |
^ self convert:howMany from:sourceUnit to:d pairsAlreadyTried:pairsAlready |
1031 | 685 |
] |
686 |
]. |
|
369 | 687 |
]. |
688 |
||
4515 | 689 |
pairsAlready add:(sourceUnit->destUnit). |
690 |
||
363 | 691 |
"/ first, get rid of scalers ... |
692 |
||
693 |
u := self unscaled:sourceUnit. |
|
694 |
u notNil ifTrue:[ |
|
4514 | 695 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit pairsAlreadyTried:pairsAlready |
363 | 696 |
]. |
697 |
||
698 |
u := self unscaled:destUnit. |
|
699 |
u notNil ifTrue:[ |
|
4514 | 700 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) pairsAlreadyTried:pairsAlready |
363 | 701 |
]. |
702 |
||
703 |
"/ and of aliases ... |
|
704 |
||
705 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
706 |
alias notNil ifTrue:[ |
|
4514 | 707 |
^ self convert:howMany from:alias to:destUnit pairsAlreadyTried:pairsAlready |
363 | 708 |
]. |
709 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
710 |
alias notNil ifTrue:[ |
|
4514 | 711 |
^ self convert:howMany from:sourceUnit to:alias pairsAlreadyTried:pairsAlready |
363 | 712 |
]. |
713 |
||
516 | 714 |
"/ any constants ? |
715 |
||
716 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 717 |
const := Constants at:sourceUnit. |
718 |
val := const at:1. |
|
719 |
unit := const at:2. |
|
4514 | 720 |
^ self convert:(howMany*val) from:unit to:destUnit pairsAlreadyTried:pairsAlready |
516 | 721 |
]. |
722 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
723 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
724 |
"/ 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
|
725 |
"/ 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
|
726 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
727 |
|
363 | 728 |
"/ working with squares or cubics ? |
729 |
||
516 | 730 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
731 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
732 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 733 |
(sU notNil and:[dU notNil]) ifTrue:[ |
4514 | 734 |
^ (self convert:(howMany sqrt) from:sU to:dU pairsAlreadyTried:pairsAlready) squared |
1031 | 735 |
]. |
736 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 737 |
]. |
516 | 738 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
739 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
740 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 741 |
(sU notNil and:[dU notNil]) ifTrue:[ |
4514 | 742 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU pairsAlreadyTried:pairsAlready) raisedTo:3 |
1031 | 743 |
]. |
744 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 745 |
]. |
746 |
||
747 |
"/ working with fractions ? |
|
748 |
||
749 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 750 |
"/ look for a constant conversion factor |
751 |
i := sourceUnit indexOf:$/. |
|
752 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
753 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
754 |
i := destUnit indexOf:$/. |
|
755 |
duNumerator := destUnit copyTo:(i - 1). |
|
756 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 757 |
|
4514 | 758 |
uN := self convert:howMany from:suNumerator to:duNumerator pairsAlreadyTried:pairsAlready. |
1031 | 759 |
uN notNil ifTrue:[ |
4514 | 760 |
uD := self convert:1 from:suDenominator to:duDenominator pairsAlreadyTried:pairsAlready. |
1031 | 761 |
uD notNil ifTrue:[ |
762 |
^ uN / uD |
|
763 |
] |
|
764 |
]. |
|
516 | 765 |
]. |
766 |
||
767 |
"/ working with products ? |
|
768 |
||
769 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 770 |
i := sourceUnit indexOf:$*. |
771 |
sF1 := sourceUnit copyTo:(i - 1). |
|
772 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
773 |
i := destUnit indexOf:$*. |
|
774 |
dF1 := destUnit copyTo:(i - 1). |
|
775 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 776 |
|
4514 | 777 |
u := self convert:howMany from:sF1 to:dF1 pairsAlreadyTried:pairsAlready. |
1031 | 778 |
u notNil ifTrue:[ |
4514 | 779 |
u := self convert:u from:sF2 to:dF2 pairsAlreadyTried:pairsAlready. |
1031 | 780 |
u notNil ifTrue:[ |
781 |
^ u |
|
782 |
] |
|
783 |
]. |
|
363 | 784 |
]. |
785 |
||
786 |
"/ the real work comes here ... |
|
787 |
||
788 |
"/ is there a direct conversion in the dataBase ? |
|
789 |
||
790 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
791 |
rslt notNil ifTrue:[^ rslt]. |
|
792 |
||
793 |
"/ try inverse conversion ... |
|
4514 | 794 |
"/ but here, only linear factors (numeric conversions) are allowed!! |
795 |
[ |
|
796 |
|conversions revFactor| |
|
797 |
||
798 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
799 |
conversions notNil ifTrue:[ |
|
800 |
revFactor := conversions at:sourceUnit ifAbsent:nil. |
|
801 |
revFactor isNumber ifTrue:[ |
|
802 |
^ howMany / revFactor |
|
803 |
]. |
|
804 |
]. |
|
805 |
] value. |
|
363 | 806 |
|
4514 | 807 |
|
516 | 808 |
"/ here's the deep recursion ... |
363 | 809 |
|
810 |
"/ try indirect conversion from source |
|
811 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
812 |
conversions notNil ifTrue:[ |
|
1031 | 813 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
4514 | 814 |
|factor2 intermediateValue result| |
363 | 815 |
|
4514 | 816 |
factor1 isBlock ifTrue:[ |
817 |
intermediateValue := factor1 value:howMany. |
|
818 |
result := self convert:intermediateValue from:intermediateUnit to:destUnit pairsAlreadyTried:pairsAlready. |
|
819 |
result notNil ifTrue:[ ^ result ]. |
|
820 |
] ifFalse:[ |
|
821 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit pairsAlreadyTried:pairsAlready. |
|
822 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
823 |
]. |
|
1031 | 824 |
]. |
363 | 825 |
]. |
826 |
||
827 |
"/ try indirect conversion from dest |
|
828 |
||
829 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
830 |
conversions notNil ifTrue:[ |
|
1031 | 831 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
832 |
|factor2| |
|
363 | 833 |
|
4514 | 834 |
factor1 isBlock ifFalse:[ |
835 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit pairsAlreadyTried:pairsAlready. |
|
836 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
837 |
]. |
|
1031 | 838 |
]. |
363 | 839 |
]. |
840 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
841 |
"/ 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
|
842 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
843 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 844 |
i := sourceUnit indexOf:$*. |
845 |
sF1 := sourceUnit copyTo:(i - 1). |
|
846 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
847 |
|
1031 | 848 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
849 |
|
1031 | 850 |
self units do:[:aUnit | |
851 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
852 |
|
1031 | 853 |
pref := sF1 , '*'. |
854 |
(aUnit startsWith:pref) ifTrue:[ |
|
855 |
"/ ok; want a/b -> x |
|
856 |
"/ found a/c -> any |
|
857 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
858 |
|
1031 | 859 |
iUnit := aUnit copyFrom:pref size + 1. |
4514 | 860 |
factor2 := self convert:1 from:sF2 to:iUnit pairsAlreadyTried:pairsAlready. |
1031 | 861 |
factor2 notNil ifTrue:[ |
862 |
"/ good ... |
|
4514 | 863 |
rslt := self convert:(factor2 * howMany) from:aUnit to:destUnit pairsAlreadyTried:pairsAlready. |
1031 | 864 |
rslt notNil ifTrue:[^ rslt]. |
865 |
] |
|
866 |
] |
|
867 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
868 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
869 |
|
516 | 870 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 871 |
|
872 |
"direct - how many meters are there in two inches: |
|
873 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 874 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 875 |
|
876 |
reverse - how many inches are there in one meter |
|
877 |
UnitConverter convert:1 from:#meter to:#inch |
|
878 |
||
879 |
with alias: |
|
880 |
UnitConverter convert:1 from:#inch to:#m |
|
881 |
UnitConverter convert:1 from:#inch to:#mm |
|
882 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
883 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
884 |
UnitConverter convert:1 from:#mm to:#km |
|
885 |
UnitConverter convert:1 from:#km to:#foot |
|
886 |
||
887 |
indirect: |
|
888 |
UnitConverter convert:1 from:#mm to:#twip |
|
889 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 890 |
UnitConverter convert:1 from:'letterH' to:#point |
891 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 892 |
|
893 |
UnitConverter convert:5 from:#barrel to:#liter |
|
894 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
895 |
||
896 |
UnitConverter convert:1 from:#liter to:#floz |
|
4514 | 897 |
UnitConverter convert:0 from:#fahrenheit to:#kelvin |
898 |
UnitConverter convert:0 from:#fahrenheit to:#celsius |
|
899 |
UnitConverter convert:0 from:#celsius to:#kelvin |
|
900 |
UnitConverter convert:0 from:#kelvin to:#celsius |
|
363 | 901 |
" |
902 |
||
4514 | 903 |
"Created: / 17-09-2017 / 10:11:40 / cg" |
4515 | 904 |
"Modified: / 18-09-2017 / 11:00:41 / cg" |
363 | 905 |
! |
906 |
||
3573 | 907 |
fileSizeBinaryStringFor:nBytes |
3575 | 908 |
"return a useful string for a size-in-bytes in programmer's notation; |
3573 | 909 |
Scales by Ki (*1024), Mi (*1024*1024) or Gi (*1024*1024*1024) or even Ti |
910 |
as required and useful. |
|
911 |
Provided here since this is so common... |
|
912 |
Notice: the scale is 1024 |
|
913 |
the 1024 scale has been standardized in 1998 by IEC to MiB, GiB, etc. |
|
914 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
915 |
||
916 |
^ self |
|
917 |
unitStringFor:nBytes |
|
3574 | 918 |
scale:1024 |
3575 | 919 |
rounded:true |
3576 | 920 |
unitStrings:#('Byte' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' 'ZiB' 'YiB') |
3573 | 921 |
|
922 |
"/ the one below was written to save divisions. |
|
923 |
"/ It seems to be not worth the effort... |
|
924 |
||
925 |
"/ |unitString n| |
|
926 |
"/ |
|
927 |
"/ nBytes isNil ifTrue:[ |
|
928 |
"/ ^ '-' |
|
929 |
"/ ]. |
|
930 |
"/ |
|
931 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
932 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
933 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
934 |
"/ nBytes < 1000 ifTrue:[ |
|
935 |
"/ n := nBytes. |
|
936 |
"/ unitString := ' '. |
|
937 |
"/ ] ifFalse:[ |
|
938 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
939 |
"/ unitString := ' K' |
|
940 |
"/ ] |
|
941 |
"/ ] ifFalse:[ |
|
942 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
943 |
"/ unitString := ' M' |
|
944 |
"/ ]. |
|
945 |
"/ ] ifFalse:[ |
|
946 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
947 |
"/ unitString := ' G' |
|
948 |
"/ ] |
|
949 |
"/ ] ifFalse:[ |
|
950 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
951 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
952 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
953 |
"/ unitString := ' E' |
|
954 |
"/ ] ifFalse:[ |
|
955 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
956 |
"/ unitString := ' P' |
|
957 |
"/ ] |
|
958 |
"/ ] ifFalse:[ |
|
959 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
960 |
"/ unitString := ' T' |
|
961 |
"/ ]. |
|
962 |
"/ ]. |
|
963 |
"/ |
|
964 |
"/"/ n truncated = n ifTrue:[ |
|
965 |
"/"/ n := n truncated |
|
966 |
"/"/ ]. |
|
967 |
"/ ^ n printString , unitString. |
|
968 |
||
969 |
" |
|
970 |
self fileSizeStringFor:nil |
|
971 |
self fileSizeStringFor:10 |
|
972 |
self fileSizeStringFor:100 |
|
973 |
self fileSizeStringFor:1000 |
|
974 |
self fileSizeStringFor:8192 |
|
975 |
self fileSizeStringFor:10000 |
|
976 |
self fileSizeStringFor:100000 |
|
977 |
self fileSizeStringFor:1000000 |
|
978 |
self fileSizeStringFor:10000000 |
|
979 |
self fileSizeStringFor:100000000 |
|
980 |
self fileSizeStringFor:1000000000 |
|
981 |
self fileSizeStringFor:10000000000 |
|
982 |
self fileSizeStringFor:100000000000 |
|
983 |
self fileSizeStringFor:1000000000000 |
|
984 |
self fileSizeStringFor:10000000000000 |
|
985 |
self fileSizeStringFor:10000000000000000 |
|
986 |
self fileSizeStringFor:100000000000000000000 |
|
987 |
self fileSizeStringFor:1000000000000000000000 |
|
988 |
" |
|
989 |
||
990 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
991 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
992 |
! |
|
993 |
||
1292 | 994 |
fileSizeFromString:fileSizeString |
3573 | 995 |
"given a fileSize string (i.e. a number followed by optional kB, MB, GB etc. or KiB, MiB, GiB,...). |
996 |
Notice that in 1998, IEC standardized the scales to 1000 for k,M,G,... and 1024 for Ki, Mi, Gi, etc. |
|
997 |
See https://en.wikipedia.org/wiki/Mebibyte. |
|
1292 | 998 |
Provided here since this is so common..." |
999 |
||
3573 | 1000 |
|numericValue s scaleChar factor scale| |
1292 | 1001 |
|
1002 |
fileSizeString isEmpty ifTrue:[ |
|
1003 |
^ 0 |
|
1004 |
]. |
|
1005 |
||
1006 |
scale := 1. |
|
1007 |
s := fileSizeString readStream. |
|
1008 |
numericValue := Number readFrom:s onError:[0]. |
|
1009 |
s skipSeparators. |
|
1010 |
s atEnd ifFalse:[ |
|
1011 |
scaleChar := s next asLowercase. |
|
3573 | 1012 |
factor := 1000. |
1013 |
s peek == $i ifTrue:[ |
|
1014 |
factor := 1024 |
|
1015 |
]. |
|
3486 | 1016 |
scaleChar == $k ifTrue:[ |
3573 | 1017 |
scale := factor |
1292 | 1018 |
] ifFalse:[ |
3486 | 1019 |
scaleChar == $m ifTrue:[ |
3573 | 1020 |
scale := factor * factor |
1292 | 1021 |
] ifFalse:[ |
3486 | 1022 |
scaleChar == $g ifTrue:[ |
3573 | 1023 |
scale := factor * factor * factor |
1292 | 1024 |
] ifFalse:[ |
3486 | 1025 |
scaleChar == $t ifTrue:[ |
3573 | 1026 |
scale := factor * factor * factor * factor |
3142 | 1027 |
] ifFalse:[ |
3486 | 1028 |
scaleChar == $p ifTrue:[ |
3573 | 1029 |
scale := factor * factor * factor * factor * factor |
3142 | 1030 |
] ifFalse:[ |
3486 | 1031 |
scaleChar == $e ifTrue:[ |
3573 | 1032 |
scale := factor * factor * factor * factor * factor * factor |
1033 |
] ifFalse:[ |
|
1034 |
scaleChar == $z ifTrue:[ |
|
1035 |
scale := factor * factor * factor * factor * factor * factor * factor |
|
1036 |
] ifFalse:[ |
|
1037 |
scaleChar == $y ifTrue:[ |
|
1038 |
scale := factor * factor * factor * factor * factor * factor * factor * factor |
|
1039 |
] |
|
1040 |
] |
|
3142 | 1041 |
] |
1042 |
] |
|
1292 | 1043 |
] |
1044 |
] |
|
1045 |
] |
|
1046 |
]. |
|
1047 |
]. |
|
1048 |
^ (numericValue*scale) asInteger |
|
1049 |
||
1050 |
" |
|
1051 |
self fileSizeFromString:'1234' |
|
1295 | 1052 |
self fileSizeFromString:'10 k' |
3573 | 1053 |
self fileSizeFromString:'10 kB' |
1054 |
self fileSizeFromString:'10 KiB' |
|
1295 | 1055 |
self fileSizeFromString:'0.6 k' |
1056 |
self fileSizeFromString:'1.8 M' |
|
3573 | 1057 |
self fileSizeFromString:'1.8 MB' |
1058 |
self fileSizeFromString:'1.8 MiB' |
|
1295 | 1059 |
self fileSizeFromString:'0.5 G' |
1060 |
self fileSizeFromString:'0.5 T' |
|
3142 | 1061 |
self fileSizeFromString:'0.5 P' |
1062 |
self fileSizeFromString:'0.5 E' |
|
1292 | 1063 |
" |
3142 | 1064 |
|
1065 |
"Modified (comment): / 17-12-2013 / 16:56:56 / cg" |
|
1292 | 1066 |
! |
1067 |
||
3575 | 1068 |
fileSizeSIStringFor:nBytes |
1069 |
"return a useful string for a size-in-bytes in SI standard notation; |
|
3573 | 1070 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
1294 | 1071 |
as required and useful. |
3573 | 1072 |
Provided here since this is so common... |
1073 |
Notice: the scale is 1000 |
|
1074 |
the 1000 scale has been standardized in 1998 by IEC to MB, GB, etc. |
|
1075 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1143 | 1076 |
|
3573 | 1077 |
^ self |
1078 |
unitStringFor:nBytes |
|
1079 |
scale:1000 |
|
3575 | 1080 |
rounded:true |
4535 | 1081 |
unitStrings:#('Byte' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' 'ZB' 'YB') |
3575 | 1082 |
|
1083 |
"/ |unitString n| |
|
1084 |
"/ |
|
1085 |
"/ nBytes isNil ifTrue:[ |
|
1086 |
"/ ^ '-' |
|
1087 |
"/ ]. |
|
1088 |
"/ |
|
1089 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1090 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1091 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1092 |
"/ nBytes < 1000 ifTrue:[ |
|
1093 |
"/ n := nBytes. |
|
1094 |
"/ unitString := ' '. |
|
1095 |
"/ ] ifFalse:[ |
|
1096 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1097 |
"/ unitString := ' K' |
|
1098 |
"/ ] |
|
1099 |
"/ ] ifFalse:[ |
|
1100 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1101 |
"/ unitString := ' M' |
|
1102 |
"/ ]. |
|
1103 |
"/ ] ifFalse:[ |
|
1104 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1105 |
"/ unitString := ' G' |
|
1106 |
"/ ] |
|
1107 |
"/ ] ifFalse:[ |
|
1108 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1109 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1110 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1111 |
"/ unitString := ' E' |
|
1112 |
"/ ] ifFalse:[ |
|
1113 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1114 |
"/ unitString := ' P' |
|
1115 |
"/ ] |
|
1116 |
"/ ] ifFalse:[ |
|
1117 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1118 |
"/ unitString := ' T' |
|
1119 |
"/ ]. |
|
1120 |
"/ ]. |
|
1121 |
"/ |
|
1122 |
"/"/ n truncated = n ifTrue:[ |
|
1123 |
"/"/ n := n truncated |
|
1124 |
"/"/ ]. |
|
1125 |
"/ ^ n printString , unitString. |
|
1126 |
||
1127 |
" |
|
1128 |
self fileSizeStringFor:nil |
|
1129 |
self fileSizeStringFor:10 |
|
1130 |
self fileSizeStringFor:100 |
|
1131 |
self fileSizeStringFor:1000 |
|
1132 |
self fileSizeStringFor:8192 |
|
1133 |
self fileSizeStringFor:10000 |
|
1134 |
self fileSizeStringFor:100000 |
|
1135 |
self fileSizeStringFor:1000000 |
|
1136 |
self fileSizeStringFor:10000000 |
|
1137 |
self fileSizeStringFor:100000000 |
|
1138 |
self fileSizeStringFor:1000000000 |
|
1139 |
self fileSizeStringFor:10000000000 |
|
1140 |
self fileSizeStringFor:100000000000 |
|
1141 |
self fileSizeStringFor:1000000000000 |
|
1142 |
self fileSizeStringFor:10000000000000 |
|
1143 |
self fileSizeStringFor:10000000000000000 |
|
1144 |
self fileSizeStringFor:100000000000000000000 |
|
1145 |
self fileSizeStringFor:1000000000000000000000 |
|
1146 |
" |
|
1147 |
||
1148 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
4535 | 1149 |
"Modified: / 17-11-2017 / 10:30:49 / cg" |
3575 | 1150 |
! |
1151 |
||
1152 |
fileSizeStringFor:nBytes |
|
1153 |
"return a useful string for a size-in-bytes. |
|
1154 |
Scales either by 1000 or 1024, depending on the user preferences. |
|
1155 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1156 |
||
1157 |
^ self |
|
1158 |
unitStringFor:nBytes |
|
1159 |
scale:(UserPreferences current unitForFileSize) |
|
1160 |
rounded:true |
|
1161 |
unitStrings:(UserPreferences current unitStringsForFileSize) |
|
3457 | 1162 |
|
3573 | 1163 |
"/ |unitString n| |
1164 |
"/ |
|
1165 |
"/ nBytes isNil ifTrue:[ |
|
1166 |
"/ ^ '-' |
|
1346 | 1167 |
"/ ]. |
3573 | 1168 |
"/ |
1169 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1170 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1171 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1172 |
"/ nBytes < 1000 ifTrue:[ |
|
1173 |
"/ n := nBytes. |
|
1174 |
"/ unitString := ' '. |
|
1175 |
"/ ] ifFalse:[ |
|
1176 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1177 |
"/ unitString := ' K' |
|
1178 |
"/ ] |
|
1179 |
"/ ] ifFalse:[ |
|
1180 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1181 |
"/ unitString := ' M' |
|
1182 |
"/ ]. |
|
1183 |
"/ ] ifFalse:[ |
|
1184 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1185 |
"/ unitString := ' G' |
|
1186 |
"/ ] |
|
1187 |
"/ ] ifFalse:[ |
|
1188 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1189 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1190 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1191 |
"/ unitString := ' E' |
|
1192 |
"/ ] ifFalse:[ |
|
1193 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1194 |
"/ unitString := ' P' |
|
1195 |
"/ ] |
|
1196 |
"/ ] ifFalse:[ |
|
1197 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1198 |
"/ unitString := ' T' |
|
1199 |
"/ ]. |
|
1200 |
"/ ]. |
|
1201 |
"/ |
|
1202 |
"/"/ n truncated = n ifTrue:[ |
|
1203 |
"/"/ n := n truncated |
|
1204 |
"/"/ ]. |
|
1205 |
"/ ^ n printString , unitString. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1206 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1207 |
" |
3457 | 1208 |
self fileSizeStringFor:nil |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1209 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1210 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1211 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1212 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1213 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1214 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1215 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1216 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1217 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1218 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1219 |
self fileSizeStringFor:10000000000 |
1293 | 1220 |
self fileSizeStringFor:100000000000 |
1221 |
self fileSizeStringFor:1000000000000 |
|
1222 |
self fileSizeStringFor:10000000000000 |
|
3141 | 1223 |
self fileSizeStringFor:10000000000000000 |
3142 | 1224 |
self fileSizeStringFor:100000000000000000000 |
1225 |
self fileSizeStringFor:1000000000000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1226 |
" |
3141 | 1227 |
|
3142 | 1228 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
3457 | 1229 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1230 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1231 |
|
3120 | 1232 |
frequencyFromString:frequencyString |
1233 |
"given a frequency string (i.e. a number followed by optional kHz, Mhz or Hhz as required. |
|
1234 |
Provided here since this is so common..." |
|
1235 |
||
1236 |
|numericValue s scaleChar scale rest| |
|
1237 |
||
1238 |
frequencyString isEmpty ifTrue:[ |
|
1239 |
^ 0 |
|
1240 |
]. |
|
1241 |
||
1242 |
scale := 1. |
|
1243 |
s := frequencyString readStream. |
|
1244 |
numericValue := Number readFrom:s onError:[0]. |
|
1245 |
s skipSeparators. |
|
1246 |
s atEnd ifFalse:[ |
|
1247 |
scaleChar := s next asLowercase. |
|
3486 | 1248 |
scaleChar == $/ ifTrue:[ |
3120 | 1249 |
rest := s upToSeparator asLowercase. |
1250 |
(rest = 'm' or:[rest = 'min']) ifTrue:[ |
|
1251 |
scale := 1/60 |
|
1252 |
] ifFalse:[ |
|
1253 |
(rest = 'h' or:[rest = 'hr' or:[rest = 'hour']]) ifTrue:[ |
|
1254 |
scale := 1/60/60 |
|
1255 |
] ifFalse:[ |
|
1256 |
(rest = 'd' or:[rest = 'dy' or:[rest = 'day']]) ifTrue:[ |
|
1257 |
scale := 1/60/60/24 |
|
1258 |
]. |
|
1259 |
]. |
|
1260 |
]. |
|
1261 |
] ifFalse:[ |
|
3486 | 1262 |
scaleChar == $k ifTrue:[ |
3120 | 1263 |
scale := 1000 |
1264 |
] ifFalse:[ |
|
3486 | 1265 |
scaleChar == $m ifTrue:[ |
3120 | 1266 |
scale := 1000 * 1000 |
1267 |
] ifFalse:[ |
|
3487 | 1268 |
scaleChar == $g ifTrue:[ |
3120 | 1269 |
scale := 1000 * 1000 * 1000 |
1270 |
] ifFalse:[ |
|
3487 | 1271 |
scaleChar == $t ifTrue:[ |
3120 | 1272 |
scale := 1000 * 1000 * 1000 * 1000 |
1273 |
] |
|
1274 |
] |
|
1275 |
] |
|
1276 |
]. |
|
1277 |
] |
|
1278 |
]. |
|
1279 |
^ (numericValue*scale) |
|
1280 |
||
1281 |
" |
|
1282 |
self frequencyFromString:'1234' |
|
1283 |
self frequencyFromString:'10 kHz' |
|
1284 |
self frequencyFromString:'0.6 k' |
|
1285 |
self frequencyFromString:'1.8 Mhz' |
|
1286 |
self frequencyFromString:'0.5 Ghz' |
|
1287 |
self frequencyFromString:'0.5 Thz' |
|
3577 | 1288 |
self frequencyFromString:'1000/s' |
3120 | 1289 |
self frequencyFromString:'10/s' |
1290 |
self frequencyFromString:'10/min' |
|
1291 |
self frequencyFromString:'10/hr' |
|
1292 |
" |
|
1293 |
! |
|
1294 |
||
1295 |
frequencyStringFor:hertz |
|
1296 |
"return a useful string for a frequency; |
|
1297 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
|
1298 |
as required and useful. |
|
1299 |
Provided here since this is so common..." |
|
1300 |
||
3573 | 1301 |
^ self |
1302 |
unitStringFor:hertz |
|
1303 |
scale:1000 |
|
3574 | 1304 |
rounded:false |
4535 | 1305 |
unitStrings:#('Hz' 'kHz' 'MHz' 'GHz' 'THz') |
3120 | 1306 |
|
1307 |
" |
|
3577 | 1308 |
self frequencyStringFor:0.1 |
3120 | 1309 |
self frequencyStringFor:10 |
1310 |
self frequencyStringFor:100 |
|
1311 |
self frequencyStringFor:1000 |
|
1312 |
self frequencyStringFor:8192 |
|
1313 |
self frequencyStringFor:10000 |
|
1314 |
self frequencyStringFor:100000 |
|
1315 |
self frequencyStringFor:1000000 |
|
1316 |
self frequencyStringFor:10000000 |
|
1317 |
self frequencyStringFor:100000000 |
|
1318 |
self frequencyStringFor:1000000000 |
|
1319 |
self frequencyStringFor:10000000000 |
|
1320 |
self frequencyStringFor:100000000000 |
|
1321 |
self frequencyStringFor:1000000000000 |
|
1322 |
self frequencyStringFor:10000000000000 |
|
1323 |
" |
|
4535 | 1324 |
|
1325 |
"Modified: / 17-11-2017 / 10:32:53 / cg" |
|
3120 | 1326 |
! |
1327 |
||
363 | 1328 |
inchToMillimeter:inches |
364 | 1329 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 1330 |
|
1331 |
^ inches * 25.4 |
|
1332 |
||
1333 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 1334 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 1335 |
! |
1336 |
||
1337 |
millimeterToInch:mm |
|
364 | 1338 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 1339 |
|
1340 |
^ mm / 25.4 |
|
1341 |
||
1342 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 1343 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
3573 | 1344 |
! |
1345 |
||
3574 | 1346 |
unitStringFor:nBytes scale:k rounded:rounded unitStrings:unitStrings |
3573 | 1347 |
"common helper for SI (1000-based) and binary (1024-based) human readable string generators. |
3574 | 1348 |
Notice that both forms are often encountered and useful. |
1349 |
If rounded is true, round to nearest integer" |
|
3573 | 1350 |
|
3576 | 1351 |
|n kN idx val unitString| |
3573 | 1352 |
|
1353 |
nBytes isNil ifTrue:[ |
|
1354 |
^ '-' |
|
1355 |
]. |
|
1356 |
||
1357 |
idx := 1. |
|
1358 |
n := nBytes. |
|
1359 |
kN := 1. |
|
1360 |
[ (idx < unitStrings size) and:[ nBytes >= (kN * k) ] ] whileTrue:[ |
|
1361 |
idx := idx + 1. |
|
1362 |
kN := kN * k. |
|
1363 |
]. |
|
3575 | 1364 |
|
1365 |
val := nBytes / kN. |
|
3576 | 1366 |
unitString := ' ',(unitStrings at:idx). |
1367 |
||
3575 | 1368 |
((rounded and:[val > 20]) or:[val isInteger]) ifTrue:[ |
3576 | 1369 |
^ (val rounded) printString , unitString |
3574 | 1370 |
]. |
3576 | 1371 |
^ (val asFixedPoint:1) printString , unitString |
3573 | 1372 |
|
1373 |
" |
|
1374 |
self fileSizeStringFor:nil |
|
1375 |
self fileSizeStringFor:10 |
|
1376 |
self fileSizeStringFor:100 |
|
3574 | 1377 |
self fileSizeStringFor:900 |
1378 |
self fileSizeStringFor:1100 |
|
1379 |
self fileSizeStringFor:1400 |
|
1380 |
self fileSizeStringFor:1499 |
|
1381 |
self fileSizeStringFor:1500 |
|
1382 |
||
3573 | 1383 |
self fileSizeStringFor:1000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
1384 |
self fileSizeStringFor:8192 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1385 |
self fileSizeStringFor:10000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1386 |
self fileSizeStringFor:100000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1387 |
self fileSizeStringFor:1000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1388 |
self fileSizeStringFor:10000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1389 |
self fileSizeStringFor:100000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1390 |
self fileSizeStringFor:1000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1391 |
self fileSizeStringFor:10000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1392 |
self fileSizeStringFor:100000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1393 |
self fileSizeStringFor:1000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1394 |
self fileSizeStringFor:10000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1395 |
self fileSizeStringFor:10000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1396 |
self fileSizeStringFor:100000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1397 |
self fileSizeStringFor:1000000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1398 |
||
1399 |
self fileSizeStringFor:8192 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1400 |
self fileSizeStringFor:81920000 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1401 |
" |
|
1402 |
||
1403 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1404 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
363 | 1405 |
! ! |
1406 |
||
516 | 1407 |
!UnitConverter class methodsFor:'missing conversion'! |
1408 |
||
1409 |
noConversionFrom:sourceUnit to:destUnit |
|
1410 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
1411 |
^ nil |
|
1412 |
||
1413 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
1414 |
! ! |
|
1415 |
||
363 | 1416 |
!UnitConverter class methodsFor:'private'! |
1417 |
||
4514 | 1418 |
addConversion:factorOrBlock from:sourceMetric to:destMetric |
363 | 1419 |
"add a conversion" |
1420 |
||
1421 |
|conversion| |
|
1422 |
||
1423 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
1424 |
conversion isNil ifTrue:[ |
|
601 | 1425 |
conversion := IdentityDictionary new. |
1426 |
Conversions at:sourceMetric put:conversion |
|
363 | 1427 |
]. |
4514 | 1428 |
conversion at:destMetric put:factorOrBlock. |
363 | 1429 |
^ conversion |
1430 |
||
1431 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 1432 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 1433 |
! |
1434 |
||
1435 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
1436 |
"given a value in sourceMetric (symbolic), try to convert it |
|
1437 |
to destMetric (symbolic); |
|
1438 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
1439 |
||
1440 |
|conversion factor| |
|
1441 |
||
550 | 1442 |
Conversions isNil ifTrue:[ |
601 | 1443 |
self initializeConversions |
550 | 1444 |
]. |
1445 |
||
363 | 1446 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
1447 |
conversion isNil ifTrue:[^ nil]. |
|
1448 |
||
1449 |
factor := conversion at:destMetric ifAbsent:nil. |
|
1450 |
factor isNil ifTrue:[^ nil]. |
|
1451 |
||
364 | 1452 |
factor isNumber ifTrue:[ |
601 | 1453 |
^ howMany * factor |
364 | 1454 |
]. |
515 | 1455 |
^ factor value:howMany |
363 | 1456 |
|
1457 |
" |
|
1458 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
1459 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 1460 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 1461 |
" |
1462 |
||
1463 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 1464 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 1465 |
! |
1466 |
||
1467 |
unscaled:what |
|
1468 |
"given a unit, return the base and a factor as assoc, |
|
1469 |
or nil if not found" |
|
1470 |
||
550 | 1471 |
Conversions isNil ifTrue:[ |
2995 | 1472 |
self initializeConversions |
550 | 1473 |
]. |
1474 |
||
363 | 1475 |
Scaling keysAndValuesDo:[:name :factor | |
2995 | 1476 |
|rest| |
363 | 1477 |
|
2995 | 1478 |
(what startsWith:name) ifTrue:[ |
1479 |
rest := what withoutPrefix:name. |
|
1480 |
rest := rest asSymbolIfInterned. |
|
1481 |
rest notNil ifTrue:[ |
|
1482 |
^ rest -> factor. |
|
1483 |
] |
|
1484 |
]. |
|
363 | 1485 |
]. |
1486 |
^ nil |
|
1487 |
||
1488 |
" |
|
1489 |
UnitConverter unscaled:#millimeter |
|
1490 |
UnitConverter unscaled:#nanometer |
|
1491 |
UnitConverter unscaled:#kilometer |
|
1492 |
UnitConverter unscaled:#fuzzymeter |
|
1493 |
" |
|
1494 |
||
550 | 1495 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1496 |
! ! |
1497 |
||
3650 | 1498 |
!UnitConverter class methodsFor:'queries'! |
1499 |
||
1500 |
isUtilityClass |
|
1501 |
^ self == UnitConverter |
|
1502 |
! ! |
|
1503 |
||
363 | 1504 |
!UnitConverter class methodsFor:'documentation'! |
1505 |
||
1506 |
version |
|
3573 | 1507 |
^ '$Header$' |
2320 | 1508 |
! |
1509 |
||
1510 |
version_CVS |
|
3573 | 1511 |
^ '$Header$' |
363 | 1512 |
! ! |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
1513 |