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_asset.cpp
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#include "utility/system_io/file.hpp"
24
25#include <algorithm>
26#include <cstddef>
27#include <limits>
28#include <vector>
29
30utility::File::File(const std::string &path, const std::string &content)
31 : _path(path)
32{
33 _content.reserve(content.size());
34 for (char c: content) {
35 _content.push_back(static_cast<std::byte>(c));
36 }
37}
38
39utility::File::File(const std::string &path,
40 const std::vector<std::byte> &content)
41 : _content(content)
42 , _path(path)
43{
44}
45
46utility::File::~File() = default;
47
48size_t utility::File::write(const void *ptr, size_t size, size_t nmemb)
49{
50 if (ptr == nullptr || size == 0 || nmemb == 0) {
51 return 0;
52 }
53
54 if (size > std::numeric_limits<size_t>::max() / nmemb) {
55 return 0;
56 }
57
58 if (_pos > _content.size()) {
59 _pos = _content.size();
60 }
61
62 const size_t bytesToWrite = size * nmemb;
63 const auto *bytes = static_cast<const std::byte *>(ptr);
64 const size_t lenBefore = _content.size();
65
66 _content.insert(_content.begin() + static_cast<std::ptrdiff_t>(_pos), bytes,
67 bytes + bytesToWrite);
68 _pos += bytesToWrite;
69 return (_content.size() - lenBefore) / size;
70}
71
72size_t utility::File::read(void *ptr, size_t size, size_t count)
73{
74 if (ptr == nullptr || size == 0 || count == 0 || _pos >= _content.size()) {
75 return 0;
76 }
77
78 if (size > std::numeric_limits<size_t>::max() / count) {
79 return 0;
80 }
81
82 size_t toRead = size * count;
83 toRead = std::min(toRead, _content.size() - _pos);
84 std::memcpy(ptr, _content.data() + _pos, toRead);
85 _pos += toRead;
86 return toRead / size;
87}
88
89size_t utility::File::read(std::string &str, size_t size, size_t count)
90{
91 if (size == 0 || count == 0 || _pos >= _content.size()) {
92 str.clear();
93 return 0;
94 }
95
96 if (size > std::numeric_limits<size_t>::max() / count) {
97 str.clear();
98 return 0;
99 }
100
101 size_t toRead = std::min(size * count, _content.size() - _pos);
102 str.resize(toRead);
103 std::memcpy(str.data(), _content.data() + _pos, toRead);
104 _pos += toRead;
105 return toRead / size;
106}
107
108int utility::File::seek(long offset, Seek whence)
109{
110 long newPos = 0;
111
112 switch (whence) {
113 case File::Seek::SET:
114 newPos = offset;
115 break;
116 case File::Seek::CUR:
117 newPos = static_cast<long>(_pos) + offset;
118 break;
119 case File::Seek::END:
120 newPos = static_cast<long>(_content.size()) + offset;
121 break;
122 default:
123 return -1;
124 }
125
126 if (newPos < 0) {
127 return -1;
128 }
129
130 _pos = std::min(static_cast<size_t>(newPos), _content.size());
131 return 0;
132}
133
135{
136 return _pos;
137}
138
140{
141 _content.clear();
142 _pos = 0;
143}
144
145size_t utility::File::remove(size_t count)
146{
147 if (count == 0 || _pos >= _content.size()) {
148 return 0;
149 }
150
151 size_t toRemove = std::min(count, _content.size() - _pos);
152 _content.erase(_content.begin() + static_cast<std::ptrdiff_t>(_pos),
153 _content.begin()
154 + static_cast<std::ptrdiff_t>(_pos + toRemove));
155 return toRemove;
156}
std::string content() const
Returns the content of the file as a string view.
Definition file.hpp:140
File(const std::string &path, const std::string &content)
Constructs a File object with its content and size.
size_t read(void *ptr, size_t size, size_t count)
Reads data from the file.
~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::vector< std::byte > _content
In-memory content of the asset.
Definition file.hpp:197
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.