cleans up main function

Tue, 23 Aug 2016 14:31:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 14:31:02 +0200
changeset 43
a8cee98c8832
parent 42
7f2403c637a7
child 44
2b4ac35d061d

cleans up main function

src/c2html.c file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Tue Aug 23 14:24:57 2016 +0200
+++ b/src/c2html.c	Tue Aug 23 14:31:02 2016 +0200
@@ -137,14 +137,14 @@
 };
 
 int main(int argc, char** argv) {
-    int retcode = EXIT_SUCCESS;
-    
+
+    /* Default settings */
     Settings settings;
     memset(&settings, 0, sizeof(settings));
     settings.showlinenumbers = 1;
-    
     enum source_type sourcetype = SOURCE_C;
 
+    /* Parse command line */
     char optc;
     while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
         switch (optc) {
@@ -186,26 +186,9 @@
 
     if (optind != argc-1) {
         printhelp();
-        return 1;
+        return EXIT_FAILURE;
     } else {
-        settings.infilename = argv[optind];
-        FILE *fout;
-        if (settings.outfilename) {
-            fout = fopen(settings.outfilename, "w");
-            if (!fout) {
-                perror("Error opening output file");
-                return EXIT_FAILURE;
-            }
-        } else {
-            fout = stdout;
-        }
-        
-        char *filebuf = malloc(FILEBUF_SIZE);
-        if (!filebuf) {
-            perror("Error allocating file buffer");
-            return EXIT_FAILURE;
-        }
-        
+        /* Configure highlighter */
         highlighter_t highlighter;
         highlighter_t *hptr = &highlighter;
         switch (sourcetype) {
@@ -220,16 +203,38 @@
                 break;
             default: /* should be unreachable */
                 fprintf(stderr, "error in enum source_type\n");
-                retcode = EXIT_FAILURE;
-                goto prog_end;
+                return EXIT_FAILURE;
         }
         
+        /* Open output file */
+        settings.infilename = argv[optind];
+        FILE *fout;
+        if (settings.outfilename) {
+            fout = fopen(settings.outfilename, "w");
+            if (!fout) {
+                perror("Error opening output file");
+                return EXIT_FAILURE;
+            }
+        } else {
+            fout = stdout;
+        }
+        
+        /* Allocate file buffer  */
+        char *filebuf = malloc(FILEBUF_SIZE);
+        if (!filebuf) {
+            perror("Error allocating file buffer");
+            return EXIT_FAILURE;
+        }
+        
+        /* Prepend header file */
         {
             FILE *headerfile = fopen(settings.headerfile, "r");
             if (!headerfile) {
                 perror("Error opening header file");
-                retcode = EXIT_FAILURE;
-                goto prog_end;
+                if (fout != stdout) {
+                    fclose(fout);
+                }
+                return EXIT_FAILURE;
             }
             ucx_stream_copy(headerfile, fout,
                     (read_func) fread, (write_func) fwrite,
@@ -237,6 +242,7 @@
             fclose(headerfile);
         }
 
+        /* Process input file */
         FILE *inputfile = fopen(settings.infilename, "r");
         if (inputfile) {
             UcxBuffer *content = ucx_buffer_new(NULL,
@@ -267,30 +273,32 @@
             ucx_buffer_free(content);
         } else {
             perror("Error opening input file");
-            retcode = EXIT_FAILURE;
+            if (fout != stdout) {
+                fclose(fout);
+            }
+            return EXIT_FAILURE;
         }
         
+        /* Append footer file */
         {
             FILE *footerfile = fopen(settings.footerfile, "r");
             if (!footerfile) {
                 perror("Error opening footer file");
-                retcode = EXIT_FAILURE;
-                goto prog_end;
+                if (fout != stdout) {
+                    fclose(fout);
+                }
+                return EXIT_FAILURE;
             }
             ucx_stream_copy(footerfile, fout,
                     (read_func) fread, (write_func) fwrite,
                     filebuf, FILEBUF_SIZE, (size_t)-1);
             fclose(footerfile);
         }
+
         
         free(filebuf);
-        
-        prog_end:        
-        if (fout != stdout) {
-            fclose(fout);
-        }
 
-        return retcode;
+        return EXIT_SUCCESS;
     }
 }
 

mercurial