- Timestamp:
- Oct 12, 2005, 8:10:53 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpUnits.cc
r83 r102 671 671 int* result ) { 672 672 673 int valLen = 0;674 673 RpUnits* toUnits = NULL; 675 674 RpUnits* fromUnits = NULL; … … 678 677 std::string convVal = ""; 679 678 double numVal = 0; 680 int idx = 0;679 // int idx = 0; 681 680 int convResult = 0; 681 char* endptr = NULL; 682 std::stringstream outVal; 682 683 683 684 … … 698 699 } 699 700 700 valLen = val.length();701 702 701 // search our string to see where the numeric part stops 703 702 // and the units part starts 704 703 // 705 // switched from the code that starts at the beginning of the string706 // and check to see where the numeric part stops707 // to the uncommented code section below that starts at the end of the708 // string checks to see where the alpha part starts. This approach709 // seems to work better so we can parse and convert strings as follows:710 704 // convert("5J", "neV") => 3.12075e+28neV 711 705 // convert("3.12075e+28neV", "J") => 4.99999J 712 706 // now we can actually get the scientific notation portion of the string. 713 707 // 714 // make sure not to stop searching if we encounter '.', '-', '+' 715 /* 716 for (idx=0; idx < valLen; idx++) { 717 if ( !((val[idx] >= '0') && (val[idx] <= '9')) ) { 718 if ( (val[idx] != '.')&&(val[idx] != '-')&&(val[idx] != '+') ) { 719 break; 720 } 721 } 722 } 723 */ 724 725 // consider using stdtod because it does error checking 726 // supposedly according to man page? 727 for (idx=valLen-1; idx >= 0; idx--) { 728 if ( !( ((val[idx] >= 'A') && (val[idx] <= 'Z')) 729 || ((val[idx] >= 'a') && (val[idx] <= 'z')) ) ) { 730 731 if ( (val[idx] != '.')&&(val[idx] != '-')&&(val[idx] != '+') ) { 732 break; 733 } 734 } 735 } 736 737 idx++; 738 739 if ( (idx < valLen) && (idx > 0) ) { 740 tmpNumVal = val.substr(0,idx); 741 if (!tmpNumVal.empty()) { 742 numVal = atof(tmpNumVal.c_str()); 743 } 744 else { 745 // tmpNumVal was empty string? 746 // set error code here and return error 747 748 if (result) { 749 *result = 1; 750 } 751 // not a big fan of multiple returns, but... 752 return val; 753 } 754 fromUnitsName = val.substr(idx, valLen-idx); 755 } 756 else { 757 // return error, no units or no number in the val string? 708 709 numVal = strtod(val.c_str(),&endptr); 710 711 if ( (numVal == 0) && (endptr == val.c_str()) ) { 712 // no conversion was done. 713 // number in incorrect format probably. 758 714 if (result) { 759 715 *result = 1; 760 716 } 761 // not a big fan of multiple returns, but...762 717 return val; 718 } 719 720 fromUnitsName = std::string(endptr); 721 if ( fromUnitsName.empty() ) { 722 // there were no units in the input string 723 // assume fromUnitsName = toUnitsName 724 // return the correct value 725 if (result) { 726 *result = 0; 727 } 728 729 if (showUnits) { 730 outVal << val << toUnitsName; 731 } 732 else { 733 outVal << val; 734 } 735 736 return outVal.str(); 763 737 } 764 738 … … 778 752 if ( (result) && (*result == 0) ) { 779 753 *result = convResult; 780 754 } 781 755 782 756 return convVal;
Note: See TracChangeset
for help on using the changeset viewer.