1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2006 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*//** |
19 |
@file |
20 |
Ascend Units Type definitions. |
21 |
*//* |
22 |
by Tom Epperly 8/18/89 |
23 |
Last in CVS: $Revision: 1.10 $ $Date: 1998/02/05 16:38:40 $ $Author: ballan $ |
24 |
*/ |
25 |
|
26 |
#ifndef ASC_UNITS_H |
27 |
#define ASC_UNITS_H |
28 |
|
29 |
#include <stdio.h> |
30 |
#include <utilities/ascConfig.h> |
31 |
#include "compiler.h" |
32 |
#include "fractions.h" |
33 |
#include "dimen.h" |
34 |
|
35 |
/** @addtogroup compiler Compiler |
36 |
@{ |
37 |
*/ |
38 |
|
39 |
#ifdef _HPUX_SOURCE |
40 |
#define ACAST char * |
41 |
#define BCAST char * |
42 |
#else |
43 |
#define ACAST const char * |
44 |
#define BCAST char * |
45 |
#endif |
46 |
/**< to shut up the hp lexer */ |
47 |
|
48 |
/** |
49 |
Value in system units = value in given units * conversion_factor. |
50 |
*/ |
51 |
struct Units { |
52 |
double conversion_factor; /**< to convert from units to system units */ |
53 |
symchar *description; /**< description of units */ |
54 |
CONST dim_type *dim; /**< dimenions of units */ |
55 |
struct Units *next; /**< not for human consumption */ |
56 |
}; |
57 |
|
58 |
/** |
59 |
Temporary structure for parsing unit definitions in ascParse.y. |
60 |
*/ |
61 |
struct UnitDefinition { |
62 |
symchar *new_name; |
63 |
CONST char *unitsexpr; |
64 |
CONST char *filename; |
65 |
int linenum; |
66 |
}; |
67 |
|
68 |
#define UNITS_HASH_SIZE (1023) |
69 |
/**< |
70 |
size of the hash table for unit structs |
71 |
*/ |
72 |
|
73 |
/* |
74 |
* Name of the basic SI units for the 10 dimensions. |
75 |
* If you have better names for them, fix that here. |
76 |
*/ |
77 |
#define UNIT_BASE_MASS "kg" |
78 |
/**< SI base unit symbol for mass. */ |
79 |
#define UNIT_BASE_QUANTITY "mol" |
80 |
/**< SI base unit symbol for quantity. */ |
81 |
#define UNIT_BASE_LENGTH "m" |
82 |
/**< SI base unit symbol for length. */ |
83 |
#define UNIT_BASE_TIME "s" |
84 |
/**< SI base unit symbol for time. */ |
85 |
#define UNIT_BASE_TEMPERATURE "K" |
86 |
/**< SI base unit symbol for temperature. */ |
87 |
#define UNIT_BASE_CURRENCY "USD" |
88 |
/**< Base unit (non SI) for currency (US dollar) */ |
89 |
#define UNIT_BASE_ELECTRIC_CURRENT "A" |
90 |
/**< SI base unit symbol for current. */ |
91 |
#define UNIT_BASE_LUMINOUS_INTENSITY "cd" |
92 |
/**< SI base unit symbol for luminosity. */ |
93 |
#define UNIT_BASE_PLANE_ANGLE "rad" |
94 |
/**< Base unit (non SI) for plane angle (radian). */ |
95 |
#define UNIT_BASE_SOLID_ANGLE "sr" |
96 |
/**< Base unit (non SI) for solid angle (steradian). */ |
97 |
|
98 |
ASC_DLLSPEC struct Units *g_units_hash_table[]; |
99 |
/**< |
100 |
* The hash table for unit structs. |
101 |
*/ |
102 |
|
103 |
extern void InitUnitsTable(void); |
104 |
/**< |
105 |
This routine initializes some internal variables, so that all the |
106 |
other units functions may be called. It must be called once and |
107 |
only once when the program is starting up. |
108 |
Must be called after dimensions table initiatialization. |
109 |
*/ |
110 |
|
111 |
extern void DestroyUnitsTable(void); |
112 |
/**< |
113 |
This routine can be called to deallocate all of the units in the table. |
114 |
*/ |
115 |
|
116 |
extern struct UnitDefinition *CreateUnitDef(symchar *new_name, |
117 |
CONST char *unitsexpr, |
118 |
CONST char *filename, |
119 |
int linenum); |
120 |
/**< |
121 |
* Create a new unit definition. |
122 |
* @param new_name Should be from the symbol table. |
123 |
* @param unitsexpr A string that we will copy, so it does not |
124 |
* need to be persistent. |
125 |
* @param filename Should be persistent. |
126 |
* @param linenum Line number. |
127 |
*/ |
128 |
|
129 |
extern void DestroyUnitDef(struct UnitDefinition *udp); |
130 |
/**< |
131 |
* Destroys udp and its unitsexpr. |
132 |
*/ |
133 |
|
134 |
extern void ProcessUnitDef(struct UnitDefinition *udp); |
135 |
/**< |
136 |
* Attempts to add the info in udp to the units table. |
137 |
* messages to ascerr if not possible. |
138 |
*/ |
139 |
|
140 |
ASC_DLLSPEC CONST struct Units*LookupUnits(CONST char *c); |
141 |
/**< |
142 |
* Check the units library for units with a description string which |
143 |
* matches c. If it is found, this function will return a non-NULL pointer; |
144 |
* otherwise, it returns NULL to indicate that units c are undefined. |
145 |
* c should not contain any blanks. |
146 |
*/ |
147 |
|
148 |
extern CONST struct Units *DefineUnits(symchar *c, double conv, CONST dim_type *dim); |
149 |
/**< |
150 |
* Define the units c with conversion factor conv and dimensions *dim. |
151 |
* This assumes that *dim was the value returned by FindOrAddDim. This |
152 |
* will check to prevent duplicate entries. The resulting unit structure is |
153 |
* returned. If you enter a duplicate entry and the dimensions or conversion |
154 |
* factor don't match, this function will return NULL. In addition to |
155 |
* the user defined units there is a wild units type which conversion equal |
156 |
* to one and wild dimensions. It is given the name "?".<br><br> |
157 |
* |
158 |
* c should not contain any spaces! It may add c to the symbol table if |
159 |
* it is not already stored there. |
160 |
* |
161 |
* @bug Memory leak if hitting an empty hash bucket. buckets never deallocated |
162 |
* except at shutdown anyway, so not a big deal. 7bytes/hit. Don't off hand |
163 |
* know where to fix it. BAA 6-94 |
164 |
*/ |
165 |
|
166 |
ASC_DLLSPEC CONST struct Units*FindOrDefineUnits(CONST char *c, |
167 |
unsigned long * CONST pos, |
168 |
int * CONST error_code); |
169 |
/**< |
170 |
* This function will attempt to parse the string c into a units |
171 |
* description. If the unit type has been defined before, the corresponding |
172 |
* units pointer will be returned. If this type hasn't been defined before, |
173 |
* it will be defined and that pointer will be returned. If it is |
174 |
* unable to parse this string it will return NULL. CheckUnitStr, below, |
175 |
* can be used to diagnose why the unit didn't parse. |
176 |
* This will not modify c in any way. It may add c to the symbol table. |
177 |
* <pre> |
178 |
* RETURN VALUE OF error_code |
179 |
* 0 string is okay, value of pos not specified |
180 |
* 1 undefined unit used in string, pos indicates the first |
181 |
* letter of the first occurence of the undefined unit |
182 |
* 2 unbalanced parenthesis, pos indicates the opening parenthesis |
183 |
* that wasn't closed |
184 |
* 3 illegal character, pos indicates the position of the offending |
185 |
* character |
186 |
* 4 illegal real value, pos indicates the first character of the |
187 |
* real value |
188 |
* 5 oversized identifier or real, pos indicates the start of the |
189 |
* offending token |
190 |
* 6 operator left out real followed by identifier or vice |
191 |
* versa, pos indicates where an operator should have been |
192 |
* inserted |
193 |
* 7 term missing after *,/, or (. |
194 |
* 8 missing term before *,/. pos is left at the operator |
195 |
* 9 too many closing parens. pos is left at the extra paren. |
196 |
* 10 bad fraction exponent. pos is at the left of the fraction field |
197 |
* 11 incompatible unit redefinition. (internal use only; not seen). |
198 |
* </pre> |
199 |
*/ |
200 |
|
201 |
ASC_DLLSPEC char**UnitsExplainError(CONST char *unitsexpr, int code, int pos); |
202 |
/**< |
203 |
* Returns an array of strings which may be helpful in |
204 |
* explaining the error. |
205 |
* - errv[0] is a message. |
206 |
* - errv[1] is the unitsexpr. |
207 |
* - errv[2] is a pointer to the error -------^ line. |
208 |
* aligned with unitsexpr given. |
209 |
* The user should never change or free errv or its content, |
210 |
* nor should the user keep the pointers. |
211 |
*/ |
212 |
|
213 |
#define UnitsDescription(u) ((u)->description) |
214 |
/**< |
215 |
* Returns the string description attribute of a units structure. |
216 |
*/ |
217 |
|
218 |
#define UnitsConvFactor(u) ((u)->conversion_factor) |
219 |
/**< |
220 |
* Returns the conversion factor for a given units structure. |
221 |
*/ |
222 |
|
223 |
#define UnitsDimensions(u) ((u)->dim) |
224 |
/**< |
225 |
* Returns the dimensions of the units structure. |
226 |
*/ |
227 |
|
228 |
ASC_DLLSPEC char *UnitsStringSI(struct Units *up); |
229 |
/**< |
230 |
* Returns the SI form of the units for the dimensionality of up. |
231 |
* Wild = *, Dimensionless = "", NULL up --> NULL return. |
232 |
* Caller is responsible for freeing the string returned. |
233 |
*/ |
234 |
|
235 |
ASC_DLLSPEC void DumpUnits(FILE *f); |
236 |
/**< Dump all defined units to f. */ |
237 |
|
238 |
/* @} */ |
239 |
|
240 |
#endif /* ASC_UNITS_H */ |
241 |
|