Wed, 10 Jul 2013 13:38:28 +0200
option for output file
src/c2html.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/c2html.c Wed Jun 12 14:50:27 2013 +0200 1.2 +++ b/src/c2html.c Wed Jul 10 13:38:28 2013 +0200 1.3 @@ -44,6 +44,10 @@ 1.4 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL 1.5 }; 1.6 1.7 +typedef struct { 1.8 + char* outfilename; 1.9 + char* infilename; 1.10 +} settings_t; 1.11 1.12 typedef struct { 1.13 size_t count; 1.14 @@ -204,7 +208,10 @@ 1.15 1.16 void printhelp() { 1.17 printf("Formats source code using HTML.\n\nUsage:\n" 1.18 - " c2html [FILE...]" 1.19 + " c2html [Options] FILE\n\n" 1.20 + " Options:\n" 1.21 + " -h Prints this help message\n" 1.22 + " -o <output> Output file (if not specified, stdout is used)\n" 1.23 "\n"); 1.24 1.25 1.26 @@ -218,23 +225,54 @@ 1.27 1.28 int main(int argc, char** argv) { 1.29 1.30 - if (argc == 1) { 1.31 + settings_t settings; 1.32 + settings.outfilename = NULL; 1.33 + 1.34 + char optc; 1.35 + while ((optc = getopt(argc, argv, "ho:")) != -1) { 1.36 + switch (optc) { 1.37 + case 'o': 1.38 + if (!(optarg[0] == '-' && optarg[1] == 0)) { 1.39 + settings.outfilename = optarg; 1.40 + } 1.41 + break; 1.42 + case 'h': 1.43 + printhelp(); 1.44 + return 0; 1.45 + default: 1.46 + return 1; 1.47 + } 1.48 + } 1.49 + 1.50 + if (optind != argc-1) { 1.51 printhelp(); 1.52 - return 0; 1.53 + return 1; 1.54 } else { 1.55 + settings.infilename = argv[optind]; 1.56 1.57 - inputfile_t *inputfile = readinput(argv[1]); 1.58 + inputfile_t *inputfile = readinput(settings.infilename); 1.59 if (inputfile) { 1.60 - printf("<pre>\n"); 1.61 + FILE *fout; 1.62 + if (settings.outfilename) { 1.63 + fout = fopen(settings.outfilename, "w"); 1.64 + } else { 1.65 + fout = stdout; 1.66 + } 1.67 + fprintf(fout, "<pre>\n"); 1.68 char *line = (char*) malloc(inputfile->maxlinewidth*64); 1.69 int lnw = lnint(inputfile->count); 1.70 for (int i = 0 ; i < inputfile->count ; i++) { 1.71 parseline(inputfile->lines[i], line); 1.72 - printf("<span class=\"c2html-lineno\">%*d:</span> %s", 1.73 + fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", 1.74 lnw, i, line); 1.75 } 1.76 free(line); 1.77 - printf("</pre>\n"); 1.78 + fprintf(fout, "</pre>\n"); 1.79 + 1.80 + if (fout != stdout) { 1.81 + fclose(fout); 1.82 + } 1.83 + 1.84 freeinputfilebuffer(inputfile); 1.85 } 1.86