Compare commits
6 Commits
35aa8f5a94
...
ad7872a0e3
Author | SHA1 | Date | |
---|---|---|---|
ad7872a0e3 | |||
f3ea2ccc21 | |||
e959a9e795 | |||
3fe5eac56d | |||
67191be78d | |||
00cfb67b64 |
@ -123,6 +123,7 @@ template <typename data_type>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/dynamic_bitset.hpp>
|
||||
#include <boost/dynamic_bitset.hpp>
|
||||
#include <boost/dynamic_bitset/serialization.hpp>
|
||||
#include <boost/endian/conversion.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
|
@ -65,11 +65,10 @@ private:
|
||||
static void create_item_meta(api_meta_map &meta, bool directory,
|
||||
const api_file &file);
|
||||
|
||||
auto do_directory_operation(
|
||||
const std::string &api_path, bool directory,
|
||||
std::function<api_error(const encrypt_config &cfg,
|
||||
const std::string &source_path)>
|
||||
callback) const -> api_error;
|
||||
auto do_fs_operation(const std::string &api_path, bool directory,
|
||||
std::function<api_error(const encrypt_config &cfg,
|
||||
const std::string &source_path)>
|
||||
callback) const -> api_error;
|
||||
|
||||
auto
|
||||
process_directory_entry(const std::filesystem::directory_entry &dir_entry,
|
||||
|
@ -27,6 +27,11 @@
|
||||
namespace repertory {
|
||||
class native_file final {
|
||||
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>;
|
||||
|
||||
public:
|
||||
@ -34,22 +39,21 @@ public:
|
||||
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;
|
||||
|
||||
[[nodiscard]] static auto create_or_open(const std::string &source_path,
|
||||
bool should_chmod,
|
||||
native_file_ptr &nf) -> api_error;
|
||||
bool read_only, native_file_ptr &ptr)
|
||||
-> api_error;
|
||||
|
||||
[[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,
|
||||
native_file_ptr &nf) -> api_error;
|
||||
native_file_ptr &ptr) -> api_error;
|
||||
|
||||
[[nodiscard]] static auto open(const std::string &source_path,
|
||||
bool should_chmod, native_file_ptr &nf)
|
||||
-> api_error;
|
||||
[[nodiscard]] static auto open(const std::string &source_path, bool read_only,
|
||||
native_file_ptr &ptr) -> api_error;
|
||||
|
||||
private:
|
||||
explicit native_file(const native_handle &handle) : handle_(handle) {}
|
||||
@ -71,7 +75,7 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
|
@ -22,15 +22,10 @@
|
||||
#ifndef 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 {
|
||||
// Prototypes
|
||||
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 {
|
||||
@ -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]] /* 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)
|
||||
-> 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)
|
||||
-> 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)
|
||||
-> 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,
|
||||
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, 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, const char &c) -> std::string;
|
||||
[[nodiscard]] auto trim_copy(std::string str, const char &trim_ch)
|
||||
-> std::string;
|
||||
} // namespace repertory::utils::string
|
||||
|
||||
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_
|
||||
|
@ -64,8 +64,8 @@ file_manager::open_file::open_file(
|
||||
}
|
||||
|
||||
if (not fsi.directory) {
|
||||
set_api_error(native_file::create_or_open(
|
||||
fsi.source_path, not provider_.is_direct_only(), nf_));
|
||||
set_api_error(native_file::create_or_open(fsi.source_path,
|
||||
provider_.is_direct_only(), nf_));
|
||||
if (get_api_error() == api_error::success) {
|
||||
if (read_state.has_value()) {
|
||||
read_state_ = read_state.value();
|
||||
|
@ -169,7 +169,7 @@ auto encrypt_provider::create_directory(const std::string &api_path,
|
||||
return api_error::not_implemented;
|
||||
}
|
||||
|
||||
auto encrypt_provider::do_directory_operation(
|
||||
auto encrypt_provider::do_fs_operation(
|
||||
const std::string &api_path, bool directory,
|
||||
std::function<api_error(const encrypt_config &cfg,
|
||||
const std::string &source_path)>
|
||||
@ -180,7 +180,8 @@ auto encrypt_provider::do_directory_operation(
|
||||
auto res =
|
||||
utils::encryption::decrypt_file_path(cfg.encryption_token, source_path);
|
||||
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 &&
|
||||
not source_path.starts_with(cfg.path +
|
||||
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);
|
||||
@ -256,7 +258,7 @@ auto encrypt_provider::get_directory_item_count(
|
||||
static const auto *function_name = __FUNCTION__;
|
||||
|
||||
std::uint64_t count{};
|
||||
auto res = do_directory_operation(
|
||||
auto res = do_fs_operation(
|
||||
api_path, true,
|
||||
[&api_path, &count](const encrypt_config & /* cfg */,
|
||||
const std::string &source_path) -> api_error {
|
||||
@ -285,7 +287,7 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
|
||||
-> api_error {
|
||||
static const auto *function_name = __FUNCTION__;
|
||||
|
||||
return do_directory_operation(
|
||||
return do_fs_operation(
|
||||
api_path, true,
|
||||
[this, &list](const encrypt_config &cfg,
|
||||
const std::string &source_path) -> api_error {
|
||||
|
@ -170,8 +170,7 @@ encrypting_reader::encrypting_reader(
|
||||
: key_(utils::encryption::generate_key(token)),
|
||||
stop_requested_(stop_requested),
|
||||
error_return_(error_return) {
|
||||
const auto res = native_file::create_or_open(
|
||||
source_path, not relative_parent_path.has_value(), source_file_);
|
||||
const auto res = native_file::create_or_open(source_path, true, source_file_);
|
||||
if (res != api_error::success) {
|
||||
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
||||
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)),
|
||||
stop_requested_(stop_requested),
|
||||
error_return_(error_return) {
|
||||
const auto res =
|
||||
native_file::create_or_open(source_path, false, source_file_);
|
||||
const auto res = native_file::create_or_open(source_path, true, source_file_);
|
||||
if (res != api_error::success) {
|
||||
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
||||
api_error_to_string(res));
|
||||
@ -265,8 +263,7 @@ encrypting_reader::encrypting_reader(
|
||||
: key_(utils::encryption::generate_key(token)),
|
||||
stop_requested_(stop_requested),
|
||||
error_return_(error_return) {
|
||||
const auto res =
|
||||
native_file::create_or_open(source_path, false, source_file_);
|
||||
const auto res = native_file::create_or_open(source_path, true, source_file_);
|
||||
if (res != api_error::success) {
|
||||
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
||||
api_error_to_string(res));
|
||||
|
@ -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;
|
||||
|
||||
#ifdef _WIN32
|
||||
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);
|
||||
#else
|
||||
source_path.resize(PATH_MAX + 1);
|
||||
#ifdef __APPLE__
|
||||
fcntl(nf->get_handle(), F_GETPATH, &source_path[0u]);
|
||||
fcntl(ptr->get_handle(), F_GETPATH, source_path.data());
|
||||
#else
|
||||
readlink(("/proc/self/fd/" + std::to_string(nf->get_handle())).c_str(),
|
||||
&source_path[0u], source_path.size());
|
||||
readlink(("/proc/self/fd/" + std::to_string(ptr->get_handle())).c_str(),
|
||||
source_path.data(), source_path.size());
|
||||
#endif
|
||||
#endif
|
||||
source_path = source_path.c_str();
|
||||
@ -63,52 +64,60 @@ auto native_file::clone(const native_file_ptr &nf) -> native_file_ptr {
|
||||
return clone;
|
||||
}
|
||||
|
||||
auto native_file::create_or_open(const std::string &source_path,
|
||||
[[maybe_unused]] bool should_chmod,
|
||||
native_file_ptr &nf) -> api_error {
|
||||
auto native_file::create_or_open(const std::string &source_path, bool read_only,
|
||||
native_file_ptr &ptr) -> api_error {
|
||||
#ifdef _WIN32
|
||||
auto handle = ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
||||
auto handle =
|
||||
read_only
|
||||
? ::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
|
||||
auto handle = should_chmod ? ::open(source_path.c_str(),
|
||||
O_CREAT | O_RDWR | O_CLOEXEC, 0600u)
|
||||
: ::open(source_path.c_str(), O_RDWR | O_CLOEXEC);
|
||||
if (should_chmod) {
|
||||
chmod(source_path.c_str(), 0600u);
|
||||
auto handle =
|
||||
read_only
|
||||
? ::open(source_path.c_str(), O_CREAT | O_RDONLY | O_CLOEXEC, 0600U)
|
||||
: ::open(source_path.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600U);
|
||||
if (not read_only) {
|
||||
chmod(source_path.c_str(), 0600U);
|
||||
}
|
||||
#endif
|
||||
nf = native_file::attach(handle);
|
||||
ptr = native_file::attach(handle);
|
||||
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
|
||||
: api_error::success);
|
||||
}
|
||||
|
||||
auto native_file::create_or_open(const std::string &source_path,
|
||||
native_file_ptr &nf) -> api_error {
|
||||
return create_or_open(source_path, true, nf);
|
||||
native_file_ptr &ptr) -> api_error {
|
||||
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 {
|
||||
return open(source_path, true, nf);
|
||||
return open(source_path, false, ptr);
|
||||
}
|
||||
|
||||
auto native_file::open(const std::string &source_path,
|
||||
[[maybe_unused]] bool should_chmod, native_file_ptr &nf)
|
||||
-> api_error {
|
||||
auto native_file::open(const std::string &source_path, bool read_only,
|
||||
native_file_ptr &ptr) -> api_error {
|
||||
#ifdef _WIN32
|
||||
auto handle = ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
||||
auto handle =
|
||||
read_only
|
||||
? ::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
|
||||
auto handle = should_chmod
|
||||
? ::open(source_path.c_str(), O_RDWR | O_CLOEXEC, 0600u)
|
||||
: ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (should_chmod) {
|
||||
chmod(source_path.c_str(), 0600u);
|
||||
auto handle = read_only ? ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC)
|
||||
: ::open(source_path.c_str(), O_RDWR | O_CLOEXEC);
|
||||
if (not read_only) {
|
||||
chmod(source_path.c_str(), 0600U);
|
||||
}
|
||||
#endif
|
||||
nf = native_file::attach(handle);
|
||||
ptr = native_file::attach(handle);
|
||||
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
|
||||
: api_error::success);
|
||||
}
|
||||
@ -139,19 +148,19 @@ void native_file::close() {
|
||||
}
|
||||
}
|
||||
|
||||
auto native_file::copy_from(const native_file_ptr &nf) -> bool {
|
||||
auto ret = false;
|
||||
std::uint64_t file_size = 0u;
|
||||
if ((ret = nf->get_file_size(file_size))) {
|
||||
auto native_file::copy_from(const native_file_ptr &ptr) -> bool {
|
||||
std::uint64_t file_size{};
|
||||
auto ret = ptr->get_file_size(file_size);
|
||||
if (ret) {
|
||||
data_buffer buffer;
|
||||
buffer.resize(65536u * 2u);
|
||||
std::uint64_t offset = 0u;
|
||||
while (ret && (file_size > 0u)) {
|
||||
buffer.resize(65536ULL * 2ULL);
|
||||
std::uint64_t offset{};
|
||||
while (ret && (file_size > 0U)) {
|
||||
std::size_t bytes_read{};
|
||||
if ((ret = nf->read_bytes(&buffer[0u], buffer.size(), offset,
|
||||
bytes_read))) {
|
||||
std::size_t bytes_written = 0u;
|
||||
ret = write_bytes(&buffer[0u], bytes_read, offset, bytes_written);
|
||||
ret = ptr->read_bytes(buffer.data(), buffer.size(), offset, bytes_read);
|
||||
if (ret) {
|
||||
std::size_t bytes_written{};
|
||||
ret = write_bytes(buffer.data(), bytes_read, offset, bytes_written);
|
||||
file_size -= 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 ret = false;
|
||||
native_file_ptr nf;
|
||||
if (native_file::create_or_open(path, nf) == api_error ::success) {
|
||||
ret = copy_from(nf);
|
||||
nf->close();
|
||||
native_file_ptr ptr;
|
||||
if (native_file::create_or_open(path, ptr) == api_error ::success) {
|
||||
ret = copy_from(ptr);
|
||||
ptr->close();
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -194,11 +203,12 @@ auto native_file::get_file_size(std::uint64_t &file_size) -> bool {
|
||||
struct stat st {};
|
||||
if (fstat(handle_, &st) >= 0) {
|
||||
#else
|
||||
struct stat64 st {};
|
||||
if (fstat64(handle_, &st) >= 0) {
|
||||
struct stat64 unix_st {};
|
||||
if (fstat64(handle_, &unix_st) >= 0) {
|
||||
#endif
|
||||
if ((ret = (st.st_size >= 0))) {
|
||||
file_size = static_cast<uint64_t>(st.st_size);
|
||||
ret = (unix_st.st_size >= 0);
|
||||
if (ret) {
|
||||
file_size = static_cast<uint64_t>(unix_st.st_size);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -289,7 +299,7 @@ auto native_file::write_bytes(const char *buffer, std::size_t write_size,
|
||||
std::uint64_t write_offset,
|
||||
std::size_t &bytes_written) -> bool {
|
||||
bytes_written = 0U;
|
||||
ssize_t result = 0U;
|
||||
ssize_t result{};
|
||||
do {
|
||||
result =
|
||||
pwrite64(handle_, &buffer[bytes_written], write_size - bytes_written,
|
||||
|
@ -30,13 +30,15 @@ namespace repertory::utils::string {
|
||||
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 {
|
||||
std::stringstream ss;
|
||||
boost::archive::text_oarchive archive(ss);
|
||||
std::stringstream stream;
|
||||
boost::archive::text_oarchive archive(stream);
|
||||
archive << bitset;
|
||||
return ss.str();
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/* constexpr c++20 */ auto is_numeric(std::string_view s) -> bool {
|
||||
if ((s.length() > 1u) && (s[0u] == '+' || s[0u] == '-')) {
|
||||
s = s.substr(1u);
|
||||
/* constexpr c++20 */ auto is_numeric(std::string_view str) -> bool {
|
||||
if ((str.length() > 1U) && (str[0U] == '+' || str[0U] == '-')) {
|
||||
str = str.substr(1U);
|
||||
}
|
||||
|
||||
if (s.empty()) {
|
||||
if (str.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto has_decimal = false;
|
||||
return std::find_if(
|
||||
s.begin(), s.end(),
|
||||
[&has_decimal](const std::string_view::value_type &c) -> bool {
|
||||
if (has_decimal && c == '.') {
|
||||
return true;
|
||||
}
|
||||
if ((has_decimal = has_decimal || c == '.')) {
|
||||
return false;
|
||||
}
|
||||
return not std::isdigit(c);
|
||||
}) == s.end();
|
||||
return std::find_if(str.begin(), str.end(),
|
||||
[&has_decimal](
|
||||
const std::string_view::value_type &cur_ch) -> bool {
|
||||
if (has_decimal && cur_ch == '.') {
|
||||
return true;
|
||||
}
|
||||
has_decimal = has_decimal || cur_ch == '.';
|
||||
if (has_decimal) {
|
||||
return false;
|
||||
}
|
||||
return std::isdigit(cur_ch) == 0;
|
||||
}) == str.end();
|
||||
}
|
||||
|
||||
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(
|
||||
std::next(arr.begin()), arr.end(), arr[0u],
|
||||
[&delim](auto s, const auto &v) { return s + delim + v; });
|
||||
std::next(arr.begin()), arr.end(), arr[0U],
|
||||
[&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 & {
|
||||
s.erase(0, s.find_first_not_of(c));
|
||||
return s;
|
||||
auto left_trim(std::string &str, const char &trim_ch) -> std::string & {
|
||||
str.erase(0, str.find_first_not_of(trim_ch));
|
||||
return str;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 & {
|
||||
s.erase(s.find_last_not_of(c) + 1);
|
||||
return s;
|
||||
auto right_trim(std::string &str, const char &trim_ch) -> std::string & {
|
||||
str.erase(str.find_last_not_of(trim_ch) + 1);
|
||||
return str;
|
||||
}
|
||||
|
||||
auto split(const std::string &str, const char &delim, bool should_trim)
|
||||
-> std::vector<std::string> {
|
||||
std::vector<std::string> ret;
|
||||
std::stringstream ss(str);
|
||||
std::stringstream stream(str);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, delim)) {
|
||||
while (std::getline(stream, item, delim)) {
|
||||
ret.push_back(should_trim ? trim(item) : item);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto to_bool(std::string val) -> bool {
|
||||
auto b = false;
|
||||
auto ret = false;
|
||||
|
||||
trim(val);
|
||||
if (is_numeric(val)) {
|
||||
if (contains(val, ".")) {
|
||||
b = (to_double(val) != 0.0);
|
||||
ret = (to_double(val) != 0.0);
|
||||
} else {
|
||||
std::istringstream(val) >> b;
|
||||
std::istringstream(val) >> ret;
|
||||
}
|
||||
} 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_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<> {
|
||||
std::stringstream ss(val);
|
||||
std::stringstream stream(val);
|
||||
boost::dynamic_bitset<> bitset;
|
||||
boost::archive::text_iarchive archive(ss);
|
||||
boost::archive::text_iarchive archive(stream);
|
||||
archive >> bitset;
|
||||
return bitset;
|
||||
}
|
||||
@ -209,15 +216,15 @@ auto trim(std::string &str) -> std::string & {
|
||||
return right_trim(left_trim(str));
|
||||
}
|
||||
|
||||
auto trim(std::string &str, const char &c) -> std::string & {
|
||||
return right_trim(left_trim(str, c), c);
|
||||
auto trim(std::string &str, const char &trim_ch) -> std::string & {
|
||||
return right_trim(left_trim(str, trim_ch), trim_ch);
|
||||
}
|
||||
|
||||
auto trim_copy(std::string str) -> std::string {
|
||||
return right_trim(left_trim(str));
|
||||
}
|
||||
|
||||
auto trim_copy(std::string str, const char &c) -> std::string {
|
||||
return right_trim(left_trim(str, c), c);
|
||||
auto trim_copy(std::string str, const char &trim_ch) -> std::string {
|
||||
return right_trim(left_trim(str, trim_ch), trim_ch);
|
||||
}
|
||||
} // namespace repertory::utils::string
|
||||
|
@ -226,14 +226,14 @@ auto get_file_time_now() -> std::uint64_t {
|
||||
void get_local_time_now(struct tm &local_time) {
|
||||
memset(&local_time, 0, sizeof(local_time));
|
||||
|
||||
static std::mutex mtx{};
|
||||
mutex_lock lock{mtx};
|
||||
|
||||
const auto now =
|
||||
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
||||
#ifdef _WIN32
|
||||
localtime_s(&local_time, &now);
|
||||
#else
|
||||
static std::mutex mtx{};
|
||||
mutex_lock lock{mtx};
|
||||
|
||||
const auto *tmp = std::localtime(&now);
|
||||
if (tmp != nullptr) {
|
||||
memcpy(&local_time, tmp, sizeof(local_time));
|
||||
|
Reference in New Issue
Block a user