Informatics

pre-preprocessor for C code files

I’ve develop a little script that extracts two (or more) different versions of C code from the same source. This is accomplish by preprocessing preprocessor directives before the C preprocessor, extracting code from this process.

The code ( pppc.pl ) can be found at github here.

For example, this simple code, in “mycompleteproject.c”:

#ifdef COMPLETE_CODE
        // each value on second argument has a special meaning.
        if (strcmp(my_array[i], “1”)==0) my_var=VALUE1;
        if (strcmp(my_array[i], “2”)==0) my_var=VALUE2;
#else
        //// this block reserves these values just for my complete version.
        // these values are reserved for future use.
        if (strcmp(my_array[i], “1”)==0 ||
                strcmp(my_array[i], “2”)==0) {
                printf (“\n’%s’ value isn’t valid.\n”, my_array[i]);
                return 1;
        }
#endif

Would end up being just the first block after this command, in the output file “project.c”:

$ perl pppc.pl mycompleteproject.c project.c COMPLETE_CODE 0

So:

        // these values are reserved for future use.
        if (strcmp(my_array[i], “1”)==0 ||
               strcmp(my_array[i], “2”)==0) {
                printf (“\n’%s’ value isn’t valid.\n”, my_array[i]);
                return 1;
        }

Note that comment lines beginning with quadruple slashes are also always extracted from the final code. This allows you to make private comments to your project.

Changing the command line to “COMPLETE_CODE 1” would have written only:

        // each value on second argument has a special meaning.
        if (strcmp(my_array[i], “1”)==0) my_var=VALUE1;
        if (strcmp(my_array[i], “2”)==0) my_var=VALUE2;

The pre-preprocessor can be chained for multiple pre-preprocessor labels and/or multiple source files in a specially crafted shell script.sh file for the project:

perl pppc.pl mycompleteproject.c /tmp/project.c COMPLETE_CODE 0
perl pppc.pl /tmp/project.c project.c JUST_ONE_OUTPUT 1
perl pppc.pl mycompleteproject.h /tmp/project.h COMPLETE_CODE 0

I just made this for fun! and for little projects: in case you need more “professional” options, check unifdef, sunifdef or coan (listed in order of appearance).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s