src/frontend.c

changeset 66
1b12cf799fee
parent 57
eba880c1705c
child 67
5da2cb5aea6b
equal deleted inserted replaced
60:9f25df78925e 66:1b12cf799fee
31 #include <stdlib.h> 31 #include <stdlib.h>
32 #include <unistd.h> 32 #include <unistd.h>
33 #include <string.h> 33 #include <string.h>
34 34
35 #include "c2html.h" 35 #include "c2html.h"
36 #include "ucx/utils.h" 36 #include <cx/utils.h>
37
38 #define FILEBUF_SIZE 4096
39 37
40 typedef struct { 38 typedef struct {
41 char* outfilename; 39 char* outfilename;
42 char* headerfile; 40 char* headerfile;
43 char* footerfile; 41 char* footerfile;
44 char* infilename; 42 char* infilename;
45 int showlinenumbers; 43 int showlinenumbers;
46 } Settings; 44 } Settings;
47 45
48 static int appendfile(const char *filename, FILE *fout, 46 static int appendfile(const char *filename, FILE *fout, const char *errmsg) {
49 char *copybuf, size_t copybuflen, const char *errmsg) { 47 FILE *fin = fopen(filename, "r");
50 FILE *headerfile = fopen(filename, "r"); 48 if (!fin) {
51 if (!headerfile) {
52 perror(errmsg); 49 perror(errmsg);
53 if (fout != stdout) { 50 if (fout != stdout) {
54 fclose(fout); 51 fclose(fout);
55 } 52 }
56 return 1; 53 return 1;
57 } 54 }
58 ucx_stream_copy(headerfile, fout, 55 cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite);
59 (read_func) fread, (write_func) fwrite, 56 fclose(fin);
60 copybuf, copybuflen, (size_t)-1);
61 fclose(headerfile);
62 return 0; 57 return 0;
63 } 58 }
64 59
65 static void printhelp() { 60 static void printhelp() {
66 printf("Formats source code using HTML.\n\nUsage:\n" 61 printf("Formats source code using HTML.\n\nUsage:\n"
84 memset(&settings, 0, sizeof(settings)); 79 memset(&settings, 0, sizeof(settings));
85 settings.showlinenumbers = 1; 80 settings.showlinenumbers = 1;
86 c2html_highlighter_func hltr = c2html_c_highlighter; 81 c2html_highlighter_func hltr = c2html_c_highlighter;
87 82
88 /* Parse command line */ 83 /* Parse command line */
89 char optc; 84 int optc;
90 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { 85 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
91 switch (optc) { 86 switch (optc) {
92 case 'o': 87 case 'o':
93 if (!(optarg[0] == '-' && optarg[1] == 0)) { 88 if (!(optarg[0] == '-' && optarg[1] == 0)) {
94 settings.outfilename = optarg; 89 settings.outfilename = optarg;
140 } 135 }
141 } else { 136 } else {
142 fout = stdout; 137 fout = stdout;
143 } 138 }
144 139
145 /* Allocate file buffer */
146 char *filebuf = malloc(FILEBUF_SIZE);
147 if (!filebuf) {
148 perror("Error allocating file buffer");
149 return EXIT_FAILURE;
150 }
151
152 /* Prepend header file */ 140 /* Prepend header file */
153 if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, 141 if (appendfile(settings.headerfile, fout,
154 "Error opening header file")) { 142 "Error opening header file")) {
155 return EXIT_FAILURE; 143 return EXIT_FAILURE;
156 } 144 }
157 145
158 /* Process input file */ 146 /* Process input file */
159 FILE *inputfile = fopen(settings.infilename, "r"); 147 FILE *inputfile = fopen(settings.infilename, "r");
160 if (inputfile) { 148 if (inputfile) {
161 c2html_fformatf( 149 CxBuffer fbuf;
162 inputfile, filebuf, FILEBUF_SIZE, 150 cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
163 fout, hltr, settings.showlinenumbers 151 cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread,
152 (cx_write_func) cxBufferWrite);
153 fclose(inputfile);
154 c2html_bformat(
155 fbuf.space, fbuf.size,
156 fout, (cx_write_func ) fwrite, hltr,
157 settings.showlinenumbers
164 ); 158 );
165 fclose(inputfile); 159 cxBufferDestroy(&fbuf);
166 } else { 160 } else {
167 perror("Error opening input file"); 161 perror("Error opening input file");
168 if (fout != stdout) { 162 if (fout != stdout) {
169 fclose(fout); 163 fclose(fout);
170 } 164 }
171 return EXIT_FAILURE; 165 return EXIT_FAILURE;
172 } 166 }
173 167
174 /* Append footer file */ 168 /* Append footer file */
175 if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, 169 if (appendfile(settings.footerfile, fout,
176 "Error opening footer file")) { 170 "Error opening footer file")) {
177 return EXIT_FAILURE; 171 return EXIT_FAILURE;
178 } 172 }
179
180 free(filebuf);
181 173
182 return EXIT_SUCCESS; 174 return EXIT_SUCCESS;
183 } 175 }
184 } 176 }
185 177

mercurial