2025
01
18
2012
02
26
C言語のコメント/*~*/を削除するプログラム
実行オプションで特定文字列を変えるorソースコード内の#defineをいじれば、「/*」 「*/」以外の 範囲を消すこともできます。 注意:特定文字列は2文字まで。
ソースコード
/* del_comment.c */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define NAME_MAX 256 /* Default setting */ #define INPUT_DEFAULT "./input.c" #define OUTPUT_DEFAULT "./output.c" #define START_DEFAULT "/*" #define END_DEFAULT "*/" typedef struct del_comment { char in_name[NAME_MAX]; char out_name[NAME_MAX]; char start[3]; char end[3]; } del_comment_t; void analyze_option(del_comment_t *p, int argc, char *argv[]); void print_info(del_comment_t *p); void print_usage(void); int main(int argc, char *argv[]) { int flag = 0; char str[3], c, str0_keep, str1_keep; FILE *in, *out; fpos_t fpos_in_keep; del_comment_t p; strcpy(p.in_name, INPUT_DEFAULT); strcpy(p.out_name, OUTPUT_DEFAULT); strcpy(p.start, START_DEFAULT); strcpy(p.end, END_DEFAULT); analyze_option(&p, argc, argv); print_info(&p); /* 入出力ファイルオープン */ if ((in = fopen(p.in_name, "r")) == NULL) { printf("open error! [in]\n"); exit(EXIT_FAILURE); } if ((out = fopen(p.out_name, "w")) == NULL) { printf("open error! [out]\n"); fclose(in); exit(EXIT_FAILURE); } /* 最初の一回目だけの特殊な処理 */ if ((str[0] = fgetc(in)) == EOF) { printf("EOF. No changed.\n"); goto eof; } if ((str[1] = fgetc(in)) == EOF) { printf("EOF. No changed.\n"); goto eof; } if ((str[0] == p.start[0]) && (str[1] == p.start[1])) { flag = 1; } if (flag == 1) { fgetpos(in, &fpos_in_keep); str0_keep = str[0]; str1_keep = str[1]; do { str[0] = str[1]; str[1] = fgetc(in); if (str[1] == EOF) { printf("EOF. Changed, but incomplete.\n"); fsetpos(in, &fpos_in_keep); fputc(str0_keep, out); fputc(str1_keep, out); while ((c = fgetc(in)) != EOF) { fputc(c, out); } goto eof; } if ((str[0] == p.end[0]) && (str[1] == p.end[1])) { flag = 0; } } while (flag == 1); str[1] = fgetc(in); } else { fputc(str[0], out); } /* 2回目以降の処理 */ do { str[0] = str[1]; str[1] = fgetc(in); if (str[1] == EOF) { fputc(str[0], out); printf("EOF, Changed all\n"); goto eof; } if ((str[0] == p.start[0]) && (str[1] == p.start[1])) { flag = 1; } if (flag == 1) { fgetpos(in, &fpos_in_keep); str0_keep = str[0]; str1_keep = str[1]; do { str[0] = str[1]; str[1] = fgetc(in); if (str[1] == EOF) { printf("EOF. Changed, but incomplete.\n"); fsetpos(in, &fpos_in_keep); fputc(str0_keep, out); fputc(str1_keep, out); while ((c = fgetc(in)) != EOF) { fputc(c, out); } goto eof; } if ((str[0] == p.end[0]) && (str[1] == p.end[1])) { flag = 0; } } while (flag == 1); str[1] = fgetc(in); } else { fputc(str[0], out); } } while (1); eof: fclose(in); fclose(out); return 0; } void analyze_option(del_comment_t *p, int argc, char *argv[]) { int result; while ((result=getopt(argc,argv,"i:o:s:e:h")) != -1) { switch (result) { case 'i': strcpy(p->in_name, optarg); break; case 'o': strcpy(p->out_name, optarg); break; case 's': if (strlen(optarg) == 2) { strcpy(p->start, optarg); } else { printf("-s option keyword must length 2.\n"); printf("Use default [%s]\n", START_DEFAULT); } break; case 'e': if (strlen(optarg) == 2) { strcpy(p->end, optarg); } else { printf("-e option keyword must length 2.\n"); printf("Use default [%s]\n", END_DEFAULT); } break; case 'h': print_usage(); break; case '?': fprintf(stdout,"unknown\n"); break; } } } void print_info(del_comment_t *p) { printf("------Information---------------\n"); printf("input_file = [%s]\n", p->in_name); printf("output_file = [%s]\n", p->out_name); printf("start = [%s]\n", p->start); printf("end = [%s]\n", p->end); printf("--------------------------------\n"); } void print_usage(void) { printf("-------------------Usage--------------------------------------------\n"); printf("./program.exe -i [input_file] -o [output_file] -s [start] -e [end]\n"); printf("--------------------------------------------------------------------\n"); printf("option is not necessary.\n"); printf("default [input_file] is [%s]\n", INPUT_DEFAULT); printf("default [output_file] is [%s]\n", OUTPUT_DEFAULT); printf("default [start] is [%s]\n", START_DEFAULT); printf("default [end] is [%s]\n", END_DEFAULT); printf("--------------------------------------------------------------------\n"); exit(EXIT_SUCCESS); }
コンパイル例
gcc -o del_comment del_comment.c
使い方
# オプションは必ず必要なわけではない # デフォルト設定 # 入力ファイル名: # 出力ファイル名: # 削除開始キーワード:/* # 削除終了キーワード:*/ ./program.exe -i [input_file] -o [output_file] -s [start] -e [end]
検索用タグ C言語
PR
2012/02/26 (Sun.) Comment(0) C言語