HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file.hpp
1// Copyright Take Vos 2019-2022.
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 "byte_string.hpp"
8#include "architecture.hpp"
9#include "cast.hpp"
10#include <mutex>
11#include <cstdint>
12#include <map>
13#include <span>
14#include <filesystem>
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
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(std::filesystem::path const& path, access_mode access_mode = access_mode::open_for_read);
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(std::filesystem::path 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(std::filesystem::path const& path);
226
227 static void create_directory(std::filesystem::path const& path, bool hierarchy = false);
228
229 static void create_directory_hierarchy(std::filesystem::path const& path);
230
231private:
234 access_mode _access_mode;
235
238 std::filesystem::path _path;
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...
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
access_mode
Definition file.hpp:20
@ read_lock
Lock the file for reading, i.e. shared-lock.
@ sequential
Hint that the data should be prefetched.
@ create
Create file if it does not exist, or fail.
@ open
Open file if it exist, or fail.
@ random
Hint the data should not be prefetched.
@ open_for_read_and_write
Default open a file for reading and writing.
@ open_for_read
Default open a file for reading.
@ write_through
Hint that writes should be send directly to disk.
@ write_lock
Lock the file for writing, i.e. exclusive-lock.
@ no_reuse
Hint that the data should not be cached.
@ read
Allow read access to a file.
@ write
Allow write access to a file.
@ create_directories
Create directory hierarchy, if the file could not be created.
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.
file(std::filesystem::path const &path, access_mode access_mode=access_mode::open_for_read)
Open a file at location.
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(std::filesystem::path const &path)
Get the size of a file on the file system.
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:22
A variant of text.
Definition label.hpp:37
T begin(T... args)
T end(T... args)
T operator>=(T... args)