source: trunk/src/cee/RpUnitsCInterface.cc @ 568

Last change on this file since 568 was 568, checked in by dkearney, 17 years ago

reworking of the Rappture Units code.
Rappture Units no longer creates an object for each metric extension of every
metric unit. Now the metric prefixes are kept in the dictionary as unit objects,
and when a unit is parsed out of the string received from the user, the unit
is checked to see if it acccepts metric prefixes, if so, a search is done for
the prefix. This allows Rappture Units code to more easily tackle case insensitive
unit name searches. Eventually the prefixes will be removed as direct Rappture Units objects and be turned into (possibly a derived object) rappture units prefix objects.

The find function of the core Rappture Units code now accepts
a "hints" function pointer. The function pointer is executed by the dictionary when
the dictionary thinks it found a matching object. This allows the user of the
dictionary to insert different objects with the same key. For Rappture Units,
multiple units objects with the same key can be inserted into the dictionary.
This is important for units like "A" which could stand for angstroms or amperes.

Additionally, the "make metric" functions were removed because the idea of being a
metric unit has been reduced to a flag inside the Rappture Units object and some
newly added logic. The role of setting the metric flag has been added to the define()
function as an additional function input. The fortran and c code has been updated to
reflect the removal of the function. No updates were made to the define function in
the fortran and c code.

The units.test file was also updated with new tests. Old tests were updated, removing
derived units from the list of compatible units. When searching for micro-meters (um),
compatible units of (A,in,m) will be provided instead of (A,in,m,um).

File size: 3.1 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  INTERFACE: C 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 "RpUnitsCInterface.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21const RpUnits*
22rpDefineUnit(const char* unitSymbol, const RpUnits* basis) {
23
24    return RpUnits::define(unitSymbol, basis);
25}
26
27const RpUnits*
28rpDefineConv(  const RpUnits* fromUnit,
29               const RpUnits* toUnit,
30               double (*convForwFxnPtr)(double),
31               double (*convBackFxnPtr)(double)    ) {
32
33    return RpUnits::define(fromUnit,toUnit,convForwFxnPtr,convBackFxnPtr);
34}
35
36const RpUnits*
37rpFind ( const char* key ) {
38
39    return RpUnits::find(key);
40}
41
42const char*
43rpGetUnits ( const RpUnits* unit ) {
44
45    static std::string retVal;
46    retVal = unit->getUnits();
47    return retVal.c_str();
48}
49
50const char*
51rpGetUnitsName ( const RpUnits* unit ) {
52
53    static std::string retVal;
54    retVal = unit->getUnitsName();
55    return retVal.c_str();
56}
57
58double
59rpGetExponent ( const RpUnits* unit ) {
60
61    return unit->getExponent();
62}
63
64const RpUnits*
65rpGetBasis ( const RpUnits* unit ) {
66
67    return unit->getBasis();
68}
69
70const char*
71rpConvert (   const char* fromVal,
72               const char* toUnitsName,
73               int showUnits,
74               int* result ) {
75
76    static std::string retVal;
77    retVal = RpUnits::convert(fromVal,toUnitsName,showUnits,result);
78    return retVal.c_str();
79}
80
81const char*
82rpConvertStr (   const char* fromVal,
83                  const char* toUnitsName,
84                  int showUnits,
85                  int* result ) {
86
87    static std::string retVal;
88    retVal = RpUnits::convert(fromVal,toUnitsName,showUnits,result);
89    return retVal.c_str();
90}
91
92const char*
93rpConvert_ObjStr ( const RpUnits* fromUnits,
94                   const RpUnits* toUnits,
95                   double val,
96                   int showUnits,
97                   int* result ) {
98
99    static std::string retVal;
100    retVal = fromUnits->convert(toUnits,val,showUnits,result);
101    return retVal.c_str();
102}
103
104double
105rpConvertDbl (    const char* fromVal,
106                  const char* toUnitsName,
107                  int* result ) {
108
109    std::string convStr;
110    double retVal = 0.0;
111
112    convStr = RpUnits::convert(fromVal,toUnitsName,0,result);
113
114    if (!convStr.empty()) {
115        retVal = atof(convStr.c_str());
116    }
117
118    return retVal;
119}
120
121double
122rpConvert_ObjDbl (   const RpUnits* fromUnits,
123                     const RpUnits* toUnits,
124                     double val,
125                     int* result ) {
126
127    return fromUnits->convert(toUnits,val,result);
128}
129
130int rpAddPresets ( const char* presetName ) {
131
132    return RpUnits::addPresets(presetName);
133}
134
135#ifdef __cplusplus
136}
137#endif
Note: See TracBrowser for help on using the repository browser.