Source File Organization
Dave Corcoran
dave at aspect.UUCP
Sat Mar 2 02:27:25 AEST 1991
In article <1991Feb26.045242.23453 at rfengr.com>, rfarris at rfengr.com (Rick Farris) writes:
> I have an enumerated type:
>
> typedef enum { A, B, C, D } CMD;
>
> and a corresponding array of ascii representations :
>
> char ltrs[] = { 'A', 'B', 'C', 'D' };
>
> Suppose I add a new CMD, "Z", is there any way to ensure
> that ltrs[] is updated?
>
run this through m4 you might be pleasantly suprised; although anyone
maintaining your code may what to drop the adjective "pleasantly" :-)
define(awkdef,
`syscmd((echo ' '$2' | sed 's/^(//;s/^)//' `>/tmp/am_$1))'
`define($1,`syscmd(awk $'`2 -f /tmp/am_$1 ifelse($'`1,,,<<@@
$'`1
@@))')'
)
awkdef(xx,( # note the leading (
BEGIN {printf "enum {"}
{
for (i=1;i<=NF;i++) printf "%s,",$i
}
END {print "} CMD;"}
)) /* note the first paren MUST be flush against the left margin */
/* note the '\'' construct is needed to get a single ' in the output */
awkdef(yy,(
BEGIN {printf "char ltrs[] = {"}
{
for (i=1;i<=NF;i++) printf "'\''%s'\'',",$i
}
END {print "};"}
))
define(letters,`xx($1)yy($1)')
letters(A B C D E F G H I J K L M)
/* if you need conditional compliation */
define(`letters',`ifdef(`header_file',`yy($1)',`xx($1)')')
/* this next line would be in a .c source */
letters(A B C)
/* these two would be in a .h header */
define(header_file)
letters(A B C)
--
David Corcoran -@@
uunet!aspect!dave ~
In a society where anything goes eventually everything will.
More information about the Comp.lang.c
mailing list