source: trunk/src/fortran/RpUnitsFInterface.cc @ 84

Last change on this file since 84 was 84, checked in by dkearney, 17 years ago
  1. added "install" target to make file for copying files over to /opt/rappture
  2. added targets for python to build and install the units module
  3. added fortran stub functions for library and units.

RpLibraryF_test.f compiles, just with warnings, because i'm still working on it.

File size: 7.0 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  INTERFACE: Fortran Rappture Units Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2005
8 *  Purdue Research Foundation, West Lafayette, IN
9 * ======================================================================
10 */
11
12#include "RpUnits.h"
13#include "RpDict.h"
14#include "string.h"
15#include "RpUnitsFInterface.h"
16#include "RpUnitsFStubs.c"
17
18#define DICT_TEMPLATE <int,std::string>
19RpDict DICT_TEMPLATE fortObjDictUnits;
20
21int storeObject_UnitsStr(std::string objectName);
22RpUnits* getObject_UnitsInt(int objKey);
23
24int
25rp_define_unit( char* unitName,
26                    int* basisName,
27                    int unitName_len    ) {
28
29    int result = -1;
30    RpUnits* newUnit = NULL;
31    RpUnits* basis = NULL;
32    std::string basisStrName = "";
33    std::string newUnitName = "";
34    char* inText = NULL;
35
36    inText = null_terminate(unitName,unitName_len);
37
38    if (basisName && *basisName) {
39        basisStrName = fortObjDictUnits.find(*basisName);
40
41        if (basisStrName != "") {
42            basis = RpUnits::find(basisStrName);
43        }
44    }
45
46    // newUnit = RpUnits::define(unitName, basis);
47    newUnit = RpUnits::define(inText, basis);
48
49    if (newUnit) {
50        result = storeObject_UnitsStr(newUnit->getUnitsName());
51    }
52
53    if (inText) {
54        free(inText);
55    }
56
57    return result;
58}
59
60
61int
62rp_find(char* searchName, int searchName_len)
63{
64
65    int result = -1;
66    char* inText = NULL;
67
68    inText = null_terminate(searchName,searchName_len);
69
70    // RpUnits* searchUnit = RpUnits::find(searchName);
71    RpUnits* searchUnit = RpUnits::find(inText);
72
73    if (searchUnit) {
74        result = storeObject_UnitsStr(searchUnit->getUnitsName());
75    }
76
77    if (inText) {
78        free(inText);
79    }
80
81    return result;
82}
83
84int
85rp_make_metric(int* basis)
86{
87    int result = -1;
88    RpUnits* newBasis = NULL;
89
90    if (basis && *basis) {
91        newBasis = getObject_UnitsInt(*basis);
92
93        if (newBasis) {
94            result = RpUnits::makeMetric(newBasis);
95        }
96    }
97
98    return result;
99}
100
101int
102rp_get_units(int* unitRefVal, char* retText, int retText_len)
103{
104    RpUnits* unitObj = NULL;
105    std::string unitNameText = "";
106    int result = 0;
107
108    if (unitRefVal && *unitRefVal) {
109        unitObj = getObject_UnitsInt(*unitRefVal);
110        if (unitObj) {
111            unitNameText = unitObj->getUnits();
112            fortranify(unitNameText.c_str(), retText, retText_len);
113        }
114    }
115
116    return result;
117}
118
119int
120rp_get_units_name(int* unitRefVal, char* retText, int retText_len)
121{
122    RpUnits* unitObj = NULL;
123    std::string unitNameText = "";
124    int result = 0;
125
126    if (unitRefVal && *unitRefVal) {
127        unitObj = getObject_UnitsInt(*unitRefVal);
128        if (unitObj) {
129            unitNameText = unitObj->getUnitsName();
130            fortranify(unitNameText.c_str(), retText, retText_len);
131        }
132    }
133
134    return result;
135}
136
137int
138rp_get_exponent(int* unitRefVal, double* retExponent)
139{
140    RpUnits* unitObj = NULL;
141    int result = 0;
142
143    if (unitRefVal && *unitRefVal) {
144        unitObj = getObject_UnitsInt(*unitRefVal);
145        if (unitObj) {
146            *retExponent = unitObj->getExponent();
147        }
148    }
149
150    return result;
151}
152
153int
154rp_get_basis(int* unitRefVal)
155{
156    RpUnits* unitObj = NULL;
157    RpUnits* basisObj = NULL;
158    int result = -1;
159
160    if (unitRefVal && *unitRefVal) {
161        unitObj = getObject_UnitsInt(*unitRefVal);
162
163        if (unitObj) {
164            basisObj = unitObj->getBasis();
165
166            if (basisObj) {
167                result = storeObject_UnitsStr(basisObj->getUnitsName());
168            }
169        }
170    }
171
172    return result;
173}
174
175int
176rp_units_convert_dbl (  char* fromVal,
177                            char* toUnitsName,
178                            double* convResult,
179                            int fromVal_len,
180                            int toUnitsName_len ) {
181
182    char* inFromVal = NULL;
183    char* inToUnitsName = NULL;
184    int result = -1;
185    int showUnits = 0;
186    std::string convStr = "";
187
188    // prepare string for c
189    inFromVal = null_terminate(fromVal,fromVal_len);
190    inToUnitsName = null_terminate(toUnitsName,toUnitsName_len);
191
192    if (inFromVal && inToUnitsName) {
193        // the call to RpUnits::convert() populates the result flag
194        convStr = RpUnits::convert(inFromVal,inToUnitsName,showUnits,&result);
195
196        if (!convStr.empty()) {
197            *convResult = atof(convStr.c_str());
198        }
199    }
200
201    // clean up memory
202    if (inFromVal) {
203        free(inFromVal);
204        inFromVal = NULL;
205    }
206
207    if (inToUnitsName) {
208        free(inToUnitsName);
209        inToUnitsName = NULL;
210    }
211
212    return result;
213}
214
215int
216rp_units_convert_str (      char* fromVal,
217                                char* toUnitsName,
218                                char* retText,
219                                int fromVal_len,
220                                int toUnitsName_len,
221                                int retText_len     ) {
222
223    char* inFromVal = NULL;
224    char* inToUnitsName = NULL;
225    std::string convStr = "";
226    int showUnits = 1;
227    int result = -1;
228
229    // prepare string for c
230    inFromVal = null_terminate(fromVal,fromVal_len);
231    inToUnitsName = null_terminate(toUnitsName,toUnitsName_len);
232
233    if (inFromVal && inToUnitsName) {
234
235        // do the conversion
236        convStr = RpUnits::convert(inFromVal,inToUnitsName,showUnits,&result);
237
238        if (!convStr.empty()) {
239            // prepare string for fortran
240            fortranify(convStr.c_str(), retText, retText_len);
241        }
242    }
243
244    // clean up out memory
245    if (inFromVal) {
246        free(inFromVal);
247        inFromVal = NULL;
248    }
249
250    if (inToUnitsName) {
251        free(inToUnitsName);
252        inToUnitsName = NULL;
253    }
254
255    // return the same result from the RpUnits::convert call.
256    return result;
257}
258
259int
260rp_units_add_presets ( char* presetName, int presetName_len) {
261
262    char* inPresetName = NULL;
263    int result = -1;
264
265    inPresetName = null_terminate(presetName,presetName_len);
266
267    result = RpUnits::addPresets(inPresetName);
268
269    if (inPresetName) {
270        free(inPresetName);
271        inPresetName = NULL;
272    }
273
274    return result;
275}
276
277
278
279
280//**********************************************************************//
281
282
283int
284storeObject_UnitsStr(std::string objectName) {
285
286    int retVal = -1;
287    int dictNextKey = fortObjDictUnits.size() + 1;
288    int newEntry = 0;
289
290    if (objectName != "") {
291        // dictionary returns a reference to the inserted value
292        // no error checking to make sure it was successful in entering
293        // the new entry.
294        fortObjDictUnits.set(dictNextKey,objectName, &newEntry);
295    }
296
297    retVal = dictNextKey;
298    return retVal;
299}
300
301RpUnits*
302getObject_UnitsInt(int objKey) {
303
304    std::string basisName = *(fortObjDictUnits.find(objKey).getValue());
305
306    if (basisName == *(fortObjDictUnits.getNullEntry().getValue())) {
307        // basisName = "";
308        return NULL;
309    }
310
311   return RpUnits::find(basisName);
312
313}
Note: See TracBrowser for help on using the repository browser.