src/c2html.c

changeset 15
398a7589297f
parent 14
b33629bf4b58
child 16
fa0bcd0444eb
equal deleted inserted replaced
14:b33629bf4b58 15:398a7589297f
361 settings.infilename = argv[optind]; 361 settings.infilename = argv[optind];
362 362
363 inputfile_t *inputfile = readinput(settings.infilename); 363 inputfile_t *inputfile = readinput(settings.infilename);
364 if (inputfile) { 364 if (inputfile) {
365 FILE *fout; 365 FILE *fout;
366 char *line;
367 if (settings.highlight) {
368 line = (char*) malloc(inputfile->maxlinewidth*64);
369 } else {
370 line = NULL;
371 }
366 if (settings.outfilename) { 372 if (settings.outfilename) {
367 fout = fopen(settings.outfilename, "w"); 373 fout = fopen(settings.outfilename, "w");
368 } else { 374 } else {
369 fout = stdout; 375 fout = stdout;
370 } 376 }
371 fprintf(fout, "<pre>\n"); 377 fprintf(fout, "<pre>\n");
372 char *line = (char*) malloc(inputfile->maxlinewidth
373 * (settings.highlight?64:0));
374 int lnw = lnint(inputfile->count); 378 int lnw = lnint(inputfile->count);
375 for (int i = 0 ; i < inputfile->count ; i++) { 379 for (int i = 0 ; i < inputfile->count ; i++) {
376 if (settings.highlight) { 380 if (settings.highlight) {
377 parseline(inputfile->lines[i], line); 381 parseline(inputfile->lines[i], line);
378 } else { 382 } else {
379 line = inputfile->lines[i]; 383 line = inputfile->lines[i];
380 } 384 }
381 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", 385 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
382 lnw, i+1, line); 386 lnw, i+1, line);
383 } 387 }
384 free(line); 388 if (settings.highlight) {
389 free(line);
390 }
385 fprintf(fout, "</pre>\n"); 391 fprintf(fout, "</pre>\n");
386 392
387 if (fout != stdout) { 393 if (fout != stdout) {
388 fclose(fout); 394 fclose(fout);
389 } 395 }

mercurial