c96f83d8697160754fb7b1636f223a8888055923
[reactos.git] / base / applications / calc / convert.c
1 /*
2 * ReactOS Calc (Unit conversion engine)
3 *
4 * Copyright 2007-2017, Carlo Bramini
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "calc.h"
22
23 /*
24 The conversion is made by converting the "from" unit
25 into the I.S. unit and then the I.S. unit is converted
26 into the "from" one.
27
28 The base units for all categories are:
29
30 ANGLE..........degrees
31 AREA...........square meters
32 CONSUMPTION....kilometers per liter
33 CURRENCY.......Euro
34 ENERGY.........joule
35 LENGTHS........meters
36 POWER..........Watt
37 PRESSURE.......Pascal
38 TEMPERATURE....kelvin
39 TIME...........seconds
40 VELOCITY.......meters per second
41 VOLUME.........liter
42 WEIGHT.........gram
43
44 The '$' character is used as "what you read into the display".
45 */
46 typedef struct {
47 DWORD unit;
48 const char *formula_from;
49 const char *formula_to;
50 } conv_t;
51
52 typedef struct {
53 const DWORD category;
54 const conv_t *items;
55 } conv_category_t;
56
57 #define DECLARE_CONV_CAT(_category) \
58 { IDS_CONV_##_category, conv_##_category },
59
60 #define DECLARE_CONV_UNIT(_category, _unit, _from, _to ) \
61 { IDS_##_category##_##_unit, _from, _to },
62
63 #define DECLARE_CONV_END \
64 { 0, NULL, NULL },
65
66 /*
67 1 gradian = 0.9 deg
68 1 radian = 57.29577951308233 deg
69 */
70 static const conv_t conv_ANGLE[] = {
71 DECLARE_CONV_UNIT(ANGLE, DEGREES, "$", "$")
72 DECLARE_CONV_UNIT(ANGLE, GRADIANS, "$*0.9", "$/0.9")
73 DECLARE_CONV_UNIT(ANGLE, RADIANS, "$/P*180", "$*P/180")
74 DECLARE_CONV_END
75 };
76
77 /*
78 1 acre ................ = 4840 square yd = 4046,8564224 mq
79 1 acre brazil ......... =
80 1 acre france ......... =
81 1 acre scots .......... = 5000 mq
82 1 acre us ............. = 4840*(36/39.37)^2 m = 6272640/1549.9969 m
83 1 are ................. = 100 mq
84 1 chou ................ = 108000*(10/33)^2 mq
85 1 danbo ............... = 991.74 mq
86 1 ha .................. = 10000 mq
87 1 jeongbo ............. = 9917.4 mq
88 1 morgen hungary ...... = 5754.642 mq
89 1 mu .................. = 2000/3 mq
90 1 ping ................ =
91 1 pyeong .............. = 400/121 mq
92 1 pyeongbangja ........ =
93 1 rai ................. = 1600 mq
94 1 se .................. = 1080*(10/33)^2 mq
95 1 square cm ........... = 0.0001 mq
96 1 square chr .......... =
97 1 square fathom ....... = 1.8288^2 = 3.34450944 mq
98 1 square fathom hungary = 1.8964838^2 = 3.59665080366244 mq
99 1 square ft ........... = 0,09290304 mq
100 1 square in ........... = 0,00064516 mq
101 1 square km ........... = 1000000 mq
102 1 square lar .......... =
103 1 square mile ......... = 1609.344^2 = 2589988.110336 mq
104 1 square mm ........... = 0,000001 mq
105 1 square shaku ........ = (10/33)^2 mq
106 1 square tsuen ........ =
107 1 square va ........... = 4 mq
108 1 square yard ......... = 0,83612736 mq
109 1 tan ................. = 10800*(10/33)^2 mq
110 1 tsubo ............... = 36*(10/33)^2 mq
111 */
112 static const conv_t conv_AREA[] = {
113 DECLARE_CONV_UNIT(AREA, ACRES, "$*4046,8564224", "$/4046,8564224")
114 // DECLARE_CONV_UNIT(AREA, ACRES_BRAZIL, "$", "$")
115 // DECLARE_CONV_UNIT(AREA, ACRES_FRANCE, "$", "$")
116 DECLARE_CONV_UNIT(AREA, ACRES_US, "$*627264/154.99969", "$/627264*154.99969")
117 DECLARE_CONV_UNIT(AREA, ACRES_SCOTS, "$*5000", "$/5000")
118 DECLARE_CONV_UNIT(AREA, ARES, "$*100", "$/100")
119 DECLARE_CONV_UNIT(AREA, CHOU, "$*10800000/1089", "$*1089/10800000")
120 DECLARE_CONV_UNIT(AREA, DANBO, "$*991.74", "$/991.74")
121 DECLARE_CONV_UNIT(AREA, HECTARES, "$*10000", "$/10000")
122 DECLARE_CONV_UNIT(AREA, JEONGBO, "$*9917.4", "$/9917.4")
123 // DECLARE_CONV_UNIT(AREA, MORGEN_HUNGARY, "$", "$")
124 DECLARE_CONV_UNIT(AREA, MU, "$*(2000/3)", "$/(2000/3)")
125 // DECLARE_CONV_UNIT(AREA, PING, "$", "$")
126 DECLARE_CONV_UNIT(AREA, PYEONG, "$*400/121", "$*121/400")
127 // DECLARE_CONV_UNIT(AREA, PYEONGBANGJA, "$", "$")
128 DECLARE_CONV_UNIT(AREA, RAI, "$*1600", "$/1600")
129 DECLARE_CONV_UNIT(AREA, SE, "$*108000/1089", "$*1089/108000")
130 DECLARE_CONV_UNIT(AREA, SQUARE_CENTIMETERS, "$*0,0001", "$/0,0001")
131 // DECLARE_CONV_UNIT(AREA, SQUARE_CHR, "$", "$")
132 DECLARE_CONV_UNIT(AREA, SQUARE_FATHOMS, "$*3.34450944", "$/3.34450944")
133 DECLARE_CONV_UNIT(AREA, SQUARE_FATHOMS_HUNGARY, "$*3.59665080366244", "$/3.59665080366244")
134 DECLARE_CONV_UNIT(AREA, SQUARE_FEET, "$*0,09290304", "$/0,09290304")
135 DECLARE_CONV_UNIT(AREA, SQUARE_INCHES, "$*0,00064516", "$/0.00064516")
136 DECLARE_CONV_UNIT(AREA, SQUARE_KILOMETERS, "$*1000000", "$/1000000")
137 // DECLARE_CONV_UNIT(AREA, SQUARE_LAR, "$", "$")
138 DECLARE_CONV_UNIT(AREA, SQUARE_METER, "$", "$")
139 DECLARE_CONV_UNIT(AREA, SQUARE_MILES, "$*2589988.110336", "$/2589988.110336")
140 DECLARE_CONV_UNIT(AREA, SQUARE_MILLIMETERS, "$*1000000", "$/1000000")
141 DECLARE_CONV_UNIT(AREA, SQUARE_SHAKU, "$*100/1089", "$/1089*100")
142 // DECLARE_CONV_UNIT(AREA, SQUARE_TSUEN, "$", "$")
143 DECLARE_CONV_UNIT(AREA, SQUARE_VA, "$*4", "$/4")
144 DECLARE_CONV_UNIT(AREA, SQUARE_YARD, "$*0,83612736", "$/0,83612736")
145 DECLARE_CONV_UNIT(AREA, TAN, "$*1080000/1089", "$*1089/1080000")
146 DECLARE_CONV_UNIT(AREA, TSUBO, "$*1188/1089", "$*1089/1188")
147 DECLARE_CONV_END
148 };
149
150 /*
151 1 l/100Km = 100 km/l -> y = 100/x
152 1 miles/gal uk = 1.609344/4.54609 km/l
153 1 miles/gal us = 1.609344/3.785411784 km/l
154 */
155 static const conv_t conv_CONSUMPTION[] = {
156 DECLARE_CONV_UNIT(CONSUMPTION, KM_PER_L, "$", "$")
157 DECLARE_CONV_UNIT(CONSUMPTION, L_PER_100_KM, "100/$", "100/$")
158 DECLARE_CONV_UNIT(CONSUMPTION, MILES_GALLON_UK, "$*1.609344/4.54609", "$/1.609344*4.54609")
159 DECLARE_CONV_UNIT(CONSUMPTION, MILES_GALLON_US, "$*1.609344/3.785411784", "$/1.609344*3.785411784")
160 DECLARE_CONV_END
161 };
162
163 static const conv_t conv_CURRENCY[] = {
164 DECLARE_CONV_UNIT(CURRENCY, AUSTRIAN_SCHILLING, "$/13,7603", "$*13,7603")
165 DECLARE_CONV_UNIT(CURRENCY, BELGIAN_FRANC, "$/40,3399", "$*40,3399")
166 DECLARE_CONV_UNIT(CURRENCY, CYPRIOT_POUND, "$/0,585274","$*0,585274")
167 DECLARE_CONV_UNIT(CURRENCY, DEUTSCHE_MARK, "$/1,95583", "$*1,95583")
168 DECLARE_CONV_UNIT(CURRENCY, DUTCH_GUILDER, "$/2,20371", "$*2,20371")
169 DECLARE_CONV_UNIT(CURRENCY, ESTONIAN_KROON, "$/15,6466", "$*15,6466")
170 DECLARE_CONV_UNIT(CURRENCY, EURO, "$", "$")
171 DECLARE_CONV_UNIT(CURRENCY, FINNISH_MARKKA, "$/5,94573", "$*5,94573")
172 DECLARE_CONV_UNIT(CURRENCY, FRENCH_FRANC, "$/6,55957", "$*6,55957")
173 DECLARE_CONV_UNIT(CURRENCY, GREEK_DRACHMA, "$/340,751", "$*340,751")
174 DECLARE_CONV_UNIT(CURRENCY, IRISH_POUND, "$/0,787564","$*0,787564")
175 DECLARE_CONV_UNIT(CURRENCY, ITALIAN_LIRA, "$/1936.27", "$*1936.27")
176 DECLARE_CONV_UNIT(CURRENCY, LATVIAN_LATS, "$/0,7028", "$*0,7028")
177 DECLARE_CONV_UNIT(CURRENCY, LITHUANIAN_LITAS, "$/3,45280", "$*3,45280")
178 DECLARE_CONV_UNIT(CURRENCY, LUXEMBOURG_FRANC, "$/40,3399", "$*40,3399")
179 DECLARE_CONV_UNIT(CURRENCY, MALTESE_LIRA, "$/0.42930", "$*0.42930")
180 DECLARE_CONV_UNIT(CURRENCY, PORTOGUESE_ESCUDO, "$/200,482", "$*200,482")
181 DECLARE_CONV_UNIT(CURRENCY, SLOVAK_KORUNA, "$/30,126", "$*30,126")
182 DECLARE_CONV_UNIT(CURRENCY, SLOVENIAN_TOLAR, "$/239,640", "$*239,640")
183 DECLARE_CONV_UNIT(CURRENCY, SPANISH_PESETA, "$/166,386", "$*166,386")
184 DECLARE_CONV_END
185 };
186
187 /*
188 1 cal15 .... = 4.1855 J
189 1 BTU ...... = 1055.056 J
190 1 ERG ...... = 0.0000001 J
191 1 eV ....... = 0.000000000000000000160217653 J
192 1 Foot-Pound = 1.3558179483314004 J
193 1 IT calorie = 4.1868 J
194 1 KCa ...... = 4186.8 J
195 1 KJ ....... = 1000 J
196 1 KWh ...... = 3600 J
197 1 IUNS ..... = 4.182 J
198 1 calth .... = 4.184 J
199 */
200 static const conv_t conv_ENERGY[] = {
201 DECLARE_CONV_UNIT(ENERGY, 15_C_CALORIES, "$*4.1855", "$/4.1855")
202 DECLARE_CONV_UNIT(ENERGY, BTUS, "$*1055.056", "$/1055.056")
203 DECLARE_CONV_UNIT(ENERGY, ERGS, "$*.0000001", "$/.0000001")
204 DECLARE_CONV_UNIT(ENERGY, EVS, "$*1.60217653/1X19", "$/1.60217653*1X19")
205 DECLARE_CONV_UNIT(ENERGY, FOOT_POUNDS, "$*1.3558179483314004", "$/1.3558179483314004")
206 DECLARE_CONV_UNIT(ENERGY, IT_CALORIES, "$*4.1868", "$/4.1868")
207 DECLARE_CONV_UNIT(ENERGY, IT_KILOCALORIES, "$*4186.8", "$/4186.8")
208 DECLARE_CONV_UNIT(ENERGY, JOULES, "$", "$")
209 DECLARE_CONV_UNIT(ENERGY, KILOJOULES, "$*1000", "$/1000")
210 DECLARE_CONV_UNIT(ENERGY, KILOWATT_HOURS, "$*3600", "$/3600")
211 DECLARE_CONV_UNIT(ENERGY, NUTRITION_CALORIES, "$*4.182", "$/4.182")
212 DECLARE_CONV_UNIT(ENERGY, TH_CALORIES, "$*4.184", "$/4.184")
213 DECLARE_CONV_END
214 };
215
216 /*
217 1 angstrom ....... = 0.0000000001 m
218 1 astronomical unit= 149598000000 m
219 1 barleycorn ..... = 1/3 inch = 0.9144/108 m
220 1 cm ............. = 1/100 m
221 1 chain uk ....... = 22 yards = 22*0.9144 m
222 1 chi ............ = 1/3 m
223 1 chou ........... = 3600/33 m
224 1 chr ............ =
225 1 cun ............ = 1/10 chi = 1/30 m
226 1 fathom ......... = 2 yard = 2*0.9144 m
227 1 fathom ungary .. = 1.8964838 m (fixed)
228 1 feet ........... = 12 inch = 0.9144/3 m
229 1 furlong ........ = 10 chains = 220*0.9144 m
230 1 gan ............ =
231 1 hand ........... = 4 inches = 0.9144/9 m
232 1 hunh ........... = 1/8 nieu = 9.144/3456 m
233 1 inch ........... = yard/36 = 0.9144/36 m
234 1 ja ............. =
235 1 jeong .......... =
236 1 kabiet ......... = 1/4 nieu = 9.144/1728 m
237 1 ken ............ = 60/33 m
238 1 keub ........... = 12 nieu = 10 inch = 9.144/36 m
239 1 km ............. = 1000 m
240 1 lar ............ =
241 1 light year ..... = 9460730472580800 m
242 1 link uk ........ = 0.01 chains = 0.22*0.9144 m
243 1 micron ......... = 0.000001 m
244 1 mile ........... = 1760 yards = 1609.344 m
245 1 millimeter ..... = 1/1000 m
246 1 nautical mile .. = 1852 m
247 1 nieu ........... = 10/12 inch = 9.144/432 m
248 1 parsec ......... = 30856800000000000 m
249 1 pica ........... = yard/216 = 0.9144/216 m
250 1 ri japan ....... = 129600/33 m
251 1 ri korea ....... = 12960/33 m
252 1 rod ............ = 5.0292 m
253 1 sawk ........... = 2 keub = 20 inch = 9.144/18 m
254 1 sen ............ = 20 va = 40.64 m
255 1 shaku .......... = 10/33 m
256 1 span ........... = 9 inches = 0.9144/4 m
257 1 sun ............ = 1/33 m
258 1 tsuen .......... =
259 1 va ............. = 4 sawk = 80 inch = 2.032 m
260 1 yard ........... = 0.9144 m
261 1 yote ........... = 400 sen = 16256 m
262 1 zhang .......... = 3+1/3 m = 10/3 m
263 */
264 static const conv_t conv_LENGTH[] = {
265 DECLARE_CONV_UNIT(LENGTH, ANGSTROMS, "$/1X10", "$*1X10")
266 DECLARE_CONV_UNIT(LENGTH, ASTRONOMICAL_UNITS, "$*149598000000", "$/149598000000")
267 DECLARE_CONV_UNIT(LENGTH, BARLEYCORNS, "$*0.9144/108", "$/0.9144*108")
268 DECLARE_CONV_UNIT(LENGTH, CENTIMETERS, "$/100", "$*100")
269 DECLARE_CONV_UNIT(LENGTH, CHAINS_UK, "$*20.1168", "$/20.1168")
270 DECLARE_CONV_UNIT(LENGTH, CHI, "$/3", "$*3")
271 DECLARE_CONV_UNIT(LENGTH, CHOU, "$*3600/33", "$*33/3600")
272 // DECLARE_CONV_UNIT(LENGTH, CHR, "$", "$")
273 DECLARE_CONV_UNIT(LENGTH, CUN, "$/30", "$*30")
274 DECLARE_CONV_UNIT(LENGTH, FATHOMS, "$*1.8288", "$/1.8288")
275 DECLARE_CONV_UNIT(LENGTH, FATHOMS_HUNGARY, "$*1.8964838", "$/1.8964838")
276 DECLARE_CONV_UNIT(LENGTH, FEET, "$*0.3048", "$/0.3048")
277 DECLARE_CONV_UNIT(LENGTH, FURLONGS, "$*201.168", "$/201.168")
278 // DECLARE_CONV_UNIT(LENGTH, GAN, "$", "$")
279 DECLARE_CONV_UNIT(LENGTH, HANDS, "$*0,1016", "$/0,1016")
280 DECLARE_CONV_UNIT(LENGTH, HUNH, "$*9.144/3456", "$/9.144*3456")
281 DECLARE_CONV_UNIT(LENGTH, INCHES, "$*0.0254", "$/0.0254")
282 // DECLARE_CONV_UNIT(LENGTH, JA, "$", "$")
283 // DECLARE_CONV_UNIT(LENGTH, JEONG, "$", "$")
284 DECLARE_CONV_UNIT(LENGTH, KABIET, "$*9.144/1728", "$/9.144*1728")
285 DECLARE_CONV_UNIT(LENGTH, KEN, "$*60/33", "$*33/60")
286 DECLARE_CONV_UNIT(LENGTH, KEUB, "$*9.144/36", "$/9.144*36")
287 DECLARE_CONV_UNIT(LENGTH, KILOMETERS, "$*1000", "$/1000")
288 // DECLARE_CONV_UNIT(LENGTH, LAR, "$", "$")
289 DECLARE_CONV_UNIT(LENGTH, LIGHT_YEARS, "$*9460730472580800", "$/9460730472580800")
290 DECLARE_CONV_UNIT(LENGTH, LINKS_UK, "$*0.201168", "$/0.201168")
291 DECLARE_CONV_UNIT(LENGTH, METERS, "$", "$")
292 DECLARE_CONV_UNIT(LENGTH, MICRONS, "$*0.000001", "$/0.000001")
293 DECLARE_CONV_UNIT(LENGTH, MILES, "$*1609.344", "$/1609.344")
294 DECLARE_CONV_UNIT(LENGTH, MILLIMETERS, "$/1000", "$*1000")
295 DECLARE_CONV_UNIT(LENGTH, NAUTICAL_MILES, "$*1852", "$/1852")
296 DECLARE_CONV_UNIT(LENGTH, NIEU, "$*9.144/432", "$/9.144*432")
297 DECLARE_CONV_UNIT(LENGTH, PARSECS, "$*3.085678X16", "$/3.085678X16")
298 DECLARE_CONV_UNIT(LENGTH, PICAS, "$*0.9144/216", "$/0.9144*216")
299 DECLARE_CONV_UNIT(LENGTH, RODS, "$*5.0292", "$/5.0292")
300 DECLARE_CONV_UNIT(LENGTH, RI_JAPAN, "$*129600/33", "$*33/129600")
301 DECLARE_CONV_UNIT(LENGTH, RI_KOREA, "$*12960/33", "$*33/12960")
302 DECLARE_CONV_UNIT(LENGTH, SAWK, "$*9.144/18", "$/9.144*18")
303 DECLARE_CONV_UNIT(LENGTH, SEN, "$*40.64", "$/40.64")
304 DECLARE_CONV_UNIT(LENGTH, SHAKU, "$*10/33", "$*33/10")
305 DECLARE_CONV_UNIT(LENGTH, SPAN, "$*0.9144/4", "$*4/0.9144")
306 DECLARE_CONV_UNIT(LENGTH, SUN, "$*1/33", "$*33")
307 // DECLARE_CONV_UNIT(LENGTH, TSUEN, "$", "$")
308 DECLARE_CONV_UNIT(LENGTH, VA, "$*2.032", "$/2.032")
309 DECLARE_CONV_UNIT(LENGTH, YARDS, "$*0.9144", "$/0.9144")
310 DECLARE_CONV_UNIT(LENGTH, YOTE, "$*16256", "$/16256")
311 DECLARE_CONV_UNIT(LENGTH, ZHANG, "$/0.3", "$*0.3")
312 DECLARE_CONV_END
313 };
314
315 /*
316 1 BTU/min = 17.5842642 W
317 1 Foot-pound/minute = 0.02259696580552333 W
318 1 hp = 745.69987158227022 W
319 1 KW = 1000 W
320 1 MW = 1000000 W
321 */
322 static const conv_t conv_POWER[] = {
323 DECLARE_CONV_UNIT(POWER, BTUS_PER_MINUTE, "$*17.5842642", "$/17.5842642")
324 DECLARE_CONV_UNIT(POWER, FPS_PER_MINUTE, "$*0.02259696580552333", "$/0.02259696580552333")
325 DECLARE_CONV_UNIT(POWER, HORSEPOWER, "$*745.69987158227022", "$/745.69987158227022")
326 DECLARE_CONV_UNIT(POWER, KILOWATTS, "$*1000", "$/1000")
327 DECLARE_CONV_UNIT(POWER, MEGAWATTS, "$*1000000", "$/1000000")
328 DECLARE_CONV_UNIT(POWER, WATTS, "$", "$")
329 DECLARE_CONV_END
330 };
331
332 /*
333 1 ATM = 101325 Pa
334 1 BAR = 100000 Pa
335 1 hPa = 100 Pa
336 1 kPa = 1000 Pa
337 1 mm HG = 133.322 Pa
338 1 psi = 6894.757 Pa
339 */
340 static const conv_t conv_PRESSURE[] = {
341 DECLARE_CONV_UNIT(PRESSURE, ATMOSPHERES, "$*101325", "$/101325")
342 DECLARE_CONV_UNIT(PRESSURE, BARS, "$*100000", "$/100000")
343 DECLARE_CONV_UNIT(PRESSURE, HECTOPASCALS, "$*100", "$/100")
344 DECLARE_CONV_UNIT(PRESSURE, KILOPASCALS, "$*1000", "$/1000")
345 DECLARE_CONV_UNIT(PRESSURE, MM_OF_MERCURY, "$*133.322", "$/133.322")
346 DECLARE_CONV_UNIT(PRESSURE, PASCALS, "$", "$")
347 DECLARE_CONV_UNIT(PRESSURE, PSI, "$*6894.757", "$/6894.757")
348 DECLARE_CONV_END
349 };
350
351 /*
352 1 day ....... = 86400 s
353 1 hour ...... = 3600 s
354 1 microsecond = 0.000001 s
355 1 millisecond = 0.001 s
356 1 nanosecond. = 0.000000001 s
357 1 minute .... = 60 s
358 1 week ...... = 669600 s
359 */
360 static const conv_t conv_TIME[] = {
361 DECLARE_CONV_UNIT(TIME, MINUTES, "$*60", "$/60")
362 DECLARE_CONV_UNIT(TIME, DAYS, "$*86400", "$/86400")
363 DECLARE_CONV_UNIT(TIME, HOURS, "$*3600", "$/3600")
364 DECLARE_CONV_UNIT(TIME, MILLISECONDS, "$*0.001", "$/0.001")
365 DECLARE_CONV_UNIT(TIME, MICROSECONDS, "$*0.000001", "$/0.000001")
366 DECLARE_CONV_UNIT(TIME, NANOSECONDS, "$*0.000000001", "$/0.000000001")
367 DECLARE_CONV_UNIT(TIME, SECONDS, "$", "$")
368 DECLARE_CONV_UNIT(TIME, WEEKS, "$*604800", "$/604800")
369 DECLARE_CONV_UNIT(TIME, YEARS, "$*31556952", "$/31556952")
370 DECLARE_CONV_END
371 };
372
373 /*
374 C = K - 273.15
375 F = K * 9/5 - 459.67
376 R = K * 9/5
377 */
378 static const conv_t conv_TEMPERATURE[] = {
379 DECLARE_CONV_UNIT(TEMPERATURE, CELSIUS, "$+273.15", "$-273.15")
380 DECLARE_CONV_UNIT(TEMPERATURE, FAHRENHEIT, "($+459.67)*5/9", "$*9/5-459.67")
381 DECLARE_CONV_UNIT(TEMPERATURE, KELVIN, "$", "$")
382 DECLARE_CONV_UNIT(TEMPERATURE, RANKINE, "$*5/9", "$*9/5")
383 DECLARE_CONV_END
384 };
385
386 /*
387 1 cm/s = 0.01 m/s
388 1 f/h = 0.0000846666667 m/s
389 1 f/s = 0.3048 m/s
390 1 Km/h = 10/36 m/s -> 0.27778 m/s
391 1 knot = 18.52/36 m/s -> 0.51444444 m/s
392 1 mach = 340.3 m/s
393 1 mph = 0.44704 m/s
394 */
395 static const conv_t conv_VELOCITY[] = {
396 DECLARE_CONV_UNIT(VELOCITY, CMS_SECOND, "$*.01", "$/.01")
397 DECLARE_CONV_UNIT(VELOCITY, FEET_SECOND, "$*.3048", "$/.3048")
398 DECLARE_CONV_UNIT(VELOCITY, FEET_HOUR, "$*.0000846666667", "$/.0000846666667")
399 DECLARE_CONV_UNIT(VELOCITY, KILOMETERS_HOUR, "$*10/36", "$*36/10")
400 DECLARE_CONV_UNIT(VELOCITY, KNOTS, "$*18.52/36", "$*36/18.52")
401 DECLARE_CONV_UNIT(VELOCITY, MACH, "$*340.3", "$/340.3")
402 DECLARE_CONV_UNIT(VELOCITY, METERS_SECOND, "$", "$")
403 DECLARE_CONV_UNIT(VELOCITY, MILES_HOUR, "$*.44704", "$/.44704")
404 DECLARE_CONV_END
405 };
406
407 /*
408 1 barrel uk ...... = 163.65924 l
409 1 barrel oil ..... = 158.987295 l
410 1 bun ............ = 1000 l
411 1 bushel uk ...... = 36.36872 l
412 1 bushel us ...... = 35.23907017 l
413 1 cubic cm ...... = 0.001 l
414 1 cubic feet ..... = 28.316846 l
415 1 cubic inch ..... = 0.016387064 l
416 1 cubic meter .... = 1000 l
417 1 cubic yard ..... = 764.554857 l
418 1 doe ............ =
419 1 fluid ounce uk = 0.0284130625 l
420 1 fluid ounce us = 0.0295735295625 l
421 1 gallon uk ...... = 4.54609 l
422 1 gallon dry us .. = 4.40488377086 l
423 1 gallon liquid us = 3.785411784 l
424 1 gou ............ = 0.1809 l
425 1 hop ............ =
426 1 icce ........... =
427 1 kwian .......... = 2000 l
428 1 mal ............ =
429 1 milliliter ..... = 0.001 l
430 1 pint uk ........ = 0.56826125 l
431 1 pint dry us .... = 0.5506104713575 l
432 1 pint liquid us = 0.473176473 l
433 1 quart uk ....... = 1.1365225 l
434 1 quart dry us ... = 1.101220942715 l
435 1 quart liquid us = 0.946352946 l
436 1 seki ........... =
437 1 syou ........... =
438 1 tananloung ..... = 1 l
439 1 tang ........... = 20 l
440 1 to ............. = 18040 l
441 */
442 static const conv_t conv_VOLUME[] = {
443 DECLARE_CONV_UNIT(VOLUME, BARRELS_UK, "$*163.65924", "$/163.65924")
444 DECLARE_CONV_UNIT(VOLUME, BARRELS_OIL, "$*158.987295", "$/158.987295")
445 DECLARE_CONV_UNIT(VOLUME, BUN, "$*1000", "$/1000")
446 DECLARE_CONV_UNIT(VOLUME, BUSHELS_UK, "$*36.36872", "$/36.36872")
447 DECLARE_CONV_UNIT(VOLUME, BUSHELS_US, "$*35.23907017", "$/35.23907017")
448 DECLARE_CONV_UNIT(VOLUME, CUBIC_CENTIMETERS, "$*0.001", "$/0.001")
449 DECLARE_CONV_UNIT(VOLUME, CUBIC_FEET, "$*28.316846", "$/28.316846")
450 DECLARE_CONV_UNIT(VOLUME, CUBIC_INCHES, "$*0.016387064", "$/0.016387064")
451 DECLARE_CONV_UNIT(VOLUME, CUBIC_METERS, "$*1000", "$/1000")
452 DECLARE_CONV_UNIT(VOLUME, CUBIC_YARDS, "$*764.554857", "$/764.554857")
453 // DECLARE_CONV_UNIT(VOLUME, DOE, "$", "$")
454 DECLARE_CONV_UNIT(VOLUME, FLUID_OUNCES_UK, "$*0.0284130625", "$/0.0284130625")
455 DECLARE_CONV_UNIT(VOLUME, FLUID_OUNCES_US, "$*0.0295735295625", "$/0.0295735295625")
456 DECLARE_CONV_UNIT(VOLUME, GALLONS_UK, "$*4.54609", "$/4.54609")
457 DECLARE_CONV_UNIT(VOLUME, GALLONS_DRY_US, "$*4.40488377086", "$/4.40488377086")
458 DECLARE_CONV_UNIT(VOLUME, GALLONS_LIQUID_US, "$*3.785411784", "$/3.785411784")
459 DECLARE_CONV_UNIT(VOLUME, GOU, "$*0.1809", "$/0.1809")
460 // DECLARE_CONV_UNIT(VOLUME, HOP, "$", "$")
461 // DECLARE_CONV_UNIT(VOLUME, ICCE, "$", "$")
462 DECLARE_CONV_UNIT(VOLUME, KWIAN, "$*2000", "$/2000")
463 DECLARE_CONV_UNIT(VOLUME, LITERS, "$", "$")
464 // DECLARE_CONV_UNIT(VOLUME, MAL, "$", "$")
465 DECLARE_CONV_UNIT(VOLUME, MILLILITERS, "$*0.001", "$/0.001")
466 DECLARE_CONV_UNIT(VOLUME, PINTS_UK, "$*0.56826125", "$/0.56826125")
467 DECLARE_CONV_UNIT(VOLUME, PINTS_DRY_US, "$*0.5506104713575", "$/0.5506104713575")
468 DECLARE_CONV_UNIT(VOLUME, PINTS_LIQUID_US, "$*0.473176473", "$/0.473176473")
469 DECLARE_CONV_UNIT(VOLUME, QUARTS_UK, "$*1.1365225", "$/1.1365225")
470 DECLARE_CONV_UNIT(VOLUME, QUARTS_DRY_US, "$*1.101220942715", "$/1.101220942715")
471 DECLARE_CONV_UNIT(VOLUME, QUARTS_LIQUID_US, "$*0.946352946", "$/0.946352946")
472 // DECLARE_CONV_UNIT(VOLUME, SEKI, "$", "$")
473 // DECLARE_CONV_UNIT(VOLUME, SYOU, "$", "$")
474 DECLARE_CONV_UNIT(VOLUME, TANANLOUNG, "$", "$")
475 DECLARE_CONV_UNIT(VOLUME, TANG, "$*20", "$/20")
476 DECLARE_CONV_UNIT(VOLUME, TO, "$*18040", "$/18040")
477 DECLARE_CONV_END
478 };
479
480 /*
481 1 baht ............ = 12.244 g
482 1 carat ........... = 0.2 g
483 1 chung ........... =
484 1 don ............. =
485 1 geun ............ =
486 1 gwan ............ =
487 1 harb ............ =
488 1 jin china ....... =
489 1 jin taiwan ...... =
490 1 Kan ............. = 3750 g
491 1 Kilograms ....... = 1000 g
492 1 Kin ............. = 600 g
493 1 Liang China ..... =
494 1 Liang Taiwan .... =
495 1 monme ........... = 3.75 g
496 1 ounce avoirdupois = 28.349523125 g
497 1 ounce troy ...... = 31.1034768 g
498 1 pound ........... = 453.59237 g
499 1 quintal metric .. = 100000 g
500 1 saloung ......... =
501 1 stone ........... = 6350.29318 g
502 1 tamlung ......... =
503 1 ton ............. = 1000000 g
504 1 ton uk .......... = 1016046.9088 g // long ton
505 1 ton us .......... = 907184.74 g // short ton
506 */
507 /*
508 1 chang = 20 tamlung
509 1 tamlung = 4 baht
510 1 baht = 4 saloung
511 1 saloung =
512
513 1 chang = 6/5 kg = 1200 g
514 1 tamlung = 1/20 chang = 60 g
515 1 baht = 1/4 tamlung = 15 g
516 1 saloung = 1/4 bath = 15/4 g
517 */
518 static const conv_t conv_WEIGHT[] = {
519 DECLARE_CONV_UNIT(WEIGHT, BAHT, "$*12.244", "$/12.244")
520 DECLARE_CONV_UNIT(WEIGHT, CARATS, "$*0.2", "$/0.2")
521 // DECLARE_CONV_UNIT(WEIGHT, CHUNG, "$", "$")
522 // DECLARE_CONV_UNIT(WEIGHT, DON, "$", "$")
523 // DECLARE_CONV_UNIT(WEIGHT, GEUN, "$", "$")
524 DECLARE_CONV_UNIT(WEIGHT, GRAMS, "$", "$")
525 // DECLARE_CONV_UNIT(WEIGHT, GWAN, "$", "$")
526 // DECLARE_CONV_UNIT(WEIGHT, HARB, "$", "$")
527 // DECLARE_CONV_UNIT(WEIGHT, JIN_CHINA, "$", "$")
528 // DECLARE_CONV_UNIT(WEIGHT, JIN_TAIWAN, "$", "$")
529 DECLARE_CONV_UNIT(WEIGHT, KAN, "$*3750", "$/3750")
530 DECLARE_CONV_UNIT(WEIGHT, KILOGRAMS, "$*1000", "$/1000")
531 DECLARE_CONV_UNIT(WEIGHT, KIN, "$*600", "$/600")
532 // DECLARE_CONV_UNIT(WEIGHT, LIANG_CHINA, "$", "$")
533 // DECLARE_CONV_UNIT(WEIGHT, LIANG_TAIWAN, "$", "$")
534 DECLARE_CONV_UNIT(WEIGHT, MONME, "$*3.75", "$/3.75")
535 DECLARE_CONV_UNIT(WEIGHT, OUNCES_AVOIRDUPOIS, "$*28.349523125", "$/28.349523125")
536 DECLARE_CONV_UNIT(WEIGHT, OUNCES_TROY, "$*31.1034768", "$/31.1034768")
537 DECLARE_CONV_UNIT(WEIGHT, POUNDS, "$*453.59237", "$/453.59237")
538 DECLARE_CONV_UNIT(WEIGHT, QUINTAL_METRIC, "$*100000", "$/100000")
539 // DECLARE_CONV_UNIT(WEIGHT, SALOUNG, "$", "$")
540 DECLARE_CONV_UNIT(WEIGHT, STONES, "$*6350.29318", "$/6350.29318")
541 // DECLARE_CONV_UNIT(WEIGHT, TAMLUNG, "$", "$")
542 DECLARE_CONV_UNIT(WEIGHT, TONNES, "$*1000000", "$/1000000")
543 DECLARE_CONV_UNIT(WEIGHT, TONS_UK, "$*1016046.9088", "$/1016046.9088")
544 DECLARE_CONV_UNIT(WEIGHT, TONS_US, "$*907184.74", "$/907184.74")
545 DECLARE_CONV_END
546 };
547
548 static const conv_category_t conv_table[] = {
549 DECLARE_CONV_CAT(ANGLE)
550 DECLARE_CONV_CAT(AREA)
551 DECLARE_CONV_CAT(CONSUMPTION)
552 DECLARE_CONV_CAT(CURRENCY)
553 DECLARE_CONV_CAT(ENERGY)
554 DECLARE_CONV_CAT(LENGTH)
555 DECLARE_CONV_CAT(POWER)
556 DECLARE_CONV_CAT(PRESSURE)
557 DECLARE_CONV_CAT(TIME)
558 DECLARE_CONV_CAT(TEMPERATURE)
559 DECLARE_CONV_CAT(VELOCITY)
560 DECLARE_CONV_CAT(VOLUME)
561 DECLARE_CONV_CAT(WEIGHT)
562 };
563
564 void ConvExecute(HWND hWnd)
565 {
566 DWORD c_cat = (DWORD)SendDlgItemMessage(hWnd, IDC_COMBO_CATEGORY, CB_GETCURSEL, 0, 0);
567 const conv_t *items = NULL;
568 DWORD from = SendDlgItemMessage(hWnd, IDC_COMBO_FROM, CB_GETCURSEL, 0, 0);
569 DWORD to = SendDlgItemMessage(hWnd, IDC_COMBO_TO, CB_GETCURSEL, 0, 0);
570 TCHAR txt_cb[128];
571 TCHAR txt[128];
572 const conv_t *item;
573
574 /* do nothing if the indexes point to the same unit */
575 if (from == to)
576 return;
577
578 /* Search correct category, since it can be sorted too */
579 SendDlgItemMessage(hWnd, IDC_COMBO_CATEGORY, CB_GETLBTEXT, c_cat, (LPARAM)txt_cb);
580 for (c_cat=0; c_cat < SIZEOF(conv_table); c_cat++) {
581 LoadString(calc.hInstance, conv_table[c_cat].category, txt, SIZEOF(txt));
582 if (!_tcscmp(txt_cb, txt)) {
583 items = conv_table[c_cat].items;
584 break;
585 }
586 }
587
588 /* The units can be sorted, so I must search the exact match */
589 item = items;
590 SendDlgItemMessage(hWnd, IDC_COMBO_FROM, CB_GETLBTEXT, from, (LPARAM)txt_cb);
591 while (item->unit) {
592 LoadString(calc.hInstance, item->unit, txt, SIZEOF(txt));
593 if (!_tcscmp(txt_cb, txt)) {
594 from = item-items;
595 break;
596 }
597 item++;
598 }
599 SendDlgItemMessage(hWnd, IDC_COMBO_TO, CB_GETLBTEXT, to, (LPARAM)txt_cb);
600 item = items;
601 while (item->unit) {
602 LoadString(calc.hInstance, item->unit, txt, SIZEOF(txt));
603 if (!_tcscmp(txt_cb, txt)) {
604 to = item-items;
605 break;
606 }
607 item++;
608 }
609
610 calc.Convert[0].data = (char *)items[from].formula_from;
611 calc.Convert[1].data = (char *)items[to].formula_to;
612 calc.Convert[0].wm_msg = WM_HANDLE_FROM;
613 calc.Convert[1].wm_msg = WM_HANDLE_TO;
614 PostMessage(hWnd, WM_START_CONV, 0, MAKELPARAM(0, WM_HANDLE_FROM));
615 }
616
617 void ConvAdjust(HWND hWnd, int n_cat)
618 {
619 TCHAR txt[128];
620 TCHAR txt_cat[128];
621 HWND hFromWnd = GetDlgItem(hWnd, IDC_COMBO_FROM);
622 HWND hToWnd = GetDlgItem(hWnd, IDC_COMBO_TO);
623 const conv_t *item;
624 unsigned int n;
625
626 SendDlgItemMessage(hWnd, IDC_COMBO_CATEGORY, CB_GETLBTEXT, n_cat, (LPARAM)txt_cat);
627 for (n=0; n<SIZEOF(conv_table); n++) {
628 item = conv_table[n].items;
629 LoadString(calc.hInstance, conv_table[n].category, txt, SIZEOF(txt));
630 if (!_tcscmp(txt_cat, txt))
631 break;
632 }
633
634 SendMessage(hFromWnd, CB_RESETCONTENT, 0, 0);
635 SendMessage(hToWnd, CB_RESETCONTENT, 0, 0);
636 while (item->unit) {
637 LoadString(calc.hInstance, item->unit, txt, SIZEOF(txt));
638 SendMessage(hFromWnd, CB_ADDSTRING, 0, (LPARAM)txt);
639 SendMessage(hToWnd, CB_ADDSTRING, 0, (LPARAM)txt);
640 item++;
641 }
642 SendMessage(hFromWnd, CB_SETCURSEL, 0, 0);
643 SendMessage(hToWnd, CB_SETCURSEL, 0, 0);
644 }
645
646 void ConvInit(HWND hWnd)
647 {
648 HWND hCatWnd = GetDlgItem(hWnd, IDC_COMBO_CATEGORY);
649 TCHAR txt[128];
650 unsigned int n;
651
652 /* Fill category combo */
653 for (n=0; n<SIZEOF(conv_table); n++) {
654 LoadString(calc.hInstance, conv_table[n].category, txt, SIZEOF(txt));
655 SendMessage(hCatWnd, CB_ADDSTRING, 0, (LPARAM)txt);
656 }
657 SendMessage(hCatWnd, CB_SETCURSEL, 0, 0);
658 ConvAdjust(hWnd, 0);
659 }
660