sane makefile, sane structure, lots of updates
This commit is contained in:
44
src/utils.cpp
Normal file
44
src/utils.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utils.hpp>
|
||||
|
||||
/* SENSOR GETTERS + UTILS*/
|
||||
std::string readFile(std::string path, uint8_t trim) {
|
||||
std::string ret;
|
||||
std::stringstream ss;
|
||||
std::ifstream fs(path);
|
||||
if (!fs.good()) {
|
||||
std::cerr << "Error reading " << path << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
ss << fs.rdbuf();
|
||||
fs.close();
|
||||
ret = ss.str();
|
||||
if (ret.size() < trim) return ret;
|
||||
return ret.substr(0, ret.size() - trim - 1);
|
||||
}
|
||||
|
||||
int get_val(const std::string &target, const std::string &content) {
|
||||
int result = -1;
|
||||
std::size_t start = content.find(target);
|
||||
if (start != std::string::npos) {
|
||||
int begin = start + target.length();
|
||||
std::size_t end = content.find("kB", start);
|
||||
std::string substr = content.substr(begin, end - begin);
|
||||
result = std::stoi(substr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string findHwmonPath(std::string module) {
|
||||
uint8_t id = 0;
|
||||
for (; id < 10; id++)
|
||||
if (readFile("/sys/class/hwmon/hwmon" + std::to_string(id) + "/name") == module) {
|
||||
std::cout << ("[Hwmon] Detected " + module + " path is " + "/sys/class/hwmon/hwmon" + std::to_string(id)) << std::endl;
|
||||
return "/sys/class/hwmon/hwmon" + std::to_string(id);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
Reference in New Issue
Block a user