#include <stdio.h> #include <string.h> #include <malloc.h>
#define COLLMAX 20 #define RDBUFMX 100 #define IMAX 100
struct pznod { char *name; struct pznod *next; };
struct pznod incls[IMAX]; int inums = 0;
void addfunc(char *iname, char *fname) { int i, flag; struct pznod *p, *np; printf("add %s to <%s>\n", fname, iname); for(i = 0; i < inums; i++) { if(strcmp(incls[i].name, iname) == 0) { break; } } if(i == inums) { incls[i].name = iname; inums++; }
flag = 0; p = &incls[i]; while(p->next != NULL) { p = p->next; //假如那个文件没有问题的话,是不用做这个判断的.
if(strcmp(p->name, fname) == 0){ flag = 1; break; } } if(flag == 0){ np = (struct pznod *)malloc(sizeof(struct pznod)); np->next = NULL; np->name = fname; p->next = np; } }
void print_incls_and_funcs() { int i; struct pznod *p; for(i = 0; i < inums; i++) { printf("include: %s\n", incls[i].name); p = incls[i].next; while(p) { printf("\t%s\n", p->name); p = p->next; } } }
static char *colle[COLLMAX]; static char **collp = colle; void add_to_collection(char *incl) { int len; len = strlen(incl); *collp = (char *)malloc((len + 1) * sizeof(char)); strcpy(*collp, incl); collp++; *collp = NULL; }
void add_func_to_colle(char *fname) { char **p, *namep; int len;
collp = colle; len = strlen(fname); namep = (char *)malloc((len + 1) * sizeof(char)); strcpy(namep, fname); p = colle; while(*p){ addfunc(*p, namep); p++; } }
int readpzfile(FILE *f) { int flag, c; char buf[RDBUFMX], *sp, *sp2;
sp = buf; flag = 0; while((c = getc(f)) != EOF) {
if(c == ' ') flag = 1; else flag = 0;
//read line to buffer
while(c != '\n'){ *sp++ = c; c = getc(f); } *sp = '\0';
//if empty, continue
sp2 = sp; sp = buf; while(*sp == ' ') sp++; if(*sp == '\0') continue; //trim it
while(*(sp2 - 1) == ' '){ sp2--; } *sp2 = '\0'; if(flag == 0){ add_to_collection(sp); }else { add_func_to_colle(sp); } sp = buf; } return 0; }
int main(int argc, char **argv) { FILE *f; int i;
for(i = 1; i < argc; i++) { if((f = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "fopen failed\n"); return -1; } readpzfile(f); print_incls_and_funcs(); fclose(f); } if(argc == 1) { f = stdin; readpzfile(f); print_incls_and_funcs(); } return 0; }
|