2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
#include <algorithm> // for std::sort, std::transform
|
2021-09-13 13:45:01 +00:00
|
|
|
#include <array> // std::array
|
2022-09-21 23:34:18 +00:00
|
|
|
#include <cctype>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <cfloat> // DBL_DIG
|
2022-02-01 01:02:59 +00:00
|
|
|
#include <chrono>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <cstdint> // SIZE_MAX
|
|
|
|
#include <cstdlib> // getenv()
|
|
|
|
#include <cstring> /* strerror() */
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <ctime>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <exception>
|
2023-04-27 14:10:33 +00:00
|
|
|
#include <iostream>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <iterator> // for std::back_inserter
|
2023-04-27 14:10:33 +00:00
|
|
|
#include <locale>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <memory>
|
2022-08-17 23:26:52 +00:00
|
|
|
#include <optional>
|
2021-09-25 19:37:28 +00:00
|
|
|
#include <set>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <stdexcept> // std::runtime_error
|
2021-09-25 19:37:28 +00:00
|
|
|
#include <string>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <system_error>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <type_traits>
|
2021-09-25 19:37:28 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifdef _WIN32
|
2022-02-01 01:02:59 +00:00
|
|
|
#include <windows.h> /* Sleep(), GetEnvironmentVariable() */
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <ws2tcpip.h> /* htonl(), ntohl() */
|
2022-02-01 01:02:59 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <shellapi.h> /* CommandLineToArgv() */
|
2023-07-08 15:24:03 +00:00
|
|
|
#else
|
|
|
|
#include <arpa/inet.h>
|
2022-05-16 05:06:17 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-14 17:54:32 +00:00
|
|
|
#define UTF_CPP_CPLUSPLUS 201703L
|
2021-11-30 21:13:56 +00:00
|
|
|
#include <utf8.h>
|
2022-02-14 17:54:32 +00:00
|
|
|
|
2023-07-06 03:05:03 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2022-05-16 05:06:17 +00:00
|
|
|
#include <fast_float/fast_float.h>
|
2022-12-18 19:19:07 +00:00
|
|
|
#include <wildmat.h>
|
2022-05-16 05:06:17 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/error-types.h"
|
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/mime-types.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2023-11-09 14:39:06 +00:00
|
|
|
#include "libtransmission/values.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
using namespace std::literals;
|
2023-11-10 23:12:24 +00:00
|
|
|
using namespace libtransmission::Values;
|
2021-10-20 02:30:50 +00:00
|
|
|
|
2022-11-07 17:48:26 +00:00
|
|
|
time_t libtransmission::detail::tr_time::current_time = {};
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2009-06-12 23:01:35 +00:00
|
|
|
|
2023-11-16 04:15:40 +00:00
|
|
|
namespace libtransmission::Values
|
|
|
|
{
|
|
|
|
|
|
|
|
// default values; can be overridden by client apps
|
|
|
|
Config::Units<MemoryUnits> Config::Memory{ Config::Base::Kibi, "B"sv, "KiB"sv, "MiB"sv, "GiB"sv, "TiB"sv };
|
|
|
|
Config::Units<SpeedUnits> Config::Speed{ Config::Base::Kilo, "B/s"sv, "kB/s"sv, "MB/s"sv, "GB/s"sv, "TB/s"sv };
|
|
|
|
Config::Units<StorageUnits> Config::Storage{ Config::Base::Kilo, "B"sv, "kB"sv, "MB"sv, "GB"sv, "TB"sv };
|
|
|
|
|
|
|
|
} // namespace libtransmission::Values
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2023-10-17 00:36:37 +00:00
|
|
|
std::optional<std::locale> tr_locale_set_global(char const* locale_name) noexcept
|
2023-04-27 14:10:33 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-10-17 00:36:37 +00:00
|
|
|
return tr_locale_set_global(std::locale{ locale_name });
|
|
|
|
}
|
|
|
|
catch (std::runtime_error const&)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::locale> tr_locale_set_global(std::locale const& locale) noexcept
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto old_locale = std::locale::global(locale);
|
2023-04-27 14:10:33 +00:00
|
|
|
|
2023-06-22 22:58:16 +00:00
|
|
|
std::cout.imbue(std::locale{});
|
|
|
|
std::cerr.imbue(std::locale{});
|
2023-10-17 00:36:37 +00:00
|
|
|
|
|
|
|
return old_locale;
|
2023-04-27 14:10:33 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
2023-10-17 00:36:37 +00:00
|
|
|
return {};
|
2023-04-27 14:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool tr_file_read(std::string_view filename, std::vector<char>& contents, tr_error* error)
|
2021-11-16 06:15:13 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
auto const szfilename = tr_pathbuf{ filename };
|
2021-11-16 06:15:13 +00:00
|
|
|
|
|
|
|
/* try to stat the file */
|
2023-11-04 16:39:41 +00:00
|
|
|
auto local_error = tr_error{};
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
error = &local_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const info = tr_sys_path_get_info(szfilename, 0, error);
|
|
|
|
if (*error)
|
2021-11-16 06:15:13 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-08-03 06:15:37 +00:00
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error->message()),
|
|
|
|
fmt::arg("error_code", error->code())));
|
2021-11-16 06:15:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-13 17:11:07 +00:00
|
|
|
if (!info || !info->isFile())
|
2021-11-16 06:15:13 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", filename)));
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set(TR_ERROR_EISDIR, "Not a regular file"sv);
|
2021-11-16 06:15:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the torrent file into our buffer */
|
2023-11-04 16:39:41 +00:00
|
|
|
auto const fd = tr_sys_file_open(szfilename, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, error);
|
2021-11-16 06:15:13 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-08-03 06:15:37 +00:00
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error->message()),
|
|
|
|
fmt::arg("error_code", error->code())));
|
2021-11-16 06:15:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
contents.resize(info->size);
|
2023-11-04 16:39:41 +00:00
|
|
|
if (!tr_sys_file_read(fd, std::data(contents), info->size, nullptr, error))
|
2021-11-16 06:15:13 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-08-03 06:15:37 +00:00
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error->message()),
|
|
|
|
fmt::arg("error_code", error->code())));
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2021-11-16 06:15:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2021-11-16 06:15:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool tr_file_save(std::string_view filename, std::string_view contents, tr_error* error)
|
2021-12-05 01:32:35 +00:00
|
|
|
{
|
|
|
|
// follow symlinks to find the "real" file, to make sure the temporary
|
|
|
|
// we build with tr_sys_file_open_temp() is created on the right partition
|
2022-08-05 16:36:01 +00:00
|
|
|
if (auto const realname = tr_sys_path_resolve(filename); !std::empty(realname) && realname != filename)
|
2021-12-05 01:32:35 +00:00
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
return tr_file_save(realname, contents, error);
|
2021-12-05 01:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write it to a temp file first.
|
|
|
|
// This is a safeguard against edge cases, e.g. disk full, crash while writing, etc.
|
2022-08-05 16:36:01 +00:00
|
|
|
auto tmp = tr_pathbuf{ filename, ".tmp.XXXXXX"sv };
|
2021-12-05 01:32:35 +00:00
|
|
|
auto const fd = tr_sys_file_open_temp(std::data(tmp), error);
|
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the contents. This might take >1 pass.
|
|
|
|
auto ok = bool{ true };
|
|
|
|
while (!std::empty(contents))
|
|
|
|
{
|
|
|
|
auto n_written = uint64_t{};
|
|
|
|
if (!tr_sys_file_write(fd, std::data(contents), std::size(contents), &n_written, error))
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
contents.remove_prefix(n_written);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we saved it to disk successfully, move it from '.tmp' to the correct filename
|
2022-11-29 03:03:28 +00:00
|
|
|
if (!tr_sys_file_close(fd, error) || !ok || !tr_sys_path_rename(tmp, tr_pathbuf{ filename }, error))
|
2021-12-05 01:32:35 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
tr_logAddTrace(fmt::format("Saved '{}'", filename));
|
2021-12-05 01:32:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
size_t tr_strv_to_buf(std::string_view src, char* buf, size_t buflen)
|
2021-11-05 06:29:19 +00:00
|
|
|
{
|
2022-08-17 00:28:57 +00:00
|
|
|
size_t const len = std::size(src);
|
|
|
|
|
|
|
|
if (buflen >= len)
|
|
|
|
{
|
|
|
|
auto const out = std::copy(std::begin(src), std::end(src), buf);
|
|
|
|
|
|
|
|
if (buflen > len)
|
|
|
|
{
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 16:07:22 +00:00
|
|
|
/* User-level routine. returns whether or not 'text' and 'p' matched */
|
2022-04-08 00:20:29 +00:00
|
|
|
bool tr_wildmat(std::string_view text, std::string_view pattern)
|
2021-09-25 16:07:22 +00:00
|
|
|
{
|
2022-04-08 00:20:29 +00:00
|
|
|
// TODO(ckerr): replace wildmat with base/strings/pattern.cc
|
|
|
|
// wildmat wants these to be zero-terminated.
|
2022-12-12 22:35:40 +00:00
|
|
|
return pattern == "*"sv || DoMatch(std::string{ text }.c_str(), std::string{ pattern }.c_str()) > 0;
|
2021-09-25 16:07:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
char const* tr_strerror(int errnum)
|
2008-02-19 04:16:04 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
if (char const* const ret = strerror(errnum); ret != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
return "Unknown Error";
|
2008-02-19 04:16:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-09-05 19:11:30 +00:00
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
std::string_view tr_strv_strip(std::string_view str)
|
2021-11-01 18:50:53 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto constexpr Test = [](auto ch)
|
2021-11-01 18:50:53 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
return isspace(static_cast<unsigned char>(ch));
|
2021-11-01 18:50:53 +00:00
|
|
|
};
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const it = std::find_if_not(std::begin(str), std::end(str), Test);
|
2021-11-01 18:50:53 +00:00
|
|
|
str.remove_prefix(std::distance(std::begin(str), it));
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const rit = std::find_if_not(std::rbegin(str), std::rend(str), Test);
|
2021-11-01 18:50:53 +00:00
|
|
|
str.remove_suffix(std::distance(std::rbegin(str), rit));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
uint64_t tr_time_msec()
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
return std::chrono::system_clock::now().time_since_epoch() / 1ms;
|
2007-07-12 17:51:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
double tr_getRatio(uint64_t numerator, uint64_t denominator)
|
2008-01-04 19:13:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (denominator > 0)
|
|
|
|
{
|
2023-06-22 22:58:16 +00:00
|
|
|
return numerator / static_cast<double>(denominator);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-19 16:09:38 +00:00
|
|
|
|
|
|
|
if (numerator > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-19 16:09:38 +00:00
|
|
|
return TR_RATIO_INF;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-01-04 19:13:30 +00:00
|
|
|
|
2021-10-19 16:09:38 +00:00
|
|
|
return TR_RATIO_NA;
|
2008-01-04 19:13:30 +00:00
|
|
|
}
|
2008-02-25 20:21:22 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2023-07-30 01:57:43 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__clang__))
|
2023-03-19 15:36:16 +00:00
|
|
|
|
|
|
|
std::string tr_strv_convert_utf8(std::string_view sv)
|
|
|
|
{
|
|
|
|
return tr_strv_replace_invalid(sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-01-09 04:21:31 +00:00
|
|
|
std::string tr_strv_replace_invalid(std::string_view sv, uint32_t replacement)
|
2021-11-30 21:13:56 +00:00
|
|
|
{
|
2023-03-19 15:36:16 +00:00
|
|
|
// stripping characters after first \0
|
|
|
|
if (auto first_null = sv.find('\0'); first_null != std::string::npos)
|
|
|
|
{
|
|
|
|
sv = { std::data(sv), first_null };
|
|
|
|
}
|
2022-08-17 02:47:07 +00:00
|
|
|
auto out = std::string{};
|
2023-03-19 15:36:16 +00:00
|
|
|
out.reserve(std::size(sv));
|
2023-01-09 04:21:31 +00:00
|
|
|
utf8::unchecked::replace_invalid(std::data(sv), std::data(sv) + std::size(sv), std::back_inserter(out), replacement);
|
2022-08-17 02:47:07 +00:00
|
|
|
return out;
|
2021-11-30 21:13:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 20:08:20 +00:00
|
|
|
#ifdef _WIN32
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
std::string tr_win32_native_to_utf8(std::wstring_view in)
|
2022-08-03 23:07:38 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
auto out = std::string{};
|
|
|
|
out.resize(WideCharToMultiByte(CP_UTF8, 0, std::data(in), std::size(in), nullptr, 0, nullptr, nullptr));
|
2022-08-06 19:27:37 +00:00
|
|
|
[[maybe_unused]] auto
|
|
|
|
len = WideCharToMultiByte(CP_UTF8, 0, std::data(in), std::size(in), std::data(out), std::size(out), nullptr, nullptr);
|
2022-08-05 16:36:01 +00:00
|
|
|
TR_ASSERT(len == std::size(out));
|
|
|
|
return out;
|
2022-08-03 23:07:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
std::wstring tr_win32_utf8_to_native(std::string_view in)
|
|
|
|
{
|
|
|
|
auto out = std::wstring{};
|
|
|
|
out.resize(MultiByteToWideChar(CP_UTF8, 0, std::data(in), std::size(in), nullptr, 0));
|
2022-08-06 19:27:37 +00:00
|
|
|
[[maybe_unused]] auto len = MultiByteToWideChar(CP_UTF8, 0, std::data(in), std::size(in), std::data(out), std::size(out));
|
2022-08-05 16:36:01 +00:00
|
|
|
TR_ASSERT(len == std::size(out));
|
|
|
|
return out;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
std::string tr_win32_format_message(uint32_t code)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
wchar_t* wide_text = nullptr;
|
2022-08-03 23:07:38 +00:00
|
|
|
auto const wide_size = FormatMessageW(
|
2021-08-15 09:41:48 +00:00
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
code,
|
|
|
|
0,
|
|
|
|
(LPWSTR)&wide_text,
|
|
|
|
0,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_size == 0)
|
|
|
|
{
|
2023-11-21 15:02:03 +00:00
|
|
|
return fmt::format("Unknown error ({:#08x})", code);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
auto text = std::string{};
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (wide_size != 0 && wide_text != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
text = tr_win32_native_to_utf8({ wide_text, wide_size });
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalFree(wide_text);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
// Most (all?) messages contain "\r\n" in the end, chop it
|
|
|
|
while (!std::empty(text) && isspace(text.back()))
|
2015-10-11 16:15:19 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
text.resize(text.size() - 1);
|
2015-10-11 16:15:19 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return text;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace tr_main_win32_impl
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
std::optional<std::vector<std::string>> win32MakeUtf8Argv()
|
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
auto argv = std::vector<std::string>{};
|
|
|
|
if (wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc); wargv != nullptr)
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2022-08-17 23:26:52 +00:00
|
|
|
for (int i = 0; i < argc; ++i)
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2022-08-17 23:26:52 +00:00
|
|
|
if (wargv[i] == nullptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto str = tr_win32_native_to_utf8(wargv[i]);
|
|
|
|
if (std::empty(str))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
argv.emplace_back(std::move(str));
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
LocalFree(wargv);
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
if (static_cast<int>(std::size(argv)) == argc)
|
|
|
|
{
|
|
|
|
return argv;
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
return {};
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
} // namespace tr_main_win32_impl
|
|
|
|
} // namespace
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**))
|
2015-04-21 10:07:57 +00:00
|
|
|
{
|
2022-08-17 23:26:52 +00:00
|
|
|
using namespace tr_main_win32_impl;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2022-08-17 23:26:52 +00:00
|
|
|
|
|
|
|
// build an argv from GetCommandLineW + CommandLineToArgvW
|
|
|
|
if (auto argv_strs = win32MakeUtf8Argv(); argv_strs)
|
|
|
|
{
|
|
|
|
auto argv_cstrs = std::vector<char*>{};
|
|
|
|
argv_cstrs.reserve(std::size(*argv_strs));
|
|
|
|
std::transform(
|
|
|
|
std::begin(*argv_strs),
|
|
|
|
std::end(*argv_strs),
|
|
|
|
std::back_inserter(argv_cstrs),
|
|
|
|
[](auto& str) { return std::data(str); });
|
|
|
|
argv_cstrs.push_back(nullptr); // argv is nullptr-terminated
|
|
|
|
return (*real_main)(std::size(*argv_strs), std::data(argv_cstrs));
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:30:03 +00:00
|
|
|
return (*real_main)(argc, argv);
|
2015-04-21 10:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
#endif
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2009-02-09 17:25:48 +00:00
|
|
|
|
2022-08-17 23:26:52 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
namespace tr_num_parse_range_impl
|
2022-08-17 23:26:52 +00:00
|
|
|
{
|
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
struct number_range
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int low;
|
|
|
|
int high;
|
2009-02-09 17:25:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should be a single number (ex. "6") or a range (ex. "6-9").
|
|
|
|
* Anything else is an error and will return failure.
|
|
|
|
*/
|
2022-08-17 23:26:52 +00:00
|
|
|
bool parseNumberSection(std::string_view str, number_range& range)
|
2009-02-09 17:25:48 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
auto constexpr Delimiter = "-"sv;
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const first = tr_num_parse<int>(str, &str);
|
2021-12-29 08:28:12 +00:00
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
return false;
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2021-12-29 08:28:12 +00:00
|
|
|
|
|
|
|
range.low = range.high = *first;
|
|
|
|
if (std::empty(str))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
return true;
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2021-12-29 08:28:12 +00:00
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
if (!tr_strv_starts_with(str, Delimiter))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
return false;
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
str.remove_prefix(std::size(Delimiter));
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const second = tr_num_parse<int>(str);
|
2021-12-29 08:28:12 +00:00
|
|
|
if (!second)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
range.high = *second;
|
|
|
|
return true;
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
} // namespace tr_num_parse_range_impl
|
2022-08-17 23:26:52 +00:00
|
|
|
} // namespace
|
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
/**
|
|
|
|
* Given a string like "1-4" or "1-4,6,9,14-51", this allocates and returns an
|
|
|
|
* array of setmeCount ints of all the values in the array.
|
|
|
|
* For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
|
2021-10-06 16:32:17 +00:00
|
|
|
* If a fragment of the string can't be parsed, nullptr is returned.
|
2009-02-09 17:25:48 +00:00
|
|
|
*/
|
2023-06-30 14:49:58 +00:00
|
|
|
std::vector<int> tr_num_parse_range(std::string_view str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
using namespace tr_num_parse_range_impl;
|
2022-08-17 23:26:52 +00:00
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
auto values = std::set<int>{};
|
2021-11-10 00:13:47 +00:00
|
|
|
auto token = std::string_view{};
|
|
|
|
auto range = number_range{};
|
2023-06-30 14:49:58 +00:00
|
|
|
while (tr_strv_sep(&str, &token, ',') && parseNumberSection(token, range))
|
2009-02-09 17:25:48 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
for (auto i = range.low; i <= range.high; ++i)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
values.insert(i);
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
return { std::begin(values), std::end(values) };
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
2009-03-04 16:16:02 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2009-03-04 16:16:02 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
double tr_truncd(double x, int decimal_places)
|
2009-03-04 16:16:02 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
auto buf = std::array<char, 128>{};
|
2023-06-10 22:09:18 +00:00
|
|
|
auto const [out, len] = fmt::format_to_n(std::data(buf), std::size(buf) - 1, "{:.{}f}", x, DBL_DIG);
|
2022-04-04 18:36:48 +00:00
|
|
|
*out = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-06-10 22:09:18 +00:00
|
|
|
if (auto* const pt = strchr(std::data(buf), '.'); pt != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
pt[decimal_places != 0 ? decimal_places + 1 : 0] = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
return tr_num_parse<double>(std::data(buf)).value_or(0.0);
|
2010-06-24 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_strpercent(double x)
|
2010-06-22 22:30:58 +00:00
|
|
|
{
|
2022-01-25 16:52:22 +00:00
|
|
|
if (x < 5.0)
|
|
|
|
{
|
2023-03-24 00:48:45 +00:00
|
|
|
return fmt::format("{:.2Lf}", tr_truncd(x, 2));
|
2022-01-25 16:52:22 +00:00
|
|
|
}
|
2022-04-04 18:36:48 +00:00
|
|
|
|
|
|
|
if (x < 100.0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-03-24 00:48:45 +00:00
|
|
|
return fmt::format("{:.1Lf}", tr_truncd(x, 1));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2023-03-24 00:48:45 +00:00
|
|
|
return fmt::format("{:.0Lf}", x);
|
2010-06-22 22:30:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_strratio(double ratio, char const* infinity)
|
2009-03-04 16:16:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((int)ratio == TR_RATIO_NA)
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return _("None");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-12-31 00:30:21 +00:00
|
|
|
|
|
|
|
if ((int)ratio == TR_RATIO_INF)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-12-24 19:33:33 +00:00
|
|
|
return infinity != nullptr ? infinity : "";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
return tr_strpercent(ratio);
|
2010-06-22 22:30:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool tr_file_move(std::string_view oldpath_in, std::string_view newpath_in, tr_error* error)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2022-04-08 00:20:29 +00:00
|
|
|
auto const oldpath = tr_pathbuf{ oldpath_in };
|
|
|
|
auto const newpath = tr_pathbuf{ newpath_in };
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto local_error = tr_error{};
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
error = &local_error;
|
|
|
|
}
|
|
|
|
|
2022-04-08 00:20:29 +00:00
|
|
|
// make sure the old file exists
|
2022-08-02 17:41:04 +00:00
|
|
|
auto const info = tr_sys_path_get_info(oldpath, 0, error);
|
|
|
|
if (!info)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->prefix_message("Unable to get information on old file: ");
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
2022-08-02 17:41:04 +00:00
|
|
|
if (!info->isFile())
|
2009-10-27 23:00:34 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set(TR_ERROR_EINVAL, "Old path does not point to a file."sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2009-10-27 23:00:34 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
// ensure the target directory exists
|
2022-05-22 01:17:00 +00:00
|
|
|
auto newdir = tr_pathbuf{ newpath.sv() };
|
|
|
|
newdir.popdir();
|
|
|
|
if (!tr_sys_dir_create(newdir, TR_SYS_DIR_CREATE_PARENTS, 0777, error))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->prefix_message("Unable to create directory for new file: ");
|
2022-04-08 00:20:29 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* they might be on the same filesystem... */
|
2022-03-27 17:37:29 +00:00
|
|
|
if (tr_sys_path_rename(oldpath, newpath))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
/* Otherwise, copy the file. */
|
|
|
|
if (!tr_sys_path_copy(oldpath, newpath, error))
|
2014-12-10 18:37:58 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->prefix_message("Unable to copy: ");
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2014-12-10 18:37:58 +00:00
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (auto log_error = tr_error{}; !tr_sys_path_remove(oldpath, &log_error))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 22:42:51 +00:00
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't remove '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", oldpath),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", log_error.message()),
|
|
|
|
fmt::arg("error_code", log_error.code())));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-12-10 18:37:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
2010-02-23 06:06:45 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-02-23 06:06:45 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
uint64_t tr_htonll(uint64_t hostlonglong)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2011-03-13 20:23:23 +00:00
|
|
|
#ifdef HAVE_HTONLL
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
return htonll(hostlonglong);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
/* fallback code by bdonlan at https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */
|
2017-04-19 12:04:45 +00:00
|
|
|
union
|
|
|
|
{
|
2022-08-26 18:35:28 +00:00
|
|
|
std::array<uint32_t, 2> lx;
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t llx;
|
2022-09-23 05:51:15 +00:00
|
|
|
} u = {};
|
2023-01-28 23:58:20 +00:00
|
|
|
u.lx[0] = htonl(hostlonglong >> 32);
|
|
|
|
u.lx[1] = htonl(hostlonglong & 0xFFFFFFFFULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
return u.llx;
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
uint64_t tr_ntohll(uint64_t netlonglong)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2011-03-13 20:23:23 +00:00
|
|
|
#ifdef HAVE_NTOHLL
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
return ntohll(netlonglong);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
/* fallback code by bdonlan at https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */
|
2017-04-19 12:04:45 +00:00
|
|
|
union
|
|
|
|
{
|
2022-08-26 18:35:28 +00:00
|
|
|
std::array<uint32_t, 2> lx;
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t llx;
|
2022-09-23 05:51:15 +00:00
|
|
|
} u = {};
|
2023-01-28 23:58:20 +00:00
|
|
|
u.llx = netlonglong;
|
2017-04-19 12:04:45 +00:00
|
|
|
return ((uint64_t)ntohl(u.lx[0]) << 32) | (uint64_t)ntohl(u.lx[1]);
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- ENVIRONMENT
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_env_key_exists(char const* key)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(key != nullptr);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-09-15 00:18:09 +00:00
|
|
|
return GetEnvironmentVariableA(key, nullptr, 0) != 0;
|
2014-09-21 18:05:14 +00:00
|
|
|
#else
|
2021-09-15 00:18:09 +00:00
|
|
|
return getenv(key) != nullptr;
|
2014-09-21 18:05:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-27 04:26:37 +00:00
|
|
|
std::string tr_env_get_string(std::string_view key, std::string_view default_value)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const wide_key = tr_win32_utf8_to_native(key); !std::empty(wide_key))
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const size = GetEnvironmentVariableW(wide_key.c_str(), nullptr, 0); size != 0)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
auto wide_val = std::wstring{};
|
2022-07-27 04:26:37 +00:00
|
|
|
wide_val.resize(size);
|
2022-08-03 23:07:38 +00:00
|
|
|
if (GetEnvironmentVariableW(wide_key.c_str(), std::data(wide_val), std::size(wide_val)) == std::size(wide_val) - 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
TR_ASSERT(wide_val.back() == L'\0');
|
|
|
|
wide_val.resize(std::size(wide_val) - 1);
|
|
|
|
return tr_win32_native_to_utf8(wide_val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-07-27 04:26:37 +00:00
|
|
|
auto const szkey = tr_strbuf<char, 256>{ key };
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2022-07-27 04:26:37 +00:00
|
|
|
if (auto const* const value = getenv(szkey); value != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-27 04:26:37 +00:00
|
|
|
return value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
#endif
|
2022-07-27 04:26:37 +00:00
|
|
|
|
|
|
|
return std::string{ default_value };
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
2015-10-11 16:35:51 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2015-10-11 16:35:51 +00:00
|
|
|
|
2023-07-06 03:05:03 +00:00
|
|
|
tr_net_init_mgr::tr_net_init_mgr()
|
2015-10-11 16:35:51 +00:00
|
|
|
{
|
2023-07-06 03:05:03 +00:00
|
|
|
// try to init curl with default settings (currently ssl support + win32 sockets)
|
|
|
|
// but if that fails, we need to init win32 sockets as a bare minimum
|
|
|
|
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
|
2015-10-11 16:35:51 +00:00
|
|
|
{
|
2023-07-06 03:05:03 +00:00
|
|
|
curl_global_init(CURL_GLOBAL_WIN32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_net_init_mgr::~tr_net_init_mgr()
|
|
|
|
{
|
|
|
|
curl_global_cleanup();
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-07-06 03:05:03 +00:00
|
|
|
std::unique_ptr<tr_net_init_mgr> tr_net_init_mgr::create()
|
|
|
|
{
|
|
|
|
if (!initialised)
|
|
|
|
{
|
|
|
|
initialised = true;
|
|
|
|
return std::unique_ptr<tr_net_init_mgr>{ new tr_net_init_mgr };
|
2015-10-11 16:35:51 +00:00
|
|
|
}
|
2023-07-06 03:05:03 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_net_init_mgr::initialised = false;
|
|
|
|
|
|
|
|
std::unique_ptr<tr_net_init_mgr> tr_lib_init()
|
|
|
|
{
|
|
|
|
return tr_net_init_mgr::create();
|
2015-10-11 16:35:51 +00:00
|
|
|
}
|
2020-10-13 15:33:56 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- mime-type
|
2020-10-13 15:33:56 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
std::string_view tr_get_mime_type_for_filename(std::string_view filename)
|
2020-10-13 15:33:56 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto constexpr Compare = [](mime_type_suffix const& entry, auto const& suffix)
|
2020-10-13 15:33:56 +00:00
|
|
|
{
|
2021-10-01 22:55:19 +00:00
|
|
|
return entry.suffix < suffix;
|
|
|
|
};
|
2020-10-13 15:33:56 +00:00
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
if (auto const pos = filename.rfind('.'); pos != std::string_view::npos)
|
2021-10-01 22:55:19 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
auto const suffix_lc = tr_strlower(filename.substr(pos + 1));
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const it = std::lower_bound(std::begin(mime_type_suffixes), std::end(mime_type_suffixes), suffix_lc, Compare);
|
2021-10-01 22:55:19 +00:00
|
|
|
if (it != std::end(mime_type_suffixes) && suffix_lc == it->suffix)
|
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
return it->mime_type;
|
2020-10-25 21:13:48 +00:00
|
|
|
}
|
2020-10-13 15:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
|
|
|
// application/octet-stream is the default value.
|
|
|
|
// An unknown file type should use this type.
|
|
|
|
auto constexpr Fallback = "application/octet-stream"sv;
|
|
|
|
return Fallback;
|
2020-10-13 15:33:56 +00:00
|
|
|
}
|
2022-05-16 05:06:17 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- parseNum()
|
2022-05-16 05:06:17 +00:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && !__has_include(<charconv>)
|
|
|
|
|
|
|
|
#include <iomanip> // std::setbase
|
|
|
|
#include <sstream>
|
|
|
|
|
2023-11-21 15:02:03 +00:00
|
|
|
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
2023-06-30 14:49:58 +00:00
|
|
|
[[nodiscard]] std::optional<T> tr_num_parse(std::string_view str, std::string_view* remainder, int base)
|
2022-05-16 05:06:17 +00:00
|
|
|
{
|
|
|
|
auto val = T{};
|
2022-09-02 06:04:09 +00:00
|
|
|
auto const tmpstr = std::string(std::data(str), std::min(std::size(str), size_t{ 64 }));
|
|
|
|
auto sstream = std::stringstream{ tmpstr };
|
2022-05-16 05:06:17 +00:00
|
|
|
auto const oldpos = sstream.tellg();
|
|
|
|
/* The base parameter only works for bases 8, 10 and 16.
|
|
|
|
All other bases will be converted to 0 which activates the
|
|
|
|
prefix based parsing and therefore decimal in our usual cases.
|
|
|
|
This differs from the from_chars solution below. */
|
|
|
|
sstream >> std::setbase(base) >> val;
|
|
|
|
auto const newpos = sstream.tellg();
|
|
|
|
if ((newpos == oldpos) || (sstream.fail() && !sstream.eof()))
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-09-02 06:04:09 +00:00
|
|
|
if (remainder != nullptr)
|
|
|
|
{
|
|
|
|
*remainder = str;
|
|
|
|
remainder->remove_prefix(sstream.eof() ? std::size(str) : newpos - oldpos);
|
|
|
|
}
|
2022-05-16 05:06:17 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // #if defined(__GNUC__) && !__has_include(<charconv>)
|
|
|
|
|
|
|
|
#include <charconv> // std::from_chars()
|
|
|
|
|
2023-11-21 15:02:03 +00:00
|
|
|
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool>>
|
2023-06-30 14:49:58 +00:00
|
|
|
[[nodiscard]] std::optional<T> tr_num_parse(std::string_view str, std::string_view* remainder, int base)
|
2022-05-16 05:06:17 +00:00
|
|
|
{
|
|
|
|
auto val = T{};
|
2022-09-02 06:04:09 +00:00
|
|
|
auto const* const begin_ch = std::data(str);
|
|
|
|
auto const* const end_ch = begin_ch + std::size(str);
|
2022-05-16 05:06:17 +00:00
|
|
|
/* The base parameter works for any base from 2 to 36 (inclusive).
|
|
|
|
This is different from the behaviour of the stringstream
|
|
|
|
based solution above. */
|
|
|
|
auto const result = std::from_chars(begin_ch, end_ch, val, base);
|
|
|
|
if (result.ec != std::errc{})
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-09-02 06:04:09 +00:00
|
|
|
if (remainder != nullptr)
|
|
|
|
{
|
|
|
|
*remainder = str;
|
|
|
|
remainder->remove_prefix(result.ptr - std::data(str));
|
|
|
|
}
|
2022-05-16 05:06:17 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #if defined(__GNUC__) && !__has_include(<charconv>)
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
template std::optional<long long> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<long> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<int> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<char> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
2022-07-04 04:28:41 +00:00
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
template std::optional<unsigned long long> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<unsigned long> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<unsigned int> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<unsigned short> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
|
|
|
template std::optional<unsigned char> tr_num_parse(std::string_view str, std::string_view* remainder, int base);
|
2022-05-16 05:06:17 +00:00
|
|
|
|
2023-11-21 15:02:03 +00:00
|
|
|
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool>>
|
2023-06-30 14:49:58 +00:00
|
|
|
[[nodiscard]] std::optional<T> tr_num_parse(std::string_view str, std::string_view* remainder)
|
2022-05-16 05:06:17 +00:00
|
|
|
{
|
2022-09-02 06:04:09 +00:00
|
|
|
auto const* const begin_ch = std::data(str);
|
|
|
|
auto const* const end_ch = begin_ch + std::size(str);
|
2022-05-16 05:06:17 +00:00
|
|
|
auto val = T{};
|
|
|
|
auto const result = fast_float::from_chars(begin_ch, end_ch, val);
|
|
|
|
if (result.ec != std::errc{})
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-09-02 06:04:09 +00:00
|
|
|
if (remainder != nullptr)
|
|
|
|
{
|
|
|
|
*remainder = str;
|
|
|
|
remainder->remove_prefix(result.ptr - std::data(str));
|
|
|
|
}
|
2022-05-16 05:06:17 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
template std::optional<double> tr_num_parse(std::string_view sv, std::string_view* remainder);
|