|
9 | 9 | #include <stdio.h> |
10 | 10 | #include <stdlib.h> |
11 | 11 | #include <string.h> |
| 12 | +#include <stdarg.h> |
12 | 13 | #include "ezxml.h" |
13 | 14 | #include "registry_types.h" |
14 | 15 |
|
@@ -263,3 +264,78 @@ int check_persistence(const char * persistence){/*{{{*/ |
263 | 264 | return PERSISTENT; |
264 | 265 | } |
265 | 266 | }/*}}}*/ |
| 267 | + |
| 268 | + |
| 269 | +/****************************************************************************** |
| 270 | + * |
| 271 | + * parse_macros |
| 272 | + * |
| 273 | + * Given an array of strings that are assumed to be in the form of C |
| 274 | + * pre-processor macro definitions, e.g., |
| 275 | + * |
| 276 | + * { "-DMPAS_NAMELIST_SUFFIX=test", |
| 277 | + * "-DSINGLE_PRECISION", |
| 278 | + * "-DHISTORY=Not available" } |
| 279 | + * |
| 280 | + * which could come from the command-line arguments |
| 281 | + * |
| 282 | + * -DMPAS_NAMELIST_SUFFIX=test -DSINGLE_PRECISION -DHISTORY="Not available" |
| 283 | + * |
| 284 | + * this routine parses the macro name and macro definition from each string, |
| 285 | + * and invokes a callback routine with the macro name and definition. The macro |
| 286 | + * name is the name of the macro itself, without the "-D" definition prefix. |
| 287 | + * |
| 288 | + * Any arguments after the macros argument to this function are passed as a |
| 289 | + * va_list to the callback. |
| 290 | + * |
| 291 | + * For the above array of macro definition strings, the callback would be |
| 292 | + * invoked three times with the following arguments: |
| 293 | + * |
| 294 | + * "MPAS_NAMELIST_SUFFIX", "test" |
| 295 | + * "SINGLE_PRECISION", "" |
| 296 | + * "HISTORY", "Not available" |
| 297 | + * |
| 298 | + * The callback function may be NULL. |
| 299 | + * |
| 300 | + * Upon successful completion, a value of 0 is returned. If errors were |
| 301 | + * encountered in parsing macro definition strings, a non-zero value is |
| 302 | + * returned. |
| 303 | + * |
| 304 | + ******************************************************************************/ |
| 305 | +int parse_macros(void(*callback)(const char *macro, const char *val, va_list ap), |
| 306 | + int count, const char **macros, ...) |
| 307 | +{ |
| 308 | + int i; |
| 309 | + |
| 310 | + for (i = 0; i < count; i++) { |
| 311 | + char *tmp; |
| 312 | + char *macrotmp; |
| 313 | + char *macro; |
| 314 | + char *val; |
| 315 | + |
| 316 | + tmp = strdup(macros[i]); |
| 317 | + macrotmp = strtok_r(tmp, "=", &val); |
| 318 | + |
| 319 | + if (macrotmp == NULL || val == NULL) { |
| 320 | + return 1; |
| 321 | + } |
| 322 | + |
| 323 | + if (strstr(macrotmp, "-D") == macrotmp) { |
| 324 | + macro = ¯otmp[2]; |
| 325 | + } else { |
| 326 | + macro = macrotmp; |
| 327 | + } |
| 328 | + |
| 329 | + if (callback != NULL) { |
| 330 | + va_list ap; |
| 331 | + |
| 332 | + va_start(ap, macros); |
| 333 | + callback(macro, val, ap); |
| 334 | + va_end(ap); |
| 335 | + } |
| 336 | + |
| 337 | + free(tmp); |
| 338 | + } |
| 339 | + |
| 340 | + return 0; |
| 341 | +} |
0 commit comments