Unity 0.6
Parser for unit strings
|
00001 #ifndef UNITY_H_LOADED 00002 #define UNITY_H_LOADED 1 00003 /* From known-syntaxes.h */ 00004 /* KNOWN_SYNTAXES+ */ 00010 #define UNITY_SYNTAX_FITS 0 00011 00016 #define UNITY_SYNTAX_OGIP 1 00017 00022 #define UNITY_SYNTAX_CDS 2 00023 00028 #define UNITY_SYNTAX_VOU 3 00029 #ifdef UNITY_INTERNAL 00030 /* Number of syntaxes */ 00031 #define U_NSYNTAXES 4 00032 #endif 00033 /* KNOWN_SYNTAXES- */ 00034 00035 /* From unitylib.h ... */ 00036 #ifndef UNITYLIB_H_LOADED 00037 #define UNITYLIB_H_LOADED 1 00038 00043 /* forward definition -- see unit-definition.h */ 00044 struct unit_definition; 00045 00050 struct unit_struct { 00055 int prefix_power; /* -24 to +24 */ 00061 const struct unit_definition* base_unit_def; 00066 char* base_unit_string; /* eg Hz or aangstrom */ 00071 float power; 00073 struct unit_struct *next; 00074 }; 00076 typedef struct unit_struct Unit; 00077 00081 typedef struct { 00086 float log_factor; 00090 const Unit* unit_sequence; 00091 } UnitExpression; 00092 00093 const UnitExpression* unity_parse_string(const char* unit_string, const int syntax); 00094 00095 /* unity_parse_error is defined in lex.lex */ 00106 const char* unity_parse_error(void); 00107 00108 const char** unity_parser_names(void); 00109 00110 int unity_identify_parser(const char* parser_name); 00111 const char* unity_parser_name(const int parser_id); 00112 void unity_free_expression(const UnitExpression* u); 00113 const char* unity_version_string(); 00114 int unity_version_number(); 00115 00116 /* Checking units */ 00118 #define UNITY_CHECK_RECOGNISED 1 00119 00120 #define UNITY_CHECK_RECOMMENDED 2 00121 00122 #define UNITY_CHECK_CONSTRAINTS 4 00123 00128 #define UNITY_CHECK_ALL UNITY_CHECK_RECOGNISED | UNITY_CHECK_RECOMMENDED | UNITY_CHECK_CONSTRAINTS 00129 int unity_check_unit(const Unit* u, const int syntax, const int flags); 00130 int unity_check_expression(const UnitExpression* ue, const int syntax, const int flags); 00131 00132 #ifdef UNITY_INTERNAL 00133 /* Internal functions */ 00134 Unit* u_new_unit(const char* unitString, const float power, int syntax); 00135 Unit* u_unit_append(Unit* u1, Unit* u2); 00136 void u_receive_result(float, const Unit* unit_sequence); 00137 int u_prefix_to_power(const char pfx); 00138 //char* u_copy_string(const char c, const char* s); 00139 Unit* u_divide_units(Unit* num, Unit* den); 00140 Unit* u_unit_reciprocal(Unit* u); 00141 void u_free_unit(const Unit* u); 00142 #endif 00143 00144 #endif 00145 /* ...end unitylib.h */ 00146 00147 /* From writers.h ... */ 00148 #ifndef WRITERS_H_LOADED 00149 #define WRITERS_H_LOADED 1 00150 00151 #include <stdio.h> 00152 00153 #ifdef UNITY_INTERNAL 00154 typedef char* (*writef_ptr) (char*, int, const UnitExpression*); 00155 #endif 00156 00157 char* unity_write_formatted(char* buf, int buflen, const UnitExpression*, const char*); 00158 const char** unity_formatter_names(void); 00159 00160 #endif 00161 /* ...end writers.h */ 00162 00163 /* From unit-definitions.h ... */ 00164 #ifndef UNIT_DEFINITIONS_H 00165 #define UNIT_DEFINITIONS_H 1 00166 00167 00168 /* The functions exported by this module are the only ones which need 00169 access to the data structures dumped into this file, which are 00170 potentially subject to change. The implementation of this header 00171 file is generated by ../grammar/ParseUnits.java, using the 00172 unit-definitions.c.template file in that directory. 00173 00174 The structure elements with leading underscores are intended to be internal to 00175 this unit-definition.c module. 00176 */ 00177 00178 struct unit_definition { 00179 const char* uri; /* the name for this unit, as a URI */ 00180 int _idx; /* the internal index for this unit, in the list of unit_definition entries */ 00181 const char* name; /* the name of the unit, such as 'Metre' -- not the abbreviation 'm' */ 00182 const char* type; /* type such as 'length', as a URI */ 00183 const char* dimension; /* the dimensions of this unit, as a string, eg "M L-2" */ 00184 const char* description; /* for example 'Hz = s-1', NULL if no notes present */ 00185 const char* latex_form; /* an abbrev suitable for LaTeX, if different from a preferred syntax */ 00186 }; 00187 typedef struct unit_definition UnitDef; 00188 00189 #if UNITY_INTERNAL 00190 /* A unit_representation indicates how a particular unit may be used in a particular syntax. */ 00191 struct unit_representation { 00192 const char* symbol; /* a symbol for this unit, in one syntax */ 00193 int _unit_index; /* the index in the array of unit_definition entries */ 00194 int si_prefixes_p; /* true if this unit may be given SI prefixes */ 00195 int is_deprecated_p; /* true if this unit is deprecated */ 00196 int is_preferred_p; /* true if this unit is the preferred unit amongst some alternatives */ 00197 }; 00198 typedef struct unit_representation UnitRep; 00199 UnitRep* u_get_unit_representation(const Unit*, const int syntax); 00200 #endif 00201 00202 const UnitDef* unity_get_unit(const char* abbrev, int syntax); 00203 const char* unity_get_syntax_name(int syntax); 00204 const char* unity_get_unit_uri(const UnitDef*); 00205 const char* unity_get_unit_name(const UnitDef*); 00206 const char* unity_get_unit_type(const UnitDef*); 00207 const char* unity_get_unit_dimension(const UnitDef*); 00208 const char* unity_get_unit_description(const UnitDef*); 00209 const char* unity_get_unit_latex_form(const UnitDef*); 00210 #endif /* UNIT_DEFINITIONS_H */ 00211 /* ...end unit-definitions.h */ 00212 #endif