2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2013-2023 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.
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2023-04-18 21:23:20 +00:00
|
|
|
#include <string>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2023-04-18 21:23:20 +00:00
|
|
|
#include <vector>
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-04-18 21:23:20 +00:00
|
|
|
#include "libtransmission/tr-strbuf.h"
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-11-27 18:11:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static auto constexpr NativeEol = "\r\n"sv;
|
|
|
|
#else
|
|
|
|
static auto constexpr NativeEol = "\n"sv;
|
|
|
|
#endif
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
bool tr_sys_file_write_line(tr_sys_file_t handle, std::string_view buffer, tr_error** error)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
bool ret = tr_sys_file_write(handle, std::data(buffer), std::size(buffer), nullptr, error);
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
2022-11-27 18:11:25 +00:00
|
|
|
ret = tr_sys_file_write(handle, std::data(NativeEol), std::size(NativeEol), nullptr, error);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 18:08:56 +00:00
|
|
|
}
|
2023-04-18 21:23:20 +00:00
|
|
|
|
|
|
|
std::vector<std::string> tr_sys_dir_get_files(
|
|
|
|
std::string_view folder,
|
|
|
|
std::function<bool(std::string_view)> const& test,
|
|
|
|
tr_error** error)
|
|
|
|
{
|
|
|
|
if (auto const info = tr_sys_path_get_info(folder); !info || !info->isFolder())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const odir = tr_sys_dir_open(folder, error);
|
|
|
|
if (odir == TR_BAD_SYS_DIR)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto filenames = std::vector<std::string>{};
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char const* const name = tr_sys_dir_read_name(odir, error);
|
|
|
|
|
|
|
|
if (name == nullptr)
|
|
|
|
{
|
|
|
|
tr_sys_dir_close(odir, error);
|
|
|
|
return filenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test(name))
|
|
|
|
{
|
|
|
|
filenames.emplace_back(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|