#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 GetSubfunctionFromSrcFile(FILE *fp_src,FUNCINFO *pInfo);
char* RepladeTabWithSpace(char* buffer);
unsigned int
GetUseLinenum(char* p);
int getSrcLineNum(FILE *fp_src);
void getcommonhead(FILE * fp_src,FILE * fp_subFunc,FUNCINFO *pInfo);
#endif
----------------------
#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, FUNCINFO *pFuncInfo)
{
char buffer[MAXNUM];
char srcbuffer[MAXNUM];
char *buf = NULL;
int bufferCount = 0;
int i = 0;
int k = 0;
int j = 0;
int n = 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;
}
/*
申请内存
*/
for(int j=0;j<4;j++)
{
p[i] = (char*)malloc(SUBFUNCNAMELEN);
if(p[i] == NULL)
{
printf("mallco error.\n");
}
}
strcpy(Info[k].funcname,p[0]);
strcpy(Info[k].srcname ,p[1]);
strcpy(Info[k].lineno
,p[2]);
strcpy(Info[k].flag
,p[3]);
Info[k].uselineno = GetUseLinenum(Info[k].lineno);
i = 0;
k++;
}
subFuncNum = k;
/*--------测试NO.1-------------*/
for(n = 0; n < subFuncNum;n++)
{
//printf("Info[%d].funcname = %s--%s--%s--%s",n,Info[n].funcname,Info[n].srcname,Info[n].lineno,Info[n].flag);
}
printf("%d\n",subFuncNum);
}
/*
统计 '/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_subFunc,FUNCINFO *pInfo)
{
int i = 0;
char buffer[MAXNUM];
for(i = 0;i < pInfo[0].uselineno - 1;i++)
{
fgets(buffer,MAXNUM,fp_src);
fprintf(fp_subFunc,"%s",buffer);
}
}
/*
分解源文件获取子函数 */
void GetSubfunctionFromSrcFile(FILE *fp_src,
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_subFunc,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()
{
int i = 0;
FILE *fp_tags;
FILE *fp_src;
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");
}
/* tags文件函数 读取,截取信息,提取行号*/
ReadTagsFile(fp_tags, Info);
//GetSubfunctionFromSrcFile(fp_src,Info);
/*
释放内存
*/
for(i = 0;i <subFuncNum; i++ )
{
free(Info[i].funcname);
free(Info[i].srcname);
free(Info[i].lineno);
free(Info[i].flag);
}
if(fclose(fp_tags) != 0)
{
printf("close tags file error.\n");
}
if(fclose(fp_src) != 0)
{
printf("close src file error.\n");
}
return 0;
}