6 Commits

Author SHA1 Message Date
ad7872a0e3 refactor
Some checks failed
BlockStorage/repertory_osx_builds/pipeline/head There was a failure building this commit
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good
2023-12-14 09:11:49 -06:00
f3ea2ccc21 fix warnings 2023-12-14 09:06:30 -06:00
e959a9e795 fix 2023-12-14 09:00:21 -06:00
3fe5eac56d fix 2023-12-14 08:58:33 -06:00
67191be78d fix encryption provider 2023-12-14 08:55:04 -06:00
00cfb67b64 fix file read 2023-12-14 08:46:44 -06:00
10 changed files with 155 additions and 138 deletions

View File

@ -123,6 +123,7 @@ template <typename data_type>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/bind/bind.hpp> #include <boost/bind/bind.hpp>
#include <boost/dynamic_bitset.hpp> #include <boost/dynamic_bitset.hpp>
#include <boost/dynamic_bitset.hpp>
#include <boost/dynamic_bitset/serialization.hpp> #include <boost/dynamic_bitset/serialization.hpp>
#include <boost/endian/conversion.hpp> #include <boost/endian/conversion.hpp>
#include <boost/serialization/vector.hpp> #include <boost/serialization/vector.hpp>

View File

@ -65,11 +65,10 @@ private:
static void create_item_meta(api_meta_map &meta, bool directory, static void create_item_meta(api_meta_map &meta, bool directory,
const api_file &file); const api_file &file);
auto do_directory_operation( auto do_fs_operation(const std::string &api_path, bool directory,
const std::string &api_path, bool directory, std::function<api_error(const encrypt_config &cfg,
std::function<api_error(const encrypt_config &cfg, const std::string &source_path)>
const std::string &source_path)> callback) const -> api_error;
callback) const -> api_error;
auto auto
process_directory_entry(const std::filesystem::directory_entry &dir_entry, process_directory_entry(const std::filesystem::directory_entry &dir_entry,

View File

@ -27,6 +27,11 @@
namespace repertory { namespace repertory {
class native_file final { class native_file final {
public: public:
native_file(const native_file &) = delete;
native_file(native_file &&) = delete;
auto operator=(const native_file &) -> native_file & = delete;
auto operator=(native_file &&) -> native_file & = delete;
using native_file_ptr = std::shared_ptr<native_file>; using native_file_ptr = std::shared_ptr<native_file>;
public: public:
@ -34,22 +39,21 @@ public:
return std::shared_ptr<native_file>(new native_file(handle)); return std::shared_ptr<native_file>(new native_file(handle));
} }
[[nodiscard]] static auto clone(const native_file_ptr &nativeFile) [[nodiscard]] static auto clone(const native_file_ptr &ptr)
-> native_file_ptr; -> native_file_ptr;
[[nodiscard]] static auto create_or_open(const std::string &source_path, [[nodiscard]] static auto create_or_open(const std::string &source_path,
bool should_chmod, bool read_only, native_file_ptr &ptr)
native_file_ptr &nf) -> api_error; -> api_error;
[[nodiscard]] static auto create_or_open(const std::string &source_path, [[nodiscard]] static auto create_or_open(const std::string &source_path,
native_file_ptr &nf) -> api_error; native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path, [[nodiscard]] static auto open(const std::string &source_path,
native_file_ptr &nf) -> api_error; native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path, [[nodiscard]] static auto open(const std::string &source_path, bool read_only,
bool should_chmod, native_file_ptr &nf) native_file_ptr &ptr) -> api_error;
-> api_error;
private: private:
explicit native_file(const native_handle &handle) : handle_(handle) {} explicit native_file(const native_handle &handle) : handle_(handle) {}
@ -71,7 +75,7 @@ public:
void close(); void close();
[[nodiscard]] auto copy_from(const native_file_ptr &source) -> bool; [[nodiscard]] auto copy_from(const native_file_ptr &ptr) -> bool;
[[nodiscard]] auto copy_from(const std::string &path) -> bool; [[nodiscard]] auto copy_from(const std::string &path) -> bool;

View File

@ -22,15 +22,10 @@
#ifndef INCLUDE_UTILS_STRING_UTILS_HPP_ #ifndef INCLUDE_UTILS_STRING_UTILS_HPP_
#define INCLUDE_UTILS_STRING_UTILS_HPP_ #define INCLUDE_UTILS_STRING_UTILS_HPP_
#include <boost/dynamic_bitset.hpp>
#include <string>
#include <string_view>
#include <vector>
namespace repertory::utils::string { namespace repertory::utils::string {
// Prototypes // Prototypes
constexpr auto begins_with(std::string_view str, std::string_view val) -> bool { constexpr auto begins_with(std::string_view str, std::string_view val) -> bool {
return (str.find(val) == 0u); return (str.find(val) == 0U);
} }
constexpr auto contains(std::string_view str, std::string_view search) -> bool { constexpr auto contains(std::string_view str, std::string_view search) -> bool {
@ -48,14 +43,15 @@ constexpr auto contains(std::string_view str, std::string_view search) -> bool {
[[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring; [[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring;
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view s) -> bool; [[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view str)
-> bool;
[[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim) [[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim)
-> std::string; -> std::string;
auto left_trim(std::string &s) -> std::string &; auto left_trim(std::string &str) -> std::string &;
auto left_trim(std::string &s, const char &c) -> std::string &; auto left_trim(std::string &str, const char &trim_ch) -> std::string &;
auto replace(std::string &src, const char &character, const char &with) auto replace(std::string &src, const char &character, const char &with)
-> std::string &; -> std::string &;
@ -70,9 +66,9 @@ auto replace(std::string &src, const std::string &find, const std::string &with,
const std::string &with, size_t start_pos = 0) const std::string &with, size_t start_pos = 0)
-> std::string; -> std::string;
auto right_trim(std::string &s) -> std::string &; auto right_trim(std::string &str) -> std::string &;
auto right_trim(std::string &s, const char &c) -> std::string &; auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto split(const std::string &str, const char &delim, [[nodiscard]] auto split(const std::string &str, const char &delim,
bool should_trim = true) -> std::vector<std::string>; bool should_trim = true) -> std::vector<std::string>;
@ -108,11 +104,12 @@ auto right_trim(std::string &s, const char &c) -> std::string &;
auto trim(std::string &str) -> std::string &; auto trim(std::string &str) -> std::string &;
auto trim(std::string &str, const char &c) -> std::string &; auto trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto trim_copy(std::string str) -> std::string; [[nodiscard]] auto trim_copy(std::string str) -> std::string;
[[nodiscard]] auto trim_copy(std::string str, const char &c) -> std::string; [[nodiscard]] auto trim_copy(std::string str, const char &trim_ch)
-> std::string;
} // namespace repertory::utils::string } // namespace repertory::utils::string
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_ #endif // INCLUDE_UTILS_STRING_UTILS_HPP_

View File

@ -64,8 +64,8 @@ file_manager::open_file::open_file(
} }
if (not fsi.directory) { if (not fsi.directory) {
set_api_error(native_file::create_or_open( set_api_error(native_file::create_or_open(fsi.source_path,
fsi.source_path, not provider_.is_direct_only(), nf_)); provider_.is_direct_only(), nf_));
if (get_api_error() == api_error::success) { if (get_api_error() == api_error::success) {
if (read_state.has_value()) { if (read_state.has_value()) {
read_state_ = read_state.value(); read_state_ = read_state.value();

View File

@ -169,7 +169,7 @@ auto encrypt_provider::create_directory(const std::string &api_path,
return api_error::not_implemented; return api_error::not_implemented;
} }
auto encrypt_provider::do_directory_operation( auto encrypt_provider::do_fs_operation(
const std::string &api_path, bool directory, const std::string &api_path, bool directory,
std::function<api_error(const encrypt_config &cfg, std::function<api_error(const encrypt_config &cfg,
const std::string &source_path)> const std::string &source_path)>
@ -180,7 +180,8 @@ auto encrypt_provider::do_directory_operation(
auto res = auto res =
utils::encryption::decrypt_file_path(cfg.encryption_token, source_path); utils::encryption::decrypt_file_path(cfg.encryption_token, source_path);
if (res != api_error::success) { if (res != api_error::success) {
return res; return directory ? api_error::directory_not_found
: api_error::item_not_found;
} }
} }
@ -189,7 +190,8 @@ auto encrypt_provider::do_directory_operation(
if (source_path != cfg.path && if (source_path != cfg.path &&
not source_path.starts_with(cfg.path + not source_path.starts_with(cfg.path +
utils::path::directory_seperator)) { utils::path::directory_seperator)) {
return api_error::directory_not_found; return directory ? api_error::directory_not_found
: api_error::item_not_found;
} }
auto exists = utils::file::is_file(source_path); auto exists = utils::file::is_file(source_path);
@ -256,7 +258,7 @@ auto encrypt_provider::get_directory_item_count(
static const auto *function_name = __FUNCTION__; static const auto *function_name = __FUNCTION__;
std::uint64_t count{}; std::uint64_t count{};
auto res = do_directory_operation( auto res = do_fs_operation(
api_path, true, api_path, true,
[&api_path, &count](const encrypt_config & /* cfg */, [&api_path, &count](const encrypt_config & /* cfg */,
const std::string &source_path) -> api_error { const std::string &source_path) -> api_error {
@ -285,7 +287,7 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
-> api_error { -> api_error {
static const auto *function_name = __FUNCTION__; static const auto *function_name = __FUNCTION__;
return do_directory_operation( return do_fs_operation(
api_path, true, api_path, true,
[this, &list](const encrypt_config &cfg, [this, &list](const encrypt_config &cfg,
const std::string &source_path) -> api_error { const std::string &source_path) -> api_error {

View File

@ -170,8 +170,7 @@ encrypting_reader::encrypting_reader(
: key_(utils::encryption::generate_key(token)), : key_(utils::encryption::generate_key(token)),
stop_requested_(stop_requested), stop_requested_(stop_requested),
error_return_(error_return) { error_return_(error_return) {
const auto res = native_file::create_or_open( const auto res = native_file::create_or_open(source_path, true, source_file_);
source_path, not relative_parent_path.has_value(), source_file_);
if (res != api_error::success) { if (res != api_error::success) {
throw std::runtime_error("file open failed|src|" + source_path + '|' + throw std::runtime_error("file open failed|src|" + source_path + '|' +
api_error_to_string(res)); api_error_to_string(res));
@ -223,8 +222,7 @@ encrypting_reader::encrypting_reader(const std::string &encrypted_file_path,
: key_(utils::encryption::generate_key(token)), : key_(utils::encryption::generate_key(token)),
stop_requested_(stop_requested), stop_requested_(stop_requested),
error_return_(error_return) { error_return_(error_return) {
const auto res = const auto res = native_file::create_or_open(source_path, true, source_file_);
native_file::create_or_open(source_path, false, source_file_);
if (res != api_error::success) { if (res != api_error::success) {
throw std::runtime_error("file open failed|src|" + source_path + '|' + throw std::runtime_error("file open failed|src|" + source_path + '|' +
api_error_to_string(res)); api_error_to_string(res));
@ -265,8 +263,7 @@ encrypting_reader::encrypting_reader(
: key_(utils::encryption::generate_key(token)), : key_(utils::encryption::generate_key(token)),
stop_requested_(stop_requested), stop_requested_(stop_requested),
error_return_(error_return) { error_return_(error_return) {
const auto res = const auto res = native_file::create_or_open(source_path, true, source_file_);
native_file::create_or_open(source_path, false, source_file_);
if (res != api_error::success) { if (res != api_error::success) {
throw std::runtime_error("file open failed|src|" + source_path + '|' + throw std::runtime_error("file open failed|src|" + source_path + '|' +
api_error_to_string(res)); api_error_to_string(res));

View File

@ -35,20 +35,21 @@ native_file::~native_file() {
} }
} }
auto native_file::clone(const native_file_ptr &nf) -> native_file_ptr { auto native_file::clone(const native_file_ptr &ptr) -> native_file_ptr {
std::string source_path; std::string source_path;
#ifdef _WIN32 #ifdef _WIN32
source_path.resize(MAX_PATH + 1); source_path.resize(MAX_PATH + 1);
::GetFinalPathNameByHandleA(nf->get_handle(), &source_path[0u], MAX_PATH + 1, ::GetFinalPathNameByHandleA(ptr->get_handle(), source_path.data(),
MAX_PATH + 1,
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
#else #else
source_path.resize(PATH_MAX + 1); source_path.resize(PATH_MAX + 1);
#ifdef __APPLE__ #ifdef __APPLE__
fcntl(nf->get_handle(), F_GETPATH, &source_path[0u]); fcntl(ptr->get_handle(), F_GETPATH, source_path.data());
#else #else
readlink(("/proc/self/fd/" + std::to_string(nf->get_handle())).c_str(), readlink(("/proc/self/fd/" + std::to_string(ptr->get_handle())).c_str(),
&source_path[0u], source_path.size()); source_path.data(), source_path.size());
#endif #endif
#endif #endif
source_path = source_path.c_str(); source_path = source_path.c_str();
@ -63,52 +64,60 @@ auto native_file::clone(const native_file_ptr &nf) -> native_file_ptr {
return clone; return clone;
} }
auto native_file::create_or_open(const std::string &source_path, auto native_file::create_or_open(const std::string &source_path, bool read_only,
[[maybe_unused]] bool should_chmod, native_file_ptr &ptr) -> api_error {
native_file_ptr &nf) -> api_error {
#ifdef _WIN32 #ifdef _WIN32
auto handle = ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE, auto handle =
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, read_only
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr); ? ::CreateFileA(source_path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr)
: ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
#else #else
auto handle = should_chmod ? ::open(source_path.c_str(), auto handle =
O_CREAT | O_RDWR | O_CLOEXEC, 0600u) read_only
: ::open(source_path.c_str(), O_RDWR | O_CLOEXEC); ? ::open(source_path.c_str(), O_CREAT | O_RDONLY | O_CLOEXEC, 0600U)
if (should_chmod) { : ::open(source_path.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600U);
chmod(source_path.c_str(), 0600u); if (not read_only) {
chmod(source_path.c_str(), 0600U);
} }
#endif #endif
nf = native_file::attach(handle); ptr = native_file::attach(handle);
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
: api_error::success); : api_error::success);
} }
auto native_file::create_or_open(const std::string &source_path, auto native_file::create_or_open(const std::string &source_path,
native_file_ptr &nf) -> api_error { native_file_ptr &ptr) -> api_error {
return create_or_open(source_path, true, nf); return create_or_open(source_path, false, ptr);
} }
auto native_file::open(const std::string &source_path, native_file_ptr &nf) auto native_file::open(const std::string &source_path, native_file_ptr &ptr)
-> api_error { -> api_error {
return open(source_path, true, nf); return open(source_path, false, ptr);
} }
auto native_file::open(const std::string &source_path, auto native_file::open(const std::string &source_path, bool read_only,
[[maybe_unused]] bool should_chmod, native_file_ptr &nf) native_file_ptr &ptr) -> api_error {
-> api_error {
#ifdef _WIN32 #ifdef _WIN32
auto handle = ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE, auto handle =
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, read_only
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr); ? ::CreateFileA(source_path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr)
: ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
#else #else
auto handle = should_chmod auto handle = read_only ? ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC)
? ::open(source_path.c_str(), O_RDWR | O_CLOEXEC, 0600u) : ::open(source_path.c_str(), O_RDWR | O_CLOEXEC);
: ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC); if (not read_only) {
if (should_chmod) { chmod(source_path.c_str(), 0600U);
chmod(source_path.c_str(), 0600u);
} }
#endif #endif
nf = native_file::attach(handle); ptr = native_file::attach(handle);
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
: api_error::success); : api_error::success);
} }
@ -139,19 +148,19 @@ void native_file::close() {
} }
} }
auto native_file::copy_from(const native_file_ptr &nf) -> bool { auto native_file::copy_from(const native_file_ptr &ptr) -> bool {
auto ret = false; std::uint64_t file_size{};
std::uint64_t file_size = 0u; auto ret = ptr->get_file_size(file_size);
if ((ret = nf->get_file_size(file_size))) { if (ret) {
data_buffer buffer; data_buffer buffer;
buffer.resize(65536u * 2u); buffer.resize(65536ULL * 2ULL);
std::uint64_t offset = 0u; std::uint64_t offset{};
while (ret && (file_size > 0u)) { while (ret && (file_size > 0U)) {
std::size_t bytes_read{}; std::size_t bytes_read{};
if ((ret = nf->read_bytes(&buffer[0u], buffer.size(), offset, ret = ptr->read_bytes(buffer.data(), buffer.size(), offset, bytes_read);
bytes_read))) { if (ret) {
std::size_t bytes_written = 0u; std::size_t bytes_written{};
ret = write_bytes(&buffer[0u], bytes_read, offset, bytes_written); ret = write_bytes(buffer.data(), bytes_read, offset, bytes_written);
file_size -= bytes_read; file_size -= bytes_read;
offset += bytes_read; offset += bytes_read;
} }
@ -164,10 +173,10 @@ auto native_file::copy_from(const native_file_ptr &nf) -> bool {
auto native_file::copy_from(const std::string &path) -> bool { auto native_file::copy_from(const std::string &path) -> bool {
auto ret = false; auto ret = false;
native_file_ptr nf; native_file_ptr ptr;
if (native_file::create_or_open(path, nf) == api_error ::success) { if (native_file::create_or_open(path, ptr) == api_error ::success) {
ret = copy_from(nf); ret = copy_from(ptr);
nf->close(); ptr->close();
} }
return ret; return ret;
@ -194,11 +203,12 @@ auto native_file::get_file_size(std::uint64_t &file_size) -> bool {
struct stat st {}; struct stat st {};
if (fstat(handle_, &st) >= 0) { if (fstat(handle_, &st) >= 0) {
#else #else
struct stat64 st {}; struct stat64 unix_st {};
if (fstat64(handle_, &st) >= 0) { if (fstat64(handle_, &unix_st) >= 0) {
#endif #endif
if ((ret = (st.st_size >= 0))) { ret = (unix_st.st_size >= 0);
file_size = static_cast<uint64_t>(st.st_size); if (ret) {
file_size = static_cast<uint64_t>(unix_st.st_size);
} }
} }
#endif #endif
@ -289,7 +299,7 @@ auto native_file::write_bytes(const char *buffer, std::size_t write_size,
std::uint64_t write_offset, std::uint64_t write_offset,
std::size_t &bytes_written) -> bool { std::size_t &bytes_written) -> bool {
bytes_written = 0U; bytes_written = 0U;
ssize_t result = 0U; ssize_t result{};
do { do {
result = result =
pwrite64(handle_, &buffer[bytes_written], write_size - bytes_written, pwrite64(handle_, &buffer[bytes_written], write_size - bytes_written,

View File

@ -30,13 +30,15 @@ namespace repertory::utils::string {
return std::equal(val.rbegin(), val.rend(), str.rbegin()); return std::equal(val.rbegin(), val.rend(), str.rbegin());
} }
auto from_bool(bool val) -> std::string { return std::to_string(val); } auto from_bool(bool val) -> std::string {
return std::to_string(static_cast<int>(val));
}
auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string { auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string {
std::stringstream ss; std::stringstream stream;
boost::archive::text_oarchive archive(ss); boost::archive::text_oarchive archive(stream);
archive << bitset; archive << bitset;
return ss.str(); return stream.str();
} }
auto from_utf8(const std::string &str) -> std::wstring { auto from_utf8(const std::string &str) -> std::wstring {
@ -46,27 +48,28 @@ auto from_utf8(const std::string &str) -> std::wstring {
.from_bytes(str); .from_bytes(str);
} }
/* constexpr c++20 */ auto is_numeric(std::string_view s) -> bool { /* constexpr c++20 */ auto is_numeric(std::string_view str) -> bool {
if ((s.length() > 1u) && (s[0u] == '+' || s[0u] == '-')) { if ((str.length() > 1U) && (str[0U] == '+' || str[0U] == '-')) {
s = s.substr(1u); str = str.substr(1U);
} }
if (s.empty()) { if (str.empty()) {
return false; return false;
} }
auto has_decimal = false; auto has_decimal = false;
return std::find_if( return std::find_if(str.begin(), str.end(),
s.begin(), s.end(), [&has_decimal](
[&has_decimal](const std::string_view::value_type &c) -> bool { const std::string_view::value_type &cur_ch) -> bool {
if (has_decimal && c == '.') { if (has_decimal && cur_ch == '.') {
return true; return true;
} }
if ((has_decimal = has_decimal || c == '.')) { has_decimal = has_decimal || cur_ch == '.';
return false; if (has_decimal) {
} return false;
return not std::isdigit(c); }
}) == s.end(); return std::isdigit(cur_ch) == 0;
}) == str.end();
} }
auto join(const std::vector<std::string> &arr, const char &delim) auto join(const std::vector<std::string> &arr, const char &delim)
@ -76,15 +79,17 @@ auto join(const std::vector<std::string> &arr, const char &delim)
} }
return std::accumulate( return std::accumulate(
std::next(arr.begin()), arr.end(), arr[0u], std::next(arr.begin()), arr.end(), arr[0U],
[&delim](auto s, const auto &v) { return s + delim + v; }); [&delim](auto str, const auto &cur) { return str + delim + cur; });
} }
auto left_trim(std::string &s) -> std::string & { return left_trim(s, ' '); } auto left_trim(std::string &str) -> std::string & {
return left_trim(str, ' ');
}
auto left_trim(std::string &s, const char &c) -> std::string & { auto left_trim(std::string &str, const char &trim_ch) -> std::string & {
s.erase(0, s.find_first_not_of(c)); str.erase(0, str.find_first_not_of(trim_ch));
return s; return str;
} }
auto replace(std::string &src, const char &character, const char &with) auto replace(std::string &src, const char &character, const char &with)
@ -115,47 +120,49 @@ auto replace_copy(std::string src, const std::string &find,
return replace(src, find, with, start_pos); return replace(src, find, with, start_pos);
} }
auto right_trim(std::string &s) -> std::string & { return right_trim(s, ' '); } auto right_trim(std::string &str) -> std::string & {
return right_trim(str, ' ');
}
auto right_trim(std::string &s, const char &c) -> std::string & { auto right_trim(std::string &str, const char &trim_ch) -> std::string & {
s.erase(s.find_last_not_of(c) + 1); str.erase(str.find_last_not_of(trim_ch) + 1);
return s; return str;
} }
auto split(const std::string &str, const char &delim, bool should_trim) auto split(const std::string &str, const char &delim, bool should_trim)
-> std::vector<std::string> { -> std::vector<std::string> {
std::vector<std::string> ret; std::vector<std::string> ret;
std::stringstream ss(str); std::stringstream stream(str);
std::string item; std::string item;
while (std::getline(ss, item, delim)) { while (std::getline(stream, item, delim)) {
ret.push_back(should_trim ? trim(item) : item); ret.push_back(should_trim ? trim(item) : item);
} }
return ret; return ret;
} }
auto to_bool(std::string val) -> bool { auto to_bool(std::string val) -> bool {
auto b = false; auto ret = false;
trim(val); trim(val);
if (is_numeric(val)) { if (is_numeric(val)) {
if (contains(val, ".")) { if (contains(val, ".")) {
b = (to_double(val) != 0.0); ret = (to_double(val) != 0.0);
} else { } else {
std::istringstream(val) >> b; std::istringstream(val) >> ret;
} }
} else { } else {
std::istringstream(to_lower(val)) >> std::boolalpha >> b; std::istringstream(to_lower(val)) >> std::boolalpha >> ret;
} }
return b; return ret;
} }
auto to_double(const std::string &str) -> double { return std::stod(str); } auto to_double(const std::string &str) -> double { return std::stod(str); }
auto to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<> { auto to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<> {
std::stringstream ss(val); std::stringstream stream(val);
boost::dynamic_bitset<> bitset; boost::dynamic_bitset<> bitset;
boost::archive::text_iarchive archive(ss); boost::archive::text_iarchive archive(stream);
archive >> bitset; archive >> bitset;
return bitset; return bitset;
} }
@ -209,15 +216,15 @@ auto trim(std::string &str) -> std::string & {
return right_trim(left_trim(str)); return right_trim(left_trim(str));
} }
auto trim(std::string &str, const char &c) -> std::string & { auto trim(std::string &str, const char &trim_ch) -> std::string & {
return right_trim(left_trim(str, c), c); return right_trim(left_trim(str, trim_ch), trim_ch);
} }
auto trim_copy(std::string str) -> std::string { auto trim_copy(std::string str) -> std::string {
return right_trim(left_trim(str)); return right_trim(left_trim(str));
} }
auto trim_copy(std::string str, const char &c) -> std::string { auto trim_copy(std::string str, const char &trim_ch) -> std::string {
return right_trim(left_trim(str, c), c); return right_trim(left_trim(str, trim_ch), trim_ch);
} }
} // namespace repertory::utils::string } // namespace repertory::utils::string

View File

@ -226,14 +226,14 @@ auto get_file_time_now() -> std::uint64_t {
void get_local_time_now(struct tm &local_time) { void get_local_time_now(struct tm &local_time) {
memset(&local_time, 0, sizeof(local_time)); memset(&local_time, 0, sizeof(local_time));
static std::mutex mtx{};
mutex_lock lock{mtx};
const auto now = const auto now =
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
#ifdef _WIN32 #ifdef _WIN32
localtime_s(&local_time, &now); localtime_s(&local_time, &now);
#else #else
static std::mutex mtx{};
mutex_lock lock{mtx};
const auto *tmp = std::localtime(&now); const auto *tmp = std::localtime(&now);
if (tmp != nullptr) { if (tmp != nullptr) {
memcpy(&local_time, tmp, sizeof(local_time)); memcpy(&local_time, tmp, sizeof(local_time));