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
8
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#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
17#include "file_win32_impl.hpp"
18#endif
19#include <mutex>
20#include <cstdint>
21#include <map>
22#include <span>
23#include <filesystem>
24
25hi_export_module(hikogui.file.file);
26
27hi_export namespace hi { inline namespace v1 {
28namespace detail {
29class file_impl;
30}
31
35hi_export class file {
36public:
37 ~file() = default;
38 file(file const& other) noexcept = default;
39 file(file&& other) noexcept = default;
40 file& operator=(file const& other) noexcept = default;
41 file& operator=(file&& other) noexcept = default;
42
47 file(std::filesystem::path const& path, access_mode access_mode = access_mode::open_for_read) :
48 _pimpl(std::make_shared<detail::file_impl>(path, access_mode))
49 {
50 }
51
52 [[nodiscard]] hi::access_mode access_mode() const noexcept
53 {
54 return _pimpl->access_mode();
55 }
56
57 [[nodiscard]] std::shared_ptr<detail::file_impl> pimpl() const noexcept
58 {
59 return _pimpl;
60 }
61
64 void close()
65 {
66 return _pimpl->close();
67 }
68
73 void flush()
74 {
75 return _pimpl->flush();
76 }
77
85 void rename(std::filesystem::path const& destination, bool overwrite_existing = true)
86 {
87 return _pimpl->rename(destination, overwrite_existing);
88 }
89
92 [[nodiscard]] std::size_t size() const
93 {
94 return _pimpl->size();
95 }
96
103 {
104 return _pimpl->seek(offset, whence);
105 }
106
110 {
111 return seek(0, seek_whence::current);
112 }
113
120 void write(void const *data, std::size_t size)
121 {
122 return _pimpl->write(data, size);
123 }
124
132 [[nodiscard]] std::size_t read(void *data, std::size_t size)
133 {
134 return _pimpl->read(data, size);
135 }
136
142 void write(std::span<std::byte const> bytes)
143 {
144 return write(bytes.data(), ssize(bytes));
145 }
146
152 void write(bstring_view text)
153 {
154 return write(text.data(), ssize(text));
155 }
156
162 void write(bstring const& text)
163 {
164 return write(text.data(), ssize(text));
165 }
166
172 void write(std::string_view text)
173 {
174 return write(text.data(), ssize(text));
175 }
176
183 [[nodiscard]] bstring read_bstring(std::size_t max_size = 10'000'000)
184 {
185 auto const offset = get_seek();
186 auto const size_ = std::min(max_size, this->size() - offset);
187
188 auto r = bstring{};
189 // XXX c++23 resize_and_overwrite()
190 r.resize(size_);
191 auto const bytes_read = read(r.data(), size_);
192 r.resize(bytes_read);
193 return r;
194 }
195
206 [[nodiscard]] std::string read_string(std::size_t max_size = 10'000'000)
207 {
208 hi_assert(get_seek() == 0);
209
210 auto const size_ = size();
211 if (size_ > max_size) {
212 throw io_error("read_string() requires the file size to be smaler than max_size.");
213 }
214
215 auto r = std::string{};
216 // XXX c++23 resize_and_overwrite()
217 r.resize(size_);
218 auto const bytes_read = read(r.data(), size_);
219 r.resize(bytes_read);
220 return r;
221 }
222
223private:
225};
226
227}} // 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.
Definition seek_whence.hpp:16
@ begin
Start from the beginning of the file.
Definition seek_whence.hpp:15
@ open_for_read
Default open a file for reading.
Definition access_mode.hpp:32
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:24
STL namespace.
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
A File object.
Definition file_intf.hpp:35
void rename(std::filesystem::path const &destination, bool overwrite_existing=true)
Rename an open file.
Definition file_intf.hpp:85
std::size_t read(void *data, std::size_t size)
Read data from a file.
Definition file_intf.hpp:132
std::size_t get_seek()
Get the current seek location.
Definition file_intf.hpp:109
std::string read_string(std::size_t max_size=10 '000 '000)
Read a UTF-8 string from the file.
Definition file_intf.hpp:206
std::size_t seek(std::ptrdiff_t offset, seek_whence whence=seek_whence::begin)
Set the seek location.
Definition file_intf.hpp:102
void write(bstring const &text)
Write data to a file.
Definition file_intf.hpp:162
void write(std::span< std::byte const > bytes)
Write data to a file.
Definition file_intf.hpp:142
void close()
Close the file.
Definition file_intf.hpp:64
std::size_t size() const
Return the size of the file.
Definition file_intf.hpp:92
void flush()
Flush and block until all data is physically written to disk.
Definition file_intf.hpp:73
void write(bstring_view text)
Write data to a file.
Definition file_intf.hpp:152
file(std::filesystem::path const &path, access_mode access_mode=access_mode::open_for_read)
Open a file at location.
Definition file_intf.hpp:47
bstring read_bstring(std::size_t max_size=10 '000 '000)
Read bytes from the file.
Definition file_intf.hpp:183
void write(std::string_view text)
Write data to a file.
Definition file_intf.hpp:172
void write(void const *data, std::size_t size)
Write data to a file.
Definition file_intf.hpp:120
Definition file_win32_impl.hpp:19
Exception thrown during I/O on an error.
Definition exception_intf.hpp:173
T min(T... args)
T resize(T... args)