Add malloc-free file contents reader

If you have a file that can be read safely in realtime code, this
provides a convenient wrapper for grabbing the entire contents of
said file. This should also just be a bit faster than
ReadFileToStringOrDie just by virtue of avoiding all the malloc's in
std::string and not having to re-open the file.

Change-Id: I90a3fa9433ac3a8773027327bde245ebf0c13b10
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/util/file_test.cc b/aos/util/file_test.cc
index 7c93fb3..8e76154 100644
--- a/aos/util/file_test.cc
+++ b/aos/util/file_test.cc
@@ -4,6 +4,9 @@
 #include <string>
 
 #include "gtest/gtest.h"
+#include "aos/realtime.h"
+
+DECLARE_bool(die_on_malloc);
 
 namespace aos {
 namespace util {
@@ -38,6 +41,21 @@
   EXPECT_TRUE(PathExists(test_file));
 }
 
+// Basic test of reading a normal file.
+TEST(FileTest, ReadNormalFileNoMalloc) {
+  const ::std::string tmpdir(getenv("TEST_TMPDIR"));
+  const ::std::string test_file = tmpdir + "/test_file";
+  ASSERT_EQ(0, system(("echo 971 > " + test_file).c_str()));
+
+  FileReader reader(test_file);
+
+  FLAGS_die_on_malloc = true;
+  RegisterMallocHook();
+  aos::ScopedRealtime realtime;
+  EXPECT_EQ("971\n", reader.ReadContents());
+  EXPECT_EQ(971, reader.ReadInt());
+}
+
 }  // namespace testing
 }  // namespace util
 }  // namespace aos