1. 程式人生 > >原始檔註釋格式定製

原始檔註釋格式定製

原始檔定製

Linux-C/C++

我也有技術恐懼症,所以自己認識一項藝術性的技術,就要去學會它。這固然是優點,但伴隨的缺點是從不能很好的堅持。希望現在在真正的工作了,可以養成學習一個東西就要堅持的好習慣,這個markdown上手可能還是有點麻煩的,不過我舉得只要上手了就應該會簡單點吧。我喜歡簡潔的語言來表述美麗的東西!

今天在寫程式碼的時候,突然覺得老是自己手寫註釋有點麻煩,自己就動手寫了個,自動生成必要註釋的工具,主要目的的是為了讓自己重新把C++撿起來一部分。

程式碼如下:

gen_src.cpp

  1. /**
  2. * @FUNCTION: generate source file in my style
  3. * @AUTHOR: Aleda([email protected])
  4. * @DATE: 2014/9/2
  5. * @FILEIN: FILE_NAME
  6. * @FILEOUT: SOURCE_FILE
  7. */
  8. #include"gen_src.h"
  9. char SRC_CPP[MAX_CPP_LINE][MAX_CPP_LENGTH]={
  10. "/**\n",
  11. " * @FUNCTION: TODO\n",
  12. " * @AUTHOR: Aleda([email protected])\n",
  13. " * @DATE: ",
  14. " * @FILEIN: TODO\n",
  15. " * @FILEOUT: TODO\n",
  16. "*/\n\n\n"
    ,
  17. };
  18. char SRC_H[MAX_H_LINE][MAX_H_LENGTH]={
  19. "/**\n",
  20. " * @FUNCTION: TODO\n",
  21. " * @AUTHOR: Aleda([email protected])\n",
  22. " * @DATE: ",
  23. " * @FILEIN: TODO\n",
  24. " * @FILEOUT: TODO\n",
  25. "*/\n\n\n",
  26. };
  27. char SRC_SH[MAX_SH_LINE][MAX_SH_LENGTH]={
  28. "#!/bin/bash\n\n",
  29. "##! @FUNCTION: TODO\n",
  30. "##! @AUTHOR: Aleda([email protected]
    )\n"
    ,
  31. "##! @DATE: ",
  32. "##! @FILEIN: TODO\n",
  33. "##! @FILEOUT: TODO\n\n\n",
  34. "##!\n",
  35. "##! @Global variable\n",
  36. "##!\n\n\n",
  37. "##!\n",
  38. "##! @Functions\n",
  39. "##!\n\n\n",
  40. "##!\n",
  41. "##! @Running\n",
  42. "##!\n\n\n",
  43. };
  44. char SRC_PY[MAX_PY_LINE][MAX_PY_LENGTH]={
  45. "#!/usr/bin/env python\n\n",
  46. "##! @FUNCTION: TODO\n",
  47. "##! @AUTHOR: Aleda([email protected])\n",
  48. "##! @DATE: ",
  49. "##! @FILEIN: TODO\n",
  50. "##! @FILEOUT: TODO\n\n\n",
  51. };
  52. int output_usage(char* text)
  53. {
  54. fprintf(stderr,"Usage:\n");
  55. fprintf(stderr,"\t./gen_src SRC_FILE\n");
  56. fprintf(stderr,"\n\tFor Example: ./gen_src exam.sh\n");
  57. if(text != NULL)
  58. {
  59. fprintf(stderr,"\n\r\tERROR: %s\n", text);
  60. return-1;
  61. }
  62. return0;
  63. }
  64. int init_param(int argc,char* argv[], PCONF p_conf)
  65. {
  66. char file_name[MAX_FILE_NAME];
  67. if(argc <=1)
  68. {
  69. return output_usage(NULL);
  70. }
  71. elseif(argc >2)
  72. {
  73. return output_usage("The number of param is error!");
  74. }
  75. else
  76. {
  77. snprintf(p_conf->file_name, MAX_FILE_NAME,"%s", argv[1]);
  78. if(access(p_conf->file_name, F_OK)==0)
  79. {
  80. return output_usage("file exist!");
  81. }
  82. if((p_conf->fp_out = fopen(p_conf->file_name,"w"))== NULL)
  83. {
  84. return output_usage("file open error!");
  85. }
  86. return1;
  87. }
  88. }
  89. int solve(PCONF p_conf)
  90. {
  91. char file_name[MAX_FILE_NAME];
  92. snprintf(file_name, MAX_FILE_NAME,"%s", p_conf->file_name);
  93. int len_name = strlen(file_name);
  94. char suffix[MAX_SUFFIX_LENGTH];
  95. int cnt =0;
  96. char str_time[20];
  97. time_t t;
  98. struct tm *local;
  99. t = time(NULL);
  100. local= localtime(&t);
  101. snprintf(str_time,20,"%d/%d/%d\n",local->tm_year +1900,local->tm_mon +1,local->tm_mday);
  102. for(int i = len_name -1; i >=0; i--)
  103. {
  104. if(file_name[i]=='.')
  105. {
  106. if(i ==0)
  107. {
  108. return output_usage("File_name error!");
  109. }
  110. if(cnt > MAX_SUFFIX_LENGTH)
  111. {
  112. return output_usage("file_name_length is too long!");
  113. }
  114. suffix[cnt++]='\0';
  115. break;
  116. }
  117. if(cnt > MAX_SUFFIX_LENGTH)
  118. {
  119. return output_usage("file_name_length is too long!");
  120. }
  121. suffix[cnt++]= file_name[i];
  122. }
  123. if(strcmp(suffix,"ppc")==0)
  124. {
  125. for(int i =0; i < MAX_CPP_LINE; i++)
  126. {
  127. if(i ==3)
  128. {
  129. fprintf(p_conf->fp_out,"%s%s", SRC_CPP[i], str_time);
  130. fflush(p_conf->fp_out);
  131. continue;
  132. }
  133. fprintf(p_conf->fp_out,"%s", SRC_CPP[i]);
  134. fflush(p_conf->fp_out);
  135. }
  136. }
  137. elseif(strcmp(suffix,"h")==0)
  138. {
  139. for(int i =0; i < MAX_H_LINE; i++)
  140. {
  141. if(i ==3)
  142. {
  143. fprintf(p_conf->fp_out,"%s%s", SRC_H[i], str_time);
  144. fflush(p_conf->fp_out);
  145. continue;
  146. }
  147. fprintf(p_conf->fp_out,"%s", SRC_H[i]);
  148. fflush(p_conf->fp_out);
  149. }
  150. }
  151. elseif(strcmp(suffix,"hs")==0)