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 "cast.hpp"
11#include <mutex>
12#include <cstdint>
13#include <map>
14#include <span>
15
16namespace hi::inline v1 {
17
18enum class seek_whence { begin, current, end };
19
20enum class access_mode {
21 read = 0x1,
22 write = 0x2,
23 rename = 0x4,
24 read_lock = 0x10,
25 write_lock = 0x20,
26 open = 0x100,
27 create = 0x200,
28 truncate = 0x400,
29 random = 0x1000,
30 sequential = 0x2000,
31 no_reuse = 0x4000,
32 write_through = 0x8000,
33 create_directories = 0x10000,
34
35 open_for_read = open | read,
36 open_for_read_and_write = open | read | write,
37 truncate_or_create_for_write = create_directories | open | create | truncate | write
38};
39
40[[nodiscard]] constexpr access_mode operator|(access_mode const& lhs, access_mode const& rhs) noexcept
41{
42 return static_cast<access_mode>(to_underlying(lhs) | to_underlying(rhs));
43}
44
45[[nodiscard]] constexpr access_mode operator&(access_mode const& lhs, access_mode const& rhs) noexcept
46{
47 return static_cast<access_mode>(to_underlying(lhs) & to_underlying(rhs));
48}
49
50bool operator>=(access_mode const& lhs, access_mode const& rhs) = delete;
51
52[[nodiscard]] constexpr bool any(access_mode const& rhs) noexcept
53{
54 return static_cast<bool>(to_underlying(rhs));
55}
56
59[[nodiscard]] inline bool operator>=(access_mode lhs, access_mode rhs) noexcept
60{
61 return (lhs & rhs) == rhs;
62}
63
66class file {
67public:
72 file(URL const& location, access_mode accessMode);
73
74 ~file() noexcept;
75
76 file(file const& other) = delete;
77 file(file&& other) = delete;
78 file& operator=(file const& other) = delete;
79 file& operator=(file&& other) = delete;
80
83 void close();
84
89 void flush();
90
98 void rename(URL const& destination, bool overwrite_existing = true);
99
102 std::size_t size() const;
103
109 std::size_t seek(ssize_t offset, seek_whence whence = seek_whence::begin);
110
113 std::size_t get_seek()
114 {
115 return seek(0, seek_whence::current);
116 }
117
126 std::size_t write(std::byte const *data, std::size_t size, ssize_t offset = -1);
127
136 std::size_t write(void const *data, std::size_t size, ssize_t offset = -1)
137 {
138 return write(reinterpret_cast<std::byte const *>(data), size, offset);
139 }
140
149 ssize_t write(char const *data, std::size_t size, ssize_t offset = -1)
150 {
151 return write(reinterpret_cast<std::byte const *>(data), size, offset);
152 }
153
161 ssize_t write(std::span<std::byte const> bytes, std::size_t offset = -1)
162 {
163 return write(bytes.data(), ssize(bytes), offset);
164 }
165
173 ssize_t write(bstring_view text, ssize_t offset = -1)
174 {
175 return write(text.data(), ssize(text), offset);
176 }
177
185 ssize_t write(bstring const& text, ssize_t offset = -1)
186 {
187 return write(text.data(), ssize(text), offset);
188 }
189
196 ssize_t write(std::string_view text)
197 {
198 return write(text.data(), ssize(text));
199 }
200
209 ssize_t read(std::byte *data, std::size_t size, ssize_t offset = -1);
210
211 ssize_t read(void *data, std::size_t size, ssize_t offset = -1)
212 {
213 return read(reinterpret_cast<std::byte *>(data), size, offset);
214 }
215
223 bstring read_bstring(std::size_t size = 10'000'000, ssize_t offset = -1);
224
237 std::string read_string(std::size_t max_size = 10'000'000);
238
251 std::u8string read_u8string(std::size_t max_size = 10'000'000);
252
256 [[nodiscard]] static std::size_t file_size(URL const& url);
257
258 static void create_directory(URL const& url, bool hierarchy = false);
259
260 static void create_directory_hierarchy(URL const& url);
261
262private:
265 access_mode _access_mode;
266
269 URL _location;
270
273 file_handle _file_handle;
274
275 friend class file_mapping;
276 friend class file_view;
277};
278
279} // namespace hi::inline v1
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
Combine vertical and horizontal alignment.
Definition alignment.hpp:200
STL namespace.
A File object.
Definition file.hpp:66
std::u8string read_u8string(std::size_t max_size=10 '000 '000)
Read the whole file as a UTF-8 string.
bstring read_bstring(std::size_t size=10 '000 '000, ssize_t offset=-1)
Read bytes from the file.
std::string read_string(std::size_t max_size=10 '000 '000)
Read the whole file as a UTF-8 string.
ssize_t write(char const *data, std::size_t size, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:149
ssize_t write(std::span< std::byte const > bytes, std::size_t offset=-1)
Write data to a file.
Definition file.hpp:161
std::size_t write(void const *data, std::size_t size, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:136
std::size_t write(std::byte const *data, std::size_t size, ssize_t offset=-1)
Write data to a file.
static std::size_t file_size(URL const &url)
Get the size of a file on the file system.
file(URL const &location, access_mode accessMode)
Open a file at location.
ssize_t write(bstring_view text, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:173
ssize_t read(std::byte *data, std::size_t size, ssize_t offset=-1)
Read data from a file.
ssize_t write(std::string_view text)
Write data to a file.
Definition file.hpp:196
ssize_t write(bstring const &text, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:185
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)