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.h63
1 files changed, 31 insertions, 32 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 9bb3c4109..5bc7fbf7c 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -150,31 +150,6 @@ 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 prior to the last '/' or '\' in the path.
158std::string GetParentPath(const std::string& path);
159
160// Gets the filename of the path
161std::string GetFilename(std::string path);
162
163// Gets the extension of the filename
164std::string GetExtensionFromFilename(const std::string& name);
165
166// Removes the final '/' or '\' if one exists
167std::string RemoveTrailingSlash(const std::string& path);
168
169// Creates a new vector containing indices [first, last) from the original.
170template <typename T>
171std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t last) {
172 if (first >= last)
173 return {};
174 last = std::min<size_t>(last, vector.size());
175 return std::vector<T>(vector.begin() + first, vector.begin() + first + last);
176}
177
178// simple wrapper for cstdlib file functions to 153// simple wrapper for cstdlib file functions to
179// hopefully will make error checking easier 154// hopefully will make error checking easier
180// and make forgetting an fclose() harder 155// and make forgetting an fclose() harder
@@ -197,27 +172,41 @@ public:
197 bool Close(); 172 bool Close();
198 173
199 template <typename T> 174 template <typename T>
200 size_t ReadArray(T* data, size_t length) const { 175 size_t ReadArray(T* data, size_t length) {
201 static_assert(std::is_trivially_copyable<T>(), 176 static_assert(std::is_trivially_copyable<T>(),
202 "Given array does not consist of trivially copyable objects"); 177 "Given array does not consist of trivially copyable objects");
203 178
204 if (!IsOpen()) 179 if (!IsOpen()) {
180 m_good = false;
205 return -1; 181 return -1;
182 }
206 183
207 return std::fread(data, sizeof(T), length, m_file); 184 size_t items_read = std::fread(data, sizeof(T), length, m_file);
185 if (items_read != length)
186 m_good = false;
187
188 return items_read;
208 } 189 }
209 190
210 template <typename T> 191 template <typename T>
211 size_t WriteArray(const T* data, size_t length) { 192 size_t WriteArray(const T* data, size_t length) {
212 static_assert(std::is_trivially_copyable<T>(), 193 static_assert(std::is_trivially_copyable<T>(),
213 "Given array does not consist of trivially copyable objects"); 194 "Given array does not consist of trivially copyable objects");
214 if (!IsOpen()) 195
196 if (!IsOpen()) {
197 m_good = false;
215 return -1; 198 return -1;
216 return std::fwrite(data, sizeof(T), length, m_file); 199 }
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;
217 } 206 }
218 207
219 template <typename T> 208 template <typename T>
220 size_t ReadBytes(T* data, size_t length) const { 209 size_t ReadBytes(T* data, size_t length) {
221 static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); 210 static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable");
222 return ReadArray(reinterpret_cast<char*>(data), length); 211 return ReadArray(reinterpret_cast<char*>(data), length);
223 } 212 }
@@ -242,7 +231,15 @@ public:
242 return nullptr != m_file; 231 return nullptr != m_file;
243 } 232 }
244 233
245 bool Seek(s64 off, int origin) const; 234 // m_good is set to false when a read, write or other function fails
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);
246 u64 Tell() const; 243 u64 Tell() const;
247 u64 GetSize() const; 244 u64 GetSize() const;
248 bool Resize(u64 size); 245 bool Resize(u64 size);
@@ -250,11 +247,13 @@ public:
250 247
251 // clear error state 248 // clear error state
252 void Clear() { 249 void Clear() {
250 m_good = true;
253 std::clearerr(m_file); 251 std::clearerr(m_file);
254 } 252 }
255 253
256private: 254private:
257 std::FILE* m_file = nullptr; 255 std::FILE* m_file = nullptr;
256 bool m_good = true;
258}; 257};
259 258
260} // namespace FileUtil 259} // namespace FileUtil