Add a file reading utility function
Going to use this for parsing config protobufs.
Change-Id: I471030b31dcf0450d40566560b3ce7338e7a7d21
diff --git a/aos/common/util/file.cc b/aos/common/util/file.cc
new file mode 100644
index 0000000..4c859cf
--- /dev/null
+++ b/aos/common/util/file.cc
@@ -0,0 +1,28 @@
+#include "aos/common/util/file.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "aos/common/scoped_fd.h"
+
+namespace aos {
+namespace util {
+
+::std::string ReadFileToStringOrDie(const ::std::string &filename) {
+ ::std::string r;
+ ScopedFD fd(PCHECK(open(filename.c_str(), O_RDONLY)));
+ while (true) {
+ char buffer[1024];
+ const ssize_t result = read(fd.get(), buffer, sizeof(buffer));
+ if (result < 0) {
+ PLOG(FATAL, "reading from %s", filename.c_str());
+ } else if (result == 0) {
+ break;
+ }
+ r.append(buffer, result);
+ }
+ return r;
+}
+
+} // namespace util
+} // namespace aos