This commit is contained in:
149
support/src/utils/common.cpp
Normal file
149
support/src/utils/common.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "utils/common.hpp"
|
||||
|
||||
#include "utils/path.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::utils {
|
||||
auto compare_version_strings(std::string version1,
|
||||
std::string version2) -> std::int32_t {
|
||||
|
||||
if (utils::string::contains(version1, "-")) {
|
||||
version1 = utils::string::split(version1, '-', true)[0U];
|
||||
}
|
||||
|
||||
if (utils::string::contains(version2, "-")) {
|
||||
version2 = utils::string::split(version2, '-', true)[0U];
|
||||
}
|
||||
|
||||
auto nums1 = utils::string::split(version1, '.', true);
|
||||
auto nums2 = utils::string::split(version2, '.', true);
|
||||
|
||||
while (nums1.size() > nums2.size()) {
|
||||
nums2.emplace_back("0");
|
||||
}
|
||||
|
||||
while (nums2.size() > nums1.size()) {
|
||||
nums1.emplace_back("0");
|
||||
}
|
||||
|
||||
for (std::size_t idx = 0U; idx < nums1.size(); idx++) {
|
||||
auto int1 = utils::string::to_uint32(nums1[idx]);
|
||||
auto int2 = utils::string::to_uint32(nums2[idx]);
|
||||
auto res = std::memcmp(&int1, &int2, sizeof(int1));
|
||||
if (res != 0) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto compare_version_strings(std::wstring_view version1,
|
||||
std::wstring_view version2) -> std::int32_t {
|
||||
return compare_version_strings(utils::string::to_utf8(version1),
|
||||
utils::string::to_utf8(version2));
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_STDUUID)
|
||||
auto create_uuid_string() -> std::string {
|
||||
std::random_device random_device;
|
||||
auto seed_data = std::array<int, std::mt19937::state_size>{};
|
||||
std::generate(std::begin(seed_data), std::end(seed_data),
|
||||
std::ref(random_device));
|
||||
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
|
||||
std::mt19937 generator(seq);
|
||||
uuids::uuid_random_generator gen{generator};
|
||||
|
||||
return uuids::to_string(gen());
|
||||
}
|
||||
|
||||
auto create_uuid_wstring() -> std::wstring {
|
||||
return utils::string::from_utf8(create_uuid_string());
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_STDUUID)
|
||||
|
||||
#if defined(PROJECT_ENABLE_LIBSODIUM)
|
||||
auto generate_random_string(std::uint16_t length) -> std::string {
|
||||
std::string ret;
|
||||
ret.resize(length);
|
||||
for (std::uint16_t i = 0U; i < length; i++) {
|
||||
do {
|
||||
ret[i] = static_cast<char>(generate_random<std::uint8_t>() % 74 + 48);
|
||||
} while (((ret[i] >= 91) && (ret[i] <= 96)) ||
|
||||
((ret[i] >= 58) && (ret[i] <= 64)));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
||||
|
||||
auto get_environment_variable(std::string_view variable) -> std::string {
|
||||
static std::mutex mtx{};
|
||||
mutex_lock lock{mtx};
|
||||
|
||||
const auto *val = std::getenv(std::string{variable}.c_str());
|
||||
return std::string{val == nullptr ? "" : val};
|
||||
}
|
||||
|
||||
auto get_environment_variable(std::wstring_view variable) -> std::wstring {
|
||||
return utils::string::from_utf8(
|
||||
get_environment_variable(utils::string::to_utf8(variable)));
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_BOOST)
|
||||
auto get_next_available_port(std::uint16_t first_port,
|
||||
std::uint16_t &available_port) -> bool {
|
||||
using namespace boost::asio;
|
||||
using ip::tcp;
|
||||
|
||||
boost::system::error_code error_code{};
|
||||
while (first_port != 0U) {
|
||||
io_service svc{};
|
||||
tcp::acceptor acceptor(svc);
|
||||
acceptor.open(tcp::v4(), error_code) ||
|
||||
acceptor.bind({tcp::v4(), first_port}, error_code);
|
||||
if (not error_code) {
|
||||
break;
|
||||
}
|
||||
|
||||
++first_port;
|
||||
}
|
||||
|
||||
if (not error_code) {
|
||||
available_port = first_port;
|
||||
}
|
||||
|
||||
return not error_code;
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_BOOST)
|
||||
|
||||
auto resolve_variables(std::string str) -> std::string {
|
||||
return utils::path::absolute(str);
|
||||
}
|
||||
|
||||
auto resolve_variables(std::wstring_view str) -> std::wstring {
|
||||
return utils::string::from_utf8(
|
||||
resolve_variables(utils::string::to_utf8(str)));
|
||||
}
|
||||
} // namespace repertory::utils
|
Reference in New Issue
Block a user