Changeset 559
- Timestamp:
- Dec 14, 2006, 9:07:20 AM (18 years ago)
- Location:
- trunk/src/core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpUnits.cc
r554 r559 484 484 485 485 std::string 486 RpUnits::getUnitsName( ) const {486 RpUnits::getUnitsName(int flags) const { 487 487 488 488 std::stringstream unitText; … … 490 490 491 491 exponent = getExponent(); 492 493 if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_POS_EXP) { 494 if (exponent < 0) { 495 exponent = exponent * -1; 496 } 497 } 498 else if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_NEG_EXP) { 499 if (exponent > 0) { 500 exponent = exponent * -1; 501 } 502 } 492 503 493 504 if (exponent == 1) { … … 765 776 766 777 int 767 RpUnits::validate ( const std::string& inUnits, 778 // RpUnits::validate ( const std::string& inUnits, 779 RpUnits::validate ( std::string& inUnits, 768 780 std::string& type, 769 781 std::list<std::string>* compatList ) { … … 782 794 // err tells us if we encountered any unrecognized units 783 795 err = RpUnits::units2list(inUnits,inUnitsList,type); 796 RpUnits::list2units(inUnitsList,inUnits); 784 797 inIter = inUnitsList.begin(); 785 798 … … 848 861 849 862 std::string 850 RpUnitsListEntry::name( ) const {863 RpUnitsListEntry::name(int flags) const { 851 864 std::stringstream name; 852 name << unit->getUnits() << exponent; 865 double myExp = exponent; 866 867 if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_POS_EXP) { 868 if (myExp < 0) { 869 myExp = myExp * -1; 870 } 871 } 872 else if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_NEG_EXP) { 873 if (myExp > 0) { 874 myExp = myExp * -1; 875 } 876 } 877 878 name << unit->getUnits(); 879 880 if ((RPUNITS_ORIG_EXP & flags) == RPUNITS_STRICT_NAME) { 881 // if the user asks for strict naming, 882 // always place the exponent on the name 883 name << myExp; 884 } 885 else if (myExp != 1.0) { 886 // if the user does not ask for strict naming, 887 // check to see if the exponent == 1. 888 // If not, then add exponent to name 889 name << myExp; 890 } 891 853 892 return std::string(name.str()); 854 893 } … … 911 950 /**********************************************************************/ 912 951 // METHOD: units2list() 913 /// Split a string of units into a list of baseunits with exponents.952 /// Split a string of units into a list of units with exponents. 914 953 /** 915 954 * Splits a string of units like cm2/kVns into a list of units like 916 * cm2, kV 1, ns1 where an exponent is provided for each list entry.955 * cm2, kV-1, ns-1 where an exponent is provided for each list entry. 917 956 * List entries are found by comparing units strings to the names 918 957 * in the dictionary. … … 965 1004 // erase the found unit's name from our search string 966 1005 outList.push_front(RpUnitsListEntry(unit,exponent)); 967 type = unit->getType() + type; 1006 if (type.compare("") == 0) { 1007 type = unit->getType(); 1008 } 1009 else if (type[0] == '/') { 1010 type = unit->getType() + type; 1011 } 1012 else { 1013 type = unit->getType() + "*" + type; 1014 } 968 1015 myInUnits.erase(idx+offset); 969 1016 } … … 1006 1053 1007 1054 /**********************************************************************/ 1055 // METHOD: list2units() 1056 /// Join a list of units into a string with proper exponents. 1057 /** 1058 * Joings a list of units like cm2, kV-1, ns-1, creating a string 1059 * like cm2/kVns. 1060 */ 1061 1062 int 1063 RpUnits::list2units ( RpUnitsList& inList, 1064 std::string& outUnitsStr) { 1065 1066 RpUnitsListIter inListIter; 1067 std::string inUnits = ""; 1068 double exp = 0; 1069 int err = 0; 1070 std::string numerator = ""; 1071 std::string denominator = ""; 1072 1073 inListIter = inList.begin(); 1074 1075 while (inListIter != inList.end()) { 1076 exp = inListIter->getExponent(); 1077 if (exp > 0) { 1078 numerator += inListIter->name(); 1079 } 1080 else if (exp < 0) { 1081 denominator += inListIter->name(RPUNITS_POS_EXP); 1082 } 1083 else { 1084 // we shouldn't get units with exponents of zero 1085 } 1086 inListIter++; 1087 } 1088 1089 outUnitsStr = numerator; 1090 if ( denominator.compare("") != 0 ) { 1091 outUnitsStr += "/" + denominator; 1092 } 1093 1094 return err; 1095 } 1096 1097 /**********************************************************************/ 1008 1098 // METHOD: compareListEntryBasis() 1009 1099 /// Compare two RpUnits objects to see if they are related by a basis … … 1162 1252 double copies = 0; 1163 1253 1254 std::list<std::string> compatList; 1255 std::string listStr; 1256 1164 1257 convertList cList; 1165 1258 convertList totalConvList; … … 1234 1327 } 1235 1328 1329 convErr = RpUnits::units2list(toUnitsName,toUnitsList,type); 1330 if (convErr) { 1331 if (result) { 1332 *result = convErr; 1333 } 1334 retStr = "Unrecognized units: \"" + toUnitsName + "\". Please specify valid Rappture Units"; 1335 return retStr; 1336 } 1337 1236 1338 convErr = RpUnits::units2list(fromUnitsName,fromUnitsList,type); 1237 1339 if (convErr) { … … 1239 1341 *result = convErr; 1240 1342 } 1241 retStr = "unrecognized units: \"" + fromUnitsName + "\""; 1242 return retStr; 1243 } 1244 1245 convErr = RpUnits::units2list(toUnitsName,toUnitsList,type); 1246 if (convErr) { 1247 if (result) { 1248 *result = convErr; 1249 } 1250 retStr = "unrecognized units: \"" + toUnitsName + "\""; 1343 type = ""; 1344 RpUnits::validate(toUnitsName,type,&compatList); 1345 list2str(compatList,listStr); 1346 retStr = "Unrecognized units: \"" + fromUnitsName 1347 + "\".\nShould be units of type " + type + " (" + listStr + ")"; 1251 1348 return retStr; 1252 1349 } … … 1316 1413 1317 1414 convErr++; 1318 retStr = " conversion unavailable: (";1415 retStr = "Conversion unavailable: ("; 1319 1416 while (fromIter != fromUnitsList.end()) { 1320 1417 /* … … 1336 1433 retStr += ")"; 1337 1434 1435 type = ""; 1436 RpUnits::validate(toUnitsName,type,&compatList); 1437 list2str(compatList,listStr); 1438 retStr += "\nPlease enter units of type " 1439 + type + " (" + listStr + ")"; 1440 1441 1338 1442 // exit and report the error 1339 1443 … … 1389 1493 } 1390 1494 retStr += ")"; 1495 type = ""; 1496 RpUnits::validate(toUnitsName,type,&compatList); 1497 list2str(compatList,listStr); 1498 retStr += "\nPlease enter units of type " 1499 + type + " (" + listStr + ")"; 1500 1391 1501 } 1392 1502 else { … … 2518 2628 RpUnitsPreset::addPresetMisc () { 2519 2629 2520 RpUnits* volt = RpUnits::define("V", NULL, RP_TYPE_E NERGY);2630 RpUnits* volt = RpUnits::define("V", NULL, RP_TYPE_EPOT); 2521 2631 RpUnits* mole = RpUnits::define("mol",NULL, RP_TYPE_MISC); 2522 2632 RpUnits* hertz = RpUnits::define("Hz", NULL, RP_TYPE_MISC); … … 2537 2647 // -------------------------------------------------------------------- // 2538 2648 2649 /**********************************************************************/ 2650 // FUNCTION: list2str() 2651 /// Convert a std::list<std::string> into a comma delimited std::string 2652 /** 2653 * Iterates through a std::list<std::string> and returns a comma 2654 * delimited std::string containing the elements of the inputted std::list. 2655 * 2656 * Returns 0 on success, anything else is error 2657 */ 2658 2659 int 2660 list2str (std::list<std::string>& inList, std::string& outString) 2661 { 2662 int retVal = 1; // return Value 0 is success, everything else is failure 2663 unsigned int counter = 0; // check if we hit all elements of inList 2664 std::list<std::string>::iterator inListIter; // list interator 2665 2666 inListIter = inList.begin(); 2667 2668 while (inListIter != inList.end()) { 2669 if ( outString.empty() ) { 2670 outString = *inListIter; 2671 } 2672 else { 2673 outString = outString + "," + *inListIter; 2674 } 2675 2676 // increment the iterator and loop counter 2677 inListIter++; 2678 counter++; 2679 } 2680 2681 if (counter == inList.size()) { 2682 retVal = 0; 2683 } 2684 2685 return retVal; 2686 } -
trunk/src/core/RpUnits.h
r554 r559 19 19 enum RP_UNITS_CONSTS { 20 20 RPUNITS_UNITS_OFF = 0, 21 RPUNITS_UNITS_ON = 1 21 RPUNITS_UNITS_ON = 1, 22 23 // units naming flags 24 RPUNITS_POS_EXP = 1, 25 RPUNITS_NEG_EXP = 2, 26 RPUNITS_STRICT_NAME = 4, 27 RPUNITS_ORIG_EXP = 7 22 28 }; 23 29 … … 27 33 //define our different types of units 28 34 #define RP_TYPE_ENERGY "energy" 35 #define RP_TYPE_EPOT "electric_potential" 29 36 #define RP_TYPE_LENGTH "length" 30 37 #define RP_TYPE_TEMP "temperature" … … 296 303 297 304 // print the name of the object 298 std::string name( ) const;305 std::string name(int flags=RPUNITS_ORIG_EXP) const; 299 306 300 307 // report the basis of the RpUnits object being stored. … … 331 338 // users member fxns 332 339 std::string getUnits() const; 333 std::string getUnitsName( ) const;340 std::string getUnitsName(int flags=RPUNITS_ORIG_EXP) const; 334 341 std::string getSearchName() const; 335 342 double getExponent() const; … … 387 394 // returns 0 on success (units are valid) 388 395 // returns !0 on failure (units not valid) 389 static int validate( const std::string& inUnits,396 static int validate(std::string& inUnits, 390 397 std::string& type, 391 398 std::list<std::string>* compatList=NULL); … … 425 432 // "all" load all available units 426 433 // RP_TYPE_ENERGY "energy" load units related to energy 434 // RP_TYPE_EPOT "electric_potential" load units related to electric potential 427 435 // RP_TYPE_LENGTH "length" load units related to length 428 436 // RP_TYPE_TEMP "temperature" load units related to temperature … … 666 674 RpUnitsList& outList, 667 675 std::string& type); 676 static int list2units( RpUnitsList& inList, 677 std::string& outUnitsStr); 668 678 static int grabExponent(const std::string& inStr, double* exp); 669 679 static int grabUnitString( const std::string& inStr); … … 696 706 /*--------------------------------------------------------------------------*/ 697 707 708 int list2str (std::list<std::string>& inList, std::string& outString); 709 698 710 #endif // ifdef __cplusplus 699 711
Note: See TracChangeset
for help on using the changeset viewer.