回复 5楼 wp231957
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ADSC_FILE.h"
#define MAXNUM 128
#define BLANKSPACE
" "
#define TAB
'\t'
#define USELINENUM 256
#define LINENUM 1024*16
#define SUBFUNCNAMELEN 32
extern unsigned int subFuncNum = 0;
/*
将tags文件中的\t替换为\n
*/
char* ReplaceTabWithSpace(char* buffer)
{
unsigned int nIndex = 0;
for (nIndex = 0; nIndex < (unsigned int)strlen(buffer); nIndex++)
{
if ( buffer[nIndex] == TAB )
{
buffer[nIndex] = ' ';
}
}
return buffer;
}
/*
提取行号,并转换为整形数
*/
unsigned int
GetUseLinenum( char* p)
{
unsigned int nIndex = 0;
char* pRet;
unsigned int ret = 0;
pRet = (char*)malloc(strlen(p));
while( (p[nIndex] >= 0x30) && p[nIndex] <= 0x39)
{
pRet[nIndex] = p[nIndex];
nIndex++;
}
pRet[nIndex] = '\0';
ret = (unsigned int)atoi(pRet);
return ret;
}
/* tags文件函数 读取,截取信息,提取行号*/
//void
ReadTagsFile(FILE *fp_tags,FILE *fp_src,FILE *fp_des, FUNCINFO *pFuncInfo)
void
ReadTagsFile(FILE *fp_tags,FILE *fp_src,FILE *fp_des)
{
char buffer[MAXNUM];
char srcbuffer[MAXNUM];
char *buf = NULL;
int bufferCount = 0;
int i = 0;
int k = 0;
char *p[4];
FUNCINFO Info[USELINENUM];
while((fgets(buffer,MAXNUM,fp_tags)) != NULL )
{
/*
delete ! info
*/
if(buffer[0] == '!')
continue;
/*
delete V info
*/
bufferCount = strlen(buffer);
if( buffer[bufferCount - 2] == 'v' )
{
continue;
}
buf = ReplaceTabWithSpace(buffer);
/*strtok()分离tags每一行*/
while((p[i]=strtok(buf,BLANKSPACE)) != NULL)
{
i++;
buf=NULL;
}
Info[k].funcname = p[0];
Info[k].srcname
= p[1];
Info[k].lineno
= p[2];
Info[k].flag
= p[3];
Info[k].uselineno = GetUseLinenum(Info[k].lineno);
i = 0;
k++;
}
subFuncNum = k;
//GetSubfunctionFromSrcFile(fp_src, fp_des, Info);
//memcpy(pFuncInfo, Info,subFuncNum*sizeof(FUNCINFO));
}
/*
统计 '/n' 数,以确定最后一个子函数的结束位置
*/
int getSrcLineNum(FILE *fp_src)
{
char ch;
int SrcLineNum = 0;
while((ch = fgetc(fp_src)) != EOF)
{
if(ch == '\n')
{
SrcLineNum++;
}
}
rewind(fp_src);
return SrcLineNum;
}
/*
获取公共的头
*/
void
getcommonhead(FILE * fp_src,FILE * fp_des,FUNCINFO *pInfo)
{
int i = 0;
char buffer[MAXNUM];
for(i = 0;i < pInfo[0].uselineno - 1;i++)
{
fgets(buffer,MAXNUM,fp_src);
fprintf(fp_des,"%s",buffer);
}
}
/*
分解源文件获取子函数 */
void GetSubfunctionFromSrcFile(FILE *fp_src, FILE *fp_des, FUNCINFO *Info)
{
char buffer[MAXNUM];
int j = 0;
int k = 0;
int n = 0;
int SrcLineNum = 0;
char ch;
char subFuncName[SUBFUNCNAMELEN];
FILE *fp_subFunc;
/*
统计 '/n' 数,以确定最后一个子函数的结束位置
*/
SrcLineNum =
getSrcLineNum(fp_src);
/*
获取前 subFuncNum-1 个子函数
*/
for(k = 0;k < subFuncNum - 1;k++ )
{
strcpy(subFuncName,Info[k].funcname);
strcat(subFuncName,".c");
fp_subFunc = fopen(subFuncName,"w+");
/*
获取公共的头
*/
getcommonhead(fp_src, fp_des,Info);
for(j = Info[k].uselineno;j < Info[k+1].uselineno;j++)
{
fgets(buffer,MAXNUM,fp_src);
fprintf(fp_subFunc,"%s",buffer);
}
fclose(fp_subFunc);
}
/*
获取最后一个子函数
*/
strcpy(subFuncName,Info[subFuncNum - 1].funcname);
strcat(subFuncName,".c");
fp_subFunc = fopen(subFuncName,"w+");
for(n = Info[subFuncNum-1].uselineno;n < SrcLineNum+1;n++)
{
fgets(buffer,MAXNUM,fp_src);
fprintf(fp_subFunc,"%s",buffer);
}
fclose(fp_subFunc);
}
int main()
{
FILE *fp_tags;
FILE *fp_src;
FILE *fp_des;
FUNCINFO Info[USELINENUM];
if((fp_tags=fopen("tags","r")) == NULL)
{
printf("fopen file error.\n");
}
if((fp_src=fopen("fuction.c","r")) == NULL)
{
printf("fopen src file error.\n");
}
if((fp_des = fopen("func.txt","w+")) == NULL)
{
printf("fopen des file error.\n");
}
/* tags文件函数 读取,截取信息,提取行号*/
ReadTagsFile(fp_tags,fp_src,fp_des);
// GetSubfunctionFromSrcFile(fp_src,fp_des,Info);
if(fclose(fp_tags) != 0)
{
printf("close tags file error.\n");
}
if(fclose(fp_src) != 0)
{
printf("close src file error.\n");
}
if(fclose(fp_des) != 0)
{
printf("close des file error.\n");
}
return 0;
}
-----------------------
#ifndef __ADSC_FILE_H__
#define __ADSC_FILE_H__
typedef struct tagFuncInfo{
char *funcname;
char *srcname;
char *lineno;
char *flag;
unsigned int
uselineno;
}FUNCINFO;
//void ReadTagsFile(FILE *fp_tags, FUNCINFO *pFuncInfo);
void
ReadTagsFile(FILE *fp_tags,FILE *fp_src,FILE *fp_des);
void GetSubfunctionFromSrcFile(FILE *fp_src,FILE *fp_des,
FUNCINFO *pInfo);
char* RepladeTabWithSpace(char* buffer);
unsigned int
GetUseLinenum(char* p);
int getSrcLineNum(FILE *fp_src);
void getcommonhead(FILE * fp_src,FILE * fp_des,FUNCINFO *pInfo);
#endif
--------------
传参什么的我也不会