HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file_intf.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
9#pragma once
10
11#include "../container/container.hpp"
12#include "../utility/utility.hpp"
13#include "../macros.hpp"
14#include "access_mode.hpp"
15#include "seek_whence.hpp"
16#include "file_win32_impl.hpp"
17#include <mutex>
18#include <cstdint>
19#include <map>
20#include <span>
21#include <filesystem>
22
23hi_export_module(hikogui.file.file);
24
25hi_export namespace hi { inline namespace v1 {
26namespace detail {
27class file_impl;
28}
29
33hi_export class file {
34public:
35 ~file() = default;
36 file(file const& other) noexcept = default;
37 file(file&& other) noexcept = default;
38 file& operator=(file const& other) noexcept = default;
39 file& operator=(file&& other) noexcept = default;
40
45 file(std::filesystem::path const& path, access_mode access_mode = access_mode::open_for_read) :
46 _pimpl(std::make_shared<detail::file_impl>(path, access_mode))
47 {
48 }
49
50 [[nodiscard]] hi::access_mode access_mode() const noexcept
51 {
52 return _pimpl->access_mode();
53 }
54
55 [[nodiscard]] std::shared_ptr<detail::file_impl> pimpl() const noexcept
56 {
57 return _pimpl;
58 }
59
62 void close()
63 {
64 return _pimpl->close();
65 }
66
71 void flush()
72 {
73 return _pimpl->flush();
74 }
75
83 void rename(std::filesystem::path const& destination, bool overwrite_existing = true)
84 {
85 return _pimpl->rename(destination, overwrite_existing);
86 }
87
90 [[nodiscard]] std::size_t size() const
91 {
92 return _pimpl->size();
93 }
94
101 {
102 return _pimpl->seek(offset, whence);
103 }
104
108 {
109 return seek(0, seek_whence::current);
110 }
111
118 void write(void const *data, std::size_t size)
119 {
120 return _pimpl->write(data, size);
121 }
122
130 [[nodiscard]] std::size_t read(void *data, std::size_t size)
131 {
132 return _pimpl->read(data, size);
133 }
134
140 void write(std::span<std::byte const> bytes)
141 {
142 return write(bytes.data(), ssize(bytes));
143 }
144
150 void write(bstring_view text)
151 {
152 return write(text.data(), ssize(text));
153 }
154
160 void write(bstring const& text)
161 {
162 return write(text.data(), ssize(text));
163 }
164
170 void write(std::string_view text)
171 {
172 return write(text.data(), ssize(text));
173 }
174
181 [[nodiscard]] bstring read_bstring(std::size_t max_size = 10'000'000)
182 {
183 auto const offset = get_seek();
184 auto const size_ = std::min(max_size, this->size() - offset);
185
186 auto r = bstring{};
187 // XXX c++23 resize_and_overwrite()
188 r.resize(size_);
189 auto const bytes_read = read(r.data(), size_);
190 r.resize(bytes_read);
191 return r;
192 }
193
204 [[nodiscard]] std::string read_string(std::size_t max_size = 10'000'000)
205 {
206 hi_assert(get_seek() == 0);
207
208 auto const size_ = size();
209 if (size_ > max_size) {
210 throw io_error("read_string() requires the file size to be smaler than max_size.");
211 }
212
213 auto r = std::string{};
214 // XXX c++23 resize_and_overwrite()
215 r.resize(size_);
216 auto const bytes_read = read(r.data(), size_);
217 r.resize(bytes_read);
218 return r;
219 }
220
221private:
223};
224
225}} // namespace hi::v1
seek_whence
The position in the file to seek from.
Definition seek_whence.hpp:14
access_mode
The mode in which way to open a file.
Definition access_mode.hpp:17
@ current
Continue from the current position.
@ begin
Start from the beginning of the file.
@ open_for_read
Default open a file for reading.
@ read
Allow read access to a file.
@ write
Allow write access to a file.
@ other
The gui_event does not have associated data.
STL namespace.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A File object.
Definition file_intf.hpp:33
void rename(std::filesystem::path const &destination, bool overwrite_existing=true)
Rename an open file.
Definition file_intf.hpp:83
std::size_t read(void *data, std::size_t size)
Read data from a file.
Definition file_intf.hpp:130
std::size_t get_seek()
Get the current seek location.
Definition file_intf.hpp:107
std::string read_string(std::size_t max_size=10 '000 '000)
Read a UTF-8 string from the file.
Definition file_intf.hpp:204
std::size_t seek(std::ptrdiff_t offset, seek_whence whence=seek_whence::begin)
Set the seek location.
Definition file_intf.hpp:100
void write(bstring const &text)
Write data to a file.
Definition file_intf.hpp:160
void write(std::span< std::byte const > bytes)
Write data to a file.
Definition file_intf.hpp:140
void close()
Close the file.
Definition file_intf.hpp:62
std::size_t size() const
Return the size of the file.
Definition file_intf.hpp:90
void flush()
Flush and block until all data is physically written to disk.
Definition file_intf.hpp:71
void write(bstring_view text)
Write data to a file.
Definition file_intf.hpp:150
file(std::filesystem::path const &path, access_mode access_mode=access_mode::open_for_read)
Open a file at location.
Definition file_intf.hpp:45
bstring read_bstring(std::size_t max_size=10 '000 '000)
Read bytes from the file.
Definition file_intf.hpp:181
void write(std::string_view text)
Write data to a file.
Definition file_intf.hpp:170
void write(void const *data, std::size_t size)
Write data to a file.
Definition file_intf.hpp:118
Exception thrown during I/O on an error.
Definition exception_intf.hpp:173
T min(T... args)
T resize(T... args)