]> granicus.if.org Git - multimarkdown/commitdiff
CHANGED: Use faster method of getting input from stdin or a file -- old method was...
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Fri, 6 May 2016 15:38:34 +0000 (11:38 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Fri, 6 May 2016 15:38:34 +0000 (11:38 -0400)
src/main.c

index 22d0ff2edaba43f5f43d30f7f1e7142487275e1f..8e88642f0675bae2b39a45104ece4b3b445893e4 100644 (file)
 
 #include "GLibFacade.h"
 
-char * stdin_buffer() {
-       /* Read from stdin and return a char *
-               `result` will need to be freed elsewhere */
+#define kBUFFERSIZE 4096       // How many bytes to read at a time
+
+GString * stdin_buffer() {
+       /* Read from stdin and return a GString *
+               `buffer` will need to be freed elsewhere */
+
+       char chunk[kBUFFERSIZE];
+       size_t bytes;
 
        GString * buffer = g_string_new("");
-       char curchar;
-       char * result;
 
-       while ((curchar = fgetc(stdin)) != EOF)
-               g_string_append_c(buffer, curchar);
+    while ((bytes = fread(chunk, 1, kBUFFERSIZE, stdin)) > 0) {
+       g_string_append_c_array(buffer, chunk, bytes);
+    }
 
        fclose(stdin);
 
-       result = buffer->str;
+       return buffer;
+}
+
+GString * scan_file(char * fname) {
+       /* Read from stdin and return a GString *
+               `buffer` will need to be freed elsewhere */
+
+       char chunk[kBUFFERSIZE];
+       size_t bytes;
+
+       FILE * file;
+
+       if ((file = fopen(fname, "r")) == NULL ) {
+               return NULL;
+       }
+
+       GString * buffer = g_string_new("");
+
+    while ((bytes = fread(chunk, 1, kBUFFERSIZE, file)) > 0) {
+       g_string_append_c_array(buffer, chunk, bytes);
+    }
 
-       g_string_free(buffer, false);
+       fclose(file);
 
-       return result;
+       return buffer;
 }
 
 int main( int argc, char** argv ) {