1 | #ifndef _RpUNITS_H |
---|
2 | #include "RpUnits.h" |
---|
3 | #endif |
---|
4 | |
---|
5 | // dict pointer |
---|
6 | // RpDict<std::string,RpUnits*> RpUnits::dict = RpDict<std::string,RpUnits*>(); |
---|
7 | RpDict<std::string,RpUnits*>* RpUnits::dict = new RpDict<std::string,RpUnits*>(); |
---|
8 | |
---|
9 | |
---|
10 | /************************************************************************ |
---|
11 | * |
---|
12 | * add RpUnits Object |
---|
13 | * |
---|
14 | ************************************************************************/ |
---|
15 | |
---|
16 | RpUnits * RpUnits::define(const std::string units, RpUnits * basis) |
---|
17 | { |
---|
18 | RpUnits * newRpUnit = NULL; |
---|
19 | |
---|
20 | if (units == "") { |
---|
21 | // raise error, user sent null units! |
---|
22 | return NULL; |
---|
23 | } |
---|
24 | |
---|
25 | // check to see if the user is trying to trick me! |
---|
26 | if ( (basis) && (units == basis->getUnits()) ) { |
---|
27 | // dont trick me! |
---|
28 | return NULL; |
---|
29 | } |
---|
30 | |
---|
31 | // char * cmpPtr = NULL; |
---|
32 | double exp = 0.0; |
---|
33 | double oldExponent = 0; |
---|
34 | double newExponent = 0; |
---|
35 | int digiSearch = 0; // flag to show we are searching digits |
---|
36 | int alphaSearch = 0; // flag to show we are searching chars |
---|
37 | std::string dbText = ""; |
---|
38 | |
---|
39 | std::string cmpStr = ""; |
---|
40 | int cmpIndex = 0; |
---|
41 | |
---|
42 | std::string::size_type length = units.length(); |
---|
43 | int srchIndex = length; |
---|
44 | std::string srchStr = units; |
---|
45 | |
---|
46 | // unit* p = NULL; |
---|
47 | |
---|
48 | while ((srchStr.length() > 0)) { |
---|
49 | |
---|
50 | srchIndex--; |
---|
51 | |
---|
52 | if (srchIndex < 0) { |
---|
53 | break; |
---|
54 | } |
---|
55 | |
---|
56 | if ( isdigit(srchStr[srchIndex]) && !digiSearch && !alphaSearch) { |
---|
57 | digiSearch = 1; |
---|
58 | } |
---|
59 | else if(!isdigit(srchStr[srchIndex]) && digiSearch && !alphaSearch) { |
---|
60 | |
---|
61 | // convert our exponent to integer |
---|
62 | |
---|
63 | // check to see if there is a + or - sign |
---|
64 | if ( ( srchStr[srchIndex] == '+' ) |
---|
65 | || ( srchStr[srchIndex] == '-' ) ) { |
---|
66 | |
---|
67 | // evaluate a '+' or '-' sign with the value |
---|
68 | srchIndex--; |
---|
69 | } |
---|
70 | |
---|
71 | srchIndex++; |
---|
72 | |
---|
73 | exp = atoi(&srchStr[srchIndex]); |
---|
74 | |
---|
75 | // we are no longer in a digit search |
---|
76 | digiSearch = 0; |
---|
77 | |
---|
78 | // place null character where the number starts |
---|
79 | // so we know what we've already parsed |
---|
80 | |
---|
81 | srchStr.erase(srchIndex); |
---|
82 | length = srchStr.length(); |
---|
83 | |
---|
84 | /* |
---|
85 | // shortcut so we dont have to go through an additional while |
---|
86 | // loop iteration. |
---|
87 | if (isalpha(srchStr[srchIndex-1])) { |
---|
88 | dbText = "alphaSearch = 1"; |
---|
89 | dbprint(1,dbText); |
---|
90 | alphaSearch = 1; |
---|
91 | srchIndex--; |
---|
92 | } |
---|
93 | */ |
---|
94 | } |
---|
95 | else if( isalpha(srchStr[srchIndex]) && !digiSearch && !alphaSearch) { |
---|
96 | alphaSearch = 1; |
---|
97 | } |
---|
98 | else if(!isalpha(srchStr[srchIndex]) && !digiSearch && alphaSearch) { |
---|
99 | |
---|
100 | // adjust the exponent if none was provided |
---|
101 | if (exp == 0) { |
---|
102 | exp = 1; |
---|
103 | } |
---|
104 | |
---|
105 | // compare unit string to see if it is a recognized system |
---|
106 | |
---|
107 | |
---|
108 | std::string cmpStr = srchStr.substr(srchIndex+1,length-srchIndex-1); |
---|
109 | cmpIndex = 0; |
---|
110 | |
---|
111 | if ( (unsigned)(cmpIndex = pre_compare(cmpStr,basis)) == |
---|
112 | std::string::npos ) { |
---|
113 | alphaSearch = 0; |
---|
114 | |
---|
115 | // there are units we did not recognize, |
---|
116 | // right now we ignore them, |
---|
117 | // we may want to deal with them differntly in the future |
---|
118 | |
---|
119 | // srchStr.erase(srchIndex+1); |
---|
120 | // erase only the last character and reprocess the string |
---|
121 | // because our precompare doesnt take care of this yet. |
---|
122 | srchStr.erase(srchStr.length()-1); |
---|
123 | length = srchStr.length(); |
---|
124 | srchIndex = length; |
---|
125 | |
---|
126 | |
---|
127 | // continue parsing characters |
---|
128 | continue; |
---|
129 | } |
---|
130 | |
---|
131 | // the compare function was successful |
---|
132 | // move the search pointer to one value infront of |
---|
133 | // where the units were found. |
---|
134 | // |
---|
135 | // cmpIndex tells us how far ahead of the srchIndex the matching |
---|
136 | // unit was found. so we add srchIndex to get the real index. |
---|
137 | cmpIndex += srchIndex+1; |
---|
138 | srchIndex = cmpIndex; |
---|
139 | std::string newUnitText = srchStr.substr(cmpIndex,length-cmpIndex); |
---|
140 | |
---|
141 | // call the function to create the unit object |
---|
142 | |
---|
143 | // we need pre-compare to return the basis of what it found. |
---|
144 | // addUnit( newUnit, exp, basis) |
---|
145 | if (newRpUnit) { |
---|
146 | // newRpUnit->addUnit( newUnitText, exp, NULL); |
---|
147 | newRpUnit->addUnit( newUnitText, exp, basis); |
---|
148 | } |
---|
149 | else { |
---|
150 | // newRpUnit= new RpUnits(newUnitText, exp, NULL); |
---|
151 | newRpUnit= new RpUnits(newUnitText, exp, basis); |
---|
152 | } |
---|
153 | |
---|
154 | // sprintf(dbText,"creating unit: %s,%d\n\0", newUnit.c_str(),exp); |
---|
155 | |
---|
156 | // place a null character at the end of the string |
---|
157 | // so we know what we've parsed so far. |
---|
158 | |
---|
159 | srchStr.erase(srchIndex); |
---|
160 | length = srchStr.length(); |
---|
161 | |
---|
162 | // fix our searching flag |
---|
163 | alphaSearch = 0; |
---|
164 | |
---|
165 | // shortcut so we dont have to go through an additional while |
---|
166 | // loop iteration. |
---|
167 | // this doesnt work out so well, maybe need to reconsider |
---|
168 | // if (isdigit(*(search--))) { |
---|
169 | // dbprint(1,"digiSearch = 1"); |
---|
170 | // digiSearch = 1; |
---|
171 | // } |
---|
172 | } |
---|
173 | // else if ( *search == '/' ) { |
---|
174 | else if( srchStr[srchIndex] == '/' ) { |
---|
175 | // have to go back to all of the objects created and |
---|
176 | // multiply their exponents by -1. |
---|
177 | |
---|
178 | if (newRpUnit) { |
---|
179 | unit* p = newRpUnit->head; |
---|
180 | while (p) { |
---|
181 | oldExponent = p->getExponent(); |
---|
182 | newExponent = oldExponent*-1; |
---|
183 | p->newExponent(newExponent); |
---|
184 | p = p->next; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | // place a null character at the end of the string |
---|
189 | // so we know what we've parsed so far. |
---|
190 | |
---|
191 | /* |
---|
192 | end = (search); |
---|
193 | *end = '\0'; |
---|
194 | */ |
---|
195 | |
---|
196 | // srchStr.erase(srchIndex+1); |
---|
197 | srchStr.erase(srchIndex); |
---|
198 | length = srchStr.length(); |
---|
199 | |
---|
200 | } |
---|
201 | else { |
---|
202 | continue; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | } // end while loop |
---|
207 | |
---|
208 | |
---|
209 | // complete the last iteration |
---|
210 | if (srchIndex < 0) { |
---|
211 | |
---|
212 | |
---|
213 | if (digiSearch) { |
---|
214 | // convert whatever is left |
---|
215 | exp = atoi(&srchStr[srchIndex+1]); |
---|
216 | |
---|
217 | // if we get here, that means units name starts with a digit |
---|
218 | // normally we wont get here, but if we do, should we place |
---|
219 | // the unit into the dictionary? i think not since digits are |
---|
220 | // always considered exponents. |
---|
221 | } |
---|
222 | else if (alphaSearch) { |
---|
223 | // adjust the exponent if none was provided |
---|
224 | if (exp == 0) { |
---|
225 | exp = 1; |
---|
226 | } |
---|
227 | |
---|
228 | // compare unit string to see if it is a recognized system |
---|
229 | |
---|
230 | std::string cmpStr = srchStr.substr(srchIndex+1,length-srchIndex-1); |
---|
231 | cmpIndex = 0; |
---|
232 | |
---|
233 | if ( (cmpIndex = pre_compare(cmpStr,basis)) < 0 ) { |
---|
234 | // no matches in the compare function |
---|
235 | |
---|
236 | // there are units we did not recognize, |
---|
237 | // right now we ignore them, |
---|
238 | // we may want to deal with them differntly in the future |
---|
239 | // ignoring unit |
---|
240 | |
---|
241 | // break out of creating the last unit. |
---|
242 | |
---|
243 | // create a new unit with basis of null to show its a |
---|
244 | // fundamental type |
---|
245 | // newRpUnit = new RpUnits(cmpStr, exp, NULL); |
---|
246 | newRpUnit = new RpUnits(cmpStr, exp, basis); |
---|
247 | |
---|
248 | // put the unit into the dictionary |
---|
249 | // newRpUnit->insert(cmpStr); |
---|
250 | // |
---|
251 | newRpUnit->insert(newRpUnit->getUnitsName()); |
---|
252 | |
---|
253 | // RpUnits::dict.set(cmpStr,newRpUnit); |
---|
254 | |
---|
255 | } |
---|
256 | else { |
---|
257 | |
---|
258 | // the compare function was successful |
---|
259 | // move the search pointer to one value infront of |
---|
260 | // where the units were found. |
---|
261 | // adjusting the search pointer to point to the units |
---|
262 | std::string newUnitText = srchStr.substr(cmpIndex,length-cmpIndex); |
---|
263 | |
---|
264 | // call the function to create the unit object |
---|
265 | |
---|
266 | // we need pre-compare to return the basis of what it found. |
---|
267 | // addUnit( newUnit, exp, basis) |
---|
268 | if (newRpUnit) { |
---|
269 | // newRpUnit->addUnit( newUnitText, exp, NULL ); |
---|
270 | newRpUnit->addUnit( newUnitText, exp, basis ); |
---|
271 | } |
---|
272 | else { |
---|
273 | // newRpUnit = new RpUnits(newUnitText, exp, NULL); |
---|
274 | newRpUnit = new RpUnits(newUnitText, exp, basis); |
---|
275 | } |
---|
276 | |
---|
277 | // creating unit |
---|
278 | // |
---|
279 | // putting unit into dictionary |
---|
280 | newRpUnit->insert(newRpUnit->getUnitsName()); |
---|
281 | } |
---|
282 | |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | // place the new object into the dictionary |
---|
287 | |
---|
288 | // return a copy of the new object to user |
---|
289 | return newRpUnit; |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | /************************************************************************ |
---|
294 | * |
---|
295 | * add relation rule |
---|
296 | * |
---|
297 | ************************************************************************/ |
---|
298 | RpUnits * RpUnits::define( RpUnits* from, |
---|
299 | RpUnits* to, |
---|
300 | double (*convForwFxnPtr)(double), |
---|
301 | double (*convBackFxnPtr)(double)) |
---|
302 | { |
---|
303 | RpUnits* conv = new RpUnits(from,to,convForwFxnPtr,convBackFxnPtr,NULL,NULL); |
---|
304 | |
---|
305 | return conv; |
---|
306 | } |
---|
307 | |
---|
308 | RpUnits * RpUnits::define( RpUnits* from, |
---|
309 | RpUnits* to, |
---|
310 | void* (*convForwFxnPtr)(void*, void*), |
---|
311 | void* convForwData, |
---|
312 | void* (*convBackFxnPtr)(void*, void*), |
---|
313 | void* convBackData) |
---|
314 | { |
---|
315 | RpUnits* conv = new RpUnits( from, |
---|
316 | to, |
---|
317 | convForwFxnPtr, |
---|
318 | convForwData, |
---|
319 | convBackFxnPtr, |
---|
320 | convBackData, |
---|
321 | NULL, |
---|
322 | NULL); |
---|
323 | |
---|
324 | return conv; |
---|
325 | } |
---|
326 | |
---|
327 | /************************************************************************ |
---|
328 | * |
---|
329 | * report the units this object represents back to the user |
---|
330 | * |
---|
331 | * **********************************************************************/ |
---|
332 | std::string RpUnits::getUnits() |
---|
333 | { |
---|
334 | // return units; |
---|
335 | |
---|
336 | std::stringstream unitText; |
---|
337 | unit* p = head; |
---|
338 | |
---|
339 | while (p) { |
---|
340 | // unitText << p->getUnits() << p->getExponent(); |
---|
341 | unitText << p->getUnits() ; |
---|
342 | p = p->next; |
---|
343 | } |
---|
344 | |
---|
345 | return (unitText.str()); |
---|
346 | } |
---|
347 | |
---|
348 | /************************************************************************ |
---|
349 | * |
---|
350 | * report the units this object represents back to the user |
---|
351 | * |
---|
352 | * **********************************************************************/ |
---|
353 | std::string RpUnits::getUnitsName() |
---|
354 | { |
---|
355 | // return units; |
---|
356 | |
---|
357 | std::stringstream unitText; |
---|
358 | unit* p = head; |
---|
359 | double exponent; |
---|
360 | |
---|
361 | while (p) { |
---|
362 | |
---|
363 | exponent = p->getExponent(); |
---|
364 | |
---|
365 | if (exponent == 1) { |
---|
366 | unitText << p->getUnits(); |
---|
367 | } |
---|
368 | else { |
---|
369 | unitText << p->getUnits() << p->getExponent(); |
---|
370 | } |
---|
371 | |
---|
372 | p = p->next; |
---|
373 | } |
---|
374 | |
---|
375 | return (unitText.str()); |
---|
376 | } |
---|
377 | |
---|
378 | /************************************************************************ |
---|
379 | * |
---|
380 | * report the exponent of the units of this object back to the user |
---|
381 | * |
---|
382 | * **********************************************************************/ |
---|
383 | double RpUnits::getExponent() |
---|
384 | { |
---|
385 | // return exponent; |
---|
386 | |
---|
387 | return head->getExponent(); |
---|
388 | |
---|
389 | return 0; |
---|
390 | } |
---|
391 | |
---|
392 | /************************************************************************ |
---|
393 | * |
---|
394 | * report the basis of this object to the user |
---|
395 | * |
---|
396 | * **********************************************************************/ |
---|
397 | RpUnits * RpUnits::getBasis() |
---|
398 | { |
---|
399 | // return basis; |
---|
400 | |
---|
401 | // check if head exists? |
---|
402 | |
---|
403 | return head->getBasis(); |
---|
404 | } |
---|
405 | |
---|
406 | /************************************************************************ |
---|
407 | * |
---|
408 | * convert the current unit to its basis units |
---|
409 | * |
---|
410 | * Return Codes |
---|
411 | * 0) no error (could also mean or no prefix was found) |
---|
412 | * in some cases, this means the value is in its basis format |
---|
413 | * 1) the prefix found does not have a built in factor associated. |
---|
414 | * |
---|
415 | ************************************************************************/ |
---|
416 | double RpUnits::makeBasis(double value, int* result) |
---|
417 | { |
---|
418 | |
---|
419 | RpUnits* basis = getBasis(); |
---|
420 | double retVal = value; |
---|
421 | |
---|
422 | if (result) { |
---|
423 | *result = 0; |
---|
424 | } |
---|
425 | |
---|
426 | if (basis == NULL) { |
---|
427 | // this unit is a basis |
---|
428 | // do nothing |
---|
429 | |
---|
430 | if (result) { |
---|
431 | *result = 1; |
---|
432 | } |
---|
433 | } |
---|
434 | else { |
---|
435 | retVal = convert(basis,value,result); |
---|
436 | } |
---|
437 | |
---|
438 | return retVal; |
---|
439 | } |
---|
440 | |
---|
441 | RpUnits& RpUnits::makeBasis(double* value, int* result) |
---|
442 | { |
---|
443 | RpUnits* basis = getBasis(); |
---|
444 | double retVal = *value; |
---|
445 | int convResult = 0; |
---|
446 | |
---|
447 | if (basis == NULL) { |
---|
448 | // this unit is a basis |
---|
449 | // do nothing |
---|
450 | |
---|
451 | if (result) { |
---|
452 | *result = 1; |
---|
453 | } |
---|
454 | } |
---|
455 | else { |
---|
456 | retVal = convert(basis,retVal,&convResult); |
---|
457 | } |
---|
458 | |
---|
459 | if ( (convResult == 1) ) { |
---|
460 | *value = retVal; |
---|
461 | } |
---|
462 | |
---|
463 | if (result) { |
---|
464 | *result = convResult; |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | |
---|
469 | return *this; |
---|
470 | } |
---|
471 | |
---|
472 | /************************************************************************ |
---|
473 | * |
---|
474 | * static int makeMetric(RpUnits * basis); |
---|
475 | * create the metric attachments for the given basis. |
---|
476 | * should only be used if this unit is of metric type |
---|
477 | * |
---|
478 | * **********************************************************************/ |
---|
479 | //static int RpUnits::makeMetric(RpUnits * basis) { |
---|
480 | int RpUnits::makeMetric(RpUnits * basis) { |
---|
481 | |
---|
482 | if (!basis) { |
---|
483 | return 0; |
---|
484 | } |
---|
485 | |
---|
486 | std::string basisName = basis->getUnits(); |
---|
487 | std::string name; |
---|
488 | std::string forw, back; |
---|
489 | |
---|
490 | name = "c" + basisName; |
---|
491 | RpUnits * centi = RpUnits::define(name, basis); |
---|
492 | RpUnits::define(centi, basis, centi2base, base2centi); |
---|
493 | |
---|
494 | name = "m" + basisName; |
---|
495 | RpUnits * milli = RpUnits::define(name, basis); |
---|
496 | RpUnits::define(milli, basis, milli2base, base2milli); |
---|
497 | |
---|
498 | name = "u" + basisName; |
---|
499 | RpUnits * micro = RpUnits::define(name, basis); |
---|
500 | RpUnits::define(micro, basis, micro2base, base2micro); |
---|
501 | |
---|
502 | name = "n" + basisName; |
---|
503 | RpUnits * nano = RpUnits::define(name, basis); |
---|
504 | RpUnits::define(nano, basis, nano2base, base2nano); |
---|
505 | |
---|
506 | name = "p" + basisName; |
---|
507 | RpUnits * pico = RpUnits::define(name, basis); |
---|
508 | RpUnits::define(pico, basis, pico2base, base2pico); |
---|
509 | |
---|
510 | name = "f" + basisName; |
---|
511 | RpUnits * femto = RpUnits::define(name, basis); |
---|
512 | RpUnits::define(femto, basis, femto2base, base2femto); |
---|
513 | |
---|
514 | name = "a" + basisName; |
---|
515 | RpUnits * atto = RpUnits::define(name, basis); |
---|
516 | RpUnits::define(atto, basis, atto2base, base2atto); |
---|
517 | |
---|
518 | name = "k" + basisName; |
---|
519 | RpUnits * kilo = RpUnits::define(name, basis); |
---|
520 | RpUnits::define(kilo, basis, kilo2base, base2kilo); |
---|
521 | |
---|
522 | name = "M" + basisName; |
---|
523 | RpUnits * mega = RpUnits::define(name, basis); |
---|
524 | RpUnits::define(mega, basis, mega2base, base2mega); |
---|
525 | |
---|
526 | name = "G" + basisName; |
---|
527 | RpUnits * giga = RpUnits::define(name, basis); |
---|
528 | RpUnits::define(giga, basis, giga2base, base2giga); |
---|
529 | |
---|
530 | name = "T" + basisName; |
---|
531 | RpUnits * tera = RpUnits::define(name, basis); |
---|
532 | RpUnits::define(tera, basis, tera2base, base2tera); |
---|
533 | |
---|
534 | name = "P" + basisName; |
---|
535 | RpUnits * peta = RpUnits::define(name, basis); |
---|
536 | RpUnits::define(peta, basis, peta2base, base2peta); |
---|
537 | |
---|
538 | // |
---|
539 | return (1); |
---|
540 | } |
---|
541 | |
---|
542 | |
---|
543 | std::string RpUnits::convert ( RpUnits* toUnits, |
---|
544 | double val, |
---|
545 | int showUnits, |
---|
546 | int* result ) |
---|
547 | { |
---|
548 | double retVal = convert(toUnits,val,result); |
---|
549 | std::stringstream unitText; |
---|
550 | |
---|
551 | |
---|
552 | if (showUnits) { |
---|
553 | unitText << retVal << toUnits->getUnits(); |
---|
554 | } |
---|
555 | else { |
---|
556 | unitText << retVal; |
---|
557 | } |
---|
558 | |
---|
559 | return (unitText.str()); |
---|
560 | |
---|
561 | } |
---|
562 | |
---|
563 | // user function to convert a value to the provided RpUnits* toUnits |
---|
564 | // if it exists as a conversion from the basis |
---|
565 | // example |
---|
566 | // cm.convert(meter,10) |
---|
567 | // cm.convert(angstrum,100) |
---|
568 | double RpUnits::convert(RpUnits* toUnit, double val, int* result) |
---|
569 | { |
---|
570 | |
---|
571 | // currently we convert this object to its basis and look for the |
---|
572 | // connection ot the toUnit object from the basis. |
---|
573 | |
---|
574 | double value = val; |
---|
575 | RpUnits* basis = this->getBasis(); |
---|
576 | RpUnits* fromUnit = this; |
---|
577 | RpUnits* dictToUnit = NULL; |
---|
578 | convEntry *p; |
---|
579 | int my_result = 0; |
---|
580 | |
---|
581 | // set *result to a default value |
---|
582 | if (result) { |
---|
583 | *result = 0; |
---|
584 | } |
---|
585 | |
---|
586 | // guard against converting to the units you are converting from... |
---|
587 | // ie. meters->meters |
---|
588 | if (this->getUnits() == toUnit->getUnits()) { |
---|
589 | if (result) { |
---|
590 | *result = 1; |
---|
591 | } |
---|
592 | return val; |
---|
593 | } |
---|
594 | |
---|
595 | // convert unit to the basis |
---|
596 | // makeBasis(&value); |
---|
597 | // trying to avoid the recursive way of converting to the basis. |
---|
598 | // need to rethink this. |
---|
599 | // |
---|
600 | if ( (basis) && (basis->getUnits() != toUnit->getUnits()) ) { |
---|
601 | value = convert(basis,value,&my_result); |
---|
602 | if (my_result) { |
---|
603 | fromUnit = basis; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | // find the toUnit in our dictionary. |
---|
608 | |
---|
609 | dictToUnit = find(toUnit->getUnits()); |
---|
610 | |
---|
611 | // did we find the unit in the dictionary? |
---|
612 | if (dictToUnit == NULL) { |
---|
613 | // toUnit was not found in the dictionary |
---|
614 | return val; |
---|
615 | } |
---|
616 | |
---|
617 | // search through the conversion list to find |
---|
618 | // the conversion to the toUnit. |
---|
619 | |
---|
620 | if (basis) { |
---|
621 | p = basis->convList; |
---|
622 | } |
---|
623 | else { |
---|
624 | p = this->convList; |
---|
625 | } |
---|
626 | |
---|
627 | if (p == NULL) { |
---|
628 | // there are no conversions |
---|
629 | return val; |
---|
630 | } |
---|
631 | |
---|
632 | // loop through our conversion list looking for the correct conversion |
---|
633 | do { |
---|
634 | |
---|
635 | if ( (p->conv->toPtr == dictToUnit) && (p->conv->fromPtr == fromUnit) ) { |
---|
636 | // we found our conversion |
---|
637 | // call the function pointer with value |
---|
638 | |
---|
639 | value = p->conv->convForwFxnPtr(value); |
---|
640 | if (result) { |
---|
641 | *result = 1; |
---|
642 | } |
---|
643 | break; |
---|
644 | } |
---|
645 | |
---|
646 | if ( (p->conv->toPtr == fromUnit) && (p->conv->fromPtr == dictToUnit) ) { |
---|
647 | // we found our conversion |
---|
648 | // call the function pointer with value |
---|
649 | |
---|
650 | value = p->conv->convBackFxnPtr(value); |
---|
651 | if (result) { |
---|
652 | *result = 1; |
---|
653 | } |
---|
654 | break; |
---|
655 | } |
---|
656 | |
---|
657 | p = p->next; |
---|
658 | |
---|
659 | } while (p != NULL); |
---|
660 | |
---|
661 | |
---|
662 | if ( p == NULL) { |
---|
663 | // we did not find the conversion |
---|
664 | return val; |
---|
665 | } |
---|
666 | |
---|
667 | // we found the conversion. |
---|
668 | // return the converted value. |
---|
669 | return value; |
---|
670 | |
---|
671 | } |
---|
672 | |
---|
673 | |
---|
674 | void* RpUnits::convert(RpUnits* toUnit, void* val, int* result) |
---|
675 | { |
---|
676 | |
---|
677 | // currently we convert this object to its basis and look for the |
---|
678 | // connection ot the toUnit object from the basis. |
---|
679 | |
---|
680 | void* value = val; |
---|
681 | RpUnits* basis = this->getBasis(); |
---|
682 | RpUnits* fromUnit = this; |
---|
683 | RpUnits* dictToUnit = NULL; |
---|
684 | convEntry *p; |
---|
685 | int my_result = 0; |
---|
686 | |
---|
687 | // set *result to a default value |
---|
688 | if (result) { |
---|
689 | *result = 0; |
---|
690 | } |
---|
691 | |
---|
692 | // guard against converting to the units you are converting from... |
---|
693 | // ie. meters->meters |
---|
694 | if (this->getUnits() == toUnit->getUnits()) { |
---|
695 | if (result) { |
---|
696 | *result = 1; |
---|
697 | } |
---|
698 | return val; |
---|
699 | } |
---|
700 | |
---|
701 | // convert unit to the basis |
---|
702 | // makeBasis(&value); |
---|
703 | // trying to avoid the recursive way of converting to the basis. |
---|
704 | // need to rethink this. |
---|
705 | // |
---|
706 | if ( (basis) && (basis->getUnits() != toUnit->getUnits()) ) { |
---|
707 | value = convert(basis,value,&my_result); |
---|
708 | if (my_result) { |
---|
709 | fromUnit = basis; |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | // find the toUnit in our dictionary. |
---|
714 | |
---|
715 | dictToUnit = find(toUnit->getUnits()); |
---|
716 | |
---|
717 | // did we find the unit in the dictionary? |
---|
718 | if (dictToUnit == NULL) { |
---|
719 | // toUnit was not found in the dictionary |
---|
720 | return val; |
---|
721 | } |
---|
722 | |
---|
723 | // search through the conversion list to find |
---|
724 | // the conversion to the toUnit. |
---|
725 | |
---|
726 | if (basis) { |
---|
727 | p = basis->convList; |
---|
728 | } |
---|
729 | else { |
---|
730 | p = this->convList; |
---|
731 | } |
---|
732 | |
---|
733 | if (p == NULL) { |
---|
734 | // there are no conversions |
---|
735 | return val; |
---|
736 | } |
---|
737 | |
---|
738 | // loop through our conversion list looking for the correct conversion |
---|
739 | do { |
---|
740 | |
---|
741 | if ( (p->conv->toPtr == dictToUnit) && (p->conv->fromPtr == fromUnit) ) { |
---|
742 | // we found our conversion |
---|
743 | // call the function pointer with value |
---|
744 | |
---|
745 | value = p->conv->convForwFxnPtrVoid(p->conv->convForwData,value); |
---|
746 | |
---|
747 | if (result) { |
---|
748 | *result = 1; |
---|
749 | } |
---|
750 | break; |
---|
751 | } |
---|
752 | |
---|
753 | if ( (p->conv->toPtr == fromUnit) && (p->conv->fromPtr == dictToUnit) ) { |
---|
754 | // we found our conversion |
---|
755 | // call the function pointer with value |
---|
756 | |
---|
757 | value = p->conv->convBackFxnPtrVoid(p->conv->convBackData,value); |
---|
758 | if (result) { |
---|
759 | *result = 1; |
---|
760 | } |
---|
761 | break; |
---|
762 | } |
---|
763 | |
---|
764 | p = p->next; |
---|
765 | |
---|
766 | } while (p != NULL); |
---|
767 | |
---|
768 | |
---|
769 | if ( p == NULL) { |
---|
770 | // we did not find the conversion |
---|
771 | return val; |
---|
772 | } |
---|
773 | |
---|
774 | // we found the conversion. |
---|
775 | // return the converted value. |
---|
776 | return value; |
---|
777 | |
---|
778 | } |
---|
779 | |
---|
780 | void RpUnits::addUnit( const std::string& units, |
---|
781 | double& exponent, |
---|
782 | RpUnits* basis |
---|
783 | ) |
---|
784 | { |
---|
785 | unit* p = NULL; |
---|
786 | |
---|
787 | // check if the list was created previously. if not, start the list |
---|
788 | if (head == 0) { |
---|
789 | head = new unit(units,exponent,basis,NULL); |
---|
790 | return; |
---|
791 | } |
---|
792 | |
---|
793 | // now add a new node at the beginning of the list: |
---|
794 | p = new unit(units,exponent,basis,head); |
---|
795 | head->prev = p; |
---|
796 | head = p; |
---|
797 | |
---|
798 | } |
---|
799 | |
---|
800 | int RpUnits::insert(std::string key) |
---|
801 | { |
---|
802 | int newRecord = 0; |
---|
803 | RpUnits* val = this; |
---|
804 | // dict pointer |
---|
805 | // RpUnits::dict.set(key,val,&newRecord); |
---|
806 | RpUnits::dict->set(key,val,&newRecord); |
---|
807 | // std::cout << "key = :" << key << ":" << std::endl; |
---|
808 | return newRecord; |
---|
809 | } |
---|
810 | |
---|
811 | |
---|
812 | int RpUnits::pre_compare( std::string& units, RpUnits* basis ) |
---|
813 | { |
---|
814 | |
---|
815 | // compare the incomming units with the previously defined units. |
---|
816 | // compareStr will hold a copy of the incomming string. |
---|
817 | // first look for the units as they are listed in the incomming variable |
---|
818 | // next look move searchStr toward the end of the string, |
---|
819 | // each time the pointer is moved, searchStr should be compared to all of |
---|
820 | // the previously defined units. |
---|
821 | // if searchStr is at the end of the string, then searchStr will be moved |
---|
822 | // back to the beginning of the string. |
---|
823 | // next it will traverse the string again, changing the case of the char |
---|
824 | // it points to and the resultant string will be compared again to all |
---|
825 | // of the previously defined units. |
---|
826 | |
---|
827 | int compareSuccess = 0; |
---|
828 | // std::string::size_type units_len = units.length(); |
---|
829 | // char * retVal = NULL; |
---|
830 | int retIndex = std::string::npos; |
---|
831 | // std::string compareStr = units; |
---|
832 | std::string searchStr = units; |
---|
833 | std::string dbText = ""; |
---|
834 | |
---|
835 | // pass 1: look for exact match of units as they came into the function |
---|
836 | // move searchStr pointer through string to find match. |
---|
837 | while ( ! compareSuccess && |
---|
838 | (searchStr.length() > 0) ) { |
---|
839 | |
---|
840 | // std::cout << "units = :" << units << ":" << std::endl; |
---|
841 | // std::cout << "searchStr = :" << searchStr << ":" << std::endl; |
---|
842 | |
---|
843 | // if (!compare(searchStr)) { |
---|
844 | // dict pointer |
---|
845 | // if (dict.find(searchStr) == dict.getNullEntry()) { |
---|
846 | if (dict->find(searchStr) == dict->getNullEntry()) { |
---|
847 | // the string was not found, |
---|
848 | // keep incrementing the pointer |
---|
849 | // searchStr (pass1) does not match"; |
---|
850 | } |
---|
851 | else { |
---|
852 | // compare was successful, |
---|
853 | // searchStr (pass1) found a match |
---|
854 | compareSuccess++; |
---|
855 | break; |
---|
856 | // is it possible to create the unit here and |
---|
857 | // keep looking for units fro mthe provided string? |
---|
858 | } |
---|
859 | |
---|
860 | searchStr.erase(0,1); |
---|
861 | |
---|
862 | if (basis) { |
---|
863 | if ( (searchStr == basis->getUnits()) && |
---|
864 | (searchStr.length() == basis->getUnits().length()) ) |
---|
865 | { |
---|
866 | break; |
---|
867 | } |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | if (compareSuccess == 0) { |
---|
873 | // refresh our searchStr var. |
---|
874 | searchStr = units; |
---|
875 | } |
---|
876 | |
---|
877 | // pass 2: capitolize the first letter of the search string and compare |
---|
878 | // for each letter in the string |
---|
879 | while ( ! compareSuccess && |
---|
880 | (searchStr.length() > 0) ) { |
---|
881 | |
---|
882 | if (islower((int)(searchStr[0]))) { |
---|
883 | searchStr[0] = (char) toupper((int)(searchStr[0])); |
---|
884 | } |
---|
885 | |
---|
886 | // if (!compare(searchStr)) { |
---|
887 | // dict pointer |
---|
888 | // if (dict.find(searchStr) == dict.getNullEntry()) { |
---|
889 | if (dict->find(searchStr) == dict->getNullEntry()) { |
---|
890 | // the string was not found, |
---|
891 | // keep incrementing the pointer |
---|
892 | // searchStr (pass2) does not match |
---|
893 | } |
---|
894 | else { |
---|
895 | // compare was successful, |
---|
896 | // searchStr (pass2) found a match |
---|
897 | compareSuccess++; |
---|
898 | break; |
---|
899 | } |
---|
900 | searchStr.erase(0,1); |
---|
901 | |
---|
902 | // check to see if we are at the basis. |
---|
903 | if (basis) { |
---|
904 | if ( (searchStr == basis->getUnits()) && |
---|
905 | (searchStr.length() == basis->getUnits().length()) ) |
---|
906 | { |
---|
907 | break; |
---|
908 | } |
---|
909 | |
---|
910 | } |
---|
911 | |
---|
912 | } |
---|
913 | |
---|
914 | |
---|
915 | |
---|
916 | // if we found a match, find the first occurance of the string which |
---|
917 | // was used to get the match, in our original units string. |
---|
918 | // this gets dicey for capitolizations. |
---|
919 | if ( compareSuccess ) { |
---|
920 | // need to think about if we want to search from the start |
---|
921 | // or the end of the string (find or rfind) |
---|
922 | // i think its the start because in this fxn (above) |
---|
923 | // we start at the beginning of the string and work |
---|
924 | // our way to the end. so if there is a second match at the |
---|
925 | // end, we would have only seen the first match. |
---|
926 | retIndex = units.find(searchStr); |
---|
927 | } |
---|
928 | |
---|
929 | return retIndex; |
---|
930 | |
---|
931 | /* |
---|
932 | if (retIndex) { |
---|
933 | // if we found a match return a |
---|
934 | // pointer to where the matching units |
---|
935 | // string can be found. |
---|
936 | dbText = "pre_compare returning an index"; |
---|
937 | dbprint(1, dbText); |
---|
938 | return retIndex; |
---|
939 | } |
---|
940 | else { |
---|
941 | // if there was no match, return a negative value. |
---|
942 | dbText = "pre_compare returning -1"; |
---|
943 | dbprint(1, dbText); |
---|
944 | return -1; |
---|
945 | } |
---|
946 | */ |
---|
947 | } |
---|
948 | |
---|
949 | |
---|
950 | void RpUnits::connectConversion(RpUnits* myRpUnit) |
---|
951 | { |
---|
952 | |
---|
953 | /* |
---|
954 | int index = 0; |
---|
955 | |
---|
956 | while ( (myRpUnit->staticConvArr[index] != NULL) |
---|
957 | && (index < myRpUnit->numBuckets) ){ |
---|
958 | |
---|
959 | index++; |
---|
960 | } |
---|
961 | |
---|
962 | if ( (myRpUnit->staticConvArr[index] == NULL) |
---|
963 | && (index < myRpUnit->numBuckets) ) { |
---|
964 | |
---|
965 | myRpUnit->staticConvArr[index] = this->conv; |
---|
966 | } |
---|
967 | */ |
---|
968 | |
---|
969 | convEntry* p = myRpUnit->convList; |
---|
970 | |
---|
971 | if (p == NULL) { |
---|
972 | myRpUnit->convList = new convEntry (this->conv,NULL,NULL); |
---|
973 | } |
---|
974 | else { |
---|
975 | while (p->next != NULL) { |
---|
976 | p = p->next; |
---|
977 | } |
---|
978 | |
---|
979 | p->next = new convEntry (this->conv,p,NULL); |
---|
980 | } |
---|
981 | |
---|
982 | } |
---|
983 | |
---|
984 | |
---|
985 | // -------------------------------------------------------------------- // |
---|
986 | |
---|