Changeset 102 for trunk/src


Ignore:
Timestamp:
Oct 12, 2005, 8:10:53 AM (19 years ago)
Author:
dkearney
Message:

adjusted units conversion in RpUnits.cc, now uses strtod() instead of atof()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpUnits.cc

    r83 r102  
    671671                    int* result ) {
    672672
    673     int valLen = 0;
    674673    RpUnits* toUnits = NULL;
    675674    RpUnits* fromUnits = NULL;
     
    678677    std::string convVal = "";
    679678    double numVal = 0;
    680     int idx = 0;
     679    // int idx = 0;
    681680    int convResult = 0;
     681    char* endptr = NULL;
     682    std::stringstream outVal;
    682683
    683684
     
    698699    }
    699700
    700     valLen = val.length();
    701 
    702701    // search our string to see where the numeric part stops
    703702    // and the units part starts
    704703    //
    705     // switched from the code that starts at the beginning of the string
    706     // and check to see where the numeric part stops
    707     // to the uncommented code section below that starts at the end of the
    708     // string checks to see where the alpha part starts. This approach
    709     // seems to work better so we can parse and convert strings as follows:
    710704    //  convert("5J", "neV") => 3.12075e+28neV
    711705    //  convert("3.12075e+28neV", "J") => 4.99999J
    712706    // now we can actually get the scientific notation portion of the string.
    713707    //
    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.
    758714        if (result) {
    759715            *result = 1;
    760716        }
    761         // not a big fan of multiple returns, but...
    762717        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();
    763737    }
    764738
     
    778752    if ( (result) && (*result == 0) ) {
    779753        *result = convResult;
    780     }
     754   }
    781755
    782756    return convVal;
Note: See TracChangeset for help on using the changeset viewer.