Changeset 562
- Timestamp:
- Dec 15, 2006 11:11:16 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/gauge.tcl
r556 r562 220 220 if {$units != ""} { 221 221 set nvUnits [Rappture::Units::Search::for $nv] 222 if { "" == $nvUnits} { 223 set msg [Rappture::Units::description $units] 224 error "Unrecognized units: $nv\n$mesg" 225 } 222 226 set newval [Rappture::Units::convert $newval \ 223 227 -context $units] -
trunk/src/core/RpUnits.cc
r559 r562 987 987 } 988 988 989 // check to see if we came across a '*' character 990 if (myInUnits[last] == '*') { 991 // type = myInUnits[last] + type; 992 // ignore * because we assume everything is multiplied together 993 myInUnits.erase(last); 994 continue; 995 } 996 989 997 // get the exponent 990 998 offset = RpUnits::grabExponent(myInUnits,&exponent); 991 999 myInUnits.erase(offset); 992 1000 idx = offset - 1; 1001 last = myInUnits.length()-1; 1002 if (last == -1) { 1003 // the string is empty, units were not correctly entered 1004 err = 1; 1005 break; 1006 } 993 1007 994 1008 // grab the largest string we can find 995 1009 offset = RpUnits::grabUnitString(myInUnits); 996 idx = offset; 1010 1011 // if idx > length, then the grabUnitString went through the whole 1012 // string and did not find a good string we could use as units. 1013 // this generally means the string was filled with non alphabetical 1014 // symbols like *&^%$#@!)(~`{}[]:;"'?/><,.-_=+\ or | 1015 1016 if (offset > last) { 1017 err = 1; 1018 // erase the last offending character 1019 myInUnits.erase(last); 1020 // reset our vars and try again 1021 idx = 0; 1022 offset = 0; 1023 exponent = 1; 1024 continue; 1025 } 1026 else { 1027 idx = offset; 1028 } 997 1029 998 1030 // figure out if we have some defined units in that string … … 1019 1051 // raise error and delete character for now 1020 1052 err = 1; 1021 myInUnits.erase( last);1053 myInUnits.erase(idx); 1022 1054 } 1023 1055 … … 1247 1279 double fromExp = 0; 1248 1280 int convErr = 0; 1249 char* endptr = NULL;1250 1281 std::stringstream outVal; 1251 1282 … … 1272 1303 // 1273 1304 1274 numVal = strtod(val.c_str(),&endptr);1305 convErr = unitSlice(val,fromUnitsName,numVal); 1275 1306 origNumVal = numVal; 1276 1307 1277 if ( (numVal == 0) && (endptr == val.c_str())) {1308 if (convErr != 0) { 1278 1309 // no conversion was done. 1279 1310 // number in incorrect format probably. … … 1284 1315 } 1285 1316 1286 fromUnitsName = std::string(endptr);1287 1288 1317 if (toUnitsName.empty()) { 1289 // there were no units in the input 1318 // there were no units in the input 1290 1319 // string or no conversion needed 1291 1320 // assume fromUnitsName = toUnitsName … … 2685 2714 return retVal; 2686 2715 } 2716 2717 /**********************************************************************/ 2718 // FUNCTION: unitSlice() 2719 /// Convert a std::string into what might be the value and units 2720 /** 2721 * Returns 0 on success, anything else is error 2722 */ 2723 2724 int 2725 unitSlice (std::string inStr, std::string& outUnits, double& outVal) 2726 { 2727 int retVal = 0; // return val 0 is success, otherwise failure 2728 char *endptr = NULL; 2729 2730 outVal = strtod(inStr.c_str(), &endptr); 2731 2732 if ( (outVal == 0) && (endptr == inStr.c_str()) ) { 2733 // no conversion performed 2734 retVal = 1; 2735 } 2736 else { 2737 } 2738 2739 outUnits = std::string(endptr); 2740 2741 return retVal; 2742 } -
trunk/src/core/RpUnits.h
r559 r562 708 708 int list2str (std::list<std::string>& inList, std::string& outString); 709 709 710 int unitSlice (std::string inStr, std::string& outUnits, double& outVal); 711 710 712 #endif // ifdef __cplusplus 711 713 -
trunk/src/tcl/src/RpUnitsTclInterface.cc
r560 r562 327 327 // parse through command line options 328 328 if (argc != 2) { 329 Tcl_AppendResult(interp, 329 Tcl_AppendResult(interp, 330 330 "wrong # args: should be \"", argv[0], 331 331 " units\"", (char*)NULL); … … 500 500 { 501 501 std::string unitsName = ""; // name of the units provided by user 502 std::string origUnitsName = ""; // name of the units provided by user 502 503 std::string type = ""; // junk variable that validate() needs 503 504 int nextarg = 1; // start parsing using the '2'th argument 504 505 int err = 0; // err code for validate 505 char* endptr = NULL;506 double val = 0; 506 507 507 508 Tcl_ResetResult(interp); … … 515 516 516 517 // find where the unitsName begins 517 strtod(argv[nextarg],&endptr); 518 unitsName = std::string(endptr); 518 unitSlice(std::string(argv[nextarg]),unitsName,val); 519 519 520 520 err = RpUnits::validate(unitsName,type); 521 521 if (err) { 522 522 /* 523 * according to tcl version, in this case we 523 * according to tcl version, in this case we 524 524 * should return an empty string. i happen to disagree. 525 525 * the next few lines is what i think the user should see. 526 526 Tcl_AppendResult(interp, 527 "The units named: \"", unitsName.c_str(), 528 "\" is not a recognized unit for rappture", 529 (char*)NULL); 527 "Unrecognized units: \"", origUnitsName.c_str(), "\"", (char*)NULL); 530 528 return TCL_ERROR; 531 529 */ -
trunk/src/tcl/tests/units.test
r560 r562 188 188 } {0 {}} 189 189 190 #test desc-2.2.0.1 {Rappture::Units::descption, valid units} { 191 ## this test passes for old tcl version, fails for new tcl bindings 192 ## because different order of units 193 # list [catch {Rappture::Units::description eV} msg] $msg 194 #} {0 {energy (eV,J)}} 195 196 test desc-2.2.0.2 {Rappture::Units::descption, valid units} { 190 test desc-2.2.0 {Rappture::Units::descption, valid units} { 197 191 # this test passes for new tcl bindings, fails for old tcl version 198 192 list [catch {Rappture::Units::description eV} msg] $msg 199 193 } {0 {energy (J,eV)}} 194 195 test desc-2.2.1 {Rappture::Units::descption, *cm2*K/Vs} { 196 list [catch {Rappture::Units::description *cm2*K/Vs} msg] $msg 197 } {0 {length*temperature/electric_potential*time (/V,/s,A2,C,F,K,R,cm2,in2,m2)}} 198 199 test desc-2.2.2 {Rappture::Units::descption, cm2 areas} { 200 list [catch {Rappture::Units::description cm2} msg] $msg 201 } {0 {area (A2,cm2,in2,m2)}} 202 203 test desc-2.2.3 {Rappture::Units::descption, cm3 volumes} { 204 list [catch {Rappture::Units::description cm3} msg] $msg 205 } {0 {volume (A3,cm3,in3,m3)}} 206 207 test desc-2.2.4 {Rappture::Units::descption, K temperature} { 208 list [catch {Rappture::Units::description K} msg] $msg 209 } {0 {temperature (C,F,K,R)}} 210 211 test desc-2.2.5 {Rappture::Units::descption, s time} { 212 list [catch {Rappture::Units::description s} msg] $msg 213 } {0 {time (s)}} 200 214 201 215 test desc-2.3.0 {Rappture::Units::descption, too many args} { … … 203 217 } {1 {wrong # args: should be "Rappture::Units::description units"}} 204 218 219 test desc-2.3.0 {Rappture::Units::descption, incorrect input type} { 220 list [catch {Rappture::Units::description 3cm} msg] $msg 221 } {0 {}} 205 222 206 223 #---------------------------------------------------------- … … 842 859 } {0 {}} 843 860 844 test search-for-6.0.1. 2{Rappture::Units::Search::for, notaunit} {861 test search-for-6.0.1.3 {Rappture::Units::Search::for, notaunit} { 845 862 list [catch {Rappture::Units::Search::for ^&} msg] $msg 846 863 } {0 {}} 847 864 865 test search-for-6.0.1.4 {Rappture::Units::Search::for, notaunit} { 866 list [catch {Rappture::Units::Search::for x44} msg] $msg 867 } {0 {}} 868 869 test search-for-6.0.1.5 {Rappture::Units::Search::for, notaunit} { 870 list [catch {Rappture::Units::Search::for 3*44eV} msg] $msg 871 } {0 {}} 872 848 873 ::tcltest::cleanupTests 849 874 return
Note: See TracChangeset
for help on using the changeset viewer.