1 | // #include "../include/RpUnitsCInterface.h" |
---|
2 | // #include "../include/RpDictCFInterface.h" |
---|
3 | // #include <stdio.h> |
---|
4 | |
---|
5 | #include "../include/RpUnits.h" |
---|
6 | #include "../include/RpDict.h" |
---|
7 | #include "string.h" |
---|
8 | |
---|
9 | #ifdef COMPNAME_ADD1UNDERSCORE |
---|
10 | # define rp_define_unit rp_define_unit_ |
---|
11 | # define rp_find rp_find_ |
---|
12 | # define rp_make_metric rp_make_metric_ |
---|
13 | # define rp_get_units rp_get_units_ |
---|
14 | # define rp_get_units_name rp_get_units_name_ |
---|
15 | # define rp_get_exponent rp_get_exponent_ |
---|
16 | # define rp_get_basis rp_get_basis_ |
---|
17 | #elif defined(COMPNAME_ADD2UNDERSCORE) |
---|
18 | # define rp_define_unit rp_define_unit__ |
---|
19 | # define rp_find rp_find__ |
---|
20 | # define rp_make_metric rp_make_metric__ |
---|
21 | # define rp_get_units rp_get_units__ |
---|
22 | # define rp_get_units_name rp_get_units_name__ |
---|
23 | # define rp_get_exponent rp_get_exponent__ |
---|
24 | # define rp_get_basis rp_get_basis__ |
---|
25 | #elif defined(COMPNAME_NOCHANGE) |
---|
26 | # define rp_define_unit rp_define_unit |
---|
27 | # define rp_find rp_find |
---|
28 | # define rp_make_metric rp_make_metric |
---|
29 | # define rp_get_units rp_get_units |
---|
30 | # define rp_get_units_name rp_get_units_name |
---|
31 | # define rp_get_exponent rp_get_exponent |
---|
32 | # define rp_get_basis rp_get_basis |
---|
33 | #elif defined(COMPNAME_UPPERCASE) |
---|
34 | # define rp_define_unit RP_DEFINE_UNIT |
---|
35 | # define rp_find RP_FIND |
---|
36 | # define rp_make_metric RP_MAKE_METRIC |
---|
37 | # define rp_get_units RP_GET_UNITS |
---|
38 | # define rp_get_units_name RP_GET_UNITS_NAME |
---|
39 | # define rp_get_exponent RP_GET_EXPONENT |
---|
40 | # define rp_get_basis RP_GET_BASIS |
---|
41 | #endif |
---|
42 | |
---|
43 | extern "C" |
---|
44 | int rp_define_unit(char* unitName, int* basisName, int unitName_len); |
---|
45 | |
---|
46 | extern "C" |
---|
47 | int rp_find(char* searchName, int searchName_len); |
---|
48 | |
---|
49 | extern "C" |
---|
50 | int rp_make_metric(int* basis); |
---|
51 | |
---|
52 | extern "C" |
---|
53 | void rp_get_units(int* unitRefVal, char* retText, int retText_len); |
---|
54 | |
---|
55 | extern "C" |
---|
56 | void rp_get_units_name(int* unitRefVal, char* retText, int retText_len); |
---|
57 | |
---|
58 | extern "C" |
---|
59 | double rp_get_exponent(int* unitRefVal); |
---|
60 | |
---|
61 | extern "C" |
---|
62 | int rp_get_basis(int* unitRefVal); |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | char* null_terminate(char* inStr, int len); |
---|
72 | int storeObject(std::string objectName); |
---|
73 | RpUnits* getObject(int objKey); |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | #define DICT_TEMPLATE <int,std::string> |
---|
79 | // RpDict<int,std::string>fortObjDict; |
---|
80 | RpDict DICT_TEMPLATE fortObjDict; |
---|
81 | |
---|
82 | int rp_define_unit( char* unitName, |
---|
83 | int* basisName, |
---|
84 | int unitName_len ) { |
---|
85 | |
---|
86 | int retVal = -1; |
---|
87 | RpUnits* newUnit = NULL; |
---|
88 | RpUnits* basis = NULL; |
---|
89 | std::string basisStrName = ""; |
---|
90 | std::string newUnitName = ""; |
---|
91 | char* inText = NULL; |
---|
92 | |
---|
93 | inText = null_terminate(unitName,unitName_len); |
---|
94 | |
---|
95 | if (basisName && *basisName) { |
---|
96 | basisStrName = fortObjDict.find(*basisName); |
---|
97 | |
---|
98 | if (basisStrName != "") { |
---|
99 | basis = RpUnits::find(basisStrName); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | // newUnit = RpUnits::define(unitName, basis); |
---|
104 | newUnit = RpUnits::define(inText, basis); |
---|
105 | |
---|
106 | if (newUnit) { |
---|
107 | retVal = storeObject(newUnit->getUnitsName()); |
---|
108 | } |
---|
109 | |
---|
110 | if (inText) { |
---|
111 | free(inText); |
---|
112 | } |
---|
113 | |
---|
114 | return retVal; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | int rp_find(char* searchName, int searchName_len) |
---|
119 | { |
---|
120 | |
---|
121 | int retVal = -1; |
---|
122 | char* inText = NULL; |
---|
123 | |
---|
124 | inText = null_terminate(searchName,searchName_len); |
---|
125 | |
---|
126 | // RpUnits* searchUnit = RpUnits::find(searchName); |
---|
127 | RpUnits* searchUnit = RpUnits::find(inText); |
---|
128 | |
---|
129 | if (searchUnit) { |
---|
130 | retVal = storeObject(searchUnit->getUnitsName()); |
---|
131 | } |
---|
132 | |
---|
133 | if (inText) { |
---|
134 | free(inText); |
---|
135 | } |
---|
136 | |
---|
137 | return retVal; |
---|
138 | } |
---|
139 | |
---|
140 | int rp_make_metric(int* basis) |
---|
141 | { |
---|
142 | int retVal = -1; |
---|
143 | RpUnits* newBasis = NULL; |
---|
144 | |
---|
145 | if (basis && *basis) { |
---|
146 | newBasis = getObject(*basis); |
---|
147 | |
---|
148 | if (newBasis) { |
---|
149 | retVal = RpUnits::makeMetric(newBasis); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return retVal; |
---|
154 | } |
---|
155 | |
---|
156 | void rp_get_units(int* unitRefVal, char* retText, int retText_len) |
---|
157 | { |
---|
158 | int length_in = 0; |
---|
159 | int length_out = 0; |
---|
160 | int i = 0; |
---|
161 | RpUnits* unitObj = NULL; |
---|
162 | std::string unitNameText = ""; |
---|
163 | |
---|
164 | if (unitRefVal && *unitRefVal) { |
---|
165 | unitObj = getObject(*unitRefVal); |
---|
166 | if (unitObj) { |
---|
167 | unitNameText = unitObj->getUnits(); |
---|
168 | |
---|
169 | length_in = unitNameText.length(); |
---|
170 | length_out = retText_len; |
---|
171 | |
---|
172 | strncpy(retText, unitNameText.c_str(), length_out); |
---|
173 | |
---|
174 | // fortran-ify the string |
---|
175 | if (length_in < length_out) { |
---|
176 | for (i = length_in; i < length_out; i++) { |
---|
177 | retText[i] = ' '; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | void rp_get_units_name(int* unitRefVal, char* retText, int retText_len) |
---|
186 | { |
---|
187 | int length_in = 0; |
---|
188 | int length_out = 0; |
---|
189 | int i = 0; |
---|
190 | RpUnits* unitObj = NULL; |
---|
191 | std::string unitNameText = ""; |
---|
192 | |
---|
193 | if (unitRefVal && *unitRefVal) { |
---|
194 | unitObj = getObject(*unitRefVal); |
---|
195 | if (unitObj) { |
---|
196 | unitNameText = unitObj->getUnitsName(); |
---|
197 | |
---|
198 | length_in = unitNameText.length(); |
---|
199 | length_out = retText_len; |
---|
200 | |
---|
201 | strncpy(retText, unitNameText.c_str(), length_out); |
---|
202 | |
---|
203 | // fortran-ify the string |
---|
204 | if (length_in < length_out) { |
---|
205 | for (i = length_in; i < length_out; i++) { |
---|
206 | retText[i] = ' '; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | double rp_get_exponent(int* unitRefVal) |
---|
216 | { |
---|
217 | RpUnits* unitObj = NULL; |
---|
218 | double retVal = 0; |
---|
219 | |
---|
220 | if (unitRefVal && *unitRefVal) { |
---|
221 | unitObj = getObject(*unitRefVal); |
---|
222 | if (unitObj) { |
---|
223 | retVal = unitObj->getExponent(); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | return retVal; |
---|
228 | } |
---|
229 | |
---|
230 | int rp_get_basis(int* unitRefVal) |
---|
231 | { |
---|
232 | RpUnits* unitObj = NULL; |
---|
233 | RpUnits* basisObj = NULL; |
---|
234 | int retVal = -1; |
---|
235 | |
---|
236 | if (unitRefVal && *unitRefVal) { |
---|
237 | unitObj = getObject(*unitRefVal); |
---|
238 | |
---|
239 | if (unitObj) { |
---|
240 | basisObj = unitObj->getBasis(); |
---|
241 | |
---|
242 | if (basisObj) { |
---|
243 | retVal = storeObject(basisObj->getUnitsName()); |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | return retVal; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | int storeObject(std::string objectName) { |
---|
262 | |
---|
263 | int retVal = -1; |
---|
264 | int dictNextKey = fortObjDict.size() + 1; |
---|
265 | int newEntry = 0; |
---|
266 | |
---|
267 | if (objectName != "") { |
---|
268 | // dictionary returns a reference to the inserted value |
---|
269 | // no error checking to make sure it was successful in entering |
---|
270 | // the new entry. |
---|
271 | fortObjDict.set(dictNextKey,objectName, &newEntry); |
---|
272 | } |
---|
273 | |
---|
274 | retVal = dictNextKey; |
---|
275 | return retVal; |
---|
276 | } |
---|
277 | |
---|
278 | RpUnits* getObject(int objKey) { |
---|
279 | |
---|
280 | std::string basisName = *(fortObjDict.find(objKey).getValue()); |
---|
281 | |
---|
282 | if (basisName == *(fortObjDict.getNullEntry().getValue())) { |
---|
283 | // basisName = ""; |
---|
284 | return NULL; |
---|
285 | } |
---|
286 | |
---|
287 | return RpUnits::find(basisName); |
---|
288 | |
---|
289 | } |
---|
290 | |
---|
291 | /* fix buffer overflow possibility*/ |
---|
292 | char* null_terminate(char* inStr, int len) |
---|
293 | { |
---|
294 | int retVal = 0; |
---|
295 | int origLen = len; |
---|
296 | char* newStr = NULL; |
---|
297 | |
---|
298 | if (inStr) { |
---|
299 | |
---|
300 | while ((len > 0) && (isspace(*(inStr+len-1)))) { |
---|
301 | // dont strip off newlines |
---|
302 | if ( (*(inStr+len-1) == '\f') |
---|
303 | || (*(inStr+len-1) == '\n') |
---|
304 | || (*(inStr+len-1) == '\r') |
---|
305 | || (*(inStr+len-1) == '\t') |
---|
306 | || (*(inStr+len-1) == '\v') ) |
---|
307 | { |
---|
308 | break; |
---|
309 | } |
---|
310 | len--; |
---|
311 | } |
---|
312 | |
---|
313 | newStr = (char*) calloc(len+1,(sizeof(char))); |
---|
314 | strncpy(newStr,inStr,len); |
---|
315 | *(newStr+len) = '\0'; |
---|
316 | |
---|
317 | retVal++; |
---|
318 | } |
---|
319 | |
---|
320 | // return retVal; |
---|
321 | |
---|
322 | return newStr; |
---|
323 | } |
---|