eolian gen2: add file read
authorDaniel Kolesa <d.kolesa@osg.samsung.com>
Thu, 15 Sep 2016 13:11:14 +0000 (15:11 +0200)
committerDaniel Kolesa <d.kolesa@osg.samsung.com>
Fri, 7 Oct 2016 09:54:23 +0000 (11:54 +0200)
src/bin/eolian2/main.c

index aa3da0b..d8d945c 100644 (file)
@@ -161,6 +161,46 @@ end:
    return fret;
 }
 
+static Eina_Strbuf *
+_read_file(const char *fname)
+{
+   FILE *f = fopen(fname, "rb");
+   if (!f)
+     return NULL;
+
+   fseek(f, 0, SEEK_END);
+   long fs = ftell(f);
+   if (fs < 0)
+     {
+        fprintf(stderr, "eolian: could not get length of '%s'\n", fname);
+        fclose(f);
+        return NULL;
+     }
+   fseek(f, 0, SEEK_SET);
+
+   char *cont = malloc(fs + 1);
+   if (!cont)
+     {
+        fprintf(stderr, "eolian: could not allocate memory for '%s'\n", fname);
+        fclose(f);
+        return NULL;
+     }
+
+   long as = fread(cont, 1, fs, f);
+   if (as != fs)
+     {
+        fprintf(stderr, "eolian: could not read %ld bytes from '%s' (got %ld)\n",
+                fs, fname, as);
+        free(cont);
+        fclose(f);
+        return NULL;
+     }
+
+   cont[fs] = '\0';
+   fclose(f);
+   return eina_strbuf_manage_new_length(cont, fs);
+}
+
 static Eina_Bool
 _write_header(const char *ofname, const char *ifname, Eina_Bool legacy)
 {