Unity
0.3
Parser for unit strings
|
00001 #ifndef UNITY_H_LOADED 00002 #define UNITY_H_LOADED 1 00003 /* unitylib.h ... */ 00004 #ifndef UNITYLIB_H_LOADED 00005 #define UNITYLIB_H_LOADED 1 00006 00011 /* forward definition -- see unit-definition.h */ 00012 struct unit_definition; 00013 00017 struct unit_struct { 00022 int prefix_power; /* -24 to +24 */ 00026 const struct unit_definition* base_unit_def; 00031 char* base_unit_string; /* eg Hz or aangstrom */ 00036 float power; 00038 struct unit_struct *next; 00039 }; 00041 typedef struct unit_struct Unit; 00042 00046 typedef struct { 00051 float log_factor; 00055 Unit* unit_sequence; 00056 } UnitExpression; 00057 00058 UnitExpression* unity_parse_string(const char* unit_string, const int syntax); 00059 /* unity_parse_error is defined in lex.lex */ 00068 const char* unity_parse_error(void); 00069 const char** unity_parser_names(void); 00070 00072 #define UNITY_SYNTAX_FITS 0 00073 00074 #define UNITY_SYNTAX_OGIP 1 00075 00076 #define UNITY_SYNTAX_CDS 2 00077 00078 /* The number of syntaxes */ 00079 #define U_NSYNTAXES 3 00080 00081 int unity_identify_parser(const char* parser_name); 00082 const char* unity_parser_name(const int parser_id); 00083 void unity_free_expression(UnitExpression* u); 00084 const char* unity_version_string(); 00085 int unity_version_number(); 00086 00087 #ifdef UNITY_INTERNAL 00088 /* Internal functions */ 00089 Unit* u_new_unit(const char* unitString, const float power); 00090 Unit* u_unit_append(Unit* u1, Unit* u2); 00091 void u_receive_result(float, Unit* unit_sequence); 00092 int u_prefix_to_power(const char pfx); 00093 //char* u_copy_string(const char c, const char* s); 00094 Unit* u_divide_units(Unit* num, Unit* den); 00095 Unit* u_unit_reciprocal(Unit* u); 00096 void u_free_unit(Unit* u); 00097 #endif 00098 00099 #endif 00100 /* ...end unitylib.h */ 00101 /* writers.h ... */ 00102 #ifndef WRITERS_H_LOADED 00103 #define WRITERS_H_LOADED 1 00104 00105 #include <stdio.h> 00106 00107 #ifdef UNITY_INTERNAL 00108 typedef char* (*writef_ptr) (char*, int, const UnitExpression*); 00109 #endif 00110 00111 char* unity_write_formatted(char* buf, int buflen, const UnitExpression*, const char*); 00112 const char** unity_formatter_names(void); 00113 00114 #endif 00115 /* ...end writers.h */ 00116 /* unit-definitions.h ... */ 00117 #ifndef UNIT_DEFINITIONS_H 00118 #define UNIT_DEFINITIONS_H 1 00119 00120 00121 struct unit_definition_syntax { 00122 const char* syntax_name; /* the name of the syntax being described (eg "fits") */ 00123 char may_have_si_prefixes; 00124 char is_deprecated; 00125 const char* preferred_abbrev; /* such as 'm' for metre */ 00126 }; 00127 typedef struct unit_definition_syntax UnitDefSyntax; 00128 00129 struct unit_definition { 00130 const char* abbrev; /* the name of one of the abbrevs for this unit */ 00131 const char* name; /* the name of the unit, such as 'metre' -- not the abbreviation 'm' */ 00132 const char* type; /* type such as 'length' */ 00133 const char* notes; /* for example 'Hz = s-1', NULL if no notes present */ 00134 const char* latex_form; /* an abbrev suitable for LaTeX, if different from a preferred syntax */ 00135 /* There is a unit_definition element for each syntax, whether or 00136 not the unit is recognised. If a syntax does not recognise a 00137 unit, this is indicated by setting syntax_name to NULL. */ 00138 struct unit_definition_syntax syntaxes[U_NSYNTAXES]; 00139 }; 00140 typedef struct unit_definition UnitDef; 00141 00142 const UnitDef* unity_get_unit(const char* abbrev); 00143 00144 #define UNITY_CHECK_RECOGNISED 1 00145 #define UNITY_CHECK_RECOMMENDED 2 00146 #define UNITY_CHECK_CONSTRAINTS 4 00147 #define UNITY_CHECK_ALL UNITY_CHECK_RECOGNISED | UNITY_CHECK_RECOMMENDED | UNITY_CHECK_CONSTRAINTS 00148 int unity_check_unit(const Unit* u, const int syntax, const int flags); 00149 00150 int unity_check_expression(const UnitExpression* ue, const int syntax, const int flags); 00151 00152 #endif /* UNIT_DEFINITIONS_H */ 00153 /* ...end unit-definitions.h */ 00154 #endif