[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(mgp-users 00034) ctlwords.c



Hi,

attached is a C version of the code to generate ctlwords.h from
globals.c -- this removes the dependence on Perl or AWK at least for
this step in the build, and makes it easier to port mgp to Windows
(where you cannot assume to have all your nice tools).

	cheers
	luigi

/*
 * ctlwords.c
 */

#if 0
 used to extract and create tokens for mgp
 Reads globals.c, produces ctlwords.h.
 Lines started by  /*CTL*/ and containing an identifier CTL_*
 generate tokens with increasing value.
 The perl code follows:

BEGIN {
	counter = 0;	# 0 origin
	printf("/* generated by ctlwords.awk. do not edit by hand. */\n");
}
/^\/\*CTL\*\// {
	word = substr($0, index($0, "CTL_"));
	word = substr(word, 0, index(word, ",") - 1);
	printf("#define\t%s\t%d\n", word, counter++);
}

#endif

#include <stdio.h>
#include <string.h>
main(int argc, char *argv[])
{
    char buf[1024];
    char *p, *q;
    int i = 0, l ;

    printf("/* Generated by ctlwords.c from globals.c. Do not edit. */\n");
    while (fgets(buf, sizeof(buf)-1, stdin)) {
	if ( strlen(buf) > 12 /* there must be enough data... */
		&& !strncmp(buf, "/*CTL*/", 7) ) { /* found marker */
	    p = strstr(buf+7, "CTL_");
	    if (p) {
		for (q = p ; *q && (isalnum(*q) || *q=='_') ; q++) ;
		*q = '\0'; /* must exist */
		printf("#define %s\t%d\n", p, i++);
	    }
	}
    }
}