-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.cpp
86 lines (73 loc) · 2.15 KB
/
file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "file.hpp"
#include "media.hpp"
#include <utility>
namespace FileX
{
File::~File()
{
[[maybe_unused]] Error error{fx_file_close(this)};
assert(error == Error::success);
}
File::Ulong64Pair File::allocate(ThreadX::Ulong64 size, AllocateOption option)
{
Error error{};
ThreadX::Ulong64 allocatedSize{};
if (option == AllocateOption::strict)
{
if (error = Error{fx_file_extended_allocate(this, size)}; error == Error::success)
{
allocatedSize = size;
}
}
else
{
error = Error{fx_file_extended_best_effort_allocate(this, size, std::addressof(allocatedSize))};
}
return {error, allocatedSize};
}
Error File::truncate(ThreadX::Ulong64 newSize, TruncateOption option)
{
if (option == TruncateOption::noRelease)
{
return Error{fx_file_extended_truncate(this, newSize)};
}
else
{
return Error{fx_file_extended_truncate_release(this, newSize)};
}
}
Error File::seek(const ThreadX::Ulong64 offset)
{
return Error{fx_file_extended_seek(this, offset)};
}
Error File::relativeSeek(const ThreadX::Ulong64 offset, const SeekFrom from)
{
return Error{fx_file_extended_relative_seek(this, offset, std::to_underlying(from))};
}
Error File::write(const std::span<std::byte> data)
{
return Error{fx_file_write(this, data.data(), data.size())};
}
Error File::write(const std::string_view str)
{
return Error{fx_file_write(this, const_cast<char *>(str.data()), str.size())};
}
File::UlongPair File::read(std::span<std::byte> buffer)
{
ThreadX::Ulong actualSize{};
Error error{fx_file_read(this, buffer.data(), buffer.size(), std::addressof(actualSize))};
return {error, actualSize};
}
File::UlongPair File::read(std::span<std::byte> buffer, const ThreadX::Ulong size)
{
assert(size <= buffer.size());
ThreadX::Ulong actualSize{};
Error error{fx_file_read(this, buffer.data(), size, std::addressof(actualSize))};
return {error, actualSize};
}
void File::writeNotifyCallback(ThreadX::Native::FX_FILE *notifyFilePtr)
{
auto &file{static_cast<File &>(*notifyFilePtr)};
file.m_writeNotifyCallback(file);
}
} // namespace FileX