6 static int check_defines(const char **defs, int defcount, char *tdef)
10 while (isspace(*tdef)) tdef++;
12 for (i = 0; i < len; i++)
13 if (tdef[i] == ' ' || tdef[i] == '\r' || tdef[i] == '\n') break;
16 for (i = 0; i < defcount; i++)
18 if (strcmp(defs[i], tdef) == 0)
25 static void do_counters(char *str)
27 static int counter_id = -1, counter;
31 while ((s = strstr(s, "@@")))
33 if (s[2] < '0' || s[2] > '9') { s++; continue; }
35 if (counter_id != s[2] - '0') {
36 counter_id = s[2] - '0';
39 snprintf(buff, sizeof(buff), "%i%s", counter++, s + 3);
44 static int my_fputs(char *s, FILE *stream)
48 for (p = s + strlen(s) - 1; p >= s; p--)
53 /* use DOS endings for better viewer compatibility */
56 return fputs(s, stream);
59 int main(int argc, char *argv[])
61 char path[256], path_file[256];
64 int skip_mode = 0, ifdef_level = 0, skip_level = 0, line = 0;
69 printf("usage:\n%s <file_in> <file_out> [defines...]\n", argv[0]);
73 fi = fopen(argv[1], "r");
76 printf("failed to open: %s\n", argv[1]);
80 fo = fopen(argv[2], "wb");
83 printf("failed to open: %s\n", argv[2]);
87 snprintf(path, sizeof(path), "%s", argv[1]);
88 for (p = path + strlen(path) - 1; p > path; p--) {
89 if (*p == '/' || *p == '\\') {
95 for (++line; !feof(fi); line++)
99 fgs = fgets(buff, sizeof(buff), fi);
100 if (fgs == NULL) break;
105 if (strncmp(buff, "#ifdef ", 7) == 0)
108 if (!skip_mode && !check_defines((void *) &argv[3], argc-3, buff + 7))
109 skip_mode = 1, skip_level = ifdef_level;
111 else if (strncmp(buff, "#ifndef ", 8) == 0)
114 if (!skip_mode && check_defines((void *) &argv[3], argc-3, buff + 8))
115 skip_mode = 1, skip_level = ifdef_level;
117 else if (strncmp(buff, "#else", 5) == 0)
119 if (!skip_mode || skip_level == ifdef_level)
120 skip_mode ^= 1, skip_level = ifdef_level;
122 else if (strncmp(buff, "#endif", 6) == 0)
124 if (skip_level == ifdef_level)
127 if (ifdef_level == 0) skip_mode = 0;
130 printf("%i: warning: #endif without #ifdef, ignoring\n", line);
134 else if (strncmp(buff, "#include ", 9) == 0)
136 char *pe, *p = buff + 9;
140 while (*p && (*p == ' ' || *p == '\"'))
142 for (pe = p + strlen(p) - 1; pe > p; pe--) {
143 if (isspace(*pe) || *pe == '\"') *pe = 0;
146 snprintf(path_file, sizeof(path_file), "%s%s", path, p);
147 ftmp = fopen(path_file, "r");
149 printf("%i: error: failed to include \"%s\"\n", line, p);
154 fgs = fgets(buff, sizeof(buff), ftmp);