今天完成自己在linux中的一个项目列子,以前所学的全是照着书上的列子比划练习,没有用到自己实际应用中,虽然这一个项目实用价值不大,但是自己还是很有成就感的,不管怎么样走出了在linux程序开发的第一步了。
linux注释工具,主要是实现对文件添加头注释和函数注释,文件注释和函数注释都支持自定义的,默认对头文件的注释为
//====================================================================== // // Copyright (C) 2007-2012 三月软件工作室 // All rights reserved // // filename :main // description :file description // All rights reserved // // created by Renji at 2012-4-15 // qq:625246906 // url:http://www.marchsoft.cn/ // //====================================================================== 默认对函数的注释为 // <description> // // </description> // <param name=$param></param> // <returns></returns> 其中$param为自定的一个标识,用来换成传递的参数名字,函数有多少参数,本行输出几次, 在程序的实现中主要使用了I/0操作,主要是文件的读取,定位、合并。使我在文件操作上有了进一步的了解和学习。 技术难点是分析每一句的是否为函数的定义语句,为函数定位语句要加上注释,我将所有的语句都默认为标准的。
效果如下。如有个c语言文件,r.c
#include<stdio.h> void b(int i) {}
int main()
{
printf("reage ");return 0;
}
使用工具后会生成,r.c.rg文件 文件内容为
//====================================================================== // // Copyright (C) 2007-2012 三月软件工作室 // All rights reserved // // filename :main // description :file description // All rights reserved // // created by Renji at 2012-4-15 // qq:625246906 // url:http://www.marchsoft.cn/ // //====================================================================== #include<stdio.h>
// <description> // // </description> // <param name=i></param> // <returns></returns> void b(int i) {} // <description> // // </description> // <returns></returns> int main()
{
printf("reage ");return 0;
} 下面为代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX 255
#define ROWMAX 1024
long fileLoc = -1;
int descrip_count =0 ;
int Get_Row(char *word)
{
int row=0 ;
int i;
char bl=0;
for(i=0;i<strlen(word);i++)
{
if(' ' != word[i] &&(word[i]>'0' && '9'>= word[i]))
{
bl =1;
row = row*10 + word[i]-'0';
}
else
{
if(bl)
break;
}
}
return row ;
}
void Operate_P(char *word,char *param,FILE *writefile)
{
char row[MAX];
char *ptr;
int i =0;
ptr = strstr(word,"$param");
strcpy(row,ptr+6);
while(*param)
{
if('\n' == *param)
{
*(ptr+i)=0;
i=0;
strcat(word,row);
fprintf(writefile,"%s",word);
}
else {*(ptr+i)=*param;i++;}
param++;
}
}
void Fun_Description(char *word,FILE *readp, FILE *writefile)
{
char row[MAX];
int i;
char *charptr;
char *ptr;
if(-1 == fileLoc)
{
while(fgets(row,MAX,readp))
{
charptr = strstr(row,"function");
if(charptr)
{
descrip_count = Get_Row(charptr+8);
break;
}
}
fileLoc = ftell(readp);
}
fseek(readp,fileLoc,SEEK_SET);
for(i=0;i<descrip_count;i++)
{
if(!fgets(row,MAX,readp))break;
ptr = strstr(row,"$param");
if(!ptr)
fprintf(writefile,"%s",row);
else
{
Operate_P(row,word,writefile);
}
}
}
int Is_Fun(char *word,FILE *readp,FILE *writefile)
{
int i;
char *pstart,*pend;
char pname[MAX] ;
int loc = 0 ;
int pcount=0,typec=0;
if(strchr(word,';'))
return -1;
if(strchr(word,'='))
return -1;
if(strchr(word,':'))
return -1;
if(strchr(word,'<'))
return -1;
if(strchr(word,'>'))
return -1;
pstart = strchr(word,'(');
pend = strchr(word,')');
if(pend == pstart+1)
return 0 ;
if( strstr(word,"void")) return 0;
if(pstart < pend)
{
pstart++;
while(pstart < pend)
{
typec =0 ;
while(' ' == *pstart && pend > pstart) {pstart++;}
while(' '!= *pstart && pend > pstart ) {pstart++;typec=1;}
while((' ' == *pstart || '*'== *pstart )&& pend > pstart){pstart++;typec=2;}
if(2 != typec)return -1;
while(' ' != *pstart && pend > pstart && ',' != *pstart ){pname[loc++]=*pstart;pstart++;typec=3;}
pname[loc++]='\n';
if(3 !=typec) return -1;
while(' ' == *pstart && pend >pstart) {pstart++;}
if(',' != *pstart && ')' != *pstart )return -1;
pstart ++;
pcount ++;
}
pcount ++;
Fun_Description(pname,readp,writefile);
return pcount;
}
else
{return -1;}
}
int main(int argc,char *argv[])
{
char infile[MAX]="1.c";
char paramfile[MAX]="head";
char writefile[MAX];
int i;
FILE *readp,*readi,*writef;
int row;
char word[MAX];
char operate=0;
char *charptr;
for(i=1;i<argc;i++)
{
if('-'==argv[i][0])
{
}
else
{
strcpy(infile,argv[i]);
}
}
if(NULL == (readp = fopen(paramfile,"r")))
{
printf("%s\n","Unable to open file.");
printf("%s\n","please check whether a file exits.");
exit(0);
}
if(NULL == (readi = fopen(infile,"r")))
{
printf("%s\n","Unable to open file.");
printf("%s\n","please check whether a file exits.");
exit(0);
}
strcpy(writefile,infile);
strcat(writefile,".rg");
i= creat(writefile,S_IRUSR|S_IWUSR);
close(i);
if(NULL == (writef = fopen(writefile,"w")))
{
printf("%s\n","Unable to create file.");
printf("%s\n","please check permissions.");
exit(0);
}
row =0;
while(fgets(word,MAX,readp))
{
charptr = strstr(word,"head");
if(charptr)
{
operate = 1;
row = Get_Row(charptr+5);
break;
}
}
if(1==operate)
{
while(fgets(word,MAX,readp))
{
fprintf(writef,"%s",word);
// for(i=0;i<strlen(word);i++)
// fputc(word[i],writef);
row--;
if(0==row)break;
}
}
while(fgets(word,MAX,readi))
{
Is_Fun(word,readp,writef);
fprintf(writef,"%s",word);
}
fclose(writef);
fclose(readp);
fclose(readi);
return 0;
}