Skip to content

Commit 3bd1cdd

Browse files
committed
Add new function 'parse_macros' to registry tool utility code
The parse_macros function takes an array of strings representing C pre-processing macro definitions (as would be used on the command-line when invoking cpp or a C/C++ compiler) and parses the macro name and macro value, passing those as two arguments to a user-provided callback function. At present, the parse_macros function is not used.
1 parent 04024a6 commit 3bd1cdd

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/tools/registry/utility.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdio.h>
1010
#include <stdlib.h>
1111
#include <string.h>
12+
#include <stdarg.h>
1213
#include "ezxml.h"
1314
#include "registry_types.h"
1415

@@ -263,3 +264,78 @@ int check_persistence(const char * persistence){/*{{{*/
263264
return PERSISTENT;
264265
}
265266
}/*}}}*/
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 = &macrotmp[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+
}

src/tools/registry/utility.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ char * check_packages(ezxml_t registry, char * packages);
1515
char * check_dimensions(ezxml_t registry, char * dims);
1616
char * check_streams(ezxml_t registry, char * streams);
1717
int check_persistence(const char * persistence);
18+
int parse_macros(void(*callback)(const char *macro, const char *val, va_list ap),
19+
int count, const char **macros, ...);

0 commit comments

Comments
 (0)