utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
file.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <cstddef>
26#include <cstring>
27#include <fstream>
28#include <memory>
29#include <string>
30#include <vector>
31
37namespace utility
38{
47 class File
48 {
49 public:
55 enum class Seek {
56 Unknown = 0,
57 SET = 0,
58 CUR = 1,
59 END = 2
60 };
61
67 File(const std::string &path, const std::string &content);
68
74 File(const std::string &path, const std::vector<std::byte> &content);
75
80
90 size_t write(const void *ptr, size_t size, size_t nmemb);
91
99 size_t read(void *ptr, size_t size, size_t count);
100
108 size_t read(std::string &str, size_t size, size_t count);
109
116 int seek(long offset, Seek whence);
117
122 size_t tell() const;
123
128 [[nodiscard]] inline const std::vector<std::byte> &data() const
129 {
130 return _content;
131 }
132
140 [[nodiscard]] inline std::string content() const
141 {
142 return std::string(reinterpret_cast<const char *>(_content.data()),
143 _content.size());
144 }
145
154 [[nodiscard]] inline const std::string &path() const
155 {
156 return _path;
157 }
158
168 [[nodiscard]] inline size_t size() const
169 {
170 return _content.size();
171 }
172
184 void clear();
185
191 size_t remove(size_t count);
192
193 protected:
197 std::vector<std::byte> _content;
198
202 std::string _path;
203
207 size_t _pos = 0;
208 };
209} // namespace utility
The File class represents a I/O file asset.
Definition file.hpp:48
std::string content() const
Returns the content of the file as a string view.
Definition file.hpp:140
size_t _pos
Current read/write cursor position in the content buffer.
Definition file.hpp:207
const std::string & path() const
Returns the path of the file.
Definition file.hpp:154
size_t read(void *ptr, size_t size, size_t count)
Reads data from the file.
const std::vector< std::byte > & data() const
Returns the binary content of the file.
Definition file.hpp:128
~File()
Destructs the File object.
size_t remove(size_t count)
Removes a number of elements from the current position.
Seek
The Seek enum represents the different seek modes for file operations.
Definition file.hpp:55
@ SET
‍Unknown seek mode
@ END
‍Seek from the current position in the file
@ CUR
‍Seek from the beginning of the file
std::string _path
Path to the file.
Definition file.hpp:202
std::vector< std::byte > _content
In-memory content of the asset.
Definition file.hpp:197
size_t size() const
Returns the size of the file content.
Definition file.hpp:168
int seek(long offset, Seek whence)
Seeks to a specific position in the file.
void clear()
Clears the content of the file.
size_t write(const void *ptr, size_t size, size_t nmemb)
Writes data to the file.
size_t tell() const
Returns the current position in the file.
The utility namespace contains classes and functions for the utility project.
Definition cache.hpp:34