HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "URL.hpp"
8#include "byte_string.hpp"
9#include "architecture.hpp"
10#include <mutex>
11#include <cstdint>
12#include <map>
13#include <span>
14
15namespace tt {
16
17enum class seek_whence {
18 begin,
19 current,
20 end
21};
22
23enum class access_mode {
24 read = 0x1,
25 write = 0x2,
26 rename = 0x4,
27 read_lock = 0x10,
28 write_lock = 0x20,
29 open = 0x100,
30 create = 0x200,
31 truncate = 0x400,
32 random = 0x1000,
33 sequential = 0x2000,
34 no_reuse = 0x4000,
35 write_through = 0x8000,
36 create_directories = 0x10000,
37
38 open_for_read = open | read,
39 open_for_read_and_write = open | read | write,
40 truncate_or_create_for_write = create_directories | open | create | truncate | write
41};
42
43[[nodiscard]] inline access_mode operator|(access_mode lhs, access_mode rhs) noexcept
44{
45 return static_cast<access_mode>(static_cast<int>(lhs) | static_cast<int>(rhs));
46}
47
48[[nodiscard]] inline access_mode operator&(access_mode lhs, access_mode rhs) noexcept
49{
50 return static_cast<access_mode>(static_cast<int>(lhs) & static_cast<int>(rhs));
51}
52
55[[nodiscard]] inline bool operator>=(access_mode lhs, access_mode rhs) noexcept
56{
57 return (lhs & rhs) == rhs;
58}
59
62class file {
63public:
68 file(URL const& location, access_mode accessMode);
69
70 ~file() noexcept;
71
72 file(file const &other) = delete;
73 file(file &&other) = delete;
74 file &operator=(file const &other) = delete;
75 file &operator=(file &&other) = delete;
76
79 void close();
80
85 void flush();
86
94 void rename(URL const &destination, bool overwrite_existing=true);
95
98 size_t size() const;
99
105 ssize_t seek(ssize_t offset, seek_whence whence = seek_whence::begin);
106
110 return seek(0, seek_whence::current);
111 }
112
121 ssize_t write(std::byte const *data, ssize_t size, ssize_t offset=-1);
122
131 ssize_t write(void const *data, ssize_t size, ssize_t offset=-1) {
132 return write(reinterpret_cast<std::byte const *>(data), size, offset);
133 }
134
143 ssize_t write(char const *data, ssize_t size, ssize_t offset=-1) {
144 return write(reinterpret_cast<std::byte const *>(data), size, offset);
145 }
146
154 ssize_t write(std::span<std::byte const> bytes, ssize_t offset = -1)
155 {
156 return write(bytes.data(), std::ssize(bytes), offset);
157 }
158
166 ssize_t write(bstring_view text, ssize_t offset = -1)
167 {
168 return write(text.data(), std::ssize(text), offset);
169 }
170
178 ssize_t write(bstring const &text, ssize_t offset = -1)
179 {
180 return write(text.data(), std::ssize(text), offset);
181 }
182
189 ssize_t write(std::string_view text)
190 {
191 return write(text.data(), std::ssize(text));
192 }
193
202 ssize_t read(std::byte *data, ssize_t size, ssize_t offset = -1);
203
204 ssize_t read(void *data, ssize_t size, ssize_t offset = -1)
205 {
206 return read(reinterpret_cast<std::byte *>(data), size, offset);
207 }
208
216 bstring read_bstring(ssize_t size = 10'000'000, ssize_t offset = -1);
217
230 std::string read_string(ssize_t max_size = 10'000'000);
231
244 std::u8string read_u8string(ssize_t max_size = 10'000'000);
245
249 [[nodiscard]] static size_t file_size(URL const &url);
250
251 static void create_directory(URL const &url, bool hierarchy=false);
252
253 static void create_directory_hierarchy(URL const &url);
254
255private:
258 access_mode _access_mode;
259
262 URL _location;
263
266 file_handle _file_handle;
267
268 friend class file_mapping;
269 friend class file_view;
270};
271
272
273}
constexpr alignment operator|(vertical_alignment lhs, horizontal_alignment rhs) noexcept
Combine vertical and horizontal alignment.
Definition alignment.hpp:91
A File object.
Definition file.hpp:62
static size_t file_size(URL const &url)
Get the size of a file on the file system.
ssize_t get_seek()
Get the current seek location.
Definition file.hpp:109
void close()
Close the file.
ssize_t seek(ssize_t offset, seek_whence whence=seek_whence::begin)
Set the seek location.
ssize_t write(bstring const &text, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:178
ssize_t write(std::span< std::byte const > bytes, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:154
std::u8string read_u8string(ssize_t max_size=10 '000 '000)
Read the whole file as a UTF-8 string.
ssize_t read(std::byte *data, ssize_t size, ssize_t offset=-1)
Read data from a file.
void flush()
Flush and block until all data is physically written to disk.
ssize_t write(std::byte const *data, ssize_t size, ssize_t offset=-1)
Write data to a file.
ssize_t write(void const *data, ssize_t size, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:131
size_t size() const
Return the size of the file.
bstring read_bstring(ssize_t size=10 '000 '000, ssize_t offset=-1)
Read bytes from the file.
std::string read_string(ssize_t max_size=10 '000 '000)
Read the whole file as a UTF-8 string.
ssize_t write(std::string_view text)
Write data to a file.
Definition file.hpp:189
file(URL const &location, access_mode accessMode)
Open a file at location.
ssize_t write(char const *data, ssize_t size, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:143
ssize_t write(bstring_view text, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:166
Definition file_mapping.hpp:19
Definition file_view.hpp:15
Definition URL.hpp:47
T begin(T... args)
T data(T... args)
T end(T... args)
T operator>=(T... args)
T rename(T... args)