1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * INTERFACE: C Rappture Units Source |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
7 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
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 "RpUnitsCInterface.h" |
---|
16 | |
---|
17 | const RpUnits* |
---|
18 | rpDefineUnit(const char* unitSymbol, const RpUnits* basis) { |
---|
19 | |
---|
20 | return RpUnits::define(unitSymbol, basis); |
---|
21 | } |
---|
22 | |
---|
23 | const RpUnits* |
---|
24 | rpDefineConv( const RpUnits* fromUnit, |
---|
25 | const RpUnits* toUnit, |
---|
26 | double (*convForwFxnPtr)(double), |
---|
27 | double (*convBackFxnPtr)(double) ) { |
---|
28 | |
---|
29 | return RpUnits::define(fromUnit,toUnit,convForwFxnPtr,convBackFxnPtr); |
---|
30 | } |
---|
31 | |
---|
32 | const RpUnits* |
---|
33 | rpFind ( const char* key ) { |
---|
34 | |
---|
35 | return RpUnits::find(key); |
---|
36 | } |
---|
37 | |
---|
38 | const char* |
---|
39 | rpGetUnits ( const RpUnits* unit ) { |
---|
40 | |
---|
41 | static std::string retVal; |
---|
42 | retVal = unit->getUnits(); |
---|
43 | return retVal.c_str(); |
---|
44 | } |
---|
45 | |
---|
46 | const char* |
---|
47 | rpGetUnitsName ( const RpUnits* unit ) { |
---|
48 | |
---|
49 | static std::string retVal; |
---|
50 | retVal = unit->getUnitsName(); |
---|
51 | return retVal.c_str(); |
---|
52 | } |
---|
53 | |
---|
54 | double |
---|
55 | rpGetExponent ( const RpUnits* unit ) { |
---|
56 | |
---|
57 | return unit->getExponent(); |
---|
58 | } |
---|
59 | |
---|
60 | const RpUnits* |
---|
61 | rpGetBasis ( const RpUnits* unit ) { |
---|
62 | |
---|
63 | return unit->getBasis(); |
---|
64 | } |
---|
65 | |
---|
66 | const char* |
---|
67 | rpConvert ( const char* fromVal, |
---|
68 | const char* toUnitsName, |
---|
69 | int showUnits, |
---|
70 | int* result ) { |
---|
71 | |
---|
72 | static std::string retVal; |
---|
73 | const char *empty = ""; |
---|
74 | |
---|
75 | if (fromVal == NULL) { |
---|
76 | return retVal.c_str(); |
---|
77 | } |
---|
78 | |
---|
79 | if (toUnitsName == NULL) { |
---|
80 | toUnitsName = empty; |
---|
81 | } |
---|
82 | |
---|
83 | retVal = RpUnits::convert(fromVal,toUnitsName,showUnits,result); |
---|
84 | return retVal.c_str(); |
---|
85 | } |
---|
86 | |
---|
87 | const char* |
---|
88 | rpConvertStr ( const char* fromVal, |
---|
89 | const char* toUnitsName, |
---|
90 | int showUnits, |
---|
91 | int* result ) { |
---|
92 | |
---|
93 | static std::string retVal; |
---|
94 | const char *empty = ""; |
---|
95 | |
---|
96 | if (fromVal == NULL) { |
---|
97 | return retVal.c_str(); |
---|
98 | } |
---|
99 | |
---|
100 | if (toUnitsName == NULL) { |
---|
101 | toUnitsName = empty; |
---|
102 | } |
---|
103 | |
---|
104 | retVal = RpUnits::convert(fromVal,toUnitsName,showUnits,result); |
---|
105 | return retVal.c_str(); |
---|
106 | } |
---|
107 | |
---|
108 | const char* |
---|
109 | rpConvert_ObjStr ( const RpUnits* fromUnits, |
---|
110 | const RpUnits* toUnits, |
---|
111 | double val, |
---|
112 | int showUnits, |
---|
113 | int* result ) { |
---|
114 | |
---|
115 | static std::string retVal; |
---|
116 | retVal = fromUnits->convert(toUnits,val,showUnits,result); |
---|
117 | return retVal.c_str(); |
---|
118 | } |
---|
119 | |
---|
120 | double |
---|
121 | rpConvertDbl ( const char* fromVal, |
---|
122 | const char* toUnitsName, |
---|
123 | int* result ) { |
---|
124 | |
---|
125 | std::string convStr; |
---|
126 | double retVal = 0.0; |
---|
127 | const char *empty = ""; |
---|
128 | |
---|
129 | if (fromVal == NULL) { |
---|
130 | return retVal; |
---|
131 | } |
---|
132 | |
---|
133 | if (toUnitsName == NULL) { |
---|
134 | toUnitsName = empty; |
---|
135 | } |
---|
136 | |
---|
137 | convStr = RpUnits::convert(fromVal,toUnitsName,0,result); |
---|
138 | |
---|
139 | if (!convStr.empty()) { |
---|
140 | retVal = atof(convStr.c_str()); |
---|
141 | } |
---|
142 | |
---|
143 | return retVal; |
---|
144 | } |
---|
145 | |
---|
146 | double |
---|
147 | rpConvert_ObjDbl ( const RpUnits* fromUnits, |
---|
148 | const RpUnits* toUnits, |
---|
149 | double val, |
---|
150 | int* result ) { |
---|
151 | |
---|
152 | return fromUnits->convert(toUnits,val,result); |
---|
153 | } |
---|
154 | |
---|
155 | int rpAddPresets ( const char* presetName ) { |
---|
156 | |
---|
157 | return RpUnits::addPresets(presetName); |
---|
158 | } |
---|
159 | |
---|