HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file_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 "file_file_win32_impl.hpp"
12#include "../container/module.hpp"
13#include "../utility/utility.hpp"
14#include "../macros.hpp"
15#include "access_mode.hpp"
16#include "seek_whence.hpp"
17#include <mutex>
18#include <cstdint>
19#include <map>
20#include <span>
21#include <filesystem>
22
23hi_export_module(hikogui.file.file : intf);
24
25namespace hi { inline namespace v1 {
26
30hi_export class file {
31public:
32 ~file() = default;
33 file(file const& other) noexcept = default;
34 file(file&& other) noexcept = default;
35 file& operator=(file const& other) noexcept = default;
36 file& operator=(file&& other) noexcept = default;
37
42 file(std::filesystem::path const& path, access_mode access_mode = access_mode::open_for_read) :
43 _pimpl(std::make_shared<detail::file_impl>(path, access_mode))
44 {
45 }
46
48 {
49 return _pimpl->access_mode();
50 }
51
53 {
54 return _pimpl;
55 }
56
59 void close()
60 {
61 return _pimpl->close();
62 }
63
68 void flush()
69 {
70 return _pimpl->flush();
71 }
72
80 void rename(std::filesystem::path const& destination, bool overwrite_existing = true)
81 {
82 return _pimpl->rename(destination, overwrite_existing);
83 }
84
88 {
89 return _pimpl->size();
90 }
91
98 {
99 return _pimpl->seek(offset, whence);
100 }
101
105 {
106 return seek(0, seek_whence::current);
107 }
108
115 void write(void const *data, std::size_t size)
116 {
117 return _pimpl->write(data, size);
118 }
119
128 {
129 return _pimpl->read(data, size);
130 }
131
137 void write(std::span<std::byte const> bytes)
138 {
139 return write(bytes.data(), ssize(bytes));
140 }
141
147 void write(bstring_view text)
148 {
149 return write(text.data(), ssize(text));
150 }
151
157 void write(bstring const& text)
158 {
159 return write(text.data(), ssize(text));
160 }
161
167 void write(std::string_view text)
168 {
169 return write(text.data(), ssize(text));
170 }
171
178 [[nodiscard]] bstring read_bstring(std::size_t max_size = 10'000'000)
179 {
180 hilet offset = get_seek();
181 hilet size_ = std::min(max_size, this->size() - offset);
182
183 auto r = bstring{};
184 // XXX c++23 resize_and_overwrite()
185 r.resize(size_);
186 hilet bytes_read = read(r.data(), size_);
187 r.resize(bytes_read);
188 return r;
189 }
190
201 [[nodiscard]] std::string read_string(std::size_t max_size = 10'000'000)
202 {
203 hi_assert(get_seek() == 0);
204
205 hilet size_ = size();
206 if (size_ > max_size) {
207 throw io_error("read_string() requires the file size to be smaler than max_size.");
208 }
209
210 auto r = std::string{};
211 // XXX c++23 resize_and_overwrite()
212 r.resize(size_);
213 hilet bytes_read = read(r.data(), size_);
214 r.resize(bytes_read);
215 return r;
216 }
217
218private:
220};
221
222}} // 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.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A File object.
Definition file_file_intf.hpp:30
void rename(std::filesystem::path const &destination, bool overwrite_existing=true)
Rename an open file.
Definition file_file_intf.hpp:80
std::size_t read(void *data, std::size_t size)
Read data from a file.
Definition file_file_intf.hpp:127
std::size_t get_seek()
Get the current seek location.
Definition file_file_intf.hpp:104
std::string read_string(std::size_t max_size=10 '000 '000)
Read a UTF-8 string from the file.
Definition file_file_intf.hpp:201
std::size_t seek(std::ptrdiff_t offset, seek_whence whence=seek_whence::begin)
Set the seek location.
Definition file_file_intf.hpp:97
void write(bstring const &text)
Write data to a file.
Definition file_file_intf.hpp:157
void write(std::span< std::byte const > bytes)
Write data to a file.
Definition file_file_intf.hpp:137
void close()
Close the file.
Definition file_file_intf.hpp:59
std::size_t size() const
Return the size of the file.
Definition file_file_intf.hpp:87
void flush()
Flush and block until all data is physically written to disk.
Definition file_file_intf.hpp:68
void write(bstring_view text)
Write data to a file.
Definition file_file_intf.hpp:147
file(std::filesystem::path const &path, access_mode access_mode=access_mode::open_for_read)
Open a file at location.
Definition file_file_intf.hpp:42
bstring read_bstring(std::size_t max_size=10 '000 '000)
Read bytes from the file.
Definition file_file_intf.hpp:178
void write(std::string_view text)
Write data to a file.
Definition file_file_intf.hpp:167
void write(void const *data, std::size_t size)
Write data to a file.
Definition file_file_intf.hpp:115
Exception thrown during I/O on an error.
Definition exception_intf.hpp:172
T min(T... args)
T resize(T... args)