about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-13 21:49:22 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-13 21:49:22 -0700
commitf3c75c738f7d19b88111158900c777b895a0b94a (patch)
treec5fb07ca4a8126fd62c81f8f3b734aa5e6278d74
parent00dd668593d4e4c1e6821e8f7703815d4aa654a7 (diff)
downloadmu-f3c75c738f7d19b88111158900c777b895a0b94a.tar.gz
3180 - safer way to slurp() from file
-rw-r--r--092persist.cc13
1 files changed, 4 insertions, 9 deletions
diff --git a/092persist.cc b/092persist.cc
index 4f3d1494..44a10bf1 100644
--- a/092persist.cc
+++ b/092persist.cc
@@ -50,18 +50,13 @@ case RESTORE: {
 }
 
 :(code)
+// http://cpp.indi.frih.net/blog/2014/09/how-to-read-an-entire-file-into-memory-in-cpp
 string slurp(const string& filename) {
-  ostringstream result;
   ifstream fin(filename.c_str());
   fin.peek();
-  if (!fin) return result.str();  // don't bother checking errno
-  const int N = 1024;
-  char buf[N];
-  while (has_data(fin)) {
-    bzero(buf, N);
-    fin.read(buf, N-1);  // leave at least one null
-    result << buf;
-  }
+  if (!fin) return "";  // don't bother checking errno
+  ostringstream result;
+  result << fin.rdbuf();
   fin.close();
   return result.str();
 }