xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision 26b815fa)
1*26b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
36292aeedSMatthew Barth #include <fstream>
46292aeedSMatthew Barth #include <string>
56292aeedSMatthew Barth 
66292aeedSMatthew Barth template <typename T>
76292aeedSMatthew Barth void read_sysfs(const std::string& path, T& val)
86292aeedSMatthew Barth {
96292aeedSMatthew Barth     std::ifstream s(path);
106292aeedSMatthew Barth     s >> val;
116292aeedSMatthew Barth }
126292aeedSMatthew Barth 
136292aeedSMatthew Barth template <typename T>
146292aeedSMatthew Barth void write_sysfs(const std::string& path, const T& val)
156292aeedSMatthew Barth {
166292aeedSMatthew Barth     std::ofstream s(path);
176292aeedSMatthew Barth     s << val;
186292aeedSMatthew Barth }
196292aeedSMatthew Barth 
206292aeedSMatthew Barth const std::string make_sysfs_path(const std::string& path,
216292aeedSMatthew Barth                                   const std::string& type,
226292aeedSMatthew Barth                                   const std::string& id,
236292aeedSMatthew Barth                                   const std::string& entry)
246292aeedSMatthew Barth {
256292aeedSMatthew Barth     using namespace std::literals;
266292aeedSMatthew Barth 
276292aeedSMatthew Barth     return path + "/"s + type + id + "_"s + entry;
286292aeedSMatthew Barth }
296292aeedSMatthew Barth 
3003476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
31