simplify readFile, use std::array instead of c-style bufs
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
#ifndef UTILS_HPP
|
#ifndef UTILS_HPP
|
||||||
#define UTILS_HPP
|
#define UTILS_HPP
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -10,27 +12,38 @@
|
|||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
constexpr char fill_char = ' ';
|
constexpr char fill_char = ' ';
|
||||||
|
enum just { right, left };
|
||||||
|
|
||||||
enum just { right,
|
std::string pad(std::string s, uint8_t len, just j = 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 center(std::string str, uint8_t len);
|
||||||
std::string pBytes(double bytes, bool isKbytes = false);
|
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);
|
std::string getFirstDir(const std::string_view& path);
|
||||||
|
|
||||||
|
/* reads file into a std::array buffer */
|
||||||
|
template <size_t N>
|
||||||
|
inline ssize_t readFile(const std::string& path, std::array<char, N>& 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 */
|
/* reads a number fast and divides it if needed */
|
||||||
template <uint32_t Divider = 1, typename T, size_t N>
|
template <uint32_t Divider = 1, typename T, size_t N>
|
||||||
inline void fastReadNum(const std::string& path, char (&buf)[N], T& result) {
|
inline void fastReadNum(const std::string& path, std::array<char, N>& buf, T& result) {
|
||||||
readFile(path, buf, N);
|
readFile(path, buf);
|
||||||
if constexpr (Divider == 1) {
|
if constexpr (Divider == 1) {
|
||||||
std::from_chars(buf, buf + N, result);
|
std::from_chars(buf.data(), buf.data() + N, result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint64_t temp{};
|
uint64_t temp{};
|
||||||
std::from_chars(buf, buf + N, temp);
|
std::from_chars(buf.data(), buf.data() + N, temp);
|
||||||
if constexpr (std::is_floating_point_v<T>)
|
if constexpr (std::is_floating_point_v<T>)
|
||||||
result = temp / static_cast<T>(Divider);
|
result = temp / static_cast<T>(Divider);
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <format>
|
|
||||||
#include <log.hpp>
|
#include <log.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@@ -41,23 +37,6 @@ std::string pBytes(double bytes, const bool isKbytes) {
|
|||||||
return "++++";
|
return "++++";
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reads maxlen of chars of the file in path into buffer */
|
|
||||||
ssize_t readFile(const char* path, char* buffer, const size_t maxlen) {
|
|
||||||
const int fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
||||||
if (fd < 0) {
|
|
||||||
applog(LOG::ERR, "Error reading {}", path);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
const ssize_t len = read(fd, buffer, maxlen - 1);
|
|
||||||
if (len >= 0) buffer[len] = '\0';
|
|
||||||
close(fd);
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* overload so it takes std::string as path */
|
|
||||||
ssize_t readFile(const std::string& path, char* buffer, const size_t maxlen) {
|
|
||||||
return readFile(path.c_str(), buffer, maxlen);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* finds first dir in path and returns it. don't refactor, you don't understand it, its fast enough, and you only use it on init */
|
/* finds first dir in path and returns it. don't refactor, you don't understand it, its fast enough, and you only use it on init */
|
||||||
std::string getFirstDir(const std::string_view& path) {
|
std::string getFirstDir(const std::string_view& path) {
|
||||||
|
|||||||
Reference in New Issue
Block a user