原始檔註釋格式定製
阿新 • • 發佈:2019-01-27
原始檔定製
Linux-C/C++
我也有技術恐懼症,所以自己認識一項藝術性的技術,就要去學會它。這固然是優點,但伴隨的缺點是從不能很好的堅持。希望現在在真正的工作了,可以養成學習一個東西就要堅持的好習慣,這個markdown上手可能還是有點麻煩的,不過我舉得只要上手了就應該會簡單點吧。我喜歡簡潔的語言來表述美麗的東西!
今天在寫程式碼的時候,突然覺得老是自己手寫註釋有點麻煩,自己就動手寫了個,自動生成必要註釋的工具,主要目的的是為了讓自己重新把C++撿起來一部分。
程式碼如下:
gen_src.cpp
/**
* @FUNCTION: generate source file in my style
* @AUTHOR: Aleda([email protected])
* @DATE: 2014/9/2
* @FILEIN: FILE_NAME
* @FILEOUT: SOURCE_FILE
*/
#include"gen_src.h"
char SRC_CPP[MAX_CPP_LINE][MAX_CPP_LENGTH]={
"/**\n",
" * @FUNCTION: TODO\n",
" * @AUTHOR: Aleda([email protected])\n",
" * @DATE: ",
" * @FILEIN: TODO\n",
" * @FILEOUT: TODO\n",
"*/\n\n\n"
};
char SRC_H[MAX_H_LINE][MAX_H_LENGTH]={
"/**\n",
" * @FUNCTION: TODO\n",
" * @AUTHOR: Aleda([email protected])\n",
" * @DATE: ",
" * @FILEIN: TODO\n",
" * @FILEOUT: TODO\n",
"*/\n\n\n",
};
char SRC_SH[MAX_SH_LINE][MAX_SH_LENGTH]={
"#!/bin/bash\n\n",
"##! @FUNCTION: TODO\n",
"##! @AUTHOR: Aleda([email protected]
"##! @DATE: ",
"##! @FILEIN: TODO\n",
"##! @FILEOUT: TODO\n\n\n",
"##!\n",
"##! @Global variable\n",
"##!\n\n\n",
"##!\n",
"##! @Functions\n",
"##!\n\n\n",
"##!\n",
"##! @Running\n",
"##!\n\n\n",
};
char SRC_PY[MAX_PY_LINE][MAX_PY_LENGTH]={
"#!/usr/bin/env python\n\n",
"##! @FUNCTION: TODO\n",
"##! @AUTHOR: Aleda([email protected])\n",
"##! @DATE: ",
"##! @FILEIN: TODO\n",
"##! @FILEOUT: TODO\n\n\n",
};
int output_usage(char* text)
{
fprintf(stderr,"Usage:\n");
fprintf(stderr,"\t./gen_src SRC_FILE\n");
fprintf(stderr,"\n\tFor Example: ./gen_src exam.sh\n");
if(text != NULL)
{
fprintf(stderr,"\n\r\tERROR: %s\n", text);
return-1;
}
return0;
}
int init_param(int argc,char* argv[], PCONF p_conf)
{
char file_name[MAX_FILE_NAME];
if(argc <=1)
{
return output_usage(NULL);
}
elseif(argc >2)
{
return output_usage("The number of param is error!");
}
else
{
snprintf(p_conf->file_name, MAX_FILE_NAME,"%s", argv[1]);
if(access(p_conf->file_name, F_OK)==0)
{
return output_usage("file exist!");
}
if((p_conf->fp_out = fopen(p_conf->file_name,"w"))== NULL)
{
return output_usage("file open error!");
}
return1;
}
}
int solve(PCONF p_conf)
{
char file_name[MAX_FILE_NAME];
snprintf(file_name, MAX_FILE_NAME,"%s", p_conf->file_name);
int len_name = strlen(file_name);
char suffix[MAX_SUFFIX_LENGTH];
int cnt =0;
char str_time[20];
time_t t;
struct tm *local;
t = time(NULL);
local= localtime(&t);
snprintf(str_time,20,"%d/%d/%d\n",local->tm_year +1900,local->tm_mon +1,local->tm_mday);
for(int i = len_name -1; i >=0; i--)
{
if(file_name[i]=='.')
{
if(i ==0)
{
return output_usage("File_name error!");
}
if(cnt > MAX_SUFFIX_LENGTH)
{
return output_usage("file_name_length is too long!");
}
suffix[cnt++]='\0';
break;
}
if(cnt > MAX_SUFFIX_LENGTH)
{
return output_usage("file_name_length is too long!");
}
suffix[cnt++]= file_name[i];
}
if(strcmp(suffix,"ppc")==0)
{
for(int i =0; i < MAX_CPP_LINE; i++)
{
if(i ==3)
{
fprintf(p_conf->fp_out,"%s%s", SRC_CPP[i], str_time);
fflush(p_conf->fp_out);
continue;
}
fprintf(p_conf->fp_out,"%s", SRC_CPP[i]);
fflush(p_conf->fp_out);
}
}
elseif(strcmp(suffix,"h")==0)
{
for(int i =0; i < MAX_H_LINE; i++)
{
if(i ==3)
{
fprintf(p_conf->fp_out,"%s%s", SRC_H[i], str_time);
fflush(p_conf->fp_out);
continue;
}
fprintf(p_conf->fp_out,"%s", SRC_H[i]);
fflush(p_conf->fp_out);
}
}
elseif(strcmp(suffix,"hs")==0)