源文件注释格式定制

@Aleda  2014-09-02 14:21  字数 4277  阅读 0

源文件定制

Linux-C/C++


我也有技术恐惧症,所以自己认识一项艺术性的技术,就要去学会它。这固然是优点,但伴随的缺点是从不能很好的坚持。希望现在在真正的工作了,可以养成学习一个东西就要坚持的好习惯,这个markdown上手可能还是有点麻烦的,不过我举得只要上手了就应该会简单点吧。我喜欢简洁的语言来表述美丽的东西!

今天在写代码的时候,突然觉得老是自己手写注释有点麻烦,自己就动手写了个,自动生成必要注释的工具,主要目的的是为了让自己重新把C++捡起来一部分。


代码如下:

gen_src.cpp

   
   
  1. /**
  2. * @FUNCTION: generate source file in my style
  3. * @AUTHOR: Aleda(lishuo02@baidu.com)
  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(lishuo02@baidu.com)\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(lishuo02@baidu.com)\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(lishuo02@baidu.com)\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(lishuo02@baidu.com)\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. return 0;
  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. else if (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. return 1;
  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. else if (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. else if (strcmp(suffix, "hs") == 0)
  152. {
  153. for (int i = 0; i < MAX_SH_LINE; i++)
  154. {
  155. if (i == 3)
  156. {
  157. fprintf(p_conf->fp_out, "%s%s", SRC_SH[i], str_time);
  158. fflush(p_conf->fp_out);
  159. continue;
  160. }
  161. fprintf(p_conf->fp_out, "%s", SRC_SH[i]);
  162. fflush(p_conf->fp_out);
  163. }
  164. }
  165. else if (strcmp(suffix, "yp") == 0)
  166. {
  167. for (int i = 0; i < MAX_PY_LINE; i++)
  168. {
  169. if (i == 3)
  170. {
  171. fprintf(p_conf->fp_out, "%s%s", SRC_PY[i], str_time);
  172. fflush(p_conf->fp_out);
  173. continue;
  174. }
  175. fprintf(p_conf->fp_out, "%s", SRC_PY[i]);
  176. fflush(p_conf->fp_out);
  177. }
  178. }
  179. else
  180. {
  181. /*
  182. for (int i = 0; i < MAX_OTHER_LINE; i++)
  183. {
  184. fprintf(p_conf->fp_out, "%s", SRC_H[i]);
  185. fflush(p_conf->fp_out);
  186. }
  187. */
  188. }
  189. }
  190. int main(int argc, char* argv[])
  191. {
  192. PCONF p_conf = (PCONF)malloc(sizeof(CONF));
  193. memset(p_conf, 0, sizeof(p_conf));
  194. if (init_param(argc, argv, p_conf) > 0)
  195. {
  196. solve(p_conf);
  197. }
  198. return 0;
  199. }

gen_src.h

   
   
  1. /**
  2. * @FUNCTION: gen_src.h
  3. * @AUTHOR: Aleda(lishuo02@baidu.com)
  4. * @DATE: 2014/9/2
  5. */
  6. #pragma once
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <iostream>
  12. #include <string>
  13. #define MAX_CPP_LINE 50
  14. #define MAX_CPP_LENGTH 300
  15. #define MAX_H_LINE 50
  16. #define MAX_H_LENGTH 300
  17. #define MAX_SH_LINE 50
  18. #define MAX_SH_LENGTH 300
  19. #define MAX_PY_LINE 50
  20. #define MAX_PY_LENGTH 300
  21. #define MAX_OTHER_LINE 50
  22. #define MAX_OTHER_LENGTH 300
  23. #define MAX_FILE_NAME 200
  24. #define MAX_SUFFIX_LENGTH 10
  25. using namespace std;
  26. typedef struct conf_t
  27. {
  28. FILE* fp_out;
  29. char file_name[MAX_FILE_NAME];
  30. }CONF, *PCONF;
  31. int output_usage(char* text);
  32. int init_param(int argc, char* argv[], PCONF p_conf);
  33. int solve(PCONF p_conf);
@Aleda  2014-09-02 14:21  字数 4277  阅读 0
正在加载文章图片,请稍后片刻...
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值