diff src/misc.c @ 180:0038a2943cc2

- Made file inputs free the loaded file after the page is left.
author jcid
date Tue, 15 Apr 2008 17:43:17 +0200
parents fb8da086d7da
children fce9380ee68a
line wrap: on
line diff
--- a/src/misc.c	Mon Apr 14 21:11:00 2008 +0200
+++ b/src/misc.c	Tue Apr 15 17:43:17 2008 +0200
@@ -269,3 +269,25 @@
    out[i] = '\0';
    return out;
 }
+
+/*
+ * Load a local file into a dStr.
+ * Return value: dStr on success, NULL on error.
+ * todo: a filesize threshold may be implemented.
+ */
+Dstr *a_Misc_file2dstr(const char *filename)
+{
+   FILE *F_in;
+   int n;
+   char buf[4096];
+   Dstr *dstr = NULL;
+
+   if ((F_in = fopen(filename, "r"))) {
+      dstr = dStr_sized_new(4096);
+      while ((n = fread (buf, 1, 4096, F_in)) > 0) {
+         dStr_append_l(dstr, buf, n);
+      }
+      fclose(F_in);
+   }
+   return dstr;
+}