summaryrefslogtreecommitdiff
path: root/src/common/file_util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/file_util.h')
-rw-r--r--src/common/file_util.h66
1 files changed, 35 insertions, 31 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 5bc7fbf7c..026c84d94 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -150,6 +150,34 @@ size_t ReadFileToString(bool text_file, const char* filename, std::string& str);
150void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, 150void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
151 std::array<char, 4>& extension); 151 std::array<char, 4>& extension);
152 152
153// Splits the path on '/' or '\' and put the components into a vector
154// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" }
155std::vector<std::string> SplitPathComponents(const std::string& filename);
156
157// Gets all of the text up to the last '/' or '\' in the path.
158std::string GetParentPath(const std::string& path);
159
160// Gets all of the text after the first '/' or '\' in the path.
161std::string GetPathWithoutTop(std::string path);
162
163// Gets the filename of the path
164std::string GetFilename(std::string path);
165
166// Gets the extension of the filename
167std::string GetExtensionFromFilename(const std::string& name);
168
169// Removes the final '/' or '\' if one exists
170std::string RemoveTrailingSlash(const std::string& path);
171
172// Creates a new vector containing indices [first, last) from the original.
173template <typename T>
174std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t last) {
175 if (first >= last)
176 return {};
177 last = std::min<size_t>(last, vector.size());
178 return std::vector<T>(vector.begin() + first, vector.begin() + first + last);
179}
180
153// simple wrapper for cstdlib file functions to 181// simple wrapper for cstdlib file functions to
154// hopefully will make error checking easier 182// hopefully will make error checking easier
155// and make forgetting an fclose() harder 183// and make forgetting an fclose() harder
@@ -172,41 +200,27 @@ public:
172 bool Close(); 200 bool Close();
173 201
174 template <typename T> 202 template <typename T>
175 size_t ReadArray(T* data, size_t length) { 203 size_t ReadArray(T* data, size_t length) const {
176 static_assert(std::is_trivially_copyable<T>(), 204 static_assert(std::is_trivially_copyable<T>(),
177 "Given array does not consist of trivially copyable objects"); 205 "Given array does not consist of trivially copyable objects");
178 206
179 if (!IsOpen()) { 207 if (!IsOpen())
180 m_good = false;
181 return -1; 208 return -1;
182 }
183
184 size_t items_read = std::fread(data, sizeof(T), length, m_file);
185 if (items_read != length)
186 m_good = false;
187 209
188 return items_read; 210 return std::fread(data, sizeof(T), length, m_file);
189 } 211 }
190 212
191 template <typename T> 213 template <typename T>
192 size_t WriteArray(const T* data, size_t length) { 214 size_t WriteArray(const T* data, size_t length) {
193 static_assert(std::is_trivially_copyable<T>(), 215 static_assert(std::is_trivially_copyable<T>(),
194 "Given array does not consist of trivially copyable objects"); 216 "Given array does not consist of trivially copyable objects");
195 217 if (!IsOpen())
196 if (!IsOpen()) {
197 m_good = false;
198 return -1; 218 return -1;
199 } 219 return std::fwrite(data, sizeof(T), length, m_file);
200
201 size_t items_written = std::fwrite(data, sizeof(T), length, m_file);
202 if (items_written != length)
203 m_good = false;
204
205 return items_written;
206 } 220 }
207 221
208 template <typename T> 222 template <typename T>
209 size_t ReadBytes(T* data, size_t length) { 223 size_t ReadBytes(T* data, size_t length) const {
210 static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); 224 static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable");
211 return ReadArray(reinterpret_cast<char*>(data), length); 225 return ReadArray(reinterpret_cast<char*>(data), length);
212 } 226 }
@@ -231,15 +245,7 @@ public:
231 return nullptr != m_file; 245 return nullptr != m_file;
232 } 246 }
233 247
234 // m_good is set to false when a read, write or other function fails 248 bool Seek(s64 off, int origin) const;
235 bool IsGood() const {
236 return m_good;
237 }
238 explicit operator bool() const {
239 return IsGood();
240 }
241
242 bool Seek(s64 off, int origin);
243 u64 Tell() const; 249 u64 Tell() const;
244 u64 GetSize() const; 250 u64 GetSize() const;
245 bool Resize(u64 size); 251 bool Resize(u64 size);
@@ -247,13 +253,11 @@ public:
247 253
248 // clear error state 254 // clear error state
249 void Clear() { 255 void Clear() {
250 m_good = true;
251 std::clearerr(m_file); 256 std::clearerr(m_file);
252 } 257 }
253 258
254private: 259private:
255 std::FILE* m_file = nullptr; 260 std::FILE* m_file = nullptr;
256 bool m_good = true;
257}; 261};
258 262
259} // namespace FileUtil 263} // namespace FileUtil