Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Preprocessor automation to define/declare array of strings |
| Date | 2012-08-06 12:51 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <jvosmb$1ct$1@dont-email.me> (permalink) |
| References | <jvojvp$vlg$1@news.newsland.it> <jvolm6$jti$1@dont-email.me> <jvomna$48e$1@news.newsland.it> |
On 8/6/2012 11:10 AM, pozz wrote:
> Eric Sosman ha scritto:
>> On 8/6/2012 10:23 AM, pozz wrote:
>>> I have a lot of array of strings defined in strings.c and declared in
>>> strings.h.
>>>
>>> --- strings.c ---
>>> const char *strlist_month[] = { "Jan", "Feb", ... };
>>> const char *strlist_day[] = { "Mon", "Thu", ... };
>>
>> Calendar reform?
>
> Ooops... :-)
>
>
>>> const char *strlist_onoff[] = { "ON", "OFF" };
>>
>> It's too bad that strlist_onoff[false] is "ON" ...
>
> You are right.
>
>
>>> [...]
>>> As you can understand, maintain synchronized strings.c and strings.h can
>>> be tedious: when I add a list in strings.c, I have to remember to add it
>>> in strings.h also and I have to take care to use the same name.
>>>
>>> I was thinking to automate the declaration/definition of lists of strings
>>> through C preprocessor. For example:
>>>
>>> --- defstrings.def ---
>>> STRINGLIST(strlist_month, { "Jan", "Feb", ... } );
>>> STRINGLIST(strlist_day, { "Mon", "Thu", ... } );
>>> STRINGLIST(strlist_onoff, { "ON", "OFF", ... } );
>>> --- defstrings.c ---
>>> #define STRINGLIST(name, list) const char *name[] = list;
>>> #include "defstrings.def"
>>> --- defstrings.h ---
>>> #define STRINGLIST(name, list) extern const char *name[];
>>>
>>> Have you a better suggestion?
>>
>> You're on the right track, but the execution is faulty. The
>> problem is the "list" piece, for which I think the best solution
>> is a C99-style variadic macro. The two versions would be
>>
>> #define STRINGLIST(name,...)
>> const char *name[] = { __VA_ARGS__ } // BTW, no ;
>> and
>> #define STRINGLIST(name,...)
>> extern const char *name[] // no ;
>>
>> You'd invoke them as
>>
>> STRINGLIST(strlist_onoff, "ON", "OFF", "HEISENBERG");
>
> And what if my "very old microcontroller" compiler doesn't support
> variadic macros?
Then I think you're out of luck.
>> See also Question 10.26 in the FAQ at <http://www.c-faq.com/>.
>
> Ok, I read 10.25. Are you suggesting to migrate toward another
> preprocessor tool instead of insisting with C preprocessor? Is my request
> so far from C preprocessor features?
I'm suggesting that this may be about as far as you want
to try to take the preprocessor, even if it could (with effort)
be taken further. Even taking it this far requires[*] a feature
introduced in C99, so if you can't use C99 capabilities you may
already be beyond the pale.
[*] "Requires" may be too strong. Some people can make the
preprocessor whistle "Dixie" while riding a bicycle backwards
and juggling lit blowtorches (Hallvard Furuseth comes to mind).
Perhaps a wizard can figure out a way to do what you want using
only C90 features, which would mean that "requires" is wrong.
> How can I use m4 to solve this problem?
Dunno; I've only used m4 in "prepackaged" settings. If I
were in your situation, unable to use C99 macros, I'd probably
either bite the bullet and maintain the two files in parallel,
or write a little twenty-line program to parse a single input
file and write .c and .h files as output. Maybe with m4 you
can do it in fewer than twenty lines.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Preprocessor automation to define/declare array of strings pozzugno@gmail.com (pozz) - 2012-08-06 16:23 +0200
Re: Preprocessor automation to define/declare array of strings Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-06 10:51 -0400
Re: Preprocessor automation to define/declare array of strings Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-06 11:01 -0400
Re: Preprocessor automation to define/declare array of strings pozzugno@gmail.com (pozz) - 2012-08-06 17:10 +0200
Re: Preprocessor automation to define/declare array of strings Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-06 12:51 -0400
Re: Preprocessor automation to define/declare array of strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-06 18:27 +0100
Re: Preprocessor automation to define/declare array of strings Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-08-10 11:35 +0300
OT (Re: Preprocessor automation to define/declare array of strings) Mark Bluemel <mark_bluemel@pobox.com> - 2012-08-10 09:56 +0100
Re: OT (Re: Preprocessor automation to define/declare array of strings) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-08-12 02:38 +0300
Re: Preprocessor automation to define/declare array of strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-10 12:45 +0100
Re: Preprocessor automation to define/declare array of strings Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-07 00:16 -0700
Re: Preprocessor automation to define/declare array of strings Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 13:49 -0700
Re: Preprocessor automation to define/declare array of strings Ike Naar <ike@faeroes.freeshell.org> - 2012-08-06 14:55 +0000
Re: Preprocessor automation to define/declare array of strings Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-08-11 06:09 +0000
csiph-web