Code auto generation tools?

This is a discussion on Code auto generation tools? within the C Programming forums, part of the General Programming Boards category; Does anyone know of tools or C libraries that might generate the following as C variable declarations: 1. static const ...

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Cool Code auto generation tools?

    Does anyone know of tools or C libraries that might generate the following as C variable declarations:

    1. static const char tables of possibly varying sizes initialized with binary data in the form of escape sequences. Eg. const char s[] ="\xE9a_00\0\0\0T.....";
    The data is read either from binary file, or a user might provide a custom s[i] function pointer.

    2. Auto generated #define PREFIX_SUFFIX[i] flag constants. reading a list of suffixes as a simple word file, and possibly accounting for a start of the flag defines.
    Eg. :
    Code:
    #define IOMODE_READ   (1<<1) 
    #define IOMODE_WRITE (1<<2)
    ...
    3. Various other long repetitive declarations that can't be simulated with preprocessor commands.

    Would anyone have any use for such a tool? I tried Googling for anything ready made but found nothing. I was considering to make a small C library to handle the situation, but Perl seemed a more appropriate tool for the job. Still since i can cover my needs with more or less short hack-code, it didn't seem like i should bother much.
    Are sparse constant array declarations a possibility in C?
    Came up with the need when i needed some compressed data within the source file.
    I wasn't happy with my solution, since i dropped the compression to represent the escaped values in the char []. Does anyone know a better way to do this?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1 & 2: I'm not aware of a tool that does that. It sounds like both 1 & 2 could be cobbled up with a reasonable size of either C-code or Perl-script (or similar).

    Further on 2: But reading from a word-document is hard to read - perhaps you could save the file as text first, then look for your "marker word(s)" and extract the data to make C-code.

    C does not support sparse arrays. You can of course solve it in the generic way: One more indirection, e.g. if you need 500 char arrays of 2000 bytes each, but indexes range from 1 to 10000, you may not want to "waste" 20MB on the case, so use an array of char-pointers, and only set the indices that you want. You can do that using for example an initilized struct array, e.g.
    Code:
    struct {
       int index;
       char *val;
    } = { 
      {  1, "String 1" },
      {  12, "String 12" }, 
      {  41, "String 41" },
      { -1, NULL  } // end marker 
    }
    You can of course use various schemes to compress data, such as "shorter replacement", e.g. replac "This string repeats often" with "#1#" - whenever you see "#1#" in the input text, you replace it with "This string repeats often". You can either write a substring search&replace function that figures out what occurs how often, and replaces it with the relevant string, or do it by hand if you know from the start what strings are common.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by matsp View Post
    C does not support sparse arrays. You can of course solve it in the generic way.
    --Mats
    Thanks for the answer. That need arose when i needed a small (8x8) array with just about 10-12 elements non zero. I tried to think if there was a workaround on this to reduce the constant initializer declaration length.
    It tried to use:
    Code:
    #define K(PREFIX, SUFFIX, N) #define PREFIX##_##SUFFIX (1<<N)
    #define PREFIX IOFLAGS
    #define FK(SUFFIX,N) K(PREFIX,SUFFIX,N)
    
    FK(READ,1)
    FK(WRITE,2)
    FK(APPEND,3)
    #undef PREFIX
    To solve the flags #define's i proposed, but i don't know hot to tell a macro to treat # as a character to be expanded and my macro fails. Any suggestions?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, the C preprocessor isn't recursive. What you could do is use the preprocessor to produce the first file from a "preprocessor input", e.g.
    Code:
    // x.pre
    #define K(PREFIX, SUFFIX, N) #define PREFIX##_##SUFFIX (1<<N)
    #define PREFIX IOFLAGS
    #define FK(SUFFIX,N) K(PREFIX,SUFFIX,N)
    And process that through gcc -E or cl -E

    I beleive that would work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. auto <cringe> generated <cringe> code <cringe>
    By RobS in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-22-2001, 04:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21