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/pcie_i2c.cpp b/pcie_i2c.cpp
index c0e7fc7..88eb66c 100644
--- a/pcie_i2c.cpp
+++ b/pcie_i2c.cpp
@@ -17,6 +17,7 @@
#include "pcie_i2c.hpp"
#include "main.hpp"
+#include "util.hpp"
#include <cstdint>
#include <cstring>
@@ -43,36 +44,6 @@
std::vector<std::tuple<uint32_t, std::string>> pcie_i2c_map;
-std::string read_file(const std::string& file_name)
-{
- std::ifstream ifs(file_name);
- std::string file_content;
- if (!ifs.is_open())
- {
- std::fprintf(stderr, "Unable to open file %s.\n", file_name.c_str());
- }
- else
- {
- if (ifs >> file_content)
- {
- // If the last character is a null terminator; remove it.
- if (!file_content.empty())
- {
- char const& back = file_content.back();
- if (back == '\0')
- file_content.pop_back();
- }
- return file_content;
- }
- else
- {
- std::fprintf(stderr, "Unable to read file %s.\n",
- file_name.c_str());
- }
- }
- return "";
-}
-
} // namespace
struct PcieSlotCountRequest
@@ -128,7 +99,7 @@
std::string pcie_slot_path = i2c_dev_path + "/of_node/pcie-slot";
std::string pcie_slot;
// Read the "pcie-slot" name from the "pcie-slot" file.
- pcie_slot = read_file(pcie_slot_path);
+ pcie_slot = readPropertyFile(pcie_slot_path);
if (pcie_slot.empty())
{
continue;
@@ -140,7 +111,7 @@
pcie_slot_full_path.append(pcie_slot);
// Read the "label" which contains the pcie slot name.
pcie_slot_full_path.append("/label");
- pcie_slot_name = read_file(pcie_slot_full_path);
+ pcie_slot_name = readPropertyFile(pcie_slot_full_path);
if (pcie_slot_name.empty())
{
continue;
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
diff --git a/util.hpp b/util.hpp
index d98a6cc..760a1b2 100644
--- a/util.hpp
+++ b/util.hpp
@@ -17,5 +17,13 @@
*/
nlohmann::json parseConfig(const std::string& file);
+/**
+ * Read a dts property file and return the contents.
+ *
+ * @param[in] file - the path to the file to parse.
+ * @return the property value or an empty string on failure.
+ */
+std::string readPropertyFile(const std::string& fileName);
+
} // namespace ipmi
} // namespace google