@Aleda
2014-09-02 14:21
字数 4277
阅读 0
源文件定制
Linux-C/C++
我也有技术恐惧症,所以自己认识一项艺术性的技术,就要去学会它。这固然是优点,但伴随的缺点是从不能很好的坚持。希望现在在真正的工作了,可以养成学习一个东西就要坚持的好习惯,这个markdown上手可能还是有点麻烦的,不过我举得只要上手了就应该会简单点吧。我喜欢简洁的语言来表述美丽的东西!
今天在写代码的时候,突然觉得老是自己手写注释有点麻烦,自己就动手写了个,自动生成必要注释的工具,主要目的的是为了让自己重新把C++捡起来一部分。
代码如下:
gen_src.cpp
/**
* @FUNCTION: generate source file in my style
* @AUTHOR: Aleda(lishuo02@baidu.com)
* @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(lishuo02@baidu.com)\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(lishuo02@baidu.com)\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(lishuo02@baidu.com)\n",
"##! @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(lishuo02@baidu.com)\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;
}
return 0;
}
int init_param(int argc, char* argv[], PCONF p_conf)
{
char file_name[MAX_FILE_NAME];
if (argc <= 1)
{
return output_usage(NULL);
}
else if (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!");
}
return 1;
}
}
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);
}
}
else if (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);
}
}
else if (strcmp(suffix, "hs") == 0)
{
for (int i = 0; i < MAX_SH_LINE; i++)
{
if (i == 3)
{
fprintf(p_conf->fp_out, "%s%s", SRC_SH[i], str_time);
fflush(p_conf->fp_out);
continue;
}
fprintf(p_conf->fp_out, "%s", SRC_SH[i]);
fflush(p_conf->fp_out);
}
}
else if (strcmp(suffix, "yp") == 0)
{
for (int i = 0; i < MAX_PY_LINE; i++)
{
if (i == 3)
{
fprintf(p_conf->fp_out, "%s%s", SRC_PY[i], str_time);
fflush(p_conf->fp_out);
continue;
}
fprintf(p_conf->fp_out, "%s", SRC_PY[i]);
fflush(p_conf->fp_out);
}
}
else
{
/*
for (int i = 0; i < MAX_OTHER_LINE; i++)
{
fprintf(p_conf->fp_out, "%s", SRC_H[i]);
fflush(p_conf->fp_out);
}
*/
}
}
int main(int argc, char* argv[])
{
PCONF p_conf = (PCONF)malloc(sizeof(CONF));
memset(p_conf, 0, sizeof(p_conf));
if (init_param(argc, argv, p_conf) > 0)
{
solve(p_conf);
}
return 0;
}
gen_src.h
/**
* @FUNCTION: gen_src.h
* @AUTHOR: Aleda(lishuo02@baidu.com)
* @DATE: 2014/9/2
*/
#pragma once
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <string>
#define MAX_CPP_LINE 50
#define MAX_CPP_LENGTH 300
#define MAX_H_LINE 50
#define MAX_H_LENGTH 300
#define MAX_SH_LINE 50
#define MAX_SH_LENGTH 300
#define MAX_PY_LINE 50
#define MAX_PY_LENGTH 300
#define MAX_OTHER_LINE 50
#define MAX_OTHER_LENGTH 300
#define MAX_FILE_NAME 200
#define MAX_SUFFIX_LENGTH 10
using namespace std;
typedef struct conf_t
{
FILE* fp_out;
char file_name[MAX_FILE_NAME];
}CONF, *PCONF;
int output_usage(char* text);
int init_param(int argc, char* argv[], PCONF p_conf);
int solve(PCONF p_conf);
@Aleda
2014-09-02 14:21
字数 4277
阅读 0
正在加载文章图片,请稍后片刻...