Changeset 538
- Timestamp:
- Oct 25, 2006 9:57:33 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpUnits.cc
r536 r538 1118 1118 std::string convVal = ""; 1119 1119 std::string type = ""; // junk var used because units2list requires it 1120 std::string retStr = ""; 1120 1121 double origNumVal = 0; 1121 1122 double numVal = 0; 1122 1123 double toExp = 0; 1123 1124 double fromExp = 0; 1124 int conv Result= 0;1125 int convErr = 0; 1125 1126 char* endptr = NULL; 1126 1127 std::stringstream outVal; … … 1200 1201 } 1201 1202 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 } 1204 1220 1205 1221 fromIter = fromUnitsList.begin(); 1206 1222 toIter = toUnitsList.begin(); 1207 1223 1208 while ( toIter != toUnitsList.end() ) {1224 while ( (toIter != toUnitsList.end()) && (fromIter != fromUnitsList.end()) && (!convErr) ) { 1209 1225 fromUnits = fromIter->getUnitsObj(); 1210 1226 toUnits = toIter->getUnitsObj(); 1211 1227 1212 1228 cList.clear(); 1213 conv Result= fromUnits->getConvertFxnList(toUnits, cList);1214 1215 if (conv Result== 0) {1229 convErr = fromUnits->getConvertFxnList(toUnits, cList); 1230 1231 if (convErr == 0) { 1216 1232 1217 1233 toExp = toIter->getExponent(); … … 1235 1251 // currently we cannot handle conversions of 1236 1252 // units where the exponents are different 1237 conv Result++;1238 } 1239 1240 } 1241 1242 if (conv Result== 0) {1253 convErr++; 1254 } 1255 1256 } 1257 1258 if (convErr == 0) { 1243 1259 // successful conversion reported 1244 1260 // remove the elements from the lists … … 1255 1271 fromIter++; 1256 1272 if (fromIter == fromUnitsList.end()) { 1273 1257 1274 fromIter = fromUnitsList.begin(); 1258 // raise error that there was an1259 // unrecognized conversion request1260 tempIter = toIter;1261 1275 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 } 1279 1378 1280 1379 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; 1295 1384 1296 1385 }
Note: See TracChangeset
for help on using the changeset viewer.