4
static int processline(char *line, char **start, char **end, int *systemheader)
6
const char *include_text = "include";
13
while(*current != '#')
15
if(*current == ' ' || *current == '\t')
16
current++; /* next char */
18
return 0; /* this catches \0 aswell */
21
current++; /* skip # */
23
/* search for first character */
26
if(*current == ' ' || *current == '\t')
28
else if(*current == 0)
37
if(*current == *include_text)
46
/* search for first character */
49
if(*current == ' ' || *current == '\t')
51
else if(*current == 0)
57
/* match starting < or " */
61
else if(*current == '"')
69
/* search for < or " to end it */
72
if(*current == '>' || *current == '"')
74
else if(*current == 0)
85
/* dependency calculator for c/c++ preprocessor */
86
int dependency_cpp_run(const char *filename,
87
int (*callback)(void *, const char *, int), void *userdata)
106
printf("cpp-dep: running on %s\n", filename);
108
file = fopen(filename, "rb");
111
/* printf("cpp-dep: error opening %s\n", filename); */
115
/* read the whole file */
116
fseek(file, 0, SEEK_END);
117
filesize = ftell(file);
118
fseek(file, 0, SEEK_SET);
120
filebuf = malloc(filesize+1); /* +1 for null termination */
124
printf("cpp-dep: error allocating %ld bytes\n", filesize);
129
/* read the file and close it */
130
readitems = fread(filebuf, 1, filesize, file);
133
if(readitems != filesize)
135
printf("cpp-dep: error reading the complete file. %ld of %ld bytes read\n", readitems, filesize);
140
filebufcur = filebuf;
141
filebufend = filebuf+filesize;
143
while(filebufcur < filebufend)
145
/* search for next line */
146
linestart = filebufcur;
147
while(filebufcur != filebufend && *filebufcur != '\n' && *filebufcur != '\r')
153
if(processline(linestart, &includestart, &includeend, &systemheader))
156
if(debug) printf("INCLUDE: %s\n", includestart);
159
errorcode = callback(userdata, includestart, systemheader);
162
printf("cpp-dep: error %d during callback\n", errorcode);
169
printf("cpp-dep: %s=%d lines\n", filename, linecount);