Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47342
  • 博文数量: 33
  • 博客积分: 1301
  • 博客等级: 中尉
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-31 21:06
文章分类
文章存档

2009年(33)

我的朋友

分类: C/C++

2009-07-06 11:07:36

 
头文件和函数整理工具,适用于很多其他的东西哦!!
哈哈,原创!
 
今天下午再添加一些说明
0.1版 --
稍微增强了readpzfile 函数,这样文件里面可以有空行,空行可以有空格和制表符,函数名前面可以有多个空格和制表符,后面也是。

#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;
}


 
0.0版

#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;

    sp = buf;
    flag = 0;
    while((c = getc(f)) != EOF) {
        while(c != '\n'){
            *sp++ = c;
            c = getc(f);
        }
        *sp = '\0';
        if(flag == 0){
            add_to_collection(buf);
        }else {
            add_func_to_colle(buf);
        }

        if((c = getc(f)) == ' ')
            flag = 1;
        else {
            flag = 0;
            ungetc(c, f);
        }
        sp = buf;
    }
    return 0;
}

int main(int argc, char **argv) {
    FILE *f;
    if((f = fopen(argv[1], "r")) == NULL) {
        fprintf(stderr, "fopen failed\n");
        return -1;
    }

    readpzfile(f);
    print_incls_and_funcs();
    
    fclose(f);
    return 0;
}

阅读(673) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~