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