author | Claus Gittinger <cg@exept.de> |
Sat, 16 Jan 2016 20:47:44 +0100 | |
changeset 3701 | 88126504abb7 |
parent 3674 | f1c3dfa54f92 |
child 4092 | 61384ffc7271 |
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] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
146 |
thats 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, |
|
3701 | 325 |
"/ thats 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 |
||
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. |
1135 | 389 |
|
3701 | 390 |
"/ ---------------- nature ---------------- |
391 |
Constants at:#planckLength put:#(1.616e-35 #'m'). |
|
392 |
Constants at:#planckMass put:#(2.176-8 #'Kg'). |
|
393 |
Constants at:#planckTime put:#(5.391e-44 #'s'). |
|
394 |
||
1135 | 395 |
" |
396 |
Conversions := nil. |
|
397 |
UnitConverter initializeConversions |
|
398 |
" |
|
399 |
||
400 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
401 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
402 |
! |
|
403 |
||
404 |
initializePrintValues |
|
363 | 405 |
"/ inch to (roughly) a typesetter point |
406 |
self addConversion:(1/72) from:#point to:#inch. |
|
407 |
||
408 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
409 |
self addConversion:(1/20) from:#twip to:#point. |
|
410 |
||
365 | 411 |
"/ US paper |
412 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
413 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
414 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
415 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
416 |
||
385 | 417 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
418 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
419 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
420 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
421 |
||
422 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
423 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
424 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
425 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
426 |
||
365 | 427 |
"/ European paper |
428 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
429 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
430 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
431 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
432 |
||
433 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
434 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
435 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
436 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
437 |
||
438 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
439 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
440 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
441 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
442 |
||
443 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
444 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
445 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
446 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
447 |
||
448 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
449 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
450 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
451 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
452 |
||
453 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
454 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
455 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
456 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
457 |
||
385 | 458 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
459 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
460 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
461 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
462 |
||
463 |
||
365 | 464 |
Aliases at:#letterW put:#'letter-page-width'. |
465 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 466 |
Aliases at:#legalW put:#'legal-page-width'. |
467 |
Aliases at:#legalH put:#'legal-page-height'. |
|
468 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
469 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 470 |
Aliases at:#a1W put:#'a1-page-width'. |
471 |
Aliases at:#a1H put:#'a1-page-height'. |
|
472 |
Aliases at:#a2W put:#'a2-page-width'. |
|
473 |
Aliases at:#a2H put:#'a2-page-height'. |
|
474 |
Aliases at:#a3W put:#'a3-page-width'. |
|
475 |
Aliases at:#a3H put:#'a3-page-height'. |
|
476 |
Aliases at:#a4W put:#'a4-page-width'. |
|
477 |
Aliases at:#a4H put:#'a4-page-height'. |
|
478 |
Aliases at:#a5W put:#'a5-page-width'. |
|
479 |
Aliases at:#a5H put:#'a5-page-height'. |
|
480 |
Aliases at:#a6W put:#'a6-page-width'. |
|
481 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 482 |
Aliases at:#b5W put:#'b5-page-width'. |
483 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 484 |
|
485 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
486 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 487 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
488 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
489 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
490 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 491 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
492 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
493 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
494 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
495 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
496 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
497 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
498 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
499 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
500 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
501 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
502 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 503 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
504 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 505 |
! |
363 | 506 |
|
1135 | 507 |
initializeScaleFactors |
508 |
Scaling := IdentityDictionary new. |
|
363 | 509 |
|
1285 | 510 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
511 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
512 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
513 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
514 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
515 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
516 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 517 |
Scaling at:#myria put:10000. |
1285 | 518 |
Scaling at:#kilo put:1000. "/ 10^3 |
519 |
Scaling at:#hecto put:100. "/ 10^2 |
|
520 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 521 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 522 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 523 |
Scaling at:#centi put:(1/100). |
1323 | 524 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
525 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
526 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
527 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
528 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
529 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
530 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
531 |
Scaling at:#yocto put:(1/1000000000000000000000000)."/ 10^-24 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
532 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
533 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
534 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
535 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
536 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
537 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
538 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
539 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
540 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
541 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
542 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
543 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
544 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
545 |
"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
|
546 |
"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
|
547 |
! |
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 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
550 |
"return the set of known units" |
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 |
|setOfUnits| |
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 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
555 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 556 |
setOfUnits add:srcUnit. |
557 |
conversionInfo keysDo:[:targetUnit | |
|
558 |
setOfUnits add:targetUnit |
|
559 |
] |
|
555
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 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
562 |
|
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 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
565 |
" |
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 |
"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
|
568 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 569 |
! ! |
570 |
||
571 |
!UnitConverter class methodsFor:'conversions'! |
|
572 |
||
573 |
convert:howMany from:sourceUnit to:destUnit |
|
574 |
"given a value in sourceUnit (symbolic), try to convert it |
|
575 |
to destUnit (symbolic); return nil, if the conversion is |
|
576 |
unknown." |
|
577 |
||
516 | 578 |
|u conversions alias rslt sU dU const val unit |
579 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
580 |
sF1 sF2 dF1 dF2 |
|
581 |
s d| |
|
363 | 582 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
583 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
584 |
|
550 | 585 |
Conversions isNil ifTrue:[ |
1031 | 586 |
self initializeConversions |
550 | 587 |
]. |
588 |
||
363 | 589 |
"/ somehow, recursion must end ... |
590 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 591 |
^ howMany |
363 | 592 |
]. |
593 |
||
369 | 594 |
sourceUnit isSymbol ifFalse:[ |
1031 | 595 |
s := sourceUnit withoutSeparators. |
596 |
sU := s asSymbolIfInterned. |
|
597 |
sU notNil ifTrue:[ |
|
598 |
^ self convert:howMany from:sU to:destUnit |
|
599 |
]. |
|
600 |
(s startsWith:$() ifTrue:[ |
|
601 |
(s endsWith:$)) ifTrue:[ |
|
602 |
s := s copyFrom:2 to:(s size - 1). |
|
603 |
^ self convert:howMany from:s to:destUnit |
|
604 |
] |
|
605 |
]. |
|
369 | 606 |
]. |
607 |
destUnit isSymbol ifFalse:[ |
|
1031 | 608 |
d := destUnit withoutSeparators. |
609 |
dU := d asSymbolIfInterned. |
|
610 |
dU notNil ifTrue:[ |
|
611 |
^ self convert:howMany from:sourceUnit to:dU |
|
612 |
]. |
|
613 |
(d startsWith:$() ifTrue:[ |
|
614 |
(d endsWith:$)) ifTrue:[ |
|
615 |
d := d copyFrom:2 to:(d size - 1). |
|
616 |
^ self convert:howMany from:sourceUnit to:d |
|
617 |
] |
|
618 |
]. |
|
369 | 619 |
]. |
620 |
||
363 | 621 |
"/ first, get rid of scalers ... |
622 |
||
623 |
u := self unscaled:sourceUnit. |
|
624 |
u notNil ifTrue:[ |
|
1031 | 625 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 626 |
]. |
627 |
||
628 |
u := self unscaled:destUnit. |
|
629 |
u notNil ifTrue:[ |
|
1031 | 630 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 631 |
]. |
632 |
||
633 |
"/ and of aliases ... |
|
634 |
||
635 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
636 |
alias notNil ifTrue:[ |
|
1031 | 637 |
^ self convert:howMany from:alias to:destUnit |
363 | 638 |
]. |
639 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
640 |
alias notNil ifTrue:[ |
|
1031 | 641 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 642 |
]. |
643 |
||
516 | 644 |
"/ any constants ? |
645 |
||
646 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 647 |
const := Constants at:sourceUnit. |
648 |
val := const at:1. |
|
649 |
unit := const at:2. |
|
650 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 651 |
]. |
652 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
653 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
654 |
"/ 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
|
655 |
"/ 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
|
656 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
657 |
|
363 | 658 |
"/ working with squares or cubics ? |
659 |
||
516 | 660 |
((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
|
661 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
662 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 663 |
(sU notNil and:[dU notNil]) ifTrue:[ |
664 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
665 |
]. |
|
666 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 667 |
]. |
516 | 668 |
((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
|
669 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
670 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 671 |
(sU notNil and:[dU notNil]) ifTrue:[ |
672 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
673 |
]. |
|
674 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 675 |
]. |
676 |
||
677 |
"/ working with fractions ? |
|
678 |
||
679 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 680 |
"/ look for a constant conversion factor |
681 |
i := sourceUnit indexOf:$/. |
|
682 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
683 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
684 |
i := destUnit indexOf:$/. |
|
685 |
duNumerator := destUnit copyTo:(i - 1). |
|
686 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 687 |
|
1031 | 688 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
689 |
uN notNil ifTrue:[ |
|
690 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
691 |
uD notNil ifTrue:[ |
|
692 |
^ uN / uD |
|
693 |
] |
|
694 |
]. |
|
516 | 695 |
]. |
696 |
||
697 |
"/ working with products ? |
|
698 |
||
699 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 700 |
i := sourceUnit indexOf:$*. |
701 |
sF1 := sourceUnit copyTo:(i - 1). |
|
702 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
703 |
i := destUnit indexOf:$*. |
|
704 |
dF1 := destUnit copyTo:(i - 1). |
|
705 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 706 |
|
1031 | 707 |
u := self convert:howMany from:sF1 to:dF1. |
708 |
u notNil ifTrue:[ |
|
709 |
u := self convert:u from:sF2 to:dF2. |
|
710 |
u notNil ifTrue:[ |
|
711 |
^ u |
|
712 |
] |
|
713 |
]. |
|
363 | 714 |
]. |
715 |
||
716 |
"/ the real work comes here ... |
|
717 |
||
718 |
"/ is there a direct conversion in the dataBase ? |
|
719 |
||
720 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
721 |
rslt notNil ifTrue:[^ rslt]. |
|
722 |
||
723 |
"/ try inverse conversion ... |
|
724 |
||
725 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
726 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
727 |
||
516 | 728 |
"/ here's the deep recursion ... |
363 | 729 |
|
730 |
||
731 |
"/ try indirect conversion from source |
|
732 |
||
733 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
734 |
conversions notNil ifTrue:[ |
|
1031 | 735 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
736 |
|factor2| |
|
363 | 737 |
|
1031 | 738 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
739 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
740 |
]. |
|
363 | 741 |
]. |
742 |
||
743 |
"/ try indirect conversion from dest |
|
744 |
||
745 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
746 |
conversions notNil ifTrue:[ |
|
1031 | 747 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
748 |
|factor2| |
|
363 | 749 |
|
1031 | 750 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
751 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
752 |
]. |
|
363 | 753 |
]. |
754 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
755 |
"/ 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
|
756 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
757 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 758 |
i := sourceUnit indexOf:$*. |
759 |
sF1 := sourceUnit copyTo:(i - 1). |
|
760 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
761 |
|
1031 | 762 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
763 |
|
1031 | 764 |
self units do:[:aUnit | |
765 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
766 |
|
1031 | 767 |
pref := sF1 , '*'. |
768 |
(aUnit startsWith:pref) ifTrue:[ |
|
769 |
"/ ok; want a/b -> x |
|
770 |
"/ found a/c -> any |
|
771 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
772 |
|
1031 | 773 |
iUnit := aUnit copyFrom:pref size + 1. |
774 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
775 |
factor2 notNil ifTrue:[ |
|
776 |
"/ good ... |
|
777 |
rslt := self convert:(factor2 * howMany) |
|
778 |
from:aUnit to:destUnit. |
|
779 |
rslt notNil ifTrue:[^ rslt]. |
|
780 |
] |
|
781 |
] |
|
782 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
783 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
784 |
|
516 | 785 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 786 |
|
787 |
"direct - how many meters are there in two inches: |
|
788 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 789 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 790 |
|
791 |
reverse - how many inches are there in one meter |
|
792 |
UnitConverter convert:1 from:#meter to:#inch |
|
793 |
||
794 |
with alias: |
|
795 |
UnitConverter convert:1 from:#inch to:#m |
|
796 |
UnitConverter convert:1 from:#inch to:#mm |
|
797 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
798 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
799 |
UnitConverter convert:1 from:#mm to:#km |
|
800 |
UnitConverter convert:1 from:#km to:#foot |
|
801 |
||
802 |
indirect: |
|
803 |
UnitConverter convert:1 from:#mm to:#twip |
|
804 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 805 |
UnitConverter convert:1 from:'letterH' to:#point |
806 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 807 |
|
808 |
UnitConverter convert:5 from:#barrel to:#liter |
|
809 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
810 |
||
811 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 812 |
" |
813 |
||
814 |
"Created: 31.5.1996 / 16:23:38 / cg" |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
815 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 816 |
! |
817 |
||
3573 | 818 |
fileSizeBinaryStringFor:nBytes |
3575 | 819 |
"return a useful string for a size-in-bytes in programmer's notation; |
3573 | 820 |
Scales by Ki (*1024), Mi (*1024*1024) or Gi (*1024*1024*1024) or even Ti |
821 |
as required and useful. |
|
822 |
Provided here since this is so common... |
|
823 |
Notice: the scale is 1024 |
|
824 |
the 1024 scale has been standardized in 1998 by IEC to MiB, GiB, etc. |
|
825 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
826 |
||
827 |
^ self |
|
828 |
unitStringFor:nBytes |
|
3574 | 829 |
scale:1024 |
3575 | 830 |
rounded:true |
3576 | 831 |
unitStrings:#('Byte' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' 'ZiB' 'YiB') |
3573 | 832 |
|
833 |
"/ the one below was written to save divisions. |
|
834 |
"/ It seems to be not worth the effort... |
|
835 |
||
836 |
"/ |unitString n| |
|
837 |
"/ |
|
838 |
"/ nBytes isNil ifTrue:[ |
|
839 |
"/ ^ '-' |
|
840 |
"/ ]. |
|
841 |
"/ |
|
842 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
843 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
844 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
845 |
"/ nBytes < 1000 ifTrue:[ |
|
846 |
"/ n := nBytes. |
|
847 |
"/ unitString := ' '. |
|
848 |
"/ ] ifFalse:[ |
|
849 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
850 |
"/ unitString := ' K' |
|
851 |
"/ ] |
|
852 |
"/ ] ifFalse:[ |
|
853 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
854 |
"/ unitString := ' M' |
|
855 |
"/ ]. |
|
856 |
"/ ] ifFalse:[ |
|
857 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
858 |
"/ unitString := ' G' |
|
859 |
"/ ] |
|
860 |
"/ ] ifFalse:[ |
|
861 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
862 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
863 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
864 |
"/ unitString := ' E' |
|
865 |
"/ ] ifFalse:[ |
|
866 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
867 |
"/ unitString := ' P' |
|
868 |
"/ ] |
|
869 |
"/ ] ifFalse:[ |
|
870 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
871 |
"/ unitString := ' T' |
|
872 |
"/ ]. |
|
873 |
"/ ]. |
|
874 |
"/ |
|
875 |
"/"/ n truncated = n ifTrue:[ |
|
876 |
"/"/ n := n truncated |
|
877 |
"/"/ ]. |
|
878 |
"/ ^ n printString , unitString. |
|
879 |
||
880 |
" |
|
881 |
self fileSizeStringFor:nil |
|
882 |
self fileSizeStringFor:10 |
|
883 |
self fileSizeStringFor:100 |
|
884 |
self fileSizeStringFor:1000 |
|
885 |
self fileSizeStringFor:8192 |
|
886 |
self fileSizeStringFor:10000 |
|
887 |
self fileSizeStringFor:100000 |
|
888 |
self fileSizeStringFor:1000000 |
|
889 |
self fileSizeStringFor:10000000 |
|
890 |
self fileSizeStringFor:100000000 |
|
891 |
self fileSizeStringFor:1000000000 |
|
892 |
self fileSizeStringFor:10000000000 |
|
893 |
self fileSizeStringFor:100000000000 |
|
894 |
self fileSizeStringFor:1000000000000 |
|
895 |
self fileSizeStringFor:10000000000000 |
|
896 |
self fileSizeStringFor:10000000000000000 |
|
897 |
self fileSizeStringFor:100000000000000000000 |
|
898 |
self fileSizeStringFor:1000000000000000000000 |
|
899 |
" |
|
900 |
||
901 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
902 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
903 |
! |
|
904 |
||
1292 | 905 |
fileSizeFromString:fileSizeString |
3573 | 906 |
"given a fileSize string (i.e. a number followed by optional kB, MB, GB etc. or KiB, MiB, GiB,...). |
907 |
Notice that in 1998, IEC standardized the scales to 1000 for k,M,G,... and 1024 for Ki, Mi, Gi, etc. |
|
908 |
See https://en.wikipedia.org/wiki/Mebibyte. |
|
1292 | 909 |
Provided here since this is so common..." |
910 |
||
3573 | 911 |
|numericValue s scaleChar factor scale| |
1292 | 912 |
|
913 |
fileSizeString isEmpty ifTrue:[ |
|
914 |
^ 0 |
|
915 |
]. |
|
916 |
||
917 |
scale := 1. |
|
918 |
s := fileSizeString readStream. |
|
919 |
numericValue := Number readFrom:s onError:[0]. |
|
920 |
s skipSeparators. |
|
921 |
s atEnd ifFalse:[ |
|
922 |
scaleChar := s next asLowercase. |
|
3573 | 923 |
factor := 1000. |
924 |
s peek == $i ifTrue:[ |
|
925 |
factor := 1024 |
|
926 |
]. |
|
3486 | 927 |
scaleChar == $k ifTrue:[ |
3573 | 928 |
scale := factor |
1292 | 929 |
] ifFalse:[ |
3486 | 930 |
scaleChar == $m ifTrue:[ |
3573 | 931 |
scale := factor * factor |
1292 | 932 |
] ifFalse:[ |
3486 | 933 |
scaleChar == $g ifTrue:[ |
3573 | 934 |
scale := factor * factor * factor |
1292 | 935 |
] ifFalse:[ |
3486 | 936 |
scaleChar == $t ifTrue:[ |
3573 | 937 |
scale := factor * factor * factor * factor |
3142 | 938 |
] ifFalse:[ |
3486 | 939 |
scaleChar == $p ifTrue:[ |
3573 | 940 |
scale := factor * factor * factor * factor * factor |
3142 | 941 |
] ifFalse:[ |
3486 | 942 |
scaleChar == $e ifTrue:[ |
3573 | 943 |
scale := factor * factor * factor * factor * factor * factor |
944 |
] ifFalse:[ |
|
945 |
scaleChar == $z ifTrue:[ |
|
946 |
scale := factor * factor * factor * factor * factor * factor * factor |
|
947 |
] ifFalse:[ |
|
948 |
scaleChar == $y ifTrue:[ |
|
949 |
scale := factor * factor * factor * factor * factor * factor * factor * factor |
|
950 |
] |
|
951 |
] |
|
3142 | 952 |
] |
953 |
] |
|
1292 | 954 |
] |
955 |
] |
|
956 |
] |
|
957 |
]. |
|
958 |
]. |
|
959 |
^ (numericValue*scale) asInteger |
|
960 |
||
961 |
" |
|
962 |
self fileSizeFromString:'1234' |
|
1295 | 963 |
self fileSizeFromString:'10 k' |
3573 | 964 |
self fileSizeFromString:'10 kB' |
965 |
self fileSizeFromString:'10 KiB' |
|
1295 | 966 |
self fileSizeFromString:'0.6 k' |
967 |
self fileSizeFromString:'1.8 M' |
|
3573 | 968 |
self fileSizeFromString:'1.8 MB' |
969 |
self fileSizeFromString:'1.8 MiB' |
|
1295 | 970 |
self fileSizeFromString:'0.5 G' |
971 |
self fileSizeFromString:'0.5 T' |
|
3142 | 972 |
self fileSizeFromString:'0.5 P' |
973 |
self fileSizeFromString:'0.5 E' |
|
1292 | 974 |
" |
3142 | 975 |
|
976 |
"Modified (comment): / 17-12-2013 / 16:56:56 / cg" |
|
1292 | 977 |
! |
978 |
||
3575 | 979 |
fileSizeSIStringFor:nBytes |
980 |
"return a useful string for a size-in-bytes in SI standard notation; |
|
3573 | 981 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
1294 | 982 |
as required and useful. |
3573 | 983 |
Provided here since this is so common... |
984 |
Notice: the scale is 1000 |
|
985 |
the 1000 scale has been standardized in 1998 by IEC to MB, GB, etc. |
|
986 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1143 | 987 |
|
3573 | 988 |
^ self |
989 |
unitStringFor:nBytes |
|
990 |
scale:1000 |
|
3575 | 991 |
rounded:true |
3576 | 992 |
unitStrings:#('Byte' 'KB' 'MB' 'GB' 'TB' 'PB' 'EB' 'ZB' 'YB') |
3575 | 993 |
|
994 |
"/ |unitString n| |
|
995 |
"/ |
|
996 |
"/ nBytes isNil ifTrue:[ |
|
997 |
"/ ^ '-' |
|
998 |
"/ ]. |
|
999 |
"/ |
|
1000 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1001 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1002 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1003 |
"/ nBytes < 1000 ifTrue:[ |
|
1004 |
"/ n := nBytes. |
|
1005 |
"/ unitString := ' '. |
|
1006 |
"/ ] ifFalse:[ |
|
1007 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1008 |
"/ unitString := ' K' |
|
1009 |
"/ ] |
|
1010 |
"/ ] ifFalse:[ |
|
1011 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1012 |
"/ unitString := ' M' |
|
1013 |
"/ ]. |
|
1014 |
"/ ] ifFalse:[ |
|
1015 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1016 |
"/ unitString := ' G' |
|
1017 |
"/ ] |
|
1018 |
"/ ] ifFalse:[ |
|
1019 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1020 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1021 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1022 |
"/ unitString := ' E' |
|
1023 |
"/ ] ifFalse:[ |
|
1024 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1025 |
"/ unitString := ' P' |
|
1026 |
"/ ] |
|
1027 |
"/ ] ifFalse:[ |
|
1028 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1029 |
"/ unitString := ' T' |
|
1030 |
"/ ]. |
|
1031 |
"/ ]. |
|
1032 |
"/ |
|
1033 |
"/"/ n truncated = n ifTrue:[ |
|
1034 |
"/"/ n := n truncated |
|
1035 |
"/"/ ]. |
|
1036 |
"/ ^ n printString , unitString. |
|
1037 |
||
1038 |
" |
|
1039 |
self fileSizeStringFor:nil |
|
1040 |
self fileSizeStringFor:10 |
|
1041 |
self fileSizeStringFor:100 |
|
1042 |
self fileSizeStringFor:1000 |
|
1043 |
self fileSizeStringFor:8192 |
|
1044 |
self fileSizeStringFor:10000 |
|
1045 |
self fileSizeStringFor:100000 |
|
1046 |
self fileSizeStringFor:1000000 |
|
1047 |
self fileSizeStringFor:10000000 |
|
1048 |
self fileSizeStringFor:100000000 |
|
1049 |
self fileSizeStringFor:1000000000 |
|
1050 |
self fileSizeStringFor:10000000000 |
|
1051 |
self fileSizeStringFor:100000000000 |
|
1052 |
self fileSizeStringFor:1000000000000 |
|
1053 |
self fileSizeStringFor:10000000000000 |
|
1054 |
self fileSizeStringFor:10000000000000000 |
|
1055 |
self fileSizeStringFor:100000000000000000000 |
|
1056 |
self fileSizeStringFor:1000000000000000000000 |
|
1057 |
" |
|
1058 |
||
1059 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1060 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
1061 |
! |
|
1062 |
||
1063 |
fileSizeStringFor:nBytes |
|
1064 |
"return a useful string for a size-in-bytes. |
|
1065 |
Scales either by 1000 or 1024, depending on the user preferences. |
|
1066 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1067 |
||
1068 |
^ self |
|
1069 |
unitStringFor:nBytes |
|
1070 |
scale:(UserPreferences current unitForFileSize) |
|
1071 |
rounded:true |
|
1072 |
unitStrings:(UserPreferences current unitStringsForFileSize) |
|
3457 | 1073 |
|
3573 | 1074 |
"/ |unitString n| |
1075 |
"/ |
|
1076 |
"/ nBytes isNil ifTrue:[ |
|
1077 |
"/ ^ '-' |
|
1346 | 1078 |
"/ ]. |
3573 | 1079 |
"/ |
1080 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1081 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1082 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1083 |
"/ nBytes < 1000 ifTrue:[ |
|
1084 |
"/ n := nBytes. |
|
1085 |
"/ unitString := ' '. |
|
1086 |
"/ ] ifFalse:[ |
|
1087 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1088 |
"/ unitString := ' K' |
|
1089 |
"/ ] |
|
1090 |
"/ ] ifFalse:[ |
|
1091 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1092 |
"/ unitString := ' M' |
|
1093 |
"/ ]. |
|
1094 |
"/ ] ifFalse:[ |
|
1095 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1096 |
"/ unitString := ' G' |
|
1097 |
"/ ] |
|
1098 |
"/ ] ifFalse:[ |
|
1099 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1100 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1101 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1102 |
"/ unitString := ' E' |
|
1103 |
"/ ] ifFalse:[ |
|
1104 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1105 |
"/ unitString := ' P' |
|
1106 |
"/ ] |
|
1107 |
"/ ] ifFalse:[ |
|
1108 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1109 |
"/ unitString := ' T' |
|
1110 |
"/ ]. |
|
1111 |
"/ ]. |
|
1112 |
"/ |
|
1113 |
"/"/ n truncated = n ifTrue:[ |
|
1114 |
"/"/ n := n truncated |
|
1115 |
"/"/ ]. |
|
1116 |
"/ ^ n printString , unitString. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1117 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1118 |
" |
3457 | 1119 |
self fileSizeStringFor:nil |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1120 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1121 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1122 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1123 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1124 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1125 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1126 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1127 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1128 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1129 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1130 |
self fileSizeStringFor:10000000000 |
1293 | 1131 |
self fileSizeStringFor:100000000000 |
1132 |
self fileSizeStringFor:1000000000000 |
|
1133 |
self fileSizeStringFor:10000000000000 |
|
3141 | 1134 |
self fileSizeStringFor:10000000000000000 |
3142 | 1135 |
self fileSizeStringFor:100000000000000000000 |
1136 |
self fileSizeStringFor:1000000000000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1137 |
" |
3141 | 1138 |
|
3142 | 1139 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
3457 | 1140 |
"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
|
1141 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1142 |
|
3120 | 1143 |
frequencyFromString:frequencyString |
1144 |
"given a frequency string (i.e. a number followed by optional kHz, Mhz or Hhz as required. |
|
1145 |
Provided here since this is so common..." |
|
1146 |
||
1147 |
|numericValue s scaleChar scale rest| |
|
1148 |
||
1149 |
frequencyString isEmpty ifTrue:[ |
|
1150 |
^ 0 |
|
1151 |
]. |
|
1152 |
||
1153 |
scale := 1. |
|
1154 |
s := frequencyString readStream. |
|
1155 |
numericValue := Number readFrom:s onError:[0]. |
|
1156 |
s skipSeparators. |
|
1157 |
s atEnd ifFalse:[ |
|
1158 |
scaleChar := s next asLowercase. |
|
3486 | 1159 |
scaleChar == $/ ifTrue:[ |
3120 | 1160 |
rest := s upToSeparator asLowercase. |
1161 |
(rest = 'm' or:[rest = 'min']) ifTrue:[ |
|
1162 |
scale := 1/60 |
|
1163 |
] ifFalse:[ |
|
1164 |
(rest = 'h' or:[rest = 'hr' or:[rest = 'hour']]) ifTrue:[ |
|
1165 |
scale := 1/60/60 |
|
1166 |
] ifFalse:[ |
|
1167 |
(rest = 'd' or:[rest = 'dy' or:[rest = 'day']]) ifTrue:[ |
|
1168 |
scale := 1/60/60/24 |
|
1169 |
]. |
|
1170 |
]. |
|
1171 |
]. |
|
1172 |
] ifFalse:[ |
|
3486 | 1173 |
scaleChar == $k ifTrue:[ |
3120 | 1174 |
scale := 1000 |
1175 |
] ifFalse:[ |
|
3486 | 1176 |
scaleChar == $m ifTrue:[ |
3120 | 1177 |
scale := 1000 * 1000 |
1178 |
] ifFalse:[ |
|
3487 | 1179 |
scaleChar == $g ifTrue:[ |
3120 | 1180 |
scale := 1000 * 1000 * 1000 |
1181 |
] ifFalse:[ |
|
3487 | 1182 |
scaleChar == $t ifTrue:[ |
3120 | 1183 |
scale := 1000 * 1000 * 1000 * 1000 |
1184 |
] |
|
1185 |
] |
|
1186 |
] |
|
1187 |
]. |
|
1188 |
] |
|
1189 |
]. |
|
1190 |
^ (numericValue*scale) |
|
1191 |
||
1192 |
" |
|
1193 |
self frequencyFromString:'1234' |
|
1194 |
self frequencyFromString:'10 kHz' |
|
1195 |
self frequencyFromString:'0.6 k' |
|
1196 |
self frequencyFromString:'1.8 Mhz' |
|
1197 |
self frequencyFromString:'0.5 Ghz' |
|
1198 |
self frequencyFromString:'0.5 Thz' |
|
3577 | 1199 |
self frequencyFromString:'1000/s' |
3120 | 1200 |
self frequencyFromString:'10/s' |
1201 |
self frequencyFromString:'10/min' |
|
1202 |
self frequencyFromString:'10/hr' |
|
1203 |
" |
|
1204 |
! |
|
1205 |
||
1206 |
frequencyStringFor:hertz |
|
1207 |
"return a useful string for a frequency; |
|
1208 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
|
1209 |
as required and useful. |
|
1210 |
Provided here since this is so common..." |
|
1211 |
||
3573 | 1212 |
^ self |
1213 |
unitStringFor:hertz |
|
1214 |
scale:1000 |
|
3574 | 1215 |
rounded:false |
3576 | 1216 |
unitStrings:#('Hz' 'Khz' 'Mhz' 'Ghz' 'Thz' 'Phz' 'Ehz') |
3120 | 1217 |
|
1218 |
" |
|
3577 | 1219 |
self frequencyStringFor:0.1 |
3120 | 1220 |
self frequencyStringFor:10 |
1221 |
self frequencyStringFor:100 |
|
1222 |
self frequencyStringFor:1000 |
|
1223 |
self frequencyStringFor:8192 |
|
1224 |
self frequencyStringFor:10000 |
|
1225 |
self frequencyStringFor:100000 |
|
1226 |
self frequencyStringFor:1000000 |
|
1227 |
self frequencyStringFor:10000000 |
|
1228 |
self frequencyStringFor:100000000 |
|
1229 |
self frequencyStringFor:1000000000 |
|
1230 |
self frequencyStringFor:10000000000 |
|
1231 |
self frequencyStringFor:100000000000 |
|
1232 |
self frequencyStringFor:1000000000000 |
|
1233 |
self frequencyStringFor:10000000000000 |
|
1234 |
" |
|
1235 |
! |
|
1236 |
||
363 | 1237 |
inchToMillimeter:inches |
364 | 1238 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 1239 |
|
1240 |
^ inches * 25.4 |
|
1241 |
||
1242 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 1243 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 1244 |
! |
1245 |
||
1246 |
millimeterToInch:mm |
|
364 | 1247 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 1248 |
|
1249 |
^ mm / 25.4 |
|
1250 |
||
1251 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 1252 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
3573 | 1253 |
! |
1254 |
||
3574 | 1255 |
unitStringFor:nBytes scale:k rounded:rounded unitStrings:unitStrings |
3573 | 1256 |
"common helper for SI (1000-based) and binary (1024-based) human readable string generators. |
3574 | 1257 |
Notice that both forms are often encountered and useful. |
1258 |
If rounded is true, round to nearest integer" |
|
3573 | 1259 |
|
3576 | 1260 |
|n kN idx val unitString| |
3573 | 1261 |
|
1262 |
nBytes isNil ifTrue:[ |
|
1263 |
^ '-' |
|
1264 |
]. |
|
1265 |
||
1266 |
idx := 1. |
|
1267 |
n := nBytes. |
|
1268 |
kN := 1. |
|
1269 |
[ (idx < unitStrings size) and:[ nBytes >= (kN * k) ] ] whileTrue:[ |
|
1270 |
idx := idx + 1. |
|
1271 |
kN := kN * k. |
|
1272 |
]. |
|
3575 | 1273 |
|
1274 |
val := nBytes / kN. |
|
3576 | 1275 |
unitString := ' ',(unitStrings at:idx). |
1276 |
||
3575 | 1277 |
((rounded and:[val > 20]) or:[val isInteger]) ifTrue:[ |
3576 | 1278 |
^ (val rounded) printString , unitString |
3574 | 1279 |
]. |
3576 | 1280 |
^ (val asFixedPoint:1) printString , unitString |
3573 | 1281 |
|
1282 |
" |
|
1283 |
self fileSizeStringFor:nil |
|
1284 |
self fileSizeStringFor:10 |
|
1285 |
self fileSizeStringFor:100 |
|
3574 | 1286 |
self fileSizeStringFor:900 |
1287 |
self fileSizeStringFor:1100 |
|
1288 |
self fileSizeStringFor:1400 |
|
1289 |
self fileSizeStringFor:1499 |
|
1290 |
self fileSizeStringFor:1500 |
|
1291 |
||
3573 | 1292 |
self fileSizeStringFor:1000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
1293 |
self fileSizeStringFor:8192 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1294 |
self fileSizeStringFor:10000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1295 |
self fileSizeStringFor:100000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1296 |
self fileSizeStringFor:1000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1297 |
self fileSizeStringFor:10000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1298 |
self fileSizeStringFor:100000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1299 |
self fileSizeStringFor:1000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1300 |
self fileSizeStringFor:10000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1301 |
self fileSizeStringFor:100000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1302 |
self fileSizeStringFor:1000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1303 |
self fileSizeStringFor:10000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1304 |
self fileSizeStringFor:10000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1305 |
self fileSizeStringFor:100000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1306 |
self fileSizeStringFor:1000000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1307 |
||
1308 |
self fileSizeStringFor:8192 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1309 |
self fileSizeStringFor:81920000 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1310 |
" |
|
1311 |
||
1312 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1313 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
363 | 1314 |
! ! |
1315 |
||
516 | 1316 |
!UnitConverter class methodsFor:'missing conversion'! |
1317 |
||
1318 |
noConversionFrom:sourceUnit to:destUnit |
|
1319 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
1320 |
^ nil |
|
1321 |
||
1322 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
1323 |
! ! |
|
1324 |
||
363 | 1325 |
!UnitConverter class methodsFor:'private'! |
1326 |
||
1327 |
addConversion:factor from:sourceMetric to:destMetric |
|
1328 |
"add a conversion" |
|
1329 |
||
1330 |
|conversion| |
|
1331 |
||
1332 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
1333 |
conversion isNil ifTrue:[ |
|
601 | 1334 |
conversion := IdentityDictionary new. |
1335 |
Conversions at:sourceMetric put:conversion |
|
363 | 1336 |
]. |
1337 |
conversion at:destMetric put:factor. |
|
1338 |
^ conversion |
|
1339 |
||
1340 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 1341 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 1342 |
! |
1343 |
||
1344 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
1345 |
"given a value in sourceMetric (symbolic), try to convert it |
|
1346 |
to destMetric (symbolic); |
|
1347 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
1348 |
||
1349 |
|conversion factor| |
|
1350 |
||
550 | 1351 |
Conversions isNil ifTrue:[ |
601 | 1352 |
self initializeConversions |
550 | 1353 |
]. |
1354 |
||
363 | 1355 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
1356 |
conversion isNil ifTrue:[^ nil]. |
|
1357 |
||
1358 |
factor := conversion at:destMetric ifAbsent:nil. |
|
1359 |
factor isNil ifTrue:[^ nil]. |
|
1360 |
||
364 | 1361 |
factor isNumber ifTrue:[ |
601 | 1362 |
^ howMany * factor |
364 | 1363 |
]. |
515 | 1364 |
^ factor value:howMany |
363 | 1365 |
|
1366 |
" |
|
1367 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
1368 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 1369 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 1370 |
" |
1371 |
||
1372 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 1373 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 1374 |
! |
1375 |
||
1376 |
unscaled:what |
|
1377 |
"given a unit, return the base and a factor as assoc, |
|
1378 |
or nil if not found" |
|
1379 |
||
550 | 1380 |
Conversions isNil ifTrue:[ |
2995 | 1381 |
self initializeConversions |
550 | 1382 |
]. |
1383 |
||
363 | 1384 |
Scaling keysAndValuesDo:[:name :factor | |
2995 | 1385 |
|rest| |
363 | 1386 |
|
2995 | 1387 |
(what startsWith:name) ifTrue:[ |
1388 |
rest := what withoutPrefix:name. |
|
1389 |
rest := rest asSymbolIfInterned. |
|
1390 |
rest notNil ifTrue:[ |
|
1391 |
^ rest -> factor. |
|
1392 |
] |
|
1393 |
]. |
|
363 | 1394 |
]. |
1395 |
^ nil |
|
1396 |
||
1397 |
" |
|
1398 |
UnitConverter unscaled:#millimeter |
|
1399 |
UnitConverter unscaled:#nanometer |
|
1400 |
UnitConverter unscaled:#kilometer |
|
1401 |
UnitConverter unscaled:#fuzzymeter |
|
1402 |
" |
|
1403 |
||
550 | 1404 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1405 |
! ! |
1406 |
||
3650 | 1407 |
!UnitConverter class methodsFor:'queries'! |
1408 |
||
1409 |
isUtilityClass |
|
1410 |
^ self == UnitConverter |
|
1411 |
! ! |
|
1412 |
||
363 | 1413 |
!UnitConverter class methodsFor:'documentation'! |
1414 |
||
1415 |
version |
|
3573 | 1416 |
^ '$Header$' |
2320 | 1417 |
! |
1418 |
||
1419 |
version_CVS |
|
3573 | 1420 |
^ '$Header$' |
363 | 1421 |
! ! |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
1422 |