optimizations of utils
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#ifndef UTILS_HPP
|
||||
#define UTILS_HPP
|
||||
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@@ -9,23 +8,23 @@
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <format>
|
||||
#include <log.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#define FILL_CHAR ' '
|
||||
|
||||
std::string pNum(double num, uint8_t decs = 0);
|
||||
std::string pNum(float num, uint8_t decs = 0);
|
||||
// pretty prints num with given decs, 0 by default. its basically a macro so we inline it
|
||||
inline std::string pNum(auto num, uint8_t decs = 0) { return std::format("{:.{}f}", num, decs); }
|
||||
|
||||
std::string pad(std::string s, uint8_t len, bool ljust = false);
|
||||
std::string center(std::string str, uint8_t len);
|
||||
std::string pBytes(double bytes, bool isKbytes = false);
|
||||
std::string readFile(std::string path, size_t trim = 4096);
|
||||
ssize_t readFile2(std::string path, char *buffer, size_t maxlen);
|
||||
ssize_t readFile2(const char *path, char *buffer, size_t maxlen);
|
||||
int get_val(const std::string &target, const std::string &content);
|
||||
std::string getFirstDir(std::string path);
|
||||
int get_val(std::string_view target, std::string_view content);
|
||||
std::string getFirstDir(std::string_view path);
|
||||
|
||||
#endif
|
||||
@@ -1,15 +1,5 @@
|
||||
#include <utils.hpp>
|
||||
|
||||
// converts number to string with given decimal places
|
||||
std::string pNum(double num, uint8_t decs) {
|
||||
std::stringstream ret;
|
||||
ret << std::fixed << std::setprecision(decs) << num;
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
std::string pNum(float num, uint8_t decs) {
|
||||
return pNum((double)num, decs);
|
||||
}
|
||||
|
||||
// pads given string to length
|
||||
std::string pad(std::string s, uint8_t len, bool ljust) {
|
||||
@@ -33,10 +23,9 @@ std::string pBytes(double bytes, bool isKbytes) {
|
||||
if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes
|
||||
bytes /= 1024.0;
|
||||
if (bytes < 1024) {
|
||||
uint8_t decs = 2;
|
||||
if (bytes > 10) decs = 1;
|
||||
if (bytes > 100) decs = 0;
|
||||
return pNum(bytes, decs) + suffix;
|
||||
if(bytes > 100) return pNum(bytes) + suffix;
|
||||
if(bytes > 10) return pNum(bytes, 1) + suffix;
|
||||
return pNum(bytes, 2) + suffix;
|
||||
}
|
||||
}
|
||||
return "++++";
|
||||
@@ -67,21 +56,19 @@ std::string readFile(std::string path, size_t trim) {
|
||||
return out.substr(0, len - 1);
|
||||
}
|
||||
|
||||
int get_val(const std::string& target, const std::string& content) {
|
||||
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);
|
||||
std::string substr = content.substr(begin, end - begin);
|
||||
result = std::stoi(substr);
|
||||
result = std::strtol(content.substr(begin, end - begin).data(), NULL, 10);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::string getFirstDir(std::string_view path) {
|
||||
// for (const auto& entry : std::filesystem::directory_iterator(path))
|
||||
// return entry.path();
|
||||
std::filesystem::directory_iterator(path)[0]; // can i just do that
|
||||
// return "";
|
||||
std::string getFirstDir(std::string_view path) {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path))
|
||||
return entry.path();
|
||||
return "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user