util: move read_file to readPropertyFile
Move a method that reads a file and discards any trailing nul-terminator
found. This is expected in dts property files.
Tested: Only ran unit-tests.
Change-Id: I4a551c0098862bcc1cfb7b20d518d7e2fa890a85
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/util.cpp b/util.cpp
index 02dab83..34b0f9b 100644
--- a/util.cpp
+++ b/util.cpp
@@ -1,5 +1,6 @@
#include "util.hpp"
+#include <cstdio>
#include <fstream>
#include <nlohmann/json.hpp>
#include <phosphor-logging/elog-errors.hpp>
@@ -34,5 +35,37 @@
return data;
}
+std::string readPropertyFile(const std::string& fileName)
+{
+ std::ifstream ifs(fileName);
+ std::string contents;
+
+ if (!ifs.is_open())
+ {
+ std::fprintf(stderr, "Unable to open file %s.\n", fileName.c_str());
+ }
+ else
+ {
+ if (ifs >> contents)
+ {
+ // If the last character is a null terminator; remove it.
+ if (!contents.empty())
+ {
+ char const& back = contents.back();
+ if (back == '\0')
+ contents.pop_back();
+ }
+
+ return contents;
+ }
+ else
+ {
+ std::fprintf(stderr, "Unable to read file %s.\n", fileName.c_str());
+ }
+ }
+
+ return "";
+}
+
} // namespace ipmi
} // namespace google