[83] | 1 | /* |
---|
| 2 | * ---------------------------------------------------------------------- |
---|
| 3 | * INTERFACE: Fortran Rappture Units Source |
---|
| 4 | * |
---|
| 5 | * ====================================================================== |
---|
| 6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
[115] | 7 | * Copyright (c) 2004-2005 Purdue Research Foundation |
---|
| 8 | * |
---|
| 9 | * See the file "license.terms" for information on usage and |
---|
| 10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[83] | 11 | * ====================================================================== |
---|
| 12 | */ |
---|
| 13 | |
---|
[76] | 14 | #include "RpUnits.h" |
---|
| 15 | #include "string.h" |
---|
| 16 | #include "RpUnitsFInterface.h" |
---|
[84] | 17 | #include "RpUnitsFStubs.c" |
---|
[93] | 18 | #include "RpBindingsDict.h" |
---|
[76] | 19 | |
---|
[1024] | 20 | extern "C" int |
---|
[84] | 21 | rp_define_unit( char* unitName, |
---|
| 22 | int* basisName, |
---|
[76] | 23 | int unitName_len ) { |
---|
| 24 | |
---|
| 25 | int result = -1; |
---|
[104] | 26 | const RpUnits* newUnit = NULL; |
---|
| 27 | const RpUnits* basis = NULL; |
---|
[76] | 28 | std::string basisStrName = ""; |
---|
| 29 | std::string newUnitName = ""; |
---|
| 30 | char* inText = NULL; |
---|
| 31 | |
---|
| 32 | inText = null_terminate(unitName,unitName_len); |
---|
| 33 | |
---|
| 34 | if (basisName && *basisName) { |
---|
[1024] | 35 | long key = (long)*basisName; |
---|
| 36 | basisStrName = ObjDictUnits.find(key); |
---|
[76] | 37 | |
---|
| 38 | if (basisStrName != "") { |
---|
| 39 | basis = RpUnits::find(basisStrName); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | newUnit = RpUnits::define(inText, basis); |
---|
[84] | 44 | |
---|
[76] | 45 | if (newUnit) { |
---|
| 46 | result = storeObject_UnitsStr(newUnit->getUnitsName()); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | if (inText) { |
---|
| 50 | free(inText); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | return result; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
[84] | 57 | int |
---|
| 58 | rp_find(char* searchName, int searchName_len) |
---|
[76] | 59 | { |
---|
| 60 | |
---|
| 61 | int result = -1; |
---|
| 62 | char* inText = NULL; |
---|
| 63 | |
---|
| 64 | inText = null_terminate(searchName,searchName_len); |
---|
| 65 | |
---|
[104] | 66 | const RpUnits* searchUnit = RpUnits::find(inText); |
---|
[76] | 67 | |
---|
| 68 | if (searchUnit) { |
---|
| 69 | result = storeObject_UnitsStr(searchUnit->getUnitsName()); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | if (inText) { |
---|
| 73 | free(inText); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return result; |
---|
| 77 | } |
---|
| 78 | |
---|
[84] | 79 | int |
---|
| 80 | rp_get_units(int* unitRefVal, char* retText, int retText_len) |
---|
[76] | 81 | { |
---|
[104] | 82 | const RpUnits* unitObj = NULL; |
---|
[76] | 83 | std::string unitNameText = ""; |
---|
[165] | 84 | int result = 1; |
---|
[76] | 85 | |
---|
| 86 | if (unitRefVal && *unitRefVal) { |
---|
[93] | 87 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
[76] | 88 | if (unitObj) { |
---|
| 89 | unitNameText = unitObj->getUnits(); |
---|
| 90 | fortranify(unitNameText.c_str(), retText, retText_len); |
---|
[165] | 91 | result = 0; |
---|
[76] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | return result; |
---|
| 96 | } |
---|
| 97 | |
---|
[84] | 98 | int |
---|
| 99 | rp_get_units_name(int* unitRefVal, char* retText, int retText_len) |
---|
[76] | 100 | { |
---|
[104] | 101 | const RpUnits* unitObj = NULL; |
---|
[76] | 102 | std::string unitNameText = ""; |
---|
[165] | 103 | int result = 1; |
---|
[76] | 104 | |
---|
| 105 | if (unitRefVal && *unitRefVal) { |
---|
[93] | 106 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
[76] | 107 | if (unitObj) { |
---|
| 108 | unitNameText = unitObj->getUnitsName(); |
---|
| 109 | fortranify(unitNameText.c_str(), retText, retText_len); |
---|
[165] | 110 | result = 0; |
---|
[76] | 111 | } |
---|
| 112 | } |
---|
[84] | 113 | |
---|
[76] | 114 | return result; |
---|
| 115 | } |
---|
| 116 | |
---|
[84] | 117 | int |
---|
| 118 | rp_get_exponent(int* unitRefVal, double* retExponent) |
---|
[76] | 119 | { |
---|
[104] | 120 | const RpUnits* unitObj = NULL; |
---|
[165] | 121 | int result = 1; |
---|
[76] | 122 | |
---|
| 123 | if (unitRefVal && *unitRefVal) { |
---|
[93] | 124 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
[76] | 125 | if (unitObj) { |
---|
| 126 | *retExponent = unitObj->getExponent(); |
---|
[165] | 127 | result = 0; |
---|
[76] | 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | return result; |
---|
| 132 | } |
---|
| 133 | |
---|
[84] | 134 | int |
---|
| 135 | rp_get_basis(int* unitRefVal) |
---|
[76] | 136 | { |
---|
[104] | 137 | const RpUnits* unitObj = NULL; |
---|
| 138 | const RpUnits* basisObj = NULL; |
---|
[76] | 139 | int result = -1; |
---|
| 140 | |
---|
| 141 | if (unitRefVal && *unitRefVal) { |
---|
[93] | 142 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
[76] | 143 | |
---|
| 144 | if (unitObj) { |
---|
| 145 | basisObj = unitObj->getBasis(); |
---|
[84] | 146 | |
---|
[76] | 147 | if (basisObj) { |
---|
| 148 | result = storeObject_UnitsStr(basisObj->getUnitsName()); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | return result; |
---|
| 154 | } |
---|
| 155 | |
---|
[84] | 156 | int |
---|
| 157 | rp_units_convert_dbl ( char* fromVal, |
---|
[104] | 158 | char* toUnitsName, |
---|
| 159 | double* convResult, |
---|
| 160 | int fromVal_len, |
---|
| 161 | int toUnitsName_len ) { |
---|
[76] | 162 | |
---|
| 163 | char* inFromVal = NULL; |
---|
| 164 | char* inToUnitsName = NULL; |
---|
| 165 | int result = -1; |
---|
| 166 | int showUnits = 0; |
---|
| 167 | std::string convStr = ""; |
---|
| 168 | |
---|
| 169 | // prepare string for c |
---|
| 170 | inFromVal = null_terminate(fromVal,fromVal_len); |
---|
| 171 | inToUnitsName = null_terminate(toUnitsName,toUnitsName_len); |
---|
| 172 | |
---|
| 173 | if (inFromVal && inToUnitsName) { |
---|
| 174 | // the call to RpUnits::convert() populates the result flag |
---|
| 175 | convStr = RpUnits::convert(inFromVal,inToUnitsName,showUnits,&result); |
---|
| 176 | |
---|
| 177 | if (!convStr.empty()) { |
---|
| 178 | *convResult = atof(convStr.c_str()); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // clean up memory |
---|
| 183 | if (inFromVal) { |
---|
| 184 | free(inFromVal); |
---|
| 185 | inFromVal = NULL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if (inToUnitsName) { |
---|
| 189 | free(inToUnitsName); |
---|
| 190 | inToUnitsName = NULL; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | return result; |
---|
| 194 | } |
---|
| 195 | |
---|
[84] | 196 | int |
---|
| 197 | rp_units_convert_str ( char* fromVal, |
---|
[104] | 198 | char* toUnitsName, |
---|
| 199 | char* retText, |
---|
| 200 | int fromVal_len, |
---|
| 201 | int toUnitsName_len, |
---|
| 202 | int retText_len ) { |
---|
[76] | 203 | |
---|
| 204 | char* inFromVal = NULL; |
---|
| 205 | char* inToUnitsName = NULL; |
---|
| 206 | std::string convStr = ""; |
---|
| 207 | int showUnits = 1; |
---|
| 208 | int result = -1; |
---|
| 209 | |
---|
| 210 | // prepare string for c |
---|
| 211 | inFromVal = null_terminate(fromVal,fromVal_len); |
---|
| 212 | inToUnitsName = null_terminate(toUnitsName,toUnitsName_len); |
---|
| 213 | |
---|
| 214 | if (inFromVal && inToUnitsName) { |
---|
| 215 | |
---|
| 216 | // do the conversion |
---|
| 217 | convStr = RpUnits::convert(inFromVal,inToUnitsName,showUnits,&result); |
---|
| 218 | |
---|
| 219 | if (!convStr.empty()) { |
---|
| 220 | // prepare string for fortran |
---|
| 221 | fortranify(convStr.c_str(), retText, retText_len); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // clean up out memory |
---|
| 226 | if (inFromVal) { |
---|
| 227 | free(inFromVal); |
---|
| 228 | inFromVal = NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | if (inToUnitsName) { |
---|
| 232 | free(inToUnitsName); |
---|
| 233 | inToUnitsName = NULL; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | // return the same result from the RpUnits::convert call. |
---|
| 237 | return result; |
---|
| 238 | } |
---|