The simplest way to do this would be to use a custom preprocessing script, which you could integrate into your build process.
I don't know what tools you have available, but you could easily use awk
as a preprocessor. If you're using make
, you could build the c
file automatically from a skeleton: (See below for the original awk script.)
file.c: file.c.in
awk ' \
/^#define X / { \
for (i = 1; i <= $$3; ++i) \
s = s "\"repr"i"\\0\"" \
} \
match($$0, / *const char \*reprx\[\] = /) { \
$$0 = substr($$0, 1, RLENGTH) s ";" \
} \
1' $^ > $@
Example:
$ cat file.c.in
/* The beginning of the program */
#define X 7
/* Some part of the program */
const char *reprx[] = "Will be replaced";
/* The rest of the program */
$ make -s file.c
$ cat file.c
/* The beginning of the program */
#define X 7
/* Some part of the program */
const char *reprx[] = "repr1\0""repr2\0""repr3\0""repr4\0""repr5\0""repr6\0""repr7\0";
/* The rest of the program */
Since it is a little obscured by the need to escape dollar signs and line endings, here's the original awk program:
awk '/^#define X / {
for (i = 1; i <= $3; ++i)
s = s "\"repr"i"\\0\""
}
match($0, / *const char \*reprx\[\] = /) {
$0 = substr($0, 1, RLENGTH) s ";"
}
1'