2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <ctime>
|
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>
|
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"
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2015-04-11 14:54:01 +00:00
|
|
|
struct evbuffer;
|
|
|
|
struct event;
|
|
|
|
struct timeval;
|
|
|
|
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-20 16:02:19 +00:00
|
|
|
char const* tr_strip_positional_args(char const* fmt);
|
2010-06-25 01:13:35 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if !defined(_)
|
|
|
|
#if defined(HAVE_LIBINTL_H) && !defined(__APPLE__)
|
|
|
|
#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
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
/* #define DISABLE_GETTEXT */
|
2010-06-21 16:44:35 +00:00
|
|
|
#ifndef DISABLE_GETTEXT
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(_WIN32) || defined(TR_LIGHTWEIGHT)
|
|
|
|
#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 _
|
|
|
|
#define _(a) tr_strip_positional_args(a)
|
2008-08-21 21:04:57 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-29 18:11:56 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
2008-10-13 22:45:05 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
#define TR_N_ELEMENTS(ary) (sizeof(ary) / sizeof(*(ary)))
|
|
|
|
|
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
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Rich Salz's classic implementation of shell-style pattern matching for ?, \, [], and * characters.
|
|
|
|
* @return 1 if the pattern matches, 0 if it doesn't, or -1 if an error occured
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_wildmat(char const* text, char const* pattern) TR_GNUC_NONNULL(1, 2);
|
2008-10-13 22:26:02 +00:00
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/**
|
2009-05-29 19:17:12 +00:00
|
|
|
* @brief Loads a file and returns its contents.
|
2008-10-03 04:49:06 +00:00
|
|
|
* On failure, NULL is returned and errno is set.
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t* tr_loadFile(char const* filename, size_t* size, struct tr_error** error) TR_GNUC_MALLOC TR_GNUC_NONNULL(1);
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
bool tr_loadFile(std::vector<char>& setme, std::string const& filename, tr_error** error = nullptr);
|
2021-11-16 06:15:13 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
bool tr_saveFile(std::string const& filename, std::string_view contents, tr_error** error = nullptr);
|
2021-12-05 01:32:35 +00:00
|
|
|
|
2021-10-31 16:38:10 +00:00
|
|
|
template<typename... T, typename std::enable_if_t<(std::is_convertible_v<T, std::string_view> && ...), bool> = true>
|
|
|
|
std::string& tr_buildBuf(std::string& setme, T... args)
|
|
|
|
{
|
|
|
|
setme.clear();
|
2022-02-01 17:30:51 +00:00
|
|
|
auto const n = (std::size(std::string_view{ args }) + ...);
|
|
|
|
if (setme.capacity() < n)
|
2021-10-31 16:38:10 +00:00
|
|
|
{
|
|
|
|
setme.reserve(n);
|
|
|
|
}
|
|
|
|
((setme += args), ...);
|
|
|
|
return setme;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2021-11-13 18:09:14 +00:00
|
|
|
tr_disk_space tr_dirSpace(std::string_view path);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Convenience wrapper around timer_add() to have a timer wake up in a number of seconds and microseconds
|
2016-03-29 03:04:54 +00:00
|
|
|
* @param timer the timer to set
|
|
|
|
* @param seconds seconds to wait
|
|
|
|
* @param microseconds microseconds to wait
|
2010-01-01 22:13:27 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_timerAdd(struct event* timer, int seconds, int microseconds) TR_GNUC_NONNULL(1);
|
2009-06-15 03:24:40 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Convenience wrapper around timer_add() to have a timer wake up in a number of milliseconds
|
2016-03-29 03:04:54 +00:00
|
|
|
* @param timer the timer to set
|
|
|
|
* @param milliseconds milliseconds to wait
|
2010-01-01 22:13:27 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_timerAddMsec(struct event* timer, int milliseconds) TR_GNUC_NONNULL(1);
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @brief return the current date in milliseconds */
|
2022-02-18 17:52:01 +00:00
|
|
|
uint64_t tr_time_msec();
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief sleep the specified number of milliseconds */
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_wait_msec(long int delay_milliseconds);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
#if defined(__GNUC__) && !__has_include(<charconv>)
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::optional<T> tr_parseNum(std::string_view& sv)
|
|
|
|
{
|
|
|
|
auto val = T{};
|
|
|
|
auto const str = std::string(std::data(sv), std::min(std::size(sv), size_t{ 64 }));
|
|
|
|
auto sstream = std::stringstream{ str };
|
|
|
|
auto const oldpos = sstream.tellg();
|
|
|
|
sstream >> val;
|
|
|
|
auto const newpos = sstream.tellg();
|
|
|
|
if ((newpos == oldpos) || (sstream.fail() && !sstream.eof()))
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
sv.remove_prefix(sstream.eof() ? std::size(sv) : newpos - oldpos);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // #if defined(__GNUC__) && !__has_include(<charconv>)
|
|
|
|
|
|
|
|
#include <charconv> // std::from_chars()
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::optional<T> tr_parseNum(std::string_view& sv)
|
|
|
|
{
|
|
|
|
auto val = T{};
|
|
|
|
auto const* const begin_ch = std::data(sv);
|
|
|
|
auto const* const end_ch = begin_ch + std::size(sv);
|
|
|
|
auto const result = std::from_chars(begin_ch, end_ch, val);
|
|
|
|
if (result.ec != std::errc{})
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
sv.remove_prefix(result.ptr - std::data(sv));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #if defined(__GNUC__) && !__has_include(<charconv>)
|
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
bool tr_utf8_validate(std::string_view sv, char const** endptr);
|
|
|
|
|
2014-11-30 20:08:20 +00:00
|
|
|
#ifdef _WIN32
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_win32_native_to_utf8(wchar_t const* text, int text_size);
|
2021-08-15 09:41:48 +00:00
|
|
|
char* tr_win32_native_to_utf8_ex(
|
|
|
|
wchar_t const* text,
|
|
|
|
int text_size,
|
|
|
|
int extra_chars_before,
|
|
|
|
int extra_chars_after,
|
2018-01-24 20:10:21 +00:00
|
|
|
int* real_result_size);
|
2017-04-20 16:02:19 +00:00
|
|
|
wchar_t* tr_win32_utf8_to_native(char const* text, int text_size);
|
2021-08-15 09:41:48 +00:00
|
|
|
wchar_t* tr_win32_utf8_to_native_ex(
|
|
|
|
char const* text,
|
|
|
|
int text_size,
|
|
|
|
int extra_chars_before,
|
|
|
|
int extra_chars_after,
|
2017-04-19 12:04:45 +00:00
|
|
|
int* real_result_size);
|
|
|
|
char* tr_win32_format_message(uint32_t code);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_win32_make_args_utf8(int* argc, char*** argv);
|
2014-09-21 18:06:28 +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
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper around malloc() in which `0' is a safe argument */
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_malloc(size_t size);
|
2010-01-01 22:13:27 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper around calloc() in which `0' is a safe argument */
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_malloc0(size_t size);
|
2010-01-01 22:13:27 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper around reallocf() in which `0' is a safe argument */
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_realloc(void* p, size_t size);
|
2015-10-25 17:13:14 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
/** @brief Portability wrapper around free() in which `nullptr' is a safe argument */
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_free(void* p);
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief make a newly-allocated copy of a chunk of memory
|
|
|
|
* @param src the memory to copy
|
|
|
|
* @param byteCount the number of bytes to copy
|
2017-04-21 07:40:57 +00:00
|
|
|
* @return a newly-allocated copy of `src' that can be freed with tr_free()
|
2010-01-01 22:13:27 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_memdup(void const* src, size_t byteCount);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
#define tr_new(struct_type, n_structs) (static_cast<struct_type*>(tr_malloc(sizeof(struct_type) * (size_t)(n_structs))))
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
#define tr_new0(struct_type, n_structs) (static_cast<struct_type*>(tr_malloc0(sizeof(struct_type) * (size_t)(n_structs))))
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
#define tr_renew(struct_type, mem, n_structs) \
|
|
|
|
(static_cast<struct_type*>(tr_realloc((mem), sizeof(struct_type) * (size_t)(n_structs))))
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief make a newly-allocated copy of a substring
|
|
|
|
* @param in is a void* so that callers can pass in both signed & unsigned without a cast
|
2017-04-21 07:40:57 +00:00
|
|
|
* @param len length of the substring to copy. if a length less than zero is passed in, strlen(len) is used
|
|
|
|
* @return a newly-allocated copy of `in' that can be freed with tr_free()
|
2010-01-01 22:13:27 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_strndup(void const* in, size_t len) TR_GNUC_MALLOC;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
|
|
|
* @brief make a newly-allocated copy of a string
|
|
|
|
* @param in is a void* so that callers can pass in both signed & unsigned without a cast
|
2017-04-21 07:40:57 +00:00
|
|
|
* @return a newly-allocated copy of `in' that can be freed with tr_free()
|
2010-01-01 22:13:27 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_strdup(void const* in);
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2011-01-03 03:01:12 +00:00
|
|
|
/**
|
2021-10-06 16:32:17 +00:00
|
|
|
* @brief like strcmp() but gracefully handles nullptr strings
|
2011-01-03 03:01:12 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_strcmp0(char const* str1, char const* str2);
|
2011-01-03 03:01:12 +00:00
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
std::string evbuffer_free_to_str(evbuffer* buf);
|
2010-12-20 02:07:51 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief sprintf() a string into a newly-allocated buffer large enough to hold it
|
|
|
|
* @return a newly-allocated string that can be freed with tr_free()
|
2010-01-01 22:13:27 +00:00
|
|
|
*/
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
char* tr_strdup_printf(char const* fmt, ...) TR_GNUC_MALLOC TR_GNUC_PRINTF(1, 2);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-21 07:40:57 +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
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper for snprintf() that uses the system implementation if available */
|
2020-08-11 18:11:55 +00:00
|
|
|
int tr_snprintf(void* buf, size_t buflen, char const* fmt, ...) TR_GNUC_PRINTF(3, 4) TR_GNUC_NONNULL(1, 3);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2021-10-06 16:32:17 +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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_strerror(int errnum);
|
2008-02-19 04:16:04 +00:00
|
|
|
|
2010-06-07 14:25:31 +00:00
|
|
|
/** @brief Returns true if the string ends with the specified case-insensitive suffix */
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_str_has_suffix(char const* str, char const* suffix);
|
2010-06-07 14:25:31 +00:00
|
|
|
|
2021-11-14 05:23:19 +00:00
|
|
|
template<typename T>
|
|
|
|
std::string tr_strlower(T in)
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
std::string tr_strupper(T in)
|
|
|
|
{
|
|
|
|
auto out = std::string{ in };
|
|
|
|
std::for_each(std::begin(out), std::end(out), [](char& ch) { ch = std::toupper(ch); });
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
/***
|
|
|
|
**** std::string_view utils
|
|
|
|
***/
|
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
template<typename... T, typename std::enable_if_t<(std::is_convertible_v<T, std::string_view> && ...), bool> = true>
|
|
|
|
std::string tr_strvPath(T... args)
|
|
|
|
{
|
|
|
|
auto setme = std::string{};
|
|
|
|
auto const n_args = sizeof...(args);
|
2022-02-01 17:30:51 +00:00
|
|
|
auto const n = n_args + (std::size(std::string_view{ args }) + ...);
|
|
|
|
if (setme.capacity() < n)
|
2021-11-13 00:10:04 +00:00
|
|
|
{
|
|
|
|
setme.reserve(n);
|
|
|
|
}
|
2021-11-14 19:53:58 +00:00
|
|
|
|
|
|
|
auto const foo = [&setme](std::string_view a)
|
2021-11-13 00:10:04 +00:00
|
|
|
{
|
|
|
|
setme += a;
|
|
|
|
setme += TR_PATH_DELIMITER;
|
|
|
|
};
|
2021-11-14 19:53:58 +00:00
|
|
|
(foo(args), ...);
|
2021-11-13 00:10:04 +00:00
|
|
|
setme.resize(setme.size() - 1);
|
|
|
|
return setme;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... T, typename std::enable_if_t<(std::is_convertible_v<T, std::string_view> && ...), bool> = true>
|
|
|
|
std::string tr_strvJoin(T... args)
|
|
|
|
{
|
|
|
|
auto setme = std::string{};
|
2022-02-01 17:30:51 +00:00
|
|
|
auto const n = (std::size(std::string_view{ args }) + ...);
|
|
|
|
if (setme.capacity() < n)
|
2021-11-13 00:10:04 +00:00
|
|
|
{
|
|
|
|
setme.reserve(n);
|
|
|
|
}
|
|
|
|
((setme += args), ...);
|
|
|
|
return setme;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
template<typename T>
|
|
|
|
constexpr bool tr_strvContains(std::string_view sv, T key) // c++23
|
|
|
|
{
|
|
|
|
return sv.find(key) != sv.npos;
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:42:18 +00:00
|
|
|
constexpr bool tr_strvStartsWith(std::string_view sv, char key) // c++20
|
|
|
|
{
|
|
|
|
return !std::empty(sv) && sv.front() == key;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
constexpr bool tr_strvStartsWith(std::string_view sv, std::string_view key) // c++20
|
|
|
|
{
|
|
|
|
return std::size(key) <= std::size(sv) && sv.substr(0, std::size(key)) == key;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool tr_strvEndsWith(std::string_view sv, std::string_view key) // c++20
|
|
|
|
{
|
|
|
|
return std::size(key) <= std::size(sv) && sv.substr(std::size(sv) - std::size(key)) == key;
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:42:18 +00:00
|
|
|
constexpr bool tr_strvEndsWith(std::string_view sv, char key) // c++20
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view tr_strvStrip(std::string_view sv);
|
|
|
|
|
|
|
|
char* tr_strvDup(std::string_view) TR_GNUC_MALLOC;
|
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
std::string& tr_strvUtf8Clean(std::string_view cleanme, std::string& setme);
|
2010-12-08 14:57:34 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-01 22:13:27 +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] */
|
2017-04-19 12:04:45 +00:00
|
|
|
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.
|
2017-04-21 07:40:57 +00:00
|
|
|
* @return a newly-allocated array of integers that must be freed with tr_free(),
|
2021-10-06 16:32:17 +00:00
|
|
|
* or nullptr if a fragment of 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.
|
|
|
|
*/
|
2021-10-15 22:15:33 +00:00
|
|
|
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.
|
|
|
|
*
|
2017-04-21 07:40:57 +00:00
|
|
|
* this can be used to prevent a printf() call from rounding up:
|
2010-01-06 00:18:33 +00:00
|
|
|
* call with the decimal_places argument equal to the number of
|
2017-04-21 07:40:57 +00:00
|
|
|
* 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 |
|
|
|
|
* +------------------------+
|
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
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 */
|
2021-12-31 00:30:21 +00:00
|
|
|
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
|
|
|
|
* @param infinity the string represntation of "infinity"
|
2009-03-04 16:16:02 +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-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper for localtime_r() that uses the system implementation if available */
|
2017-04-20 16:02:19 +00:00
|
|
|
struct tm* tr_localtime_r(time_t const* _clock, struct tm* _result);
|
2009-03-25 18:42:39 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
/** @brief Portability wrapper for gmtime_r() that uses the system implementation if available */
|
|
|
|
struct tm* tr_gmtime_r(time_t const* _clock, struct tm* _result);
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper for gettimeofday(), with tz argument dropped */
|
2022-02-01 01:02:59 +00:00
|
|
|
struct timeval tr_gettimeofday();
|
2009-03-25 18:42:39 +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
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_moveFile(char const* oldpath, char const* newpath, struct tr_error** error) TR_GNUC_NONNULL(1, 2);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/** @brief convenience function to remove an item from an array */
|
2020-08-11 18:11:55 +00:00
|
|
|
void tr_removeElementFromArray(void* array, size_t index_to_remove, size_t sizeof_element, size_t nmemb);
|
2009-03-04 16:16:02 +00:00
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Private libtransmission variable that's visible only for inlining in tr_time() */
|
2010-12-31 01:30:35 +00:00
|
|
|
extern time_t __tr_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-02-18 17:52:01 +00:00
|
|
|
static inline time_t tr_time()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return __tr_current_time;
|
|
|
|
}
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Private libtransmission function to update tr_time()'s counter */
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr void tr_timeUpdate(time_t now)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
__tr_current_time = now;
|
|
|
|
}
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper for htonll() that uses the system implementation if available */
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t tr_htonll(uint64_t);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/** @brief Portability wrapper for htonll() that uses the system implementation if available */
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t tr_ntohll(uint64_t);
|
2011-03-13 00:18:11 +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. */
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_formatter_speed_KBps(double KBps);
|
2010-07-09 20:53:38 +00:00
|
|
|
|
|
|
|
/* format a memory size from bytes into a user-readable string. */
|
2021-12-31 00:30:21 +00:00
|
|
|
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. */
|
2021-12-31 00:30:21 +00:00
|
|
|
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. */
|
2021-12-31 00:30:21 +00:00
|
|
|
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
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
static inline unsigned int tr_toSpeedBytes(unsigned int KBps)
|
|
|
|
{
|
|
|
|
return KBps * tr_speed_K;
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
static inline auto tr_toSpeedKBps(unsigned int 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
|
|
|
}
|
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
static inline auto tr_toMemBytes(unsigned int MB)
|
2021-12-15 21:25:42 +00:00
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
auto B = uint64_t(tr_mem_K) * tr_mem_K;
|
2021-12-15 21:25:42 +00:00
|
|
|
B *= MB;
|
|
|
|
return B;
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
static inline auto tr_toMemMB(uint64_t B)
|
2021-12-15 21:25:42 +00:00
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
return int(B / (tr_mem_K * tr_mem_K));
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
/** @brief Check if environment variable exists. */
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_env_key_exists(char const* key);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
/** @brief Get environment variable value as int. */
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_env_get_int(char const* key, int default_value);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
/** @brief Get environment variable value as string (should be freed afterwards). */
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_env_get_string(char const* key, char const* default_value);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-02-18 17:52:01 +00:00
|
|
|
void tr_net_init();
|