1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * INTERFACE: Fortran Rappture Units Source |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
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. |
---|
11 | * ====================================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | #include "RpUnits.h" |
---|
15 | #include "string.h" |
---|
16 | #include "RpUnitsFInterface.h" |
---|
17 | #include "RpUnitsFStubs.c" |
---|
18 | #include "RpBindingsDict.h" |
---|
19 | |
---|
20 | extern "C" int |
---|
21 | rp_define_unit( char* unitName, |
---|
22 | int* basisName, |
---|
23 | int unitName_len ) { |
---|
24 | |
---|
25 | int result = -1; |
---|
26 | const RpUnits* newUnit = NULL; |
---|
27 | const RpUnits* basis = NULL; |
---|
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) { |
---|
35 | long key = (long)*basisName; |
---|
36 | basisStrName = ObjDictUnits.find(key); |
---|
37 | |
---|
38 | if (basisStrName != "") { |
---|
39 | basis = RpUnits::find(basisStrName); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | newUnit = RpUnits::define(inText, basis); |
---|
44 | |
---|
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 | |
---|
57 | int |
---|
58 | rp_find(char* searchName, int searchName_len) |
---|
59 | { |
---|
60 | |
---|
61 | int result = -1; |
---|
62 | char* inText = NULL; |
---|
63 | |
---|
64 | inText = null_terminate(searchName,searchName_len); |
---|
65 | |
---|
66 | const RpUnits* searchUnit = RpUnits::find(inText); |
---|
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 | |
---|
79 | int |
---|
80 | rp_get_units(int* unitRefVal, char* retText, int retText_len) |
---|
81 | { |
---|
82 | const RpUnits* unitObj = NULL; |
---|
83 | std::string unitNameText = ""; |
---|
84 | int result = 1; |
---|
85 | |
---|
86 | if (unitRefVal && *unitRefVal) { |
---|
87 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
88 | if (unitObj) { |
---|
89 | unitNameText = unitObj->getUnits(); |
---|
90 | fortranify(unitNameText.c_str(), retText, retText_len); |
---|
91 | result = 0; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | return result; |
---|
96 | } |
---|
97 | |
---|
98 | int |
---|
99 | rp_get_units_name(int* unitRefVal, char* retText, int retText_len) |
---|
100 | { |
---|
101 | const RpUnits* unitObj = NULL; |
---|
102 | std::string unitNameText = ""; |
---|
103 | int result = 1; |
---|
104 | |
---|
105 | if (unitRefVal && *unitRefVal) { |
---|
106 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
107 | if (unitObj) { |
---|
108 | unitNameText = unitObj->getUnitsName(); |
---|
109 | fortranify(unitNameText.c_str(), retText, retText_len); |
---|
110 | result = 0; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | return result; |
---|
115 | } |
---|
116 | |
---|
117 | int |
---|
118 | rp_get_exponent(int* unitRefVal, double* retExponent) |
---|
119 | { |
---|
120 | const RpUnits* unitObj = NULL; |
---|
121 | int result = 1; |
---|
122 | |
---|
123 | if (unitRefVal && *unitRefVal) { |
---|
124 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
125 | if (unitObj) { |
---|
126 | *retExponent = unitObj->getExponent(); |
---|
127 | result = 0; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | return result; |
---|
132 | } |
---|
133 | |
---|
134 | int |
---|
135 | rp_get_basis(int* unitRefVal) |
---|
136 | { |
---|
137 | const RpUnits* unitObj = NULL; |
---|
138 | const RpUnits* basisObj = NULL; |
---|
139 | int result = -1; |
---|
140 | |
---|
141 | if (unitRefVal && *unitRefVal) { |
---|
142 | unitObj = getObject_UnitsStr(*unitRefVal); |
---|
143 | |
---|
144 | if (unitObj) { |
---|
145 | basisObj = unitObj->getBasis(); |
---|
146 | |
---|
147 | if (basisObj) { |
---|
148 | result = storeObject_UnitsStr(basisObj->getUnitsName()); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return result; |
---|
154 | } |
---|
155 | |
---|
156 | int |
---|
157 | rp_units_convert_dbl ( char* fromVal, |
---|
158 | char* toUnitsName, |
---|
159 | double* convResult, |
---|
160 | int fromVal_len, |
---|
161 | int toUnitsName_len ) { |
---|
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 | |
---|
196 | int |
---|
197 | rp_units_convert_str ( char* fromVal, |
---|
198 | char* toUnitsName, |
---|
199 | char* retText, |
---|
200 | int fromVal_len, |
---|
201 | int toUnitsName_len, |
---|
202 | int retText_len ) { |
---|
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 | } |
---|