updated build system
This commit is contained in:
		
							
								
								
									
										36
									
								
								support/3rd_party/include/utils/all.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								support/3rd_party/include/utils/all.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_ALL_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_ALL_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| #include "utils/collection.hpp" | ||||
| #include "utils/com_init_wrapper.hpp" | ||||
| #include "utils/common.hpp" | ||||
| #include "utils/path.hpp" | ||||
| #include "utils/string.hpp" | ||||
| #include "utils/time.hpp" | ||||
| #include "utils/unix.hpp" | ||||
| #include "utils/windows.hpp" | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_ | ||||
							
								
								
									
										174
									
								
								support/3rd_party/include/utils/base64.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										174
									
								
								support/3rd_party/include/utils/base64.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,174 @@ | ||||
| // NOLINTBEGIN | ||||
| #ifndef _MACARON_BASE64_H_ | ||||
| #define _MACARON_BASE64_H_ | ||||
|  | ||||
| /** | ||||
|  * The MIT License (MIT) | ||||
|  * Copyright (c) 2016 tomykaira | ||||
|  * | ||||
|  * 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. | ||||
|  */ | ||||
|  | ||||
| #ifdef __clang__ | ||||
| #pragma clang diagnostic push | ||||
| #pragma clang diagnostic ignored "-Wunknown-warning-option" | ||||
| #endif | ||||
|  | ||||
| #ifdef __GNUC__ | ||||
| #pragma GCC diagnostic push | ||||
| #pragma GCC diagnostic ignored "-Wconversion" | ||||
| #pragma GCC diagnostic ignored "-Wold-style-cast" | ||||
| #pragma GCC diagnostic ignored "-Wuseless-cast" | ||||
| #endif | ||||
|  | ||||
| #include <array> | ||||
| #include <string> | ||||
| #include <vector> | ||||
|  | ||||
| namespace macaron::Base64 { | ||||
| static std::string Encode(const unsigned char *data, std::size_t len) { | ||||
|   static constexpr std::array<unsigned char, 64U> sEncodingTable{ | ||||
|       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||||
|       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||||
|       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||||
|       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||||
|       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', | ||||
|   }; | ||||
|  | ||||
|   auto in_len{len}; | ||||
|   std::string ret; | ||||
|   if (in_len > 0) { | ||||
|     std::size_t out_len{4U * ((in_len + 2U) / 3U)}; | ||||
|     ret = std::string(out_len, '\0'); | ||||
|     std::size_t i; | ||||
|     auto *p = reinterpret_cast<unsigned char *>(ret.data()); | ||||
|  | ||||
|     for (i = 0U; i < in_len - 2U; i += 3U) { | ||||
|       *p++ = sEncodingTable[(data[i] >> 2U) & 0x3F]; | ||||
|       *p++ = sEncodingTable[((data[i] & 0x3) << 4U) | | ||||
|                             ((int)(data[i + 1U] & 0xF0) >> 4U)]; | ||||
|       *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | | ||||
|                             ((int)(data[i + 2U] & 0xC0) >> 6U)]; | ||||
|       *p++ = sEncodingTable[data[i + 2U] & 0x3F]; | ||||
|     } | ||||
|     if (i < in_len) { | ||||
|       *p++ = sEncodingTable[(data[i] >> 2U) & 0x3F]; | ||||
|       if (i == (in_len - 1U)) { | ||||
|         *p++ = sEncodingTable[((data[i] & 0x3) << 4U)]; | ||||
|         *p++ = '='; | ||||
|       } else { | ||||
|         *p++ = sEncodingTable[((data[i] & 0x3) << 4U) | | ||||
|                               ((int)(data[i + 1U] & 0xF0) >> 4U)]; | ||||
|         *p++ = sEncodingTable[((data[i + 1U] & 0xF) << 2U)]; | ||||
|       } | ||||
|       *p++ = '='; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| [[maybe_unused]] static std::string Encode(std::string_view data) { | ||||
|   return Encode(reinterpret_cast<const unsigned char *>(data.data()), | ||||
|                 data.size()); | ||||
| } | ||||
|  | ||||
| [[maybe_unused]] static std::vector<unsigned char> | ||||
| Decode(std::string_view input) { | ||||
|   static constexpr std::array<unsigned char, 256> kDecodingTable{ | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, | ||||
|       58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0,  1,  2,  3,  4,  5,  6, | ||||
|       7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||||
|       25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, | ||||
|       37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||
|       64, 64, 64, 64, | ||||
|   }; | ||||
|  | ||||
|   std::vector<unsigned char> out; | ||||
|   if (not input.empty()) { | ||||
|     auto in_len{input.size()}; | ||||
|     if (in_len % 4U != 0U) { | ||||
|       throw std::runtime_error("Input data size is not a multiple of 4"); | ||||
|     } | ||||
|  | ||||
|     std::size_t out_len{in_len / 4U * 3U}; | ||||
|     if (input[in_len - 1U] == '=') { | ||||
|       out_len--; | ||||
|     } | ||||
|     if (input[in_len - 2U] == '=') { | ||||
|       out_len--; | ||||
|     } | ||||
|  | ||||
|     out.resize(out_len); | ||||
|  | ||||
|     for (std::size_t i = 0U, j = 0U; i < in_len;) { | ||||
|       std::uint32_t a = | ||||
|           input.at(i) == '=' | ||||
|               ? 0U & i++ | ||||
|               : kDecodingTable[static_cast<unsigned char>(input.at(i++))]; | ||||
|       std::uint32_t b = | ||||
|           input.at(i) == '=' | ||||
|               ? 0U & i++ | ||||
|               : kDecodingTable[static_cast<unsigned char>(input.at(i++))]; | ||||
|       std::uint32_t c = | ||||
|           input.at(i) == '=' | ||||
|               ? 0U & i++ | ||||
|               : kDecodingTable[static_cast<unsigned char>(input.at(i++))]; | ||||
|       std::uint32_t d = | ||||
|           input.at(i) == '=' | ||||
|               ? 0U & i++ | ||||
|               : kDecodingTable[static_cast<unsigned char>(input.at(i++))]; | ||||
|  | ||||
|       std::uint32_t triple = | ||||
|           (a << 3U * 6U) + (b << 2U * 6U) + (c << 1U * 6U) + (d << 0U * 6U); | ||||
|  | ||||
|       if (j < out_len) | ||||
|         out[j++] = (triple >> 2U * 8U) & 0xFF; | ||||
|       if (j < out_len) | ||||
|         out[j++] = (triple >> 1U * 8U) & 0xFF; | ||||
|       if (j < out_len) | ||||
|         out[j++] = (triple >> 0U * 8U) & 0xFF; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return out; | ||||
| } | ||||
| } // namespace macaron::Base64 | ||||
|  | ||||
| #ifdef __GNUC__ | ||||
| #pragma GCC diagnostic pop | ||||
| #endif | ||||
|  | ||||
| #ifdef __clang__ | ||||
| #pragma clang diagnostic pop | ||||
| #endif | ||||
|  | ||||
| #endif /* _MACARON_BASE64_H_ */ | ||||
|  | ||||
| // NOLINTEND | ||||
							
								
								
									
										133
									
								
								support/3rd_party/include/utils/collection.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								support/3rd_party/include/utils/collection.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,133 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_COLLECTION_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_COLLECTION_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils::collection { | ||||
| template <typename col_t> | ||||
| [[nodiscard]] inline auto | ||||
| excludes(const col_t &collection, | ||||
|          const typename col_t::value_type &val) -> bool; | ||||
|  | ||||
| template <typename col_t> | ||||
| [[nodiscard]] inline auto | ||||
| includes(const col_t &collection, | ||||
|          const typename col_t::value_type &val) -> bool; | ||||
|  | ||||
| template <typename val_t> | ||||
| [[nodiscard]] inline auto from_hex_string(std::string_view str, | ||||
|                                           val_t &val) -> bool; | ||||
|  | ||||
| template <typename val_t> | ||||
| [[nodiscard]] inline auto from_hex_string(std::wstring_view str, | ||||
|                                           val_t &val) -> bool; | ||||
| template <typename col_t> | ||||
| inline auto remove_element(col_t &collection, | ||||
|                            const typename col_t::value_type &value) -> col_t &; | ||||
|  | ||||
| template <typename col_t> | ||||
| [[nodiscard]] inline auto to_hex_string(const col_t &collection) -> std::string; | ||||
|  | ||||
| template <typename col_t> | ||||
| [[nodiscard]] inline auto | ||||
| to_hex_wstring(const col_t &collection) -> std::wstring; | ||||
|  | ||||
| template <typename col_t> | ||||
| inline auto excludes(const col_t &collection, | ||||
|                      const typename col_t::value_type &val) -> bool { | ||||
|   return std::find(collection.begin(), collection.end(), val) == | ||||
|          collection.end(); | ||||
| } | ||||
|  | ||||
| template <typename col_t> | ||||
| inline auto includes(const col_t &collection, | ||||
|                      const typename col_t::value_type &val) -> bool { | ||||
|   return std::find(collection.begin(), collection.end(), val) != | ||||
|          collection.end(); | ||||
| } | ||||
|  | ||||
| template <typename val_t, typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| from_hex_string_t(std::basic_string_view<typename string_t::value_type> str, | ||||
|                   val_t &val) -> bool { | ||||
|   static constexpr const auto base16{16}; | ||||
|  | ||||
|   val.clear(); | ||||
|   if (not(str.length() % 2U)) { | ||||
|     for (std::size_t i = 0U; i < str.length(); i += 2U) { | ||||
|       val.emplace_back(static_cast<typename val_t::value_type>( | ||||
|           std::strtol(string_t{str.substr(i, 2U)}.c_str(), nullptr, base16))); | ||||
|     } | ||||
|  | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   return false; | ||||
| } | ||||
|  | ||||
| template <typename col_t> | ||||
| inline auto remove_element(col_t &collection, | ||||
|                            const typename col_t::value_type &value) -> col_t & { | ||||
|   collection.erase(std::remove(collection.begin(), collection.end(), value), | ||||
|                    collection.end()); | ||||
|   return collection; | ||||
| } | ||||
|  | ||||
| template <typename col_t, typename string_t> | ||||
| [[nodiscard]] inline auto to_hex_string_t(const col_t &collection) -> string_t { | ||||
|   static_assert(sizeof(typename col_t::value_type) == 1U, | ||||
|                 "value_type must be 1 byte in size"); | ||||
|   static constexpr const auto mask = 0xFF; | ||||
|  | ||||
|   std::basic_stringstream<typename string_t::value_type> stream{}; | ||||
|   for (auto &&val : collection) { | ||||
|     stream << std::setfill('0') << std::setw(2) << std::hex | ||||
|            << (static_cast<std::uint32_t>(val) & mask); | ||||
|   } | ||||
|  | ||||
|   return stream.str(); | ||||
| } | ||||
|  | ||||
| template <typename val_t> | ||||
| inline auto from_hex_string(std::string_view str, val_t &val) -> bool { | ||||
|   return from_hex_string_t<val_t, std::string>(str, val); | ||||
| } | ||||
|  | ||||
| template <typename val_t> | ||||
| inline auto from_hex_string(std::wstring_view str, val_t &val) -> bool { | ||||
|   return from_hex_string_t<val_t, std::wstring>(str, val); | ||||
| } | ||||
|  | ||||
| template <typename col_t> | ||||
| inline auto to_hex_string(const col_t &collection) -> std::string { | ||||
|   return to_hex_string_t<col_t, std::string>(collection); | ||||
| } | ||||
|  | ||||
| template <typename col_t> | ||||
| inline auto to_hex_wstring(const col_t &collection) -> std::wstring { | ||||
|   return to_hex_string_t<col_t, std::wstring>(collection); | ||||
| } | ||||
| } // namespace repertory::utils::collection | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_COLLECTION_HPP_ | ||||
							
								
								
									
										47
									
								
								support/3rd_party/include/utils/com_init_wrapper.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								support/3rd_party/include/utils/com_init_wrapper.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_COM_INIT_WRAPPER_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_COM_INIT_WRAPPER_HPP_ | ||||
| #if defined(_WIN32) | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils { | ||||
| class com_init_wrapper { | ||||
| public: | ||||
|   com_init_wrapper() | ||||
|       : uninit_( | ||||
|             SUCCEEDED(::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) {} | ||||
|  | ||||
|   ~com_init_wrapper() { | ||||
|     if (uninit_) { | ||||
|       ::CoUninitialize(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| private: | ||||
|   BOOL uninit_; | ||||
| }; | ||||
| } // namespace repertory::utils | ||||
|  | ||||
| #endif // defined(_WIN32) | ||||
| #endif // REPERTORY_INCLUDE_UTILS_COM_INIT_WRAPPER_HPP_ | ||||
							
								
								
									
										111
									
								
								support/3rd_party/include/utils/common.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								support/3rd_party/include/utils/common.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,111 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_COMMON_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_COMMON_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils { | ||||
| struct result final { | ||||
|   std::string_view function_name; | ||||
|   bool ok{false}; | ||||
|   std::string reason{}; | ||||
|  | ||||
|   [[nodiscard]] operator bool() const { return ok; } | ||||
| }; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| calculate_read_size(std::uint64_t total_size, std::size_t read_size, | ||||
|                     std::uint64_t offset) -> std::size_t { | ||||
|   return static_cast<std::size_t>( | ||||
|       ((offset + read_size) > total_size) | ||||
|           ? ((offset < total_size) ? total_size - offset : 0U) | ||||
|           : read_size); | ||||
| } | ||||
|  | ||||
| [[nodiscard]] auto | ||||
| compare_version_strings(std::string version1, | ||||
|                         std::string version2) -> std::int32_t; | ||||
|  | ||||
| [[nodiscard]] auto | ||||
| compare_version_strings(std::wstring_view version1, | ||||
|                         std::wstring_view version2) -> std::int32_t; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_STDUUID) | ||||
| [[nodiscard]] auto create_uuid_string() -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto create_uuid_wstring() -> std::wstring; | ||||
| #endif // defined(PROJECT_ENABLE_STDUUID) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| template <typename data_type> | ||||
| [[nodiscard]] inline auto generate_random() -> data_type; | ||||
|  | ||||
| template <typename data_type> | ||||
| [[nodiscard]] inline auto | ||||
| generate_random_between(const data_type &begin, | ||||
|                         const data_type &end) -> data_type; | ||||
|  | ||||
| [[nodiscard]] auto generate_random_string(std::uint16_t length) -> std::string; | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
|  | ||||
| [[nodiscard]] auto | ||||
| get_environment_variable(std::string_view variable) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto | ||||
| get_environment_variable(std::wstring_view variable) -> std::wstring; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| [[nodiscard]] auto | ||||
| get_next_available_port(std::uint16_t first_port, | ||||
|                         std::uint16_t &available_port) -> bool; | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
|  | ||||
| [[nodiscard]] auto resolve_variables(std::string str) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto resolve_variables(std::wstring_view str) -> std::wstring; | ||||
|  | ||||
| // template implementations | ||||
| template <typename val_t> | ||||
| [[nodiscard]] inline auto divide_with_ceiling(const val_t &n, | ||||
|                                               const val_t &d) -> val_t { | ||||
|   return n ? (n / d) + (n % d != 0) : 0; | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| template <typename data_type> | ||||
| [[nodiscard]] inline auto generate_random() -> data_type { | ||||
|   data_type ret{}; | ||||
|   randombytes_buf(&ret, sizeof(ret)); | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| template <typename data_type> | ||||
| [[nodiscard]] inline auto | ||||
| generate_random_between(const data_type &begin, | ||||
|                         const data_type &end) -> data_type { | ||||
|   return begin + generate_random<data_type>() % ((end + data_type{1}) - begin); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| } // namespace repertory::utils | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_COMMON_HPP_ | ||||
							
								
								
									
										313
									
								
								support/3rd_party/include/utils/config.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										313
									
								
								support/3rd_party/include/utils/config.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,313 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_CONFIG_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_CONFIG_HPP_ | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #define WINVER 0x0602 | ||||
| #define _WIN32_WINNT WINVER | ||||
| #define WIN32_LEAN_AND_MEAN | ||||
|  | ||||
| #include <winsock2.h> | ||||
| #include <ws2tcpip.h> | ||||
|  | ||||
| #include <windows.h> | ||||
| #if defined(PROJECT_ENABLE_WINFSP) | ||||
| #include <sddl.h> | ||||
| #endif // defined(PROJECT_ENABLE_WINFSP) | ||||
|  | ||||
| #include <direct.h> | ||||
| #include <errno.h> | ||||
| #include <fcntl.h> | ||||
| #include <io.h> | ||||
| #include <iphlpapi.h> | ||||
| #include <objbase.h> | ||||
| #include <psapi.h> | ||||
| #include <rpc.h> | ||||
| #include <share.h> | ||||
| #include <shellapi.h> | ||||
| #include <shlobj.h> | ||||
| #include <shlwapi.h> | ||||
| #include <stdio.h> | ||||
| #include <sys/stat.h> | ||||
| #include <sys/types.h> | ||||
| #include <time.h> | ||||
| #else // !defined(_WIN32) | ||||
| #include <arpa/inet.h> | ||||
| #include <dirent.h> | ||||
| #include <fcntl.h> | ||||
| #include <grp.h> | ||||
| #include <libgen.h> | ||||
| #include <netinet/in.h> | ||||
| #include <pwd.h> | ||||
| #include <sys/file.h> | ||||
| #include <sys/socket.h> | ||||
| #if defined(__LFS64__) | ||||
| #include <sys/stat64.h> | ||||
| #else | ||||
| #include <sys/stat.h> | ||||
| #endif | ||||
|  | ||||
| #if defined(__linux__) | ||||
| #include <sys/statfs.h> | ||||
| #endif //  defined(HAS_SETXATTR) | ||||
|  | ||||
| #if defined(HAS_SETXATTR) | ||||
| #include <sys/types.h> | ||||
| #include <sys/xattr.h> | ||||
| #endif // defined(HAS_SETXATTR) | ||||
|  | ||||
| #if defined(__APPLE__) | ||||
| #include <libproc.h> | ||||
| #include <sys/attr.h> | ||||
| #include <sys/mount.h> | ||||
| #include <sys/statvfs.h> | ||||
| #include <sys/vnode.h> | ||||
| #endif // defined(__APPLE__) | ||||
|  | ||||
| #include <unistd.h> | ||||
| #endif // defined(_WIN32) | ||||
|  | ||||
| #if defined(HAS_WORDEXP_H) | ||||
| #include <wordexp.h> | ||||
| #endif // defined(HAS_WORDEXP_H) | ||||
|  | ||||
| #if defined(__cplusplus) | ||||
| #include <algorithm> | ||||
| #include <array> | ||||
| #include <atomic> | ||||
| #include <bit> | ||||
| #include <chrono> | ||||
| #include <ciso646> | ||||
| #include <climits> | ||||
| #include <codecvt> | ||||
| #include <condition_variable> | ||||
| #include <cstdint> | ||||
| #include <cstdlib> | ||||
| #include <cstring> | ||||
| #include <ctime> | ||||
| #include <deque> | ||||
| #include <filesystem> | ||||
| #include <fstream> | ||||
| #include <functional> | ||||
| #include <future> | ||||
| #include <iomanip> | ||||
| #include <iostream> | ||||
| #include <iterator> | ||||
| #include <limits> | ||||
| #include <locale> | ||||
| #include <map> | ||||
| #include <memory> | ||||
| #include <mutex> | ||||
| #include <numeric> | ||||
| #include <optional> | ||||
| #include <ostream> | ||||
| #include <queue> | ||||
| #include <random> | ||||
| #include <ranges> | ||||
| #include <regex> | ||||
| #include <span> | ||||
| #include <sstream> | ||||
| #include <stdexcept> | ||||
| #include <string> | ||||
| #include <string_view> | ||||
| #include <thread> | ||||
| #include <type_traits> | ||||
| #include <unordered_map> | ||||
| #include <utility> | ||||
| #include <variant> | ||||
| #include <vector> | ||||
| #endif // defined(__cplusplus) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_CURL) | ||||
| #include "curl/curl.h" | ||||
| #include "curl/multi.h" | ||||
| #endif // defined(PROJECT_ENABLE_CURL) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_FUSE) | ||||
| #if FUSE_USE_VERSION >= 30 | ||||
| #include <fuse.h> | ||||
| #include <fuse_lowlevel.h> | ||||
| #else | ||||
| #include <fuse/fuse.h> | ||||
| #endif | ||||
| #endif // defined(PROJECT_ENABLE_FUSE) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_FZF) | ||||
| #if defined(__cplusplus) | ||||
| extern "C" { | ||||
| #endif // defined(__cplusplus) | ||||
| #include "fzf.h" | ||||
| #if defined(__cplusplus) | ||||
| } | ||||
| #endif // defined(__cplusplus) | ||||
| #endif // defined(PROJECT_ENABLE_FZF) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_LIBEVENT) | ||||
| #include "event2/buffer.h" | ||||
| #include "event2/bufferevent.h" | ||||
| #include "event2/listener.h" | ||||
| #include "event2/thread.h" | ||||
| #include "event2/util.h" | ||||
| #endif // defined(PROJECT_ENABLE_LIBEVENT) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SDL) | ||||
| #include "SDL.h" | ||||
| #include "SDL_gamecontroller.h" | ||||
| #include "SDL_joystick.h" | ||||
| #endif // defined(PROJECT_ENABLE_SDL) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| #include "sodium.h" | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SQLITE) | ||||
| #include "sqlite3.h" | ||||
| #endif // defined(PROJECT_ENABLE_SQLITE) | ||||
|  | ||||
| #if defined(__cplusplus) | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| #include "boost/archive/text_iarchive.hpp" | ||||
| #include "boost/archive/text_oarchive.hpp" | ||||
| #include "boost/asio.hpp" | ||||
| #include "boost/asio/io_context.hpp" | ||||
| #include "boost/bind/bind.hpp" | ||||
| #include "boost/dynamic_bitset.hpp" | ||||
| #include "boost/dynamic_bitset/serialization.hpp" | ||||
| #include "boost/endian/conversion.hpp" | ||||
| #include "boost/integer.hpp" | ||||
| #include "boost/interprocess/sync/named_mutex.hpp" | ||||
| #include "boost/interprocess/sync/scoped_lock.hpp" | ||||
| #include "boost/multiprecision/cpp_dec_float.hpp" | ||||
| #include "boost/multiprecision/cpp_int.hpp" | ||||
| #include "boost/serialization/vector.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_CPP_HTTPLIB) | ||||
| #include "httplib.h" | ||||
| #endif // defined(PROJECT_ENABLE_JSON) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_DTL) | ||||
| #include "dtl/dtl.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_DTL) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_JSON) | ||||
| #include "json.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_JSON) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_NANA) | ||||
| #include "nana/gui.hpp" | ||||
| #include "nana/gui/timer.hpp" | ||||
| #include "nana/gui/widgets/button.hpp" | ||||
| #include "nana/gui/widgets/combox.hpp" | ||||
| #include "nana/gui/widgets/group.hpp" | ||||
| #include "nana/gui/widgets/label.hpp" | ||||
| #include "nana/gui/widgets/panel.hpp" | ||||
| #include "nana/gui/widgets/picture.hpp" | ||||
| #include "nana/gui/widgets/tabbar.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_NANA) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_PUGIXML) | ||||
| #include "pugixml.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_PUGIXML) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_ROCKSDB) | ||||
| #include "rocksdb/db.h" | ||||
| #include "rocksdb/utilities/transaction_db.h" | ||||
| #endif // defined(PROJECT_ENABLE_ROCKSDB) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SFML) | ||||
| #include "RoundedRectangleShape.hpp" | ||||
| #include "SFML/Graphics.hpp" | ||||
| #include "Text2.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_SFML) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SAGO_PLATFORM_FOLDERS) | ||||
| #include "platform_folders.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_SAGO_PLATFORM_FOLDERS) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SPDLOG) | ||||
| #include "spdlog/async.h" | ||||
| #include "spdlog/fmt/bundled/core.h" | ||||
| #include "spdlog/fmt/bundled/format.h" | ||||
| #include "spdlog/fmt/chrono.h" | ||||
| #include "spdlog/sinks/rotating_file_sink.h" | ||||
| #include "spdlog/sinks/stdout_color_sinks.h" | ||||
| #include "spdlog/spdlog.h" | ||||
| #endif // defined(PROJECT_ENABLE_SPDLOG) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_STDUUID) | ||||
| #include "uuid.h" | ||||
| #endif // defined(PROJECT_ENABLE_STDUUID) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_TPL) | ||||
| #include "process.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_TPL) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_WINFSP) | ||||
| #include "winfsp/winfsp.hpp" | ||||
| #endif // defined(PROJECT_ENABLE_WINFSP) | ||||
|  | ||||
| #if !defined(fstat64) | ||||
| #define fstat64 fstat | ||||
| #endif // !defined(fstat64) | ||||
|  | ||||
| #if !defined(pread64) | ||||
| #define pread64 pread | ||||
| #endif // !defined(pread64) | ||||
|  | ||||
| #if !defined(pwrite64) | ||||
| #define pwrite64 pwrite | ||||
| #endif // !defined(pwrite64) | ||||
|  | ||||
| #if !defined(stat64) | ||||
| #define stat64 stat | ||||
| #endif // !defined(stat64) | ||||
|  | ||||
| #if !defined(statfs64) | ||||
| #define statfs64 statfs | ||||
| #endif // !defined(statfs64) | ||||
|  | ||||
| namespace repertory { | ||||
| using data_buffer = std::vector<unsigned char>; | ||||
| using mutex_lock = std::lock_guard<std::mutex>; | ||||
| using recur_mutex_lock = std::lock_guard<std::recursive_mutex>; | ||||
| using unique_mutex_lock = std::unique_lock<std::mutex>; | ||||
| using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>; | ||||
|  | ||||
| template <class... Ts> struct overloaded : Ts... { | ||||
|   using Ts::operator()...; | ||||
| }; | ||||
| template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>; | ||||
|  | ||||
| struct file_deleter final { | ||||
|   void operator()(FILE *file) { | ||||
|     if (file != nullptr) { | ||||
|       fclose(file); | ||||
|     } | ||||
|   } | ||||
| }; | ||||
| using file_t = std::unique_ptr<FILE, file_deleter>; | ||||
| } // namespace repertory | ||||
| #endif // defined(__cplusplus) | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_CONFIG_HPP_ | ||||
							
								
								
									
										160
									
								
								support/3rd_party/include/utils/encryption.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								support/3rd_party/include/utils/encryption.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,160 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_ENCRYPTION_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_ENCRYPTION_HPP_ | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils::encryption { | ||||
| using hash_256_t = std::array<unsigned char, 32U>; | ||||
| using hash_256_func_t = std::function<hash_256_t(std::string_view)>; | ||||
| using key_type = std::array<unsigned char, 32U>; | ||||
|  | ||||
| inline constexpr const std::uint32_t encryption_header_size{ | ||||
|     crypto_aead_xchacha20poly1305_IETF_NPUBBYTES + | ||||
|         crypto_aead_xchacha20poly1305_IETF_ABYTES, | ||||
| }; | ||||
|  | ||||
| [[nodiscard]] auto decrypt_data( | ||||
|     std::string_view data, std::string_view password, | ||||
|     std::optional<hash_256_func_t> hasher = std::nullopt) -> data_buffer; | ||||
|  | ||||
| [[nodiscard]] auto encrypt_data( | ||||
|     std::string_view data, std::string_view password, | ||||
|     std::optional<hash_256_func_t> hasher = std::nullopt) -> data_buffer; | ||||
|  | ||||
| [[nodiscard]] auto generate_key(std::string_view encryption_token) -> key_type; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| template <typename result> | ||||
| [[nodiscard]] inline auto | ||||
| decrypt_data(const key_type &key, const unsigned char *buffer, | ||||
|              std::size_t buffer_size, result &res) -> bool { | ||||
|   if (buffer_size > encryption_header_size) { | ||||
|     const std::uint32_t size = | ||||
|         boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size)); | ||||
|     res.resize(buffer_size - encryption_header_size); | ||||
|     return crypto_aead_xchacha20poly1305_ietf_decrypt_detached( | ||||
|                reinterpret_cast<unsigned char *>(res.data()), nullptr, | ||||
|                &buffer[header_size], res.size(), | ||||
|                &buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES], | ||||
|                reinterpret_cast<const unsigned char *>(&size), sizeof(size), | ||||
|                buffer, key.data()) == 0; | ||||
|   } | ||||
|  | ||||
|   return false; | ||||
| } | ||||
|  | ||||
| template <typename buffer, typename result> | ||||
| [[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf, | ||||
|                                        result &res) -> bool { | ||||
|   return decrypt_data<result>( | ||||
|       key, reinterpret_cast<const unsigned char *>(buf.data()), buf.size(), | ||||
|       res); | ||||
| } | ||||
|  | ||||
| template <typename buffer, typename result> | ||||
| [[nodiscard]] inline auto decrypt_data(std::string_view encryption_token, | ||||
|                                        const buffer &buf, result &res) -> bool { | ||||
|   return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res); | ||||
| } | ||||
|  | ||||
| template <typename result> | ||||
| [[nodiscard]] inline auto | ||||
| decrypt_data(std::string_view encryption_token, const unsigned char *buffer, | ||||
|              std::size_t buffer_size, result &res) -> bool { | ||||
|   return decrypt_data<result>(generate_key(encryption_token), buffer, | ||||
|                               buffer_size, res); | ||||
| } | ||||
|  | ||||
| template <typename result> | ||||
| inline void | ||||
| encrypt_data(const std::array<unsigned char, | ||||
|                               crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv, | ||||
|              const key_type &key, const unsigned char *buffer, | ||||
|              std::size_t buffer_size, result &res) { | ||||
|   std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{}; | ||||
|  | ||||
|   const std::uint32_t size = boost::endian::native_to_big( | ||||
|       static_cast<std::uint32_t>(buffer_size + encryption_header_size)); | ||||
|  | ||||
|   res.resize(buffer_size + header_size); | ||||
|  | ||||
|   unsigned long long mac_length{}; | ||||
|   if (crypto_aead_xchacha20poly1305_ietf_encrypt_detached( | ||||
|           reinterpret_cast<unsigned char *>(&res[encryption_header_size]), | ||||
|           mac.data(), &mac_length, buffer, buffer_size, | ||||
|           reinterpret_cast<const unsigned char *>(&size), sizeof(size), nullptr, | ||||
|           iv.data(), key.data()) != 0) { | ||||
|     throw std::runtime_error("encryption failed"); | ||||
|   } | ||||
|  | ||||
|   std::memcpy(res.data(), iv.data(), iv.size()); | ||||
|   std::memcpy(&res[iv.size()], mac.data(), mac.size()); | ||||
| } | ||||
|  | ||||
| template <typename result> | ||||
| inline void encrypt_data(const key_type &key, const unsigned char *buffer, | ||||
|                          std::size_t buffer_size, result &res) { | ||||
|   std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{}; | ||||
|   randombytes_buf(iv.data(), iv.size()); | ||||
|  | ||||
|   encrypt_data<result>(iv, key, buffer, buffer_size, res); | ||||
| } | ||||
|  | ||||
| template <typename result> | ||||
| inline void encrypt_data(std::string_view encryption_token, | ||||
|                          const unsigned char *buffer, std::size_t buffer_size, | ||||
|                          result &res) { | ||||
|   encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size, | ||||
|                        res); | ||||
| } | ||||
|  | ||||
| template <typename buffer, typename result> | ||||
| inline void encrypt_data(std::string_view encryption_token, const buffer &buf, | ||||
|                          result &res) { | ||||
|   encrypt_data<result>(generate_key(encryption_token), | ||||
|                        reinterpret_cast<const unsigned char *>(buf.data()), | ||||
|                        buf.size(), res); | ||||
| } | ||||
|  | ||||
| template <typename buffer, typename result> | ||||
| inline void encrypt_data(const key_type &key, const buffer &buf, result &res) { | ||||
|   encrypt_data<result>(key, reinterpret_cast<const unsigned char *>(buf.data()), | ||||
|                        buf.size(), res); | ||||
| } | ||||
|  | ||||
| template <typename buffer, typename result> | ||||
| inline void | ||||
| encrypt_data(const std::array<unsigned char, | ||||
|                               crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv, | ||||
|              const key_type &key, const buffer &buf, result &res) { | ||||
|   encrypt_data<result>(iv, key, | ||||
|                        reinterpret_cast<const unsigned char *>(buf.data()), | ||||
|                        buf.size(), res); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
| } // namespace repertory::utils::encryption | ||||
|  | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| #endif // REPERTORY_INCLUDE_UTILS_ENCRYPTION_HPP_ | ||||
							
								
								
									
										52
									
								
								support/3rd_party/include/utils/error.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								support/3rd_party/include/utils/error.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_ERROR_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_ERROR_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils::error { | ||||
| struct i_exception_handler { | ||||
|   virtual ~i_exception_handler() {} | ||||
|  | ||||
|   i_exception_handler(const i_exception_handler &) noexcept = delete; | ||||
|   i_exception_handler(i_exception_handler &&) noexcept = delete; | ||||
|   auto operator=(const i_exception_handler &) noexcept = delete; | ||||
|   auto operator=(i_exception_handler &&) noexcept = delete; | ||||
|  | ||||
|   virtual void handle_exception(std::string_view function_name) const = 0; | ||||
|  | ||||
|   virtual void handle_exception(std::string_view function_name, | ||||
|                                 const std::exception &ex) const = 0; | ||||
|  | ||||
| protected: | ||||
|   i_exception_handler() = default; | ||||
| }; | ||||
|  | ||||
| void handle_exception(std::string_view function_name); | ||||
|  | ||||
| void handle_exception(std::string_view function_name, const std::exception &ex); | ||||
|  | ||||
| void set_exception_handler(i_exception_handler *handler); | ||||
| } // namespace repertory::utils::error | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_ERROR_HPP_ | ||||
							
								
								
									
										147
									
								
								support/3rd_party/include/utils/file.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										147
									
								
								support/3rd_party/include/utils/file.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,147 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_FILE_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_FILE_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils::file { | ||||
| class file final { | ||||
| public: | ||||
|   [[nodiscard]] static auto open_file(std::filesystem::path path) -> file; | ||||
|  | ||||
|   [[nodiscard]] static auto | ||||
|   open_or_create_file(std::filesystem::path path) -> file; | ||||
|  | ||||
| protected: | ||||
|   file(std::fstream stream, std::filesystem::path path) | ||||
|       : path_(std::move(path)), stream_(std::move(stream)) {} | ||||
|   file() = default; | ||||
|  | ||||
| public: | ||||
|   file(const file &) = delete; | ||||
|   file(file &&file_) noexcept = default; | ||||
|  | ||||
|   ~file() { close(); } | ||||
|  | ||||
|   auto operator=(const file &) noexcept -> file & = delete; | ||||
|   auto operator=(file &&file_) noexcept -> file & = default; | ||||
|  | ||||
| private: | ||||
|   std::error_code error_{}; | ||||
|   std::filesystem::path path_; | ||||
|   std::fstream stream_; | ||||
|  | ||||
| public: | ||||
|   void close(); | ||||
|  | ||||
|   [[nodiscard]] auto get_error_code() const -> std::error_code { | ||||
|     return error_; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_path() const -> std::filesystem::path { return path_; } | ||||
|  | ||||
|   [[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool; | ||||
|  | ||||
|   [[nodiscard]] auto read(data_buffer &data, std::uint64_t offset, | ||||
|                           std::size_t *total_read = nullptr) -> bool { | ||||
|     return read(data.data(), data.size(), offset, total_read); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto remove() -> bool; | ||||
|  | ||||
|   [[nodiscard]] auto truncate() -> bool { return truncate(0U); } | ||||
|  | ||||
|   [[nodiscard]] auto truncate(std::size_t size) -> bool; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_JSON) | ||||
|   [[nodiscard]] auto write(const nlohmann::json &data, std::uint64_t offset, | ||||
|                            std::size_t *total_written = nullptr) -> bool { | ||||
|     return write(data.dump().c_str(), offset, total_written); | ||||
|   } | ||||
| #endif // defined(PROJECT_ENABLE_JSON) | ||||
|  | ||||
|   [[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset, | ||||
|                            std::size_t *total_written = nullptr) -> bool { | ||||
|     return write(data.data(), data.size(), offset, total_written); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] operator bool() const { return stream_.is_open(); } | ||||
|  | ||||
| private: | ||||
|   [[nodiscard]] auto read(unsigned char *data, std::size_t to_read, | ||||
|                           std::uint64_t offset, | ||||
|                           std::size_t *total_read) -> bool; | ||||
|  | ||||
|   [[nodiscard]] auto write(const typename data_buffer::value_type *data, | ||||
|                            std::size_t to_write, std::size_t offset, | ||||
|                            std::size_t *total_written) -> bool; | ||||
| }; | ||||
|  | ||||
| [[nodiscard]] auto get_file_size(std::string_view path, | ||||
|                                  std::uint64_t &file_size) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto get_file_size(std::wstring_view path, | ||||
|                                  std::uint64_t &file_size) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto is_directory(std::string_view path) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto is_directory(std::wstring_view path) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto is_file(std::string_view path) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto is_file(std::wstring_view path) -> bool; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_JSON) | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| [[nodiscard]] auto | ||||
| read_json_file(std::string_view path, nlohmann::json &data, | ||||
|                std::optional<std::string_view> password = std::nullopt) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto read_json_file( | ||||
|     std::wstring_view path, nlohmann::json &data, | ||||
|     std::optional<std::wstring_view> password = std::nullopt) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto write_json_file( | ||||
|     std::string_view path, const nlohmann::json &data, | ||||
|     std::optional<std::string_view> password = std::nullopt) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto write_json_file( | ||||
|     std::wstring_view path, const nlohmann::json &data, | ||||
|     std::optional<std::wstring_view> password = std::nullopt) -> bool; | ||||
| #else  // !defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| [[nodiscard]] auto read_json_file(std::string_view path, | ||||
|                                   nlohmann::json &data) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto read_json_file(std::wstring_view path, | ||||
|                                   nlohmann::json &data) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto write_json_file(std::string_view path, | ||||
|                                    const nlohmann::json &data) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto write_json_file(std::wstring_view path, | ||||
|                                    const nlohmann::json &data) -> bool; | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| #endif // defined(PROJECT_ENABLE_JSON) | ||||
| } // namespace repertory::utils::file | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_ | ||||
							
								
								
									
										403
									
								
								support/3rd_party/include/utils/path.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										403
									
								
								support/3rd_party/include/utils/path.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,403 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_PATH_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_PATH_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
| #include "utils/string.hpp" | ||||
|  | ||||
| namespace repertory::utils::path { | ||||
| inline constexpr const std::string_view backslash{"\\"}; | ||||
| inline constexpr const std::wstring_view backslash_w{L"\\"}; | ||||
| inline constexpr const std::string_view dot{"."}; | ||||
| inline constexpr const std::wstring_view dot_w{L"."}; | ||||
| inline constexpr const std::string_view dot_slash{"./"}; | ||||
| inline constexpr const std::wstring_view dot_slash_w{L"./"}; | ||||
| inline constexpr const std::string_view slash{"/"}; | ||||
| inline constexpr const std::wstring_view slash_w{L"/"}; | ||||
| #if defined(_WIN32) | ||||
| inline constexpr const std::string_view directory_seperator{backslash}; | ||||
| inline constexpr const std::wstring_view directory_seperator_w{backslash_w}; | ||||
| inline constexpr const std::string_view not_directory_seperator{slash}; | ||||
| inline constexpr const std::wstring_view not_directory_seperator_w{slash_w}; | ||||
| #else  // !defined(_WIN32) | ||||
| inline constexpr const std::string_view directory_seperator{slash}; | ||||
| inline constexpr const std::wstring_view directory_seperator_w{slash_w}; | ||||
| inline constexpr const std::string_view not_directory_seperator{backslash}; | ||||
| inline constexpr const std::wstring_view not_directory_seperator_w{backslash_w}; | ||||
| #endif // defined(_WIN32) | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_backslash() -> std::basic_string_view<char_t>; | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_backslash<char>() -> std::basic_string_view<char> { | ||||
|   return backslash; | ||||
| } | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_backslash<wchar_t>() -> std::basic_string_view<wchar_t> { | ||||
|   return backslash_w; | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline constexpr auto get_dot() -> std::basic_string_view<char_t>; | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_dot<char>() -> std::basic_string_view<char> { | ||||
|   return dot; | ||||
| } | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_dot<wchar_t>() -> std::basic_string_view<wchar_t> { | ||||
|   return dot_w; | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_dot_slash() -> std::basic_string_view<char_t>; | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_dot_slash<char>() -> std::basic_string_view<char> { | ||||
|   return dot_slash; | ||||
| } | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_dot_slash<wchar_t>() -> std::basic_string_view<wchar_t> { | ||||
|   return dot_slash_w; | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_slash() -> std::basic_string_view<char_t>; | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_slash<char>() -> std::basic_string_view<char> { | ||||
|   return slash; | ||||
| } | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_slash<wchar_t>() -> std::basic_string_view<wchar_t> { | ||||
|   return slash_w; | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_directory_seperator() -> std::basic_string_view<char_t>; | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_directory_seperator<char>() -> std::basic_string_view<char> { | ||||
|   return directory_seperator; | ||||
| } | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_directory_seperator<wchar_t>() -> std::basic_string_view<wchar_t> { | ||||
|   return directory_seperator_w; | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_not_directory_seperator() -> std::basic_string_view<char_t>; | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_not_directory_seperator<char>() -> std::basic_string_view<char> { | ||||
|   return not_directory_seperator; | ||||
| } | ||||
|  | ||||
| template <> | ||||
| [[nodiscard]] inline constexpr auto | ||||
| get_not_directory_seperator<wchar_t>() -> std::basic_string_view<wchar_t> { | ||||
|   return not_directory_seperator_w; | ||||
| } | ||||
|  | ||||
| [[nodiscard]] inline auto absolute(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] inline auto absolute(std::wstring_view path) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| combine(std::string path, | ||||
|         const std::vector<std::string_view> &paths) -> std::string; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| combine(std::wstring path, | ||||
|         const std::vector<std::wstring_view> &paths) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] auto inline create_api_path(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto inline create_api_path(std::wstring_view path) | ||||
|     -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| directory_exists_in_path(std::string_view path, | ||||
|                          std::string_view sub_directory) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| directory_exists_in_path(std::wstring_view path, | ||||
|                          std::wstring_view sub_directory) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| file_exists_in_path(std::string_view path, std::string_view file_name) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| file_exists_in_path(std::wstring_view path, | ||||
|                     std::wstring_view file_name) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto finalize(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] inline auto finalize(std::wstring_view path) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] auto | ||||
| find_program_in_path(const std::string &name_without_extension) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto | ||||
| find_program_in_path(std::wstring_view name_without_extension) -> std::wstring; | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto | ||||
| format_path(string_t &path, | ||||
|             std::basic_string_view<typename string_t::value_type> sep, | ||||
|             std::basic_string_view<typename string_t::value_type> not_sep) | ||||
|     -> string_t &; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| get_parent_api_path(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] inline auto | ||||
| get_parent_api_path(std::wstring_view path) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] auto get_parent_directory(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto get_parent_directory(std::wstring_view path) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] auto is_trash_directory(std::string_view path) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto is_trash_directory(std::wstring_view path) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto make_file_uri(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto make_file_uri(std::wstring_view path) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] auto remove_file_name(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto remove_file_name(std::wstring_view path) -> std::wstring; | ||||
|  | ||||
| #if !defined(_WIN32) | ||||
| [[nodiscard]] auto resolve(std::string path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto resolve(std::wstring_view path) -> std::wstring; | ||||
| #endif // !defined(_WIN32) | ||||
|  | ||||
| [[nodiscard]] auto strip_to_file_name(std::string_view path) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto unmake_file_uri(std::string_view uri) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto unmake_file_uri(std::wstring_view uri) -> std::wstring; | ||||
|  | ||||
| inline auto absolute(std::string_view path) -> std::string { | ||||
|   return finalize(std::filesystem::absolute(path).lexically_normal().string()); | ||||
| } | ||||
|  | ||||
| inline auto absolute(std::wstring_view path) -> std::wstring { | ||||
|   return finalize(std::filesystem::absolute(path).lexically_normal().wstring()); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto combine_t( | ||||
|     string_t path, | ||||
|     const std::vector<std::basic_string_view<typename string_t::value_type>> | ||||
|         &paths) -> string_t { | ||||
|   return absolute(std::accumulate( | ||||
|       paths.begin(), paths.end(), path, [](auto next_path, auto &&path_part) { | ||||
|         if (next_path.empty()) { | ||||
|           return string_t{path_part}; | ||||
|         } | ||||
|  | ||||
|         return next_path + | ||||
|                string_t{ | ||||
|                    get_directory_seperator<typename string_t::value_type>()} + | ||||
|                string_t{path_part}; | ||||
|       })); | ||||
| } | ||||
|  | ||||
| inline auto combine(std::string path, | ||||
|                     const std::vector<std::string_view> &paths) -> std::string { | ||||
|   return combine_t<std::string>(path, paths); | ||||
| } | ||||
|  | ||||
| inline auto | ||||
| combine(std::wstring path, | ||||
|         const std::vector<std::wstring_view> &paths) -> std::wstring { | ||||
|   return combine_t<std::wstring>(path, paths); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto create_api_path_t( | ||||
|     std::basic_string_view<typename string_t::value_type> path) -> string_t { | ||||
|   static auto backslash_t = get_backslash<typename string_t::value_type>(); | ||||
|   static auto dot_slash_t = get_dot_slash<typename string_t::value_type>(); | ||||
|   static auto dot_t = get_dot<typename string_t::value_type>(); | ||||
|   static auto slash_t = get_slash<typename string_t::value_type>(); | ||||
|  | ||||
|   if (path.empty() || (path == dot_t) || (path == slash_t)) { | ||||
|     return string_t{slash_t}; | ||||
|   } | ||||
|  | ||||
|   string_t api_path{path}; | ||||
|   format_path(api_path, slash_t, backslash_t); | ||||
|   if (api_path.find(dot_slash_t) == 0) { | ||||
|     api_path = api_path.substr(1U); | ||||
|   } | ||||
|  | ||||
|   if (api_path.at(0U) != '/') { | ||||
|     api_path = string_t{slash_t} + api_path; | ||||
|   } | ||||
|  | ||||
|   if ((api_path != slash_t) && utils::string::ends_with(api_path, slash_t)) { | ||||
|     utils::string::right_trim(api_path, '/'); | ||||
|   } | ||||
|  | ||||
|   return api_path; | ||||
| } | ||||
|  | ||||
| inline auto create_api_path(std::string_view path) -> std::string { | ||||
|   return create_api_path_t<std::string>(path); | ||||
| } | ||||
|  | ||||
| inline auto create_api_path(std::wstring_view path) -> std::wstring { | ||||
|   return create_api_path_t<std::wstring>(path); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto directory_exists_in_path_t( | ||||
|     std::basic_string_view<typename string_t::value_type> path, | ||||
|     std::basic_string_view<typename string_t::value_type> sub_directory) | ||||
|     -> bool { | ||||
|   return std::filesystem::is_directory( | ||||
|       std::filesystem::path(path).append(sub_directory)); | ||||
| } | ||||
|  | ||||
| inline auto directory_exists_in_path(std::string_view path, | ||||
|                                      std::string_view sub_directory) -> bool { | ||||
|   return directory_exists_in_path_t<std::string>(path, sub_directory); | ||||
| } | ||||
|  | ||||
| inline auto directory_exists_in_path(std::wstring_view path, | ||||
|                                      std::wstring_view sub_directory) -> bool { | ||||
|   return directory_exists_in_path_t<std::wstring>(path, sub_directory); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto file_exists_in_path_t( | ||||
|     std::basic_string_view<typename string_t::value_type> path, | ||||
|     std::basic_string_view<typename string_t::value_type> file_name) -> bool { | ||||
|   return std::filesystem::is_regular_file( | ||||
|       std::filesystem::path(path).append(file_name)); | ||||
| } | ||||
|  | ||||
| inline auto file_exists_in_path(std::string_view path, | ||||
|                                 std::string_view file_name) -> bool { | ||||
|   return file_exists_in_path_t<std::string>(path, file_name); | ||||
| } | ||||
|  | ||||
| inline auto file_exists_in_path(std::wstring_view path, | ||||
|                                 std::wstring_view file_name) -> bool { | ||||
|   return file_exists_in_path_t<std::wstring>(path, file_name); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto finalize_t( | ||||
|     std::basic_string_view<typename string_t::value_type> path) -> string_t { | ||||
|   string_t fmt_path{path}; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
|   if ((fmt_path.size() >= 2U) && (fmt_path.at(1U) == ':')) { | ||||
|     fmt_path[0U] = | ||||
|         utils::string::to_lower(string_t{1U, fmt_path.at(0U)}).at(0U); | ||||
|   } | ||||
| #endif // defined(_WIN32) | ||||
|  | ||||
|   return fmt_path; | ||||
| } | ||||
|  | ||||
| inline auto finalize(std::string_view path) -> std::string { | ||||
|   return finalize_t<std::string>(path); | ||||
| } | ||||
|  | ||||
| inline auto finalize(std::wstring_view path) -> std::wstring { | ||||
|   return finalize_t<std::wstring>(path); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto | ||||
| format_path(string_t &path, | ||||
|             std::basic_string_view<typename string_t::value_type> sep, | ||||
|             std::basic_string_view<typename string_t::value_type> not_sep) | ||||
|     -> string_t & { | ||||
|   utils::string::replace(path, not_sep, sep); | ||||
|  | ||||
|   string_t double_sep{2, sep.at(0U)}; | ||||
|   while (utils::string::contains(path, double_sep)) { | ||||
|     utils::string::replace(path, double_sep, sep); | ||||
|   } | ||||
|  | ||||
|   return path; | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto get_parent_api_path_t( | ||||
|     std::basic_string_view<typename string_t::value_type> path) -> string_t { | ||||
|   static auto slash_t = get_slash<typename string_t::value_type>(); | ||||
|  | ||||
|   string_t ret; | ||||
|   if (path == slash_t) { | ||||
|     return ret; | ||||
|   } | ||||
|  | ||||
|   ret = path.substr(0, path.rfind('/') + 1); | ||||
|   if (ret == slash_t) { | ||||
|     return ret; | ||||
|   } | ||||
|   return utils::string::right_trim(ret, '/'); | ||||
| } | ||||
|  | ||||
| inline auto get_parent_api_path(std::string_view path) -> std::string { | ||||
|   return get_parent_api_path_t<std::string>(path); | ||||
| } | ||||
|  | ||||
| inline auto get_parent_api_path(std::wstring_view path) -> std::wstring { | ||||
|   return get_parent_api_path_t<std::wstring>(path); | ||||
| } | ||||
| } // namespace repertory::utils::path | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_PATH_HPP_ | ||||
							
								
								
									
										496
									
								
								support/3rd_party/include/utils/string.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										496
									
								
								support/3rd_party/include/utils/string.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,496 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_STRING_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_STRING_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils::string { | ||||
| template <typename string_t> struct chain_replace_with_hex; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| char_to_hex(typename string_t::value_type character) -> string_t; | ||||
|  | ||||
| [[nodiscard]] inline auto begins_with(std::string_view str, | ||||
|                                       std::string_view val) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto begins_with(std::wstring_view str, | ||||
|                                       std::wstring_view val) -> bool; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto case_insensitive_find_string(string_t in_str, | ||||
|                                                        string_t for_str) -> | ||||
|     typename string_t::const_iterator; | ||||
|  | ||||
| [[nodiscard]] inline auto contains(std::string_view str, | ||||
|                                    std::string_view search) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto contains(std::wstring_view str, | ||||
|                                    std::wstring_view search) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto ends_with(std::string_view str, | ||||
|                                     std::string_view val) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto ends_with(std::wstring_view str, | ||||
|                                     std::wstring_view val) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto from_bool(bool val) -> std::string; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| [[nodiscard]] auto | ||||
| from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string; | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
|  | ||||
| [[nodiscard]] auto from_utf8(std::string_view str) -> std::wstring; | ||||
|  | ||||
| [[nodiscard]] inline auto is_numeric(std::string_view str) -> bool; | ||||
|  | ||||
| [[nodiscard]] inline auto is_numeric(std::wstring_view str) -> bool; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto join(const std::vector<string_t> &arr, | ||||
|                                typename string_t::value_type delim) -> string_t; | ||||
|  | ||||
| template <typename string_t> | ||||
| auto left_trim(string_t &str, | ||||
|                typename string_t::value_type trim_ch = ' ') -> string_t &; | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto replace(string_t &src, typename string_t::value_type character, | ||||
|                     typename string_t::value_type with, | ||||
|                     std::size_t start_pos = 0U) -> string_t &; | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto replace(string_t &src, | ||||
|                     std::basic_string_view<typename string_t::value_type> find, | ||||
|                     std::basic_string_view<typename string_t::value_type> with, | ||||
|                     std::size_t start_pos = 0U) -> string_t &; | ||||
|  | ||||
| template <typename t> | ||||
| [[nodiscard]] auto from_hex_string(const std::string &str, t &val) -> bool; | ||||
|  | ||||
| template <typename collection_t> | ||||
| [[nodiscard]] auto to_hex_string(const collection_t &collection) -> std::string; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto replace_copy(string_t src, | ||||
|                                        typename string_t::value_type character, | ||||
|                                        typename string_t::value_type with, | ||||
|                                        std::size_t start_pos = 0U) -> string_t; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| replace_copy(string_t src, | ||||
|              std::basic_string_view<typename string_t::value_type> find, | ||||
|              std::basic_string_view<typename string_t::value_type> with, | ||||
|              std::size_t start_pos = 0U) -> string_t; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| replace_with_hex(string_t &str, typename string_t::value_type character) | ||||
|     -> chain_replace_with_hex<string_t>; | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto | ||||
| right_trim(string_t &str, | ||||
|            typename string_t::value_type trim_ch = ' ') -> string_t &; | ||||
|  | ||||
| [[nodiscard]] inline auto split(std::string_view str, char delim, | ||||
|                                 bool should_trim) -> std::vector<std::string>; | ||||
|  | ||||
| [[nodiscard]] inline auto split(std::wstring_view str, wchar_t delim, | ||||
|                                 bool should_trim) -> std::vector<std::wstring>; | ||||
|  | ||||
| [[nodiscard]] inline auto split(std::string_view str, std::string_view delim, | ||||
|                                 bool should_trim) -> std::vector<std::string>; | ||||
|  | ||||
| [[nodiscard]] inline auto split(std::wstring_view str, std::wstring_view delim, | ||||
|                                 bool should_trim) -> std::vector<std::wstring>; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SFML) | ||||
| auto replace_sf(sf::String &src, const sf::String &find, const sf::String &with, | ||||
|                 std::size_t start_pos = 0U) -> sf::String &; | ||||
|  | ||||
| [[nodiscard]] auto split_sf(sf::String str, wchar_t delim, | ||||
|                             bool should_trim) -> std::vector<sf::String>; | ||||
| #endif // defined(PROJECT_ENABLE_SFML) | ||||
|  | ||||
| [[nodiscard]] auto to_bool(std::string val) -> bool; | ||||
|  | ||||
| [[nodiscard]] auto to_double(const std::string &str) -> double; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| [[nodiscard]] auto | ||||
| to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<>; | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto to_lower(string_t str) -> string_t; | ||||
|  | ||||
| [[nodiscard]] auto to_int32(const std::string &val) -> std::int32_t; | ||||
|  | ||||
| [[nodiscard]] auto to_int64(const std::string &val) -> std::int64_t; | ||||
|  | ||||
| [[nodiscard]] auto to_size_t(const std::string &val) -> std::size_t; | ||||
|  | ||||
| [[nodiscard]] auto to_uint8(const std::string &val) -> std::uint8_t; | ||||
|  | ||||
| [[nodiscard]] auto to_uint16(const std::string &val) -> std::uint16_t; | ||||
|  | ||||
| [[nodiscard]] auto to_uint32(const std::string &val) -> std::uint32_t; | ||||
|  | ||||
| [[nodiscard]] auto to_uint64(const std::string &val) -> std::uint64_t; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto to_upper(string_t str) -> string_t; | ||||
|  | ||||
| [[nodiscard]] auto to_utf8(std::string_view str) -> std::string; | ||||
|  | ||||
| [[nodiscard]] auto to_utf8(std::wstring_view str) -> std::string; | ||||
|  | ||||
| template <typename string_t> struct chain_replace_with_hex final { | ||||
|   explicit chain_replace_with_hex(string_t &value) : str(value) {} | ||||
|  | ||||
|   chain_replace_with_hex(const chain_replace_with_hex &) = delete; | ||||
|  | ||||
|   chain_replace_with_hex(chain_replace_with_hex &&) = delete; | ||||
|  | ||||
|   ~chain_replace_with_hex() = default; | ||||
|  | ||||
|   auto operator=(const chain_replace_with_hex &) -> chain_replace_with_hex & = | ||||
|                                                         delete; | ||||
|  | ||||
|   auto | ||||
|   operator=(chain_replace_with_hex &&) -> chain_replace_with_hex & = delete; | ||||
|  | ||||
|   auto operator()(typename string_t::value_type character) | ||||
|       -> chain_replace_with_hex { | ||||
|     return replace_with_hex<string_t>(str, character); | ||||
|   } | ||||
|  | ||||
|   string_t &str; | ||||
| }; | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto trim(string_t &str, | ||||
|                  typename string_t::value_type trim_ch = ' ') -> string_t &; | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| trim_copy(string_t str, | ||||
|           typename string_t::value_type trim_ch = ' ') -> string_t; | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline auto | ||||
| begins_with_t(std::basic_string_view<char_t> str, | ||||
|               std::basic_string_view<char_t> val) -> bool { | ||||
|   return (str.find(val) == 0U); | ||||
| } | ||||
|  | ||||
| inline auto begins_with(std::string_view str, std::string_view val) -> bool { | ||||
|   return begins_with_t<std::string_view::value_type>(str, val); | ||||
| } | ||||
|  | ||||
| inline auto begins_with(std::wstring_view str, std::wstring_view val) -> bool { | ||||
|   return begins_with_t<std::wstring_view::value_type>(str, val); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto case_insensitive_find_string(string_t in_str, string_t for_str) -> | ||||
|     typename string_t::const_iterator { | ||||
|   static const auto compare_chars = | ||||
|       [](typename string_t::value_type char_a, | ||||
|          typename string_t::value_type char_b) -> bool { | ||||
|     return (std::tolower(char_a) == std::tolower(char_b)); | ||||
|   }; | ||||
|  | ||||
|   return (std::search(in_str.begin(), in_str.end(), for_str.begin(), | ||||
|                       for_str.end(), compare_chars)); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto char_to_hex(typename string_t::value_type character) -> string_t { | ||||
|   std::basic_stringstream<typename string_t::value_type> stream; | ||||
|   stream << '%' << std::setfill<typename string_t::value_type>('0') | ||||
|          << std::setw(sizeof(character)) << std::hex | ||||
|          << static_cast<std::uint32_t>(character); | ||||
|   return stream.str(); | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline auto | ||||
| contains_t(std::basic_string_view<char_t> str, | ||||
|            std::basic_string_view<char_t> search) -> bool { | ||||
|   return (str.find(search) != std::basic_string_view<char_t>::npos); | ||||
| } | ||||
|  | ||||
| inline auto contains(std::string_view str, std::string_view search) -> bool { | ||||
|   return contains_t<std::string_view::value_type>(str, search); | ||||
| } | ||||
|  | ||||
| inline auto contains(std::wstring_view str, std::wstring_view search) -> bool { | ||||
|   return contains_t<std::wstring_view::value_type>(str, search); | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline auto | ||||
| ends_with_t(std::basic_string_view<char_t> str, | ||||
|             std::basic_string_view<char_t> val) -> bool { | ||||
|   if (val.size() > str.size()) { | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   return std::equal(val.rbegin(), val.rend(), str.rbegin()); | ||||
| } | ||||
|  | ||||
| inline auto ends_with(std::string_view str, std::string_view val) -> bool { | ||||
|   return ends_with_t<std::string_view::value_type>(str, val); | ||||
| } | ||||
|  | ||||
| inline auto ends_with(std::wstring_view str, std::wstring_view val) -> bool { | ||||
|   return ends_with_t<std::wstring_view::value_type>(str, val); | ||||
| } | ||||
|  | ||||
| template <typename char_t> | ||||
| [[nodiscard]] inline auto | ||||
| is_numeric_t(std::basic_string_view<char_t> str) -> bool { | ||||
|   if ((str.length() > 1U) && (str.at(0U) == '+' || str.at(0U) == '-')) { | ||||
|     str = str.substr(1U); | ||||
|   } | ||||
|  | ||||
|   if (str.empty()) { | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   auto has_decimal{false}; | ||||
|   return std::find_if(str.begin(), str.end(), | ||||
|                       [&has_decimal](auto &&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(); | ||||
| } | ||||
|  | ||||
| inline auto is_numeric(std::string_view str) -> bool { | ||||
|   return is_numeric_t<std::string_view::value_type>(str); | ||||
| } | ||||
|  | ||||
| inline auto is_numeric(std::wstring_view str) -> bool { | ||||
|   return is_numeric_t<std::wstring_view::value_type>(str); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto join(const std::vector<string_t> &arr, | ||||
|                  typename string_t::value_type delim) -> string_t { | ||||
|   if (arr.empty()) { | ||||
|     return {}; | ||||
|   } | ||||
|  | ||||
|   return std::accumulate( | ||||
|       std::next(arr.begin()), arr.end(), arr[0U], | ||||
|       [&delim](auto str, const auto &cur) { return str + delim + cur; }); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| auto left_trim(string_t &str, | ||||
|                typename string_t::value_type trim_ch) -> string_t & { | ||||
|   str.erase(0, str.find_first_not_of(trim_ch)); | ||||
|   return str; | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto replace(string_t &src, typename string_t::value_type character, | ||||
|                     typename string_t::value_type with, | ||||
|                     std::size_t start_pos) -> string_t & { | ||||
|   if (not src.empty() && (start_pos < src.size())) { | ||||
|     std::replace(std::next(src.begin(), start_pos), src.end(), character, with); | ||||
|   } | ||||
|  | ||||
|   return src; | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto replace(string_t &src, | ||||
|                     std::basic_string_view<typename string_t::value_type> find, | ||||
|                     std::basic_string_view<typename string_t::value_type> with, | ||||
|                     std::size_t start_pos) -> string_t & { | ||||
|   if (not src.empty() && (start_pos < src.size())) { | ||||
|     while ((start_pos = src.find(find, start_pos)) != string_t::npos) { | ||||
|       src.replace(start_pos, find.size(), with); | ||||
|       start_pos += with.size(); | ||||
|     } | ||||
|   } | ||||
|   return src; | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto replace_copy(string_t src, typename string_t::value_type character, | ||||
|                          typename string_t::value_type with, | ||||
|                          std::size_t start_pos) -> string_t { | ||||
|   return replace(src, character, with, start_pos); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto | ||||
| replace_copy(string_t src, | ||||
|              std::basic_string_view<typename string_t::value_type> find, | ||||
|              std::basic_string_view<typename string_t::value_type> with, | ||||
|              std::size_t start_pos) -> string_t { | ||||
|   return replace(src, find, with, start_pos); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto replace_with_hex(string_t &str, | ||||
|                              typename string_t::value_type character) | ||||
|     -> chain_replace_with_hex<string_t> { | ||||
|   return chain_replace_with_hex( | ||||
|       replace(str, string_t{1U, character}, char_to_hex<string_t>(character))); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto right_trim(string_t &str, | ||||
|                        typename string_t::value_type trim_ch) -> string_t & { | ||||
|   str.erase(str.find_last_not_of(trim_ch) + 1); | ||||
|   return str; | ||||
| } | ||||
|  | ||||
| template <typename string_t> inline auto to_lower(string_t str) -> string_t { | ||||
|   std::transform(str.begin(), str.end(), str.begin(), ::tolower); | ||||
|   return str; | ||||
| } | ||||
|  | ||||
| template <typename string_t> inline auto to_upper(string_t str) -> string_t { | ||||
|   std::transform(str.begin(), str.end(), str.begin(), ::toupper); | ||||
|   return str; | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto trim(string_t &str, | ||||
|                  typename string_t::value_type trim_ch) -> string_t & { | ||||
|   return right_trim(left_trim(str, trim_ch), trim_ch); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| inline auto trim_copy(string_t str, | ||||
|                       typename string_t::value_type trim_ch) -> string_t { | ||||
|   return trim(str, trim_ch); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| split_t(std::basic_string_view<typename string_t::value_type> str, | ||||
|         typename string_t::value_type delim, | ||||
|         bool should_trim) -> std::vector<string_t> { | ||||
|   std::vector<string_t> ret; | ||||
|   std::basic_stringstream<typename string_t::value_type> stream{string_t{str}}; | ||||
|  | ||||
|   string_t val; | ||||
|   while (std::getline(stream, val, delim)) { | ||||
|     if (should_trim) { | ||||
|       trim(val); | ||||
|     } | ||||
|     ret.push_back(std::move(val)); | ||||
|   } | ||||
|  | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| inline auto split(std::string_view str, char delim, | ||||
|                   bool should_trim) -> std::vector<std::string> { | ||||
|   return split_t<std::string>(str, delim, should_trim); | ||||
| } | ||||
|  | ||||
| inline auto split(std::wstring_view str, wchar_t delim, | ||||
|                   bool should_trim) -> std::vector<std::wstring> { | ||||
|   return split_t<std::wstring>(str, delim, should_trim); | ||||
| } | ||||
|  | ||||
| template <typename string_t> | ||||
| [[nodiscard]] inline auto | ||||
| split_t(std::basic_string_view<typename string_t::value_type> str, | ||||
|         std::basic_string_view<typename string_t::value_type> delim, | ||||
|         bool should_trim) -> std::vector<string_t> { | ||||
|   auto result = std::views::split(str, delim); | ||||
|  | ||||
|   std::vector<string_t> ret{}; | ||||
|   for (auto &&word : result) { | ||||
|     auto val = string_t{word.begin(), word.end()}; | ||||
|     if (should_trim) { | ||||
|       trim(val); | ||||
|     } | ||||
|     ret.push_back(std::move(val)); | ||||
|   } | ||||
|  | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| inline auto split(std::string_view str, std::string_view delim, | ||||
|                   bool should_trim) -> std::vector<std::string> { | ||||
|   return split_t<std::string>(str, delim, should_trim); | ||||
| } | ||||
|  | ||||
| inline auto split(std::wstring_view str, std::wstring_view delim, | ||||
|                   bool should_trim) -> std::vector<std::wstring> { | ||||
|   return split_t<std::wstring>(str, delim, should_trim); | ||||
| } | ||||
|  | ||||
| template <typename t> | ||||
| auto from_hex_string(const std::string &str, t &val) -> bool { | ||||
|   static constexpr const auto base16 = 16; | ||||
|  | ||||
|   val.clear(); | ||||
|   if (not(str.length() % 2U)) { | ||||
|     for (std::size_t i = 0U; i < str.length(); i += 2U) { | ||||
|       val.emplace_back(static_cast<typename t::value_type>( | ||||
|           strtol(str.substr(i, 2U).c_str(), nullptr, base16))); | ||||
|     } | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   return false; | ||||
| } | ||||
|  | ||||
| template <typename collection_t> | ||||
| auto to_hex_string(const collection_t &collection) -> std::string { | ||||
|   static_assert(sizeof(typename collection_t::value_type) == 1U, | ||||
|                 "value_type must be 1 byte in size"); | ||||
|   static constexpr const auto mask = 0xFF; | ||||
|  | ||||
|   std::stringstream stream; | ||||
|   for (auto &&val : collection) { | ||||
|     stream << std::setfill('0') << std::setw(2) << std::hex | ||||
|            << (static_cast<std::uint32_t>(val) & mask); | ||||
|   } | ||||
|  | ||||
|   return stream.str(); | ||||
| } | ||||
| } // namespace repertory::utils::string | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_STRING_HPP_ | ||||
							
								
								
									
										64
									
								
								support/3rd_party/include/utils/time.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								support/3rd_party/include/utils/time.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_TIME_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_TIME_HPP_ | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils::time { | ||||
| inline constexpr const auto NANOS_PER_SECOND = 1000000000L; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) | ||||
| [[nodiscard]] inline auto convert_to_utc(time_t time) -> std::time_t { | ||||
|   auto calendar_time = fmt::gmtime(time); | ||||
|   return std::mktime(&calendar_time); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| [[nodiscard]] auto | ||||
| filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t; | ||||
| #endif // defined(_WIN32) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) | ||||
| [[nodiscard]] inline auto get_current_time_utc() -> std::time_t { | ||||
|   auto calendar_time = fmt::gmtime(std::time(nullptr)); | ||||
|   return std::mktime(&calendar_time); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) | ||||
|  | ||||
| [[nodiscard]] auto get_file_time_now() -> std::uint64_t; | ||||
|  | ||||
| void get_local_time_now(struct tm &local_time); | ||||
|  | ||||
| [[nodiscard]] auto get_time_now() -> std::uint64_t; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| auto strptime(const char *s, const char *f, struct tm *tm) -> const char *; | ||||
|  | ||||
| [[nodiscard]] auto time64_to_unix_time(const __time64_t &time) -> std::uint64_t; | ||||
|  | ||||
| [[nodiscard]] auto unix_time_to_filetime(std::uint64_t unix_time) -> FILETIME; | ||||
| #endif // defined(_WIN32) | ||||
| } // namespace repertory::utils::time | ||||
|  | ||||
| #endif // REPERTORY_INCLUDE_UTILS_TIME_HPP_ | ||||
							
								
								
									
										64
									
								
								support/3rd_party/include/utils/unix.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								support/3rd_party/include/utils/unix.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_UNIX_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_UNIX_HPP_ | ||||
| #if !defined(_WIN32) | ||||
|  | ||||
| #include "utils/common.hpp" | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils { | ||||
| using passwd_callback_t = std::function<void(struct passwd *pass)>; | ||||
|  | ||||
| #if defined(__APPLE__) | ||||
| template <typename thread_t> | ||||
| [[nodiscard]] auto | ||||
| convert_to_uint64(const thread_t *thread_ptr) -> std::uint64_t; | ||||
| #else  // !defined(__APPLE__) | ||||
| [[nodiscard]] auto convert_to_uint64(const pthread_t &thread) -> std::uint64_t; | ||||
| #endif // defined(__APPLE__) | ||||
|  | ||||
| [[nodiscard]] auto get_last_error_code() -> int; | ||||
|  | ||||
| [[nodiscard]] auto get_thread_id() -> std::uint64_t; | ||||
|  | ||||
| [[nodiscard]] auto is_uid_member_of_group(const uid_t &uid, | ||||
|                                           const gid_t &gid) -> bool; | ||||
|  | ||||
| void set_last_error_code(int error_code); | ||||
|  | ||||
| [[nodiscard]] auto use_getpwuid(uid_t uid, | ||||
|                                 passwd_callback_t callback) -> utils::result; | ||||
|  | ||||
| // template implementations | ||||
| #if defined(__APPLE__) | ||||
| template <typename t> | ||||
| [[nodiscard]] auto | ||||
| convert_to_uint64(const thread_t *thread_ptr) -> std::uint64_t { | ||||
|   return static_cast<std::uint64_t>( | ||||
|       reinterpret_cast<std::uintptr_t>(thread_ptr)); | ||||
| } | ||||
| #endif // defined(__APPLE__) | ||||
| } // namespace repertory::utils | ||||
|  | ||||
| #endif // !defined(_WIN32) | ||||
| #endif // REPERTORY_INCLUDE_UTILS_UNIX_HPP_ | ||||
							
								
								
									
										43
									
								
								support/3rd_party/include/utils/windows.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								support/3rd_party/include/utils/windows.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| /* | ||||
|   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. | ||||
| */ | ||||
| #ifndef REPERTORY_INCLUDE_UTILS_WINDOWS_HPP_ | ||||
| #define REPERTORY_INCLUDE_UTILS_WINDOWS_HPP_ | ||||
| #if defined(_WIN32) | ||||
|  | ||||
| #include "utils/config.hpp" | ||||
|  | ||||
| namespace repertory::utils { | ||||
| [[nodiscard]] auto get_local_app_data_directory() -> const std::string &; | ||||
|  | ||||
| [[nodiscard]] auto get_last_error_code() -> DWORD; | ||||
|  | ||||
| [[nodiscard]] auto get_thread_id() -> std::uint64_t; | ||||
|  | ||||
| [[nodiscard]] auto is_process_elevated() -> bool; | ||||
|  | ||||
| [[nodiscard]] auto run_process_elevated(std::vector<const char *> args) -> int; | ||||
|  | ||||
| void set_last_error_code(DWORD errorCode); | ||||
| } // namespace repertory::utils | ||||
|  | ||||
| #endif // defined(_WIN32) | ||||
| #endif // REPERTORY_INCLUDE_UTILS_WINDOWS_HPP_ | ||||
		Reference in New Issue
	
	Block a user