#ifndef UTILS_HPP #define UTILS_HPP #include #include #include #include #include #include #include namespace utils { constexpr char fill_char = ' '; enum just { right, left }; std::string pad(std::string s, uint8_t len, just j = right); std::string center(std::string str, uint8_t len); std::string pBytes(double bytes, bool isKbytes = false); std::string getFirstDir(const std::string_view& path); /* reads file into a std::array buffer */ template inline ssize_t readFile(const std::string& path, std::array& buffer) { const int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); if (fd < 0) { applog(LOG::ERR, "Error reading {}", path); return 0; } const ssize_t len = read(fd, buffer.data(), buffer.size()); if (len >= 0) buffer[len] = '\0'; close(fd); return len; } /* reads a number fast and divides it if needed */ template inline void fastReadNum(const std::string& path, std::array& buf, T& result) { readFile(path, buf); if constexpr (Divider == 1) { std::from_chars(buf.data(), buf.data() + N, result); return; } uint64_t temp{}; std::from_chars(buf.data(), buf.data() + N, temp); if constexpr (std::is_floating_point_v) result = temp / static_cast(Divider); else result = static_cast(temp / Divider); } inline std::string pNum(const auto num, const uint8_t decs = 0) { static std::array buf; auto [ptr, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), num, std::chars_format::fixed, decs); return std::string(buf.data(), ptr); } inline bool fileExists(const std::string_view& path) { return std::filesystem::exists(path); } } // namespace utils #endif