Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #63603 > unrolled thread

Auto-generate C code for initialized structs

Started bypozz <pozzugno@gmail.com>
First post2015-06-19 16:22 +0200
Last post2015-06-19 18:20 -0600
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c


Contents

  Auto-generate C code for initialized structs pozz <pozzugno@gmail.com> - 2015-06-19 16:22 +0200
    Re: Auto-generate C code for initialized structs Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-06-19 17:40 +0200
    Re: Auto-generate C code for initialized structs luser droog <luser.droog@gmail.com> - 2015-06-19 09:27 -0700
    Re: Auto-generate C code for initialized structs Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-19 18:20 -0600

#63603 — Auto-generate C code for initialized structs

Frompozz <pozzugno@gmail.com>
Date2015-06-19 16:22 +0200
SubjectAuto-generate C code for initialized structs
Message-ID<mm18gg$n2f$1@dont-email.me>
For embedded platforms, it's very important to use const keyword 
whenever possible, so the linker can allocate constant data in a 
read-only memory (usually a Flash memory), saving precious RAM space.

So I often write code similar to the following:

   typedef struct {
     char *title;
     int (*get)(void);
     void (*set)(int new_value);
   } menuitem_t;
   typedef struct {
     char *title;
     menuitm_t *itm_list;
     size_t itm_num;
   } menu_t;

   const menuitem_t items1[] = {
     { "Item 1_1", get_1_1, set_1_1 },
     { "Item 1_2", get_1_2, set_1_2 },
   };
  const menuitem_t items2[] = {
     { "Item 2_1", get_2_1, NULL },
     { "Item 2_2", get_2_2, set_2_2 },
     { "Item 3_2", get_3_2, set_3_2 },
   };

   const menu_t mnu1 = { "Menu 1", items1, 2 };
   const menu_t mnu2 = { "Menu 2", items2, 3 };

If the data structures are more complex, the initialization begin to be 
complex to write and maintain.

So I was thinking it would be nice to have a non-C data file, simple to 
write and maintain, as a starting point. And a small tool that converts 
the data file to a C code.

Any suggestions on the data file and the transformation tool?

I was reading about XSLT/XML, but I couldn't find a read-to-use XSL code 
for C code generation.

[toc] | [next] | [standalone]


#63611

FromLőrinczy Zsigmond <zsiga@nospam.for.me>
Date2015-06-19 17:40 +0200
Message-ID<mm1d86$2k4$1@speranza.aioe.org>
In reply to#63603
For simple cases 'awk' will do, for more complicated ones it is yacc/bison

[toc] | [prev] | [next] | [standalone]


#63619

Fromluser droog <luser.droog@gmail.com>
Date2015-06-19 09:27 -0700
Message-ID<9e7cfbbc-61cd-4982-9df5-664a3755435e@googlegroups.com>
In reply to#63603
On Friday, June 19, 2015 at 9:22:31 AM UTC-5, pozz wrote:
> For embedded platforms, it's very important to use const keyword 
> whenever possible, so the linker can allocate constant data in a 
> read-only memory (usually a Flash memory), saving precious RAM space.
> 
> So I often write code similar to the following:
> 
>    typedef struct {
>      char *title;
>      int (*get)(void);
>      void (*set)(int new_value);
>    } menuitem_t;
>    typedef struct {
>      char *title;
>      menuitm_t *itm_list;
>      size_t itm_num;
>    } menu_t;
> 
>    const menuitem_t items1[] = {
>      { "Item 1_1", get_1_1, set_1_1 },
>      { "Item 1_2", get_1_2, set_1_2 },
>    };
>   const menuitem_t items2[] = {
>      { "Item 2_1", get_2_1, NULL },
>      { "Item 2_2", get_2_2, set_2_2 },
>      { "Item 3_2", get_3_2, set_3_2 },
>    };
> 
>    const menu_t mnu1 = { "Menu 1", items1, 2 };
>    const menu_t mnu2 = { "Menu 2", items2, 3 };
> 
> If the data structures are more complex, the initialization begin to be 
> complex to write and maintain.
> 
> So I was thinking it would be nice to have a non-C data file, simple to 
> write and maintain, as a starting point. And a small tool that converts 
> the data file to a C code.
> 
> Any suggestions on the data file and the transformation tool?
> 
> I was reading about XSLT/XML, but I couldn't find a read-to-use XSL code 
> for C code generation.

You may be able to adapt the idea I describe here (X-Macros)
and just use the preprocessor:
http://stackoverflow.com/a/22745618/733077

[toc] | [prev] | [next] | [standalone]


#63646

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2015-06-19 18:20 -0600
Message-ID<1blhfftema.fsf@pfeifferfamily.net>
In reply to#63603
pozz <pozzugno@gmail.com> writes:
>
> So I was thinking it would be nice to have a non-C data file, simple
> to write and maintain, as a starting point. And a small tool that
> converts the data file to a C code.
>
> Any suggestions on the data file and the transformation tool?
>
> I was reading about XSLT/XML, but I couldn't find a read-to-use XSL
> code for C code generation.

TSV (tab-separated-values).  Easy to maintain in any spreadsheet
program, *really* easy to parse and generate your C code.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web