Skip to content

pre-preprocessor for C: extract different versions of code from the same source

Notifications You must be signed in to change notification settings

circulosmeos/prepreprocessor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

pppc.pl

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.

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

note

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).

About

pre-preprocessor for C: extract different versions of code from the same source

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages