summaryrefslogtreecommitdiffstats
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);
void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
std::array<char, 4>& extension);
+// Splits the path on '/' or '\' and put the components into a vector
+// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" }
+std::vector<std::string> SplitPathComponents(const std::string& filename);
+
+// Gets all of the text up to the last '/' or '\' in the path.
+std::string GetParentPath(const std::string& path);
+
+// Gets all of the text after the first '/' or '\' in the path.
+std::string GetPathWithoutTop(std::string path);
+
+// Gets the filename of the path
+std::string GetFilename(std::string path);
+
+// Gets the extension of the filename
+std::string GetExtensionFromFilename(const std::string& name);
+
+// Removes the final '/' or '\' if one exists
+std::string RemoveTrailingSlash(const std::string& path);
+
+// Creates a new vector containing indices [first, last) from the original.
+template <typename T>
+std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t last) {
+ if (first >= last)
+ return {};
+ last = std::min<size_t>(last, vector.size());
+ return std::vector<T>(vector.begin() + first, vector.begin() + first + last);
+}
+
// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier
// and make forgetting an fclose() harder
@@ -172,41 +200,27 @@ public:
bool Close();
template <typename T>
- size_t ReadArray(T* data, size_t length) {
+ size_t ReadArray(T* data, size_t length) const {
static_assert(std::is_trivially_copyable<T>(),
"Given array does not consist of trivially copyable objects");
- if (!IsOpen()) {
- m_good = false;
+ if (!IsOpen())
return -1;
- }
-
- size_t items_read = std::fread(data, sizeof(T), length, m_file);
- if (items_read != length)
- m_good = false;
- return items_read;
+ return std::fread(data, sizeof(T), length, m_file);
}
template <typename T>
size_t WriteArray(const T* data, size_t length) {
static_assert(std::is_trivially_copyable<T>(),
"Given array does not consist of trivially copyable objects");
-
- if (!IsOpen()) {
- m_good = false;
+ if (!IsOpen())
return -1;
- }
-
- size_t items_written = std::fwrite(data, sizeof(T), length, m_file);
- if (items_written != length)
- m_good = false;
-
- return items_written;
+ return std::fwrite(data, sizeof(T), length, m_file);
}
template <typename T>
- size_t ReadBytes(T* data, size_t length) {
+ size_t ReadBytes(T* data, size_t length) const {
static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable");
return ReadArray(reinterpret_cast<char*>(data), length);
}
@@ -231,15 +245,7 @@ public:
return nullptr != m_file;
}
- // m_good is set to false when a read, write or other function fails
- bool IsGood() const {
- return m_good;
- }
- explicit operator bool() const {
- return IsGood();
- }
-
- bool Seek(s64 off, int origin);
+ bool Seek(s64 off, int origin) const;
u64 Tell() const;
u64 GetSize() const;
bool Resize(u64 size);
@@ -247,13 +253,11 @@ public:
// clear error state
void Clear() {
- m_good = true;
std::clearerr(m_file);
}
private:
std::FILE* m_file = nullptr;
- bool m_good = true;
};
} // namespace FileUtil