[2709] | 1 | /* |
---|
| 2 | * ---------------------------------------------------------------------- |
---|
| 3 | * INTERFACE: R Rappture Units Source |
---|
| 4 | * |
---|
| 5 | * ====================================================================== |
---|
| 6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
[3177] | 7 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[2709] | 8 | * |
---|
| 9 | * See the file "license.terms" for information on usage and |
---|
| 10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 11 | * ====================================================================== |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #include "RpUnits.h" |
---|
| 15 | #include "RpBindingsDict.h" |
---|
| 16 | |
---|
| 17 | # include "RpUnitsRInterface.h" |
---|
| 18 | |
---|
| 19 | #ifdef __cplusplus |
---|
| 20 | extern "C" { |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | /**********************************************************************/ |
---|
| 24 | // FUNCTION: RPRUnitsConvertDouble(fromVal,toUnitsName) |
---|
| 25 | /// Perform units convertion on fromVal, return a double |
---|
| 26 | /** |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | SEXP |
---|
| 30 | RPRUnitsConvertDouble( |
---|
| 31 | SEXP fromVal, |
---|
| 32 | SEXP toUnitsName) |
---|
| 33 | { |
---|
| 34 | SEXP ans; |
---|
| 35 | std::string convStr = ""; |
---|
| 36 | int showUnitsFlag = 0; |
---|
| 37 | double convResult = 0.0; |
---|
| 38 | int err = 0; |
---|
| 39 | |
---|
| 40 | ans = allocVector(REALSXP,1); |
---|
| 41 | PROTECT(ans); |
---|
| 42 | |
---|
| 43 | REAL(ans)[0] = 0.0; |
---|
| 44 | |
---|
| 45 | if (!isString(fromVal) || length(fromVal) != 1) { |
---|
| 46 | error("fromVal is not a string"); |
---|
| 47 | UNPROTECT(1); |
---|
| 48 | return ans; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | if (!isString(toUnitsName) || length(toUnitsName) != 1) { |
---|
| 52 | error("toUnitsName is not a string"); |
---|
| 53 | UNPROTECT(1); |
---|
| 54 | return ans; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // the call to RpUnits::convert() populates the result flag |
---|
| 58 | convStr = RpUnits::convert(CHAR(STRING_ELT(fromVal,0)), |
---|
| 59 | CHAR(STRING_ELT(toUnitsName,0)), |
---|
| 60 | showUnitsFlag, |
---|
| 61 | &err); |
---|
| 62 | |
---|
| 63 | if (err) { |
---|
| 64 | error("unrecognized unit conversion"); |
---|
| 65 | UNPROTECT(1); |
---|
| 66 | return ans; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | REAL(ans)[0] = atof(convStr.c_str()); |
---|
| 70 | |
---|
| 71 | UNPROTECT(1); |
---|
| 72 | |
---|
| 73 | return ans; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | /**********************************************************************/ |
---|
| 78 | // FUNCTION: RPRUnitsConvertString(fromVal,toUnitsName,showUnits) |
---|
| 79 | /// Perform units convertion on fromVal, return a string |
---|
| 80 | /** |
---|
| 81 | */ |
---|
| 82 | |
---|
| 83 | SEXP |
---|
| 84 | RPRUnitsConvertString( |
---|
| 85 | SEXP fromVal, |
---|
| 86 | SEXP toUnitsName, |
---|
| 87 | SEXP showUnits) |
---|
| 88 | { |
---|
| 89 | SEXP ans; |
---|
| 90 | std::string convStr = ""; |
---|
| 91 | int showUnitsFlag = 1; |
---|
| 92 | double convResult = 0.0; |
---|
| 93 | int err = 0; |
---|
| 94 | |
---|
| 95 | ans = allocVector(STRSXP,1); |
---|
| 96 | PROTECT(ans); |
---|
| 97 | |
---|
| 98 | SET_STRING_ELT(ans,0,mkChar("")); |
---|
| 99 | |
---|
| 100 | if (!isString(fromVal) || length(fromVal) != 1) { |
---|
| 101 | error("fromVal is not a string"); |
---|
| 102 | UNPROTECT(1); |
---|
| 103 | return ans; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (!isString(toUnitsName) || length(toUnitsName) != 1) { |
---|
| 107 | error("toUnitsName is not a string"); |
---|
| 108 | UNPROTECT(1); |
---|
| 109 | return ans; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | if (!isLogical(showUnits) || length(showUnits) != 1) { |
---|
| 113 | error("showUnits is not a logical"); |
---|
| 114 | UNPROTECT(1); |
---|
| 115 | return ans; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | showUnitsFlag = asLogical(showUnits); |
---|
| 119 | if (showUnitsFlag == 1) { |
---|
| 120 | showUnitsFlag = RPUNITS_UNITS_ON; |
---|
| 121 | } else { |
---|
| 122 | showUnitsFlag = RPUNITS_UNITS_OFF; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // the call to RpUnits::convert() populates the result flag |
---|
| 126 | convStr = RpUnits::convert(CHAR(STRING_ELT(fromVal,0)), |
---|
| 127 | CHAR(STRING_ELT(toUnitsName,0)), |
---|
| 128 | showUnitsFlag, |
---|
| 129 | &err); |
---|
| 130 | |
---|
| 131 | if (err) { |
---|
| 132 | error("unrecognized unit conversion"); |
---|
| 133 | UNPROTECT(1); |
---|
| 134 | return ans; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | SET_STRING_ELT(ans,0,mkChar(convStr.c_str())); |
---|
| 138 | |
---|
| 139 | UNPROTECT(1); |
---|
| 140 | |
---|
| 141 | return ans; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | #ifdef __cplusplus |
---|
| 145 | } |
---|
| 146 | #endif // ifdef __cplusplus |
---|
| 147 | |
---|