2023-02-11 20:49:42 +00:00
|
|
|
|
// This file Copyright © 2009-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +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
|
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
|
#pragma once
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2021-11-14 05:23:19 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cctype>
|
2023-01-12 18:03:14 +00:00
|
|
|
|
#include <chrono>
|
2022-04-08 01:50:26 +00:00
|
|
|
|
#include <cstdint> // uint8_t, uint32_t, uint64_t
|
|
|
|
|
#include <cstddef> // size_t
|
|
|
|
|
#include <ctime> // time_t
|
2021-10-31 18:48:32 +00:00
|
|
|
|
#include <optional>
|
2021-10-31 16:38:10 +00:00
|
|
|
|
#include <string>
|
2021-10-01 22:55:19 +00:00
|
|
|
|
#include <string_view>
|
2023-01-12 18:03:14 +00:00
|
|
|
|
#include <thread>
|
2021-10-31 16:38:10 +00:00
|
|
|
|
#include <type_traits>
|
2021-09-25 19:37:28 +00:00
|
|
|
|
#include <vector>
|
2007-07-14 05:28:35 +00:00
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
|
#include "platform-quota.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
#include "tr-macros.h"
|
|
|
|
|
|
2015-04-11 14:54:01 +00:00
|
|
|
|
struct tr_error;
|
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* @addtogroup utils Utilities
|
2009-08-10 20:04:08 +00:00
|
|
|
|
* @{
|
2009-05-29 19:17:12 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#if !defined(_)
|
2022-03-14 04:43:35 +00:00
|
|
|
|
#if defined(HAVE_GETTEXT) && !defined(__APPLE__)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#include <libintl.h>
|
|
|
|
|
#define _(a) gettext(a)
|
|
|
|
|
#else
|
|
|
|
|
#define _(a) (a)
|
|
|
|
|
#endif
|
2008-03-05 01:05:42 +00:00
|
|
|
|
#endif
|
2008-03-04 02:02:25 +00:00
|
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
|
#if defined(HAVE_NGETTEXT)
|
|
|
|
|
#define tr_ngettext ngettext
|
|
|
|
|
#else
|
|
|
|
|
#define tr_ngettext(singular, plural, count) ((count) == 1 ? (singular) : (plural))
|
2022-03-14 04:43:35 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
|
/* #define DISABLE_GETTEXT */
|
2010-06-21 16:44:35 +00:00
|
|
|
|
#ifndef DISABLE_GETTEXT
|
2023-01-01 22:24:12 +00:00
|
|
|
|
#if defined(_WIN32)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#define DISABLE_GETTEXT
|
|
|
|
|
#endif
|
2008-10-22 17:16:12 +00:00
|
|
|
|
#endif
|
2008-08-21 21:04:57 +00:00
|
|
|
|
#ifdef DISABLE_GETTEXT
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#undef _
|
2022-12-26 21:13:21 +00:00
|
|
|
|
#undef tr_ngettext
|
2022-04-07 13:23:04 +00:00
|
|
|
|
#define _(a) (a)
|
2022-12-26 21:13:21 +00:00
|
|
|
|
#define tr_ngettext(singular, plural, count) ((count) == 1 ? (singular) : (plural))
|
2008-08-21 21:04:57 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2008-10-13 22:45:05 +00:00
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] std::string_view tr_get_mime_type_for_filename(std::string_view filename);
|
2020-10-13 15:33:56 +00:00
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
|
/**
|
2023-01-23 16:26:11 +00:00
|
|
|
|
* @brief Rich Salz's classic implementation of shell-style pattern matching for `?`, `\`, `[]`, and `*` characters.
|
2022-04-21 14:28:38 +00:00
|
|
|
|
* @return 1 if the pattern matches, 0 if it doesn't, or -1 if an error occurred
|
2010-01-01 22:13:27 +00:00
|
|
|
|
*/
|
2022-04-08 00:20:29 +00:00
|
|
|
|
[[nodiscard]] bool tr_wildmat(std::string_view text, std::string_view pattern);
|
2008-10-13 22:26:02 +00:00
|
|
|
|
|
2022-03-26 00:31:27 +00:00
|
|
|
|
bool tr_loadFile(std::string_view filename, std::vector<char>& contents, tr_error** error = nullptr);
|
2021-11-16 06:15:13 +00:00
|
|
|
|
|
2022-03-26 00:31:27 +00:00
|
|
|
|
bool tr_saveFile(std::string_view filename, std::string_view contents, tr_error** error = nullptr);
|
|
|
|
|
|
|
|
|
|
template<typename ContiguousRange>
|
|
|
|
|
constexpr auto tr_saveFile(std::string_view filename, ContiguousRange const& x, tr_error** error = nullptr)
|
|
|
|
|
{
|
|
|
|
|
return tr_saveFile(filename, std::string_view{ std::data(x), std::size(x) }, error);
|
|
|
|
|
}
|
2021-12-05 01:32:35 +00:00
|
|
|
|
|
2013-02-09 04:05:03 +00:00
|
|
|
|
/**
|
2021-10-14 17:07:16 +00:00
|
|
|
|
* @brief Get disk capacity and free disk space (in bytes) for the specified folder.
|
|
|
|
|
* @return struct with free and total as zero or positive integer on success, -1 in case of error.
|
2013-02-09 04:05:03 +00:00
|
|
|
|
*/
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] tr_disk_space tr_dirSpace(std::string_view directory);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
|
/** @brief return the current date in milliseconds */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] uint64_t tr_time_msec();
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2023-01-12 18:03:14 +00:00
|
|
|
|
/** @brief sleep for the specified duration */
|
|
|
|
|
template<class rep, class period>
|
|
|
|
|
void tr_wait(std::chrono::duration<rep, period> const& delay)
|
|
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(delay);
|
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2022-05-16 05:06:17 +00:00
|
|
|
|
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
2022-09-02 06:04:09 +00:00
|
|
|
|
[[nodiscard]] std::optional<T> tr_parseNum(std::string_view str, std::string_view* setme_remainder = nullptr, int base = 10);
|
2021-12-29 08:28:12 +00:00
|
|
|
|
|
2022-05-16 05:06:17 +00:00
|
|
|
|
template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
2022-09-02 06:04:09 +00:00
|
|
|
|
[[nodiscard]] std::optional<T> tr_parseNum(std::string_view str, std::string_view* setme_remainder = nullptr);
|
2021-12-29 08:28:12 +00:00
|
|
|
|
|
2014-11-30 20:08:20 +00:00
|
|
|
|
#ifdef _WIN32
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] std::string tr_win32_format_message(uint32_t code);
|
|
|
|
|
[[nodiscard]] std::string tr_win32_native_to_utf8(std::wstring_view);
|
|
|
|
|
[[nodiscard]] std::wstring tr_win32_utf8_to_native(std::string_view);
|
2022-08-03 23:07:38 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
#define tr_main(...) \
|
2017-04-19 12:04:45 +00:00
|
|
|
|
main_impl(__VA_ARGS__); \
|
|
|
|
|
int main(int argc, char* argv[]) \
|
|
|
|
|
{ \
|
2017-04-30 16:30:03 +00:00
|
|
|
|
return tr_main_win32(argc, argv, &main_impl); \
|
2017-04-19 12:04:45 +00:00
|
|
|
|
} \
|
|
|
|
|
int main_impl(__VA_ARGS__)
|
2015-04-21 10:07:57 +00:00
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#define tr_main main
|
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
|
#endif
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_str_is_empty(char const* value)
|
2019-07-13 08:52:44 +00:00
|
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
|
return value == nullptr || *value == '\0';
|
2019-07-13 08:52:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 16:26:11 +00:00
|
|
|
|
/** @brief Portability wrapper for `strlcpy()` that uses the system implementation if available */
|
2020-08-11 18:11:55 +00:00
|
|
|
|
size_t tr_strlcpy(void* dst, void const* src, size_t siz);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
2023-01-23 16:26:11 +00:00
|
|
|
|
/** @brief Convenience wrapper around `strerorr()` guaranteed to not return nullptr
|
2016-03-29 03:04:54 +00:00
|
|
|
|
@param errnum the error number to describe */
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] char const* tr_strerror(int errnum);
|
2008-02-19 04:16:04 +00:00
|
|
|
|
|
2021-11-14 05:23:19 +00:00
|
|
|
|
template<typename T>
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] std::string tr_strlower(T in)
|
2021-11-14 05:23:19 +00:00
|
|
|
|
{
|
|
|
|
|
auto out = std::string{ in };
|
|
|
|
|
std::for_each(std::begin(out), std::end(out), [](char& ch) { ch = std::tolower(ch); });
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 23:41:05 +00:00
|
|
|
|
template<typename T>
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] std::string tr_strupper(T in)
|
2022-01-08 23:41:05 +00:00
|
|
|
|
{
|
|
|
|
|
auto out = std::string{ in };
|
|
|
|
|
std::for_each(std::begin(out), std::end(out), [](char& ch) { ch = std::toupper(ch); });
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// --- std::string_view utils
|
2021-11-10 00:13:47 +00:00
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-15 23:39:04 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_strvContains(std::string_view sv, T key) noexcept // c++23
|
2021-11-10 00:13:47 +00:00
|
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
|
return sv.find(key) != std::string_view::npos;
|
2021-11-10 00:13:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_strvStartsWith(std::string_view sv, char key) // c++20
|
2021-11-10 02:42:18 +00:00
|
|
|
|
{
|
|
|
|
|
return !std::empty(sv) && sv.front() == key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_strvStartsWith(std::string_view sv, std::string_view key) // c++20
|
2021-11-10 00:13:47 +00:00
|
|
|
|
{
|
|
|
|
|
return std::size(key) <= std::size(sv) && sv.substr(0, std::size(key)) == key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_strvStartsWith(std::wstring_view sv, std::wstring_view key) // c++20
|
|
|
|
|
{
|
|
|
|
|
return std::size(key) <= std::size(sv) && sv.substr(0, std::size(key)) == key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_strvEndsWith(std::string_view sv, std::string_view key) // c++20
|
2021-11-10 00:13:47 +00:00
|
|
|
|
{
|
|
|
|
|
return std::size(key) <= std::size(sv) && sv.substr(std::size(sv) - std::size(key)) == key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] constexpr bool tr_strvEndsWith(std::string_view sv, char key) // c++20
|
2021-11-10 02:42:18 +00:00
|
|
|
|
{
|
|
|
|
|
return !std::empty(sv) && sv.back() == key;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
|
constexpr std::string_view tr_strvSep(std::string_view* sv, char delim)
|
|
|
|
|
{
|
|
|
|
|
auto pos = sv->find(delim);
|
|
|
|
|
auto const ret = sv->substr(0, pos);
|
2022-02-18 17:52:01 +00:00
|
|
|
|
sv->remove_prefix(pos != std::string_view::npos ? pos + 1 : std::size(*sv));
|
2021-11-10 00:13:47 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr bool tr_strvSep(std::string_view* sv, std::string_view* token, char delim)
|
|
|
|
|
{
|
2022-02-18 17:52:01 +00:00
|
|
|
|
if (std::empty(*sv))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*token = tr_strvSep(sv, delim);
|
|
|
|
|
return true;
|
2021-11-10 00:13:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
|
[[nodiscard]] std::string_view tr_strvStrip(std::string_view str);
|
2021-11-10 00:13:47 +00:00
|
|
|
|
|
2023-03-19 15:36:16 +00:00
|
|
|
|
[[nodiscard]] std::string tr_strv_convert_utf8(std::string_view sv);
|
|
|
|
|
|
2023-01-09 16:52:19 +00:00
|
|
|
|
[[nodiscard]] std::string tr_strv_replace_invalid(std::string_view sv, uint32_t replacement = 0xFFFD /*<2A>*/);
|
2022-08-17 02:47:07 +00:00
|
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief copies `src` into `buf`.
|
|
|
|
|
*
|
|
|
|
|
* - Always returns std::size(src).
|
|
|
|
|
* - `src` will be copied into `buf` iff `buflen >= std::size(src)`
|
|
|
|
|
* - `buf` will also be zero terminated iff `buflen >= std::size(src) + 1`.
|
|
|
|
|
*/
|
|
|
|
|
size_t tr_strvToBuf(std::string_view src, char* buf, size_t buflen);
|
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
2023-01-23 16:26:11 +00:00
|
|
|
|
/** @brief return `TR_RATIO_NA`, `TR_RATIO_INF`, or a number in [0..1]
|
|
|
|
|
@return `TR_RATIO_NA`, `TR_RATIO_INF`, or a number in [0..1] */
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] double tr_getRatio(uint64_t numerator, uint64_t denominator);
|
2008-12-29 21:17:48 +00:00
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief Given a string like "1-4" or "1-4,6,9,14-51", this returns a
|
|
|
|
|
* newly-allocated array of all the integers in the set.
|
2022-08-17 00:28:57 +00:00
|
|
|
|
* @return a vector of integers, which is empty if the string can't be parsed.
|
2010-01-01 22:13:27 +00:00
|
|
|
|
*
|
|
|
|
|
* For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
|
|
|
|
|
*/
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] std::vector<int> tr_parseNumberRange(std::string_view str);
|
2008-01-04 19:13:30 +00:00
|
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief truncate a double value at a given number of decimal places.
|
|
|
|
|
*
|
2023-01-23 16:26:11 +00:00
|
|
|
|
* this can be used to prevent a `printf()` call from rounding up:
|
|
|
|
|
* call with the `decimal_places` argument equal to the number of
|
|
|
|
|
* decimal places in the `printf()`'s precision:
|
2010-01-06 00:18:33 +00:00
|
|
|
|
*
|
2017-04-21 07:40:57 +00:00
|
|
|
|
* - printf("%.2f%%", 99.999) ==> "100.00%"
|
2010-01-06 00:18:33 +00:00
|
|
|
|
*
|
2017-04-21 07:40:57 +00:00
|
|
|
|
* - printf("%.2f%%", tr_truncd(99.999, 2)) ==> "99.99%"
|
2010-01-06 00:18:33 +00:00
|
|
|
|
* ^ ^
|
|
|
|
|
* | These should match |
|
|
|
|
|
* +------------------------+
|
|
|
|
|
*/
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] double tr_truncd(double x, int decimal_places);
|
2009-07-14 20:09:46 +00:00
|
|
|
|
|
2010-06-22 22:30:58 +00:00
|
|
|
|
/* return a percent formatted string of either x.xx, xx.x or xxx */
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] std::string tr_strpercent(double x);
|
2010-06-22 22:30:58 +00:00
|
|
|
|
|
2009-03-04 16:16:02 +00:00
|
|
|
|
/**
|
2016-03-29 03:04:54 +00:00
|
|
|
|
* @param ratio the ratio to convert to a string
|
2022-04-21 14:28:38 +00:00
|
|
|
|
* @param infinity the string representation of "infinity"
|
2009-03-04 16:16:02 +00:00
|
|
|
|
*/
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] std::string tr_strratio(double ratio, char const* infinity);
|
2009-03-04 16:16:02 +00:00
|
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief move a file
|
2014-12-10 18:37:58 +00:00
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
2010-01-06 00:18:33 +00:00
|
|
|
|
*/
|
2022-04-08 00:20:29 +00:00
|
|
|
|
bool tr_moveFile(std::string_view oldpath, std::string_view newpath, struct tr_error** error = nullptr);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2009-11-26 18:47:08 +00:00
|
|
|
|
|
2022-11-07 17:48:26 +00:00
|
|
|
|
namespace libtransmission::detail::tr_time
|
|
|
|
|
{
|
|
|
|
|
extern time_t current_time;
|
|
|
|
|
}
|
2009-11-26 18:47:08 +00:00
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
|
/**
|
2021-10-06 16:32:17 +00:00
|
|
|
|
* @brief very inexpensive form of time(nullptr)
|
2010-01-01 22:13:27 +00:00
|
|
|
|
* @return the current epoch time in seconds
|
|
|
|
|
*
|
|
|
|
|
* This function returns a second counter that is updated once per second.
|
|
|
|
|
* If something blocks the libtransmission thread for more than a second,
|
|
|
|
|
* that counter may be thrown off, so this function is not guaranteed
|
2010-12-27 19:18:17 +00:00
|
|
|
|
* to always be accurate. However, it is *much* faster when 100% accuracy
|
2010-01-01 22:13:27 +00:00
|
|
|
|
* isn't needed
|
|
|
|
|
*/
|
2022-04-05 03:51:56 +00:00
|
|
|
|
[[nodiscard]] static inline time_t tr_time() noexcept
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-11-07 17:48:26 +00:00
|
|
|
|
return libtransmission::detail::tr_time::current_time;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
2009-11-26 18:47:08 +00:00
|
|
|
|
|
2023-01-23 16:26:11 +00:00
|
|
|
|
/** @brief Private libtransmission function to update `tr_time()`'s counter */
|
2022-04-05 03:51:56 +00:00
|
|
|
|
constexpr void tr_timeUpdate(time_t now) noexcept
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-11-07 17:48:26 +00:00
|
|
|
|
libtransmission::detail::tr_time::current_time = now;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
2009-11-26 18:47:08 +00:00
|
|
|
|
|
2023-01-23 16:26:11 +00:00
|
|
|
|
/** @brief Portability wrapper for `htonll()` that uses the system implementation if available */
|
2023-01-28 23:58:20 +00:00
|
|
|
|
[[nodiscard]] uint64_t tr_htonll(uint64_t hostlonglong);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
|
2023-01-23 16:26:11 +00:00
|
|
|
|
/** @brief Portability wrapper for `ntohll()` that uses the system implementation if available */
|
2023-01-28 23:58:20 +00:00
|
|
|
|
[[nodiscard]] uint64_t tr_ntohll(uint64_t netlonglong);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2009-11-26 18:47:08 +00:00
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
|
/* example: tr_formatter_size_init(1024, _("KiB"), _("MiB"), _("GiB"), _("TiB")); */
|
2010-06-22 04:34:16 +00:00
|
|
|
|
|
2021-10-26 21:06:47 +00:00
|
|
|
|
void tr_formatter_size_init(uint64_t kilo, char const* kb, char const* mb, char const* gb, char const* tb);
|
2010-06-22 04:34:16 +00:00
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
|
void tr_formatter_speed_init(size_t kilo, char const* kb, char const* mb, char const* gb, char const* tb);
|
2010-06-22 04:34:16 +00:00
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
|
void tr_formatter_mem_init(size_t kilo, char const* kb, char const* mb, char const* gb, char const* tb);
|
2010-06-22 04:34:16 +00:00
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
|
extern size_t tr_speed_K;
|
|
|
|
|
extern size_t tr_mem_K;
|
2021-10-26 21:06:47 +00:00
|
|
|
|
extern uint64_t tr_size_K; /* unused? */
|
2010-07-09 20:53:38 +00:00
|
|
|
|
|
|
|
|
|
/* format a speed from KBps into a user-readable string. */
|
2022-09-07 16:04:28 +00:00
|
|
|
|
[[nodiscard]] std::string tr_formatter_speed_KBps(double kilo_per_second);
|
2010-07-09 20:53:38 +00:00
|
|
|
|
|
|
|
|
|
/* format a memory size from bytes into a user-readable string. */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] std::string tr_formatter_mem_B(size_t bytes);
|
2010-07-09 20:53:38 +00:00
|
|
|
|
|
|
|
|
|
/* format a memory size from MB into a user-readable string. */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] static inline std::string tr_formatter_mem_MB(double MBps)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
|
return tr_formatter_mem_B((size_t)(MBps * tr_mem_K * tr_mem_K));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
2010-07-09 20:53:38 +00:00
|
|
|
|
/* format a file size from bytes into a user-readable string. */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] std::string tr_formatter_size_B(uint64_t bytes);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
void tr_formatter_get_units(void* dict);
|
2010-06-22 13:34:46 +00:00
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
|
[[nodiscard]] static inline size_t tr_toSpeedBytes(size_t KBps)
|
2021-12-15 21:25:42 +00:00
|
|
|
|
{
|
|
|
|
|
return KBps * tr_speed_K;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
|
[[nodiscard]] static inline auto tr_toSpeedKBps(size_t Bps)
|
2021-12-15 21:25:42 +00:00
|
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
|
return Bps / double(tr_speed_K);
|
2021-12-15 21:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
|
[[nodiscard]] static inline auto tr_toMemBytes(size_t MB)
|
2021-12-15 21:25:42 +00:00
|
|
|
|
{
|
2022-10-25 16:14:42 +00:00
|
|
|
|
return uint64_t(tr_mem_K) * tr_mem_K * MB;
|
2021-12-15 21:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] static inline auto tr_toMemMB(uint64_t B)
|
2021-12-15 21:25:42 +00:00
|
|
|
|
{
|
2022-10-25 16:14:42 +00:00
|
|
|
|
return size_t(B / (tr_mem_K * tr_mem_K));
|
2021-12-15 21:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2010-06-22 04:34:16 +00:00
|
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
|
/** @brief Check if environment variable exists. */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] bool tr_env_key_exists(char const* key);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
|
|
/** @brief Get environment variable value as int. */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] int tr_env_get_int(char const* key, int default_value);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
2022-07-27 04:26:37 +00:00
|
|
|
|
/** @brief Get environment variable value as string. */
|
2022-08-19 02:31:53 +00:00
|
|
|
|
[[nodiscard]] std::string tr_env_get_string(std::string_view key, std::string_view default_value = {});
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// ---
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
2022-02-18 17:52:01 +00:00
|
|
|
|
void tr_net_init();
|