Changeset 538


Ignore:
Timestamp:
Oct 25, 2006 9:57:33 AM (18 years ago)
Author:
dkearney
Message:

fixes to rappture units and new tests. this should fix the segfaults
and unclear error messages generated in earlier versions of RpUnits.
the error messages are now mostly generated in RpUnits.cc but a few
preliminary checks are generated in RpUnitsTclInterface.cc.

File:
1 edited

Legend:

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

    r536 r538  
    11181118    std::string convVal = "";
    11191119    std::string type = "";     // junk var used because units2list requires it
     1120    std::string retStr = "";
    11201121    double origNumVal = 0;
    11211122    double numVal = 0;
    11221123    double toExp = 0;
    11231124    double fromExp = 0;
    1124     int convResult = 0;
     1125    int convErr = 0;
    11251126    char* endptr = NULL;
    11261127    std::stringstream outVal;
     
    12001201    }
    12011202
    1202     RpUnits::units2list(fromUnitsName,fromUnitsList,type);
    1203     RpUnits::units2list(toUnitsName,toUnitsList,type);
     1203    convErr = RpUnits::units2list(fromUnitsName,fromUnitsList,type);
     1204    if (convErr) {
     1205        if (result) {
     1206            *result = convErr;
     1207        }
     1208        retStr = "unrecognized units: \"" + fromUnitsName + "\"";
     1209        return retStr;
     1210    }
     1211
     1212    convErr = RpUnits::units2list(toUnitsName,toUnitsList,type);
     1213    if (convErr) {
     1214        if (result) {
     1215            *result = convErr;
     1216        }
     1217        retStr = "unrecognized units: \"" + toUnitsName + "\"";
     1218        return retStr;
     1219    }
    12041220
    12051221    fromIter = fromUnitsList.begin();
    12061222    toIter = toUnitsList.begin();
    12071223
    1208     while ( toIter != toUnitsList.end() ) {
     1224    while ( (toIter != toUnitsList.end()) && (fromIter != fromUnitsList.end()) && (!convErr) ) {
    12091225        fromUnits = fromIter->getUnitsObj();
    12101226        toUnits = toIter->getUnitsObj();
    12111227
    12121228        cList.clear();
    1213         convResult = fromUnits->getConvertFxnList(toUnits, cList);
    1214 
    1215         if (convResult == 0) {
     1229        convErr = fromUnits->getConvertFxnList(toUnits, cList);
     1230
     1231        if (convErr == 0) {
    12161232
    12171233            toExp = toIter->getExponent();
     
    12351251                // currently we cannot handle conversions of
    12361252                // units where the exponents are different
    1237                 convResult++;
    1238             }
    1239 
    1240         }
    1241 
    1242         if (convResult == 0) {
     1253                convErr++;
     1254            }
     1255
     1256        }
     1257
     1258        if (convErr == 0) {
    12431259            // successful conversion reported
    12441260            // remove the elements from the lists
     
    12551271            fromIter++;
    12561272            if (fromIter == fromUnitsList.end()) {
     1273
    12571274                fromIter = fromUnitsList.begin();
    1258                 // raise error that there was an
    1259                 // unrecognized conversion request
    1260                 tempIter = toIter;
    12611275                toIter++;
    1262                 toUnitsList.erase(tempIter);
    1263             }
    1264         }
    1265 
    1266     }
    1267 
    1268     if (fromIter != fromUnitsList.end()) {
    1269         // raise error that there was an
    1270         // unrecognized conversion request
    1271     }
    1272 
    1273 
    1274     if (convResult == 0) {
    1275         convResult = applyConversion (&numVal, totalConvList);
    1276     }
    1277 
    1278 
     1276
     1277                if (toIter == toUnitsList.end())  {
     1278
     1279                    toIter = toUnitsList.begin();
     1280
     1281                    // raise error that there was an
     1282                    // unrecognized conversion request
     1283
     1284                    convErr++;
     1285                    retStr = "conversion unavailable: (";
     1286                    while (fromIter != fromUnitsList.end()) {
     1287                        /*
     1288                        if (fromIter != fromUnitsList.begin()) {
     1289                            retStr += " or ";
     1290                        }
     1291                        */
     1292                        retStr += fromIter->name();
     1293                        fromIter++;
     1294                    }
     1295                    retStr += ") -> (";
     1296
     1297                    // tempIter = toIter;
     1298
     1299                    while (toIter != toUnitsList.end()) {
     1300                        retStr += toIter->name();
     1301                        toIter++;
     1302                    }
     1303                    retStr += ")";
     1304
     1305                    // exit and report the error
     1306
     1307                    /*
     1308                    toIter = tempIter;
     1309                    toIter++;
     1310                    toUnitsList.erase(tempIter);
     1311                    */
     1312                }
     1313                else {
     1314                    // keep searching for units to convert
     1315                    // until we are out of units in the
     1316                    // fromUnitsList and toUnitsList.
     1317
     1318                    convErr = 0;
     1319                }
     1320            }
     1321            else {
     1322                // keep searching for units to convert
     1323                // until we are out of units in the
     1324                // fromUnitsList and toUnitsList.
     1325
     1326                convErr = 0;
     1327            }
     1328        }
     1329    }
     1330
     1331
     1332
     1333    if (convErr == 0) {
     1334        // if ( (fromIter != fromUnitsList.end()) || (toIter != toUnitsList.end()) ) {
     1335        if ( fromUnitsList.size() || toUnitsList.size() ) {
     1336            // raise error that there was an
     1337            // unrecognized conversion request
     1338
     1339            convErr++;
     1340            retStr = "unmatched units in conversion: (";
     1341
     1342            fromIter = fromUnitsList.begin();
     1343            while (fromIter != fromUnitsList.end()) {
     1344                retStr += fromIter->name();
     1345                fromIter++;
     1346            }
     1347
     1348            if (fromUnitsList.size() && toUnitsList.size()) {
     1349                retStr += ") -> (";
     1350            }
     1351
     1352            toIter = toUnitsList.begin();
     1353            while (toIter != toUnitsList.end()) {
     1354                retStr += toIter->name();
     1355                toIter++;
     1356            }
     1357            retStr += ")";
     1358        }
     1359        else {
     1360            // apply the conversion and check for errors
     1361            convErr = applyConversion (&numVal, totalConvList);
     1362            if (convErr == 0) {
     1363                // outVal.flags(std::ios::fixed);
     1364                // outVal.precision(10);
     1365                if (showUnits) {
     1366                    outVal << numVal << toUnitsName;
     1367                }
     1368                else {
     1369                    outVal << numVal;
     1370                }
     1371                retStr = outVal.str();
     1372            }
     1373            else {
     1374
     1375            }
     1376        }
     1377    }
    12791378
    12801379    if ( (result) && (*result == 0) ) {
    1281         *result = convResult;
    1282     }
    1283 
    1284     // outVal.flags(std::ios::fixed);
    1285     // outVal.precision(10);
    1286 
    1287     if (showUnits) {
    1288         outVal << numVal << toUnitsName;
    1289     }
    1290     else {
    1291         outVal << numVal;
    1292     }
    1293 
    1294     return std::string(outVal.str());
     1380        *result = convErr;
     1381    }
     1382
     1383    return retStr;
    12951384
    12961385}
Note: See TracChangeset for help on using the changeset viewer.