00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include "gwlib/gwlib.h"
00071 #include "wsp_strings.h"
00072
00073 #define TABLE_SIZE(table) ((long)(sizeof(table) / sizeof(table[0])))
00074
00075 static int initialized;
00076
00077
00078
00079
00080
00081 struct table
00082 {
00083 long size;
00084 Octstr **strings;
00085 long *numbers;
00086 int *versions;
00087 int linear;
00088 };
00089
00090 struct numbered_element
00091 {
00092 char *str;
00093 long number;
00094 int version;
00095 };
00096
00097 struct linear_element
00098 {
00099 char *str;
00100 int version;
00101 };
00102
00103
00104 static Octstr *number_to_string(long number, struct table *table);
00105 static unsigned char *number_to_cstr(long number, struct table *table);
00106 static long string_to_number(Octstr *ostr, struct table *table);
00107 static long string_to_versioned_number(Octstr *ostr, struct table *table, int version);
00108
00109
00110
00111
00112 #define LINEAR(name, strings) \
00113 static const struct linear_element name##_strings[] = { strings }; \
00114 static struct table name##_table;
00115 #define STRING(string) { string, 0 },
00116 #define VSTRING(version, string) { string, version },
00117 #define NUMBERED(name, strings) \
00118 static const struct numbered_element name##_strings[] = { strings }; \
00119 static struct table name##_table;
00120 #define ASSIGN(string, number) { string, number, 0 },
00121 #define VASSIGN(version, string, number) { string, number, version },
00122 #include "wsp_strings.def"
00123
00124
00125 #define LINEAR(name, strings) \
00126 Octstr *wsp_##name##_to_string(long number) { \
00127 return number_to_string(number, &name##_table); \
00128 }
00129 #include "wsp_strings.def"
00130
00131
00132 #define LINEAR(name, strings) \
00133 unsigned char *wsp_##name##_to_cstr(long number) { \
00134 return number_to_cstr(number, &name##_table); \
00135 }
00136 #include "wsp_strings.def"
00137
00138 #define LINEAR(name, strings) \
00139 long wsp_string_to_##name(Octstr *ostr) { \
00140 return string_to_number(ostr, &name##_table); \
00141 }
00142 #include "wsp_strings.def"
00143
00144 #define LINEAR(name, strings) \
00145 long wsp_string_to_versioned_##name(Octstr *ostr, int version) { \
00146 return string_to_versioned_number(ostr, &name##_table, version); \
00147 }
00148 #include "wsp_strings.def"
00149
00150 static Octstr *number_to_string(long number, struct table *table)
00151 {
00152 long i;
00153
00154 gw_assert(initialized);
00155
00156 if (table->linear) {
00157 if (number >= 0 && number < table->size)
00158 return octstr_duplicate(table->strings[number]);
00159 } else {
00160 for (i = 0; i < table->size; i++) {
00161 if (table->numbers[i] == number)
00162 return octstr_duplicate(table->strings[i]);
00163 }
00164 }
00165 return NULL;
00166 }
00167
00168 static unsigned char *number_to_cstr(long number, struct table *table)
00169 {
00170 long i;
00171
00172 gw_assert(initialized);
00173
00174 if (table->linear) {
00175 if (number >= 0 && number < table->size)
00176 return (unsigned char *)octstr_get_cstr(table->strings[number]);
00177 } else {
00178 for (i = 0; i < table->size; i++) {
00179 if (table->numbers[i] == number)
00180 return (unsigned char *)octstr_get_cstr(table->strings[i]);
00181 }
00182 }
00183 return NULL;
00184 }
00185
00186
00187 static long string_to_number(Octstr *ostr, struct table *table)
00188 {
00189 long i;
00190
00191 gw_assert(initialized);
00192
00193 for (i = 0; i < table->size; i++) {
00194 if (octstr_case_compare(ostr, table->strings[i]) == 0) {
00195 return table->linear ? i : table->numbers[i];
00196 }
00197 }
00198
00199 return -1;
00200 }
00201
00202
00203 static long string_to_versioned_number(Octstr *ostr, struct table *table,
00204 int version)
00205 {
00206 long i, ret;
00207
00208 gw_assert(initialized);
00209
00210
00211 ret = -1;
00212 for (i = 0; i < table->size; i++) {
00213 if (octstr_case_compare(ostr, table->strings[i]) == 0 &&
00214 table->versions[i] <= version) {
00215 ret = table->linear ? i : table->numbers[i];
00216 }
00217 }
00218
00219 debug("wsp.strings",0,"WSP: Mapping `%s', WSP 1.%d to 0x%04lx.",
00220 octstr_get_cstr(ostr), version, ret);
00221
00222 return ret;
00223 }
00224
00225 static void construct_linear_table(struct table *table, const struct linear_element *strings,
00226 long size)
00227 {
00228 long i;
00229
00230 table->size = size;
00231 table->strings = gw_malloc(size * (sizeof table->strings[0]));
00232 table->numbers = NULL;
00233 table->versions = gw_malloc(size * (sizeof table->versions[0]));
00234 table->linear = 1;
00235
00236 for (i = 0; i < size; i++) {
00237 table->strings[i] = octstr_imm(strings[i].str);
00238 table->versions[i] = strings[i].version;
00239 }
00240 }
00241
00242 static void construct_numbered_table(struct table *table, const struct numbered_element *strings,
00243 long size)
00244 {
00245 long i;
00246
00247 table->size = size;
00248 table->strings = gw_malloc(size * (sizeof table->strings[0]));
00249 table->numbers = gw_malloc(size * (sizeof table->numbers[0]));
00250 table->versions = gw_malloc(size * (sizeof table->versions[0]));
00251 table->linear = 0;
00252
00253 for (i = 0; i < size; i++) {
00254 table->strings[i] = octstr_imm(strings[i].str);
00255 table->numbers[i] = strings[i].number;
00256 table->versions[i] = strings[i].version;
00257 }
00258 }
00259
00260 static void destroy_table(struct table *table)
00261 {
00262
00263
00264 gw_free(table->strings);
00265 gw_free(table->numbers);
00266 gw_free(table->versions);
00267 }
00268
00269 void wsp_strings_init(void)
00270 {
00271 if (initialized > 0) {
00272 initialized++;
00273 return;
00274 }
00275
00276 #define LINEAR(name, strings) \
00277 construct_linear_table(&name##_table, \
00278 name##_strings, TABLE_SIZE(name##_strings));
00279 #define NUMBERED(name, strings) \
00280 construct_numbered_table(&name##_table, \
00281 name##_strings, TABLE_SIZE(name##_strings));
00282 #include "wsp_strings.def"
00283 initialized++;
00284 }
00285
00286 void wsp_strings_shutdown(void)
00287 {
00288
00289
00290 if (initialized > 1) {
00291 initialized--;
00292 return;
00293 }
00294
00295 #define LINEAR(name, strings) \
00296 destroy_table(&name##_table);
00297 #include "wsp_strings.def"
00298
00299 initialized = 0;
00300 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.