source: trunk/src/fortran/RpUnits_fortran.c @ 21

Last change on this file since 21 was 21, checked in by dkearney, 19 years ago

created Makefile for compiling rappture libraries.
created README which starts to outline how to compile a rappture library
adjusted #include lines in most .h,.c,.cc files and added Include path

searched to the makefile.

files changed listed below:
M include/core/RpUnits.h
M include/core/RpDict.h
M include/cee/rappture_interface.h
A src/python/PyRpUnits_setup.py
M src/core/RpUnits.cc
M src/fortran/RpUnits_fortran.c
M src/fortran/rappture_fortran.c
M src/cee/rappture_interface.c
M src/cee/RpUnitsCInterface.cc
A src/README
A src/Makefile

File size: 7.3 KB
Line 
1// #include "../include/RpUnitsCInterface.h"
2// #include "../include/RpDictCFInterface.h"
3// #include <stdio.h>
4
5#include "RpUnits.h"
6#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
43extern "C"
44int rp_define_unit(char* unitName, int* basisName, int unitName_len);
45
46extern "C"
47int rp_find(char* searchName, int searchName_len);
48
49extern "C"
50int rp_make_metric(int* basis);
51
52extern "C"
53void rp_get_units(int* unitRefVal, char* retText, int retText_len);
54
55extern "C"
56void rp_get_units_name(int* unitRefVal, char* retText, int retText_len);
57
58extern "C"
59double rp_get_exponent(int* unitRefVal);
60
61extern "C"
62int rp_get_basis(int* unitRefVal);
63
64
65
66
67
68
69
70
71char* null_terminate(char* inStr, int len);
72int storeObject(std::string objectName);
73RpUnits* getObject(int objKey);
74
75
76
77
78#define DICT_TEMPLATE <int,std::string>
79// RpDict<int,std::string>fortObjDict;
80RpDict DICT_TEMPLATE fortObjDict;
81
82int 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
118int 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
140int 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
156void 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
185void 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
215double 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
230int 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
261int 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
278RpUnits* 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*/
292char* 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}
Note: See TracBrowser for help on using the repository browser.