rename funcs, move get_val

This commit is contained in:
2025-12-14 14:45:06 +01:00
parent cd94018426
commit a271711441
5 changed files with 28 additions and 29 deletions

View File

@@ -43,7 +43,7 @@ std::string pBytes(double bytes, bool isKbytes) {
}
/* SENSOR GETTERS + UTILS*/
ssize_t readFile2(const char* path, char* buffer, size_t maxlen) {
ssize_t readFile(const char* path, char* buffer, size_t maxlen) {
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
log(LOG::ERR, "readFile2", "Error reading {}", path);
@@ -55,30 +55,19 @@ ssize_t readFile2(const char* path, char* buffer, size_t maxlen) {
return len;
}
ssize_t readFile2(std::string path, char* buffer, size_t maxlen) {
return readFile2(path.c_str(), buffer, maxlen);
ssize_t readFile(std::string path, char* buffer, size_t maxlen) {
return readFile(path.c_str(), buffer, maxlen);
}
std::string readFile(std::string path, size_t trim) {
std::string readFileToStr(std::string path, size_t trim) {
char* buf = new char[trim + 2];
ssize_t len = readFile2(path, buf, trim + 2);
ssize_t len = readFile(path, buf, trim + 2);
std::string out(buf);
delete[] buf;
if (!len) return "0";
return out.substr(0, len - 1);
}
int get_val(std::string_view target, std::string_view 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);
result = std::strtol(content.substr(begin, end - begin).data(), NULL, 10);
}
return result;
}
std::string getFirstDir(std::string_view path) {
for (const auto& entry : std::filesystem::directory_iterator(path))
return entry.path();