54 lines
1.4 KiB
C++
54 lines
1.4 KiB
C++
#ifndef UTILS_HPP
|
|
#define UTILS_HPP
|
|
|
|
|
|
#include <charconv>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <string>
|
|
|
|
namespace utils {
|
|
|
|
constexpr char fill_char = ' ';
|
|
|
|
enum just { right,
|
|
left };
|
|
|
|
std::string pad(std::string s, uint8_t len, just j = just::right);
|
|
std::string center(std::string str, uint8_t len);
|
|
std::string pBytes(double bytes, bool isKbytes = false);
|
|
ssize_t readFile(const std::string& path, char* buffer, size_t maxlen);
|
|
ssize_t readFile(const char* path, char* buffer, size_t maxlen);
|
|
std::string getFirstDir(const std::string_view& path);
|
|
|
|
/* reads a number from file REALLY fast */
|
|
template <typename T, size_t N>
|
|
inline void fastReadNum(const std::string& path, char (&buf)[N], T& result) {
|
|
readFile(path, buf, N);
|
|
std::from_chars(buf, buf + N, result);
|
|
}
|
|
|
|
/* reads a number slightly slower and divides it */
|
|
template <uint32_t Divider, typename T, size_t N>
|
|
inline void fastReadNumDiv(const std::string& path, char (&buf)[N], T& result) {
|
|
uint64_t temp{};
|
|
fastReadNum(path, buf, temp);
|
|
if constexpr (std::is_floating_point_v<T>)
|
|
result = temp / static_cast<T>(Divider);
|
|
else
|
|
result = static_cast<T>(temp / Divider);
|
|
}
|
|
|
|
inline std::string pNum(const auto num, const uint8_t decs = 0) {
|
|
return std::format("{:.{}f}", num, decs);
|
|
}
|
|
|
|
inline bool fileExists(const std::string_view& path) {
|
|
return std::filesystem::exists(path);
|
|
}
|
|
|
|
} // namespace utils
|
|
|
|
#endif
|