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 to_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(void const *data, std::size_t size, ssize_t offset = -1);
127
135 ssize_t write(std::span<std::byte const> bytes, std::size_t offset = -1)
136 {
137 return write(bytes.data(), ssize(bytes), offset);
138 }
139
147 ssize_t write(bstring_view text, ssize_t offset = -1)
148 {
149 return write(text.data(), ssize(text), offset);
150 }
151
159 ssize_t write(bstring const& text, ssize_t offset = -1)
160 {
161 return write(text.data(), ssize(text), offset);
162 }
163
170 ssize_t write(std::string_view text)
171 {
172 return write(text.data(), ssize(text));
173 }
174
183 ssize_t read(void *data, std::size_t size, ssize_t offset = -1);
184
192 bstring read_bstring(std::size_t size = 10'000'000, ssize_t offset = -1);
193
206 std::string read_string(std::size_t max_size = 10'000'000);
207
220 std::u8string read_u8string(std::size_t max_size = 10'000'000);
221
225 [[nodiscard]] static std::size_t file_size(URL const& url);
226
227 static void create_directory(URL const& url, bool hierarchy = false);
228
229 static void create_directory_hierarchy(URL const& url);
230
231private:
234 access_mode _access_mode;
235
238 URL _location;
239
242 file_handle _file_handle;
243
244 friend class file_mapping;
245 friend class file_view;
246};
247
248} // 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(std::span< std::byte const > bytes, std::size_t offset=-1)
Write data to a file.
Definition file.hpp:135
std::size_t write(void const *data, std::size_t size, ssize_t offset=-1)
Write data to a file.
ssize_t read(void *data, std::size_t size, ssize_t offset=-1)
Read data from 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:147
ssize_t write(std::string_view text)
Write data to a file.
Definition file.hpp:170
ssize_t write(bstring const &text, ssize_t offset=-1)
Write data to a file.
Definition file.hpp:159
Definition file_mapping.hpp:19
Definition file_view.hpp:21
Definition URL.hpp:47
T begin(T... args)
T end(T... args)
T operator>=(T... args)
T rename(T... args)