2014-09-21 18:08:56 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring> /* strlen() */
|
2014-09-21 18:08:56 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2015-04-11 14:54:01 +00:00
|
|
|
#include "error.h"
|
2014-09-21 18:08:56 +00:00
|
|
|
#include "file.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2014-09-21 18:08:56 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_read_line(tr_sys_file_t handle, char* buffer, size_t buffer_size, 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);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(buffer_size > 0);
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
auto ret = bool{};
|
|
|
|
auto offset = size_t{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (buffer_size > 0)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
size_t const bytes_needed = std::min(buffer_size, size_t{ 1024 });
|
2021-10-23 15:43:15 +00:00
|
|
|
auto bytes_read = uint64_t{};
|
2019-03-13 02:32:35 +00:00
|
|
|
ret = tr_sys_file_read(handle, buffer + offset, bytes_needed, &bytes_read, error);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!ret || (offset == 0 && bytes_read == 0))
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = false;
|
|
|
|
break;
|
2014-09-21 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 02:32:35 +00:00
|
|
|
TR_ASSERT(bytes_read <= bytes_needed);
|
|
|
|
TR_ASSERT(bytes_read <= buffer_size);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
int64_t delta = 0;
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
for (size_t i = 0; i < bytes_read; ++i)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (buffer[offset] == '\n')
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
delta = i - (int64_t)bytes_read + 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2014-09-21 18:08:56 +00:00
|
|
|
}
|
2020-11-08 19:54:40 +00:00
|
|
|
|
|
|
|
++offset;
|
|
|
|
--buffer_size;
|
2014-09-21 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
if (delta != 0 || buffer_size == 0 || bytes_read == 0)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (delta != 0)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
ret = tr_sys_file_seek(handle, delta, TR_SEEK_CUR, nullptr, error);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-09-21 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (offset > 0 && buffer[offset - 1] == '\r')
|
|
|
|
{
|
|
|
|
buffer[offset - 1] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[offset] = '\0';
|
|
|
|
}
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_file_write_line(tr_sys_file_t handle, char const* 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);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr);
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
bool ret = tr_sys_file_write(handle, buffer, strlen(buffer), nullptr, error);
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
ret = tr_sys_file_write(handle, TR_NATIVE_EOL_STR, TR_NATIVE_EOL_STR_SIZE, 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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_file_write_fmt(tr_sys_file_t handle, char const* format, tr_error** error, ...)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(format != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
va_list args;
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, error);
|
2021-10-23 15:43:15 +00:00
|
|
|
char* const buffer = tr_strdup_vprintf(format, args);
|
2017-04-19 12:04:45 +00:00
|
|
|
va_end(args);
|
2014-09-21 18:08:56 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (buffer != nullptr)
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
ret = tr_sys_file_write(handle, buffer, strlen(buffer), nullptr, error);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(buffer);
|
2014-09-21 18:08:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-09-21 18:08:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_set_literal(error, 0, "Unable to format message.");
|
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
|
|
|
}
|