Xref: utzoo comp.lang.c:36667 comp.software-eng:4957 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!aspect!dave From: dave@aspect.UUCP (Dave Corcoran) Newsgroups: comp.lang.c,comp.software-eng Subject: Re: Source File Organization Summary: yet another possible m4 solution Message-ID: <8001@aspect.UUCP> Date: 1 Mar 91 15:27:25 GMT References: <1991Feb26.045242.23453@rfengr.com> Followup-To: poster Organization: Aspect Telecommunications, San Jose, Ca Lines: 62 In article <1991Feb26.045242.23453@rfengr.com>, rfarris@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.