diff options
Diffstat (limited to '')
| -rw-r--r-- | src/common/file_util.h | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h index 2f13d0b6b..24c1e413c 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -143,8 +143,9 @@ const std::string& GetExeDirectory(); | |||
| 143 | std::string AppDataRoamingDirectory(); | 143 | std::string AppDataRoamingDirectory(); |
| 144 | #endif | 144 | #endif |
| 145 | 145 | ||
| 146 | size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename); | 146 | std::size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename); |
| 147 | size_t ReadFileToString(bool text_file, const char* filename, std::string& str); | 147 | |
| 148 | std::size_t ReadFileToString(bool text_file, const char* filename, std::string& str); | ||
| 148 | 149 | ||
| 149 | /** | 150 | /** |
| 150 | * Splits the filename into 8.3 format | 151 | * Splits the filename into 8.3 format |
| @@ -177,10 +178,10 @@ std::string_view RemoveTrailingSlash(std::string_view path); | |||
| 177 | 178 | ||
| 178 | // Creates a new vector containing indices [first, last) from the original. | 179 | // Creates a new vector containing indices [first, last) from the original. |
| 179 | template <typename T> | 180 | template <typename T> |
| 180 | std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t last) { | 181 | std::vector<T> SliceVector(const std::vector<T>& vector, std::size_t first, std::size_t last) { |
| 181 | if (first >= last) | 182 | if (first >= last) |
| 182 | return {}; | 183 | return {}; |
| 183 | last = std::min<size_t>(last, vector.size()); | 184 | last = std::min<std::size_t>(last, vector.size()); |
| 184 | return std::vector<T>(vector.begin() + first, vector.begin() + first + last); | 185 | return std::vector<T>(vector.begin() + first, vector.begin() + first + last); |
| 185 | } | 186 | } |
| 186 | 187 | ||
| @@ -213,47 +214,47 @@ public: | |||
| 213 | bool Close(); | 214 | bool Close(); |
| 214 | 215 | ||
| 215 | template <typename T> | 216 | template <typename T> |
| 216 | size_t ReadArray(T* data, size_t length) const { | 217 | std::size_t ReadArray(T* data, std::size_t length) const { |
| 217 | static_assert(std::is_trivially_copyable_v<T>, | 218 | static_assert(std::is_trivially_copyable_v<T>, |
| 218 | "Given array does not consist of trivially copyable objects"); | 219 | "Given array does not consist of trivially copyable objects"); |
| 219 | 220 | ||
| 220 | if (!IsOpen()) { | 221 | if (!IsOpen()) { |
| 221 | return std::numeric_limits<size_t>::max(); | 222 | return std::numeric_limits<std::size_t>::max(); |
| 222 | } | 223 | } |
| 223 | 224 | ||
| 224 | return std::fread(data, sizeof(T), length, m_file); | 225 | return std::fread(data, sizeof(T), length, m_file); |
| 225 | } | 226 | } |
| 226 | 227 | ||
| 227 | template <typename T> | 228 | template <typename T> |
| 228 | size_t WriteArray(const T* data, size_t length) { | 229 | std::size_t WriteArray(const T* data, std::size_t length) { |
| 229 | static_assert(std::is_trivially_copyable_v<T>, | 230 | static_assert(std::is_trivially_copyable_v<T>, |
| 230 | "Given array does not consist of trivially copyable objects"); | 231 | "Given array does not consist of trivially copyable objects"); |
| 231 | if (!IsOpen()) { | 232 | if (!IsOpen()) { |
| 232 | return std::numeric_limits<size_t>::max(); | 233 | return std::numeric_limits<std::size_t>::max(); |
| 233 | } | 234 | } |
| 234 | 235 | ||
| 235 | return std::fwrite(data, sizeof(T), length, m_file); | 236 | return std::fwrite(data, sizeof(T), length, m_file); |
| 236 | } | 237 | } |
| 237 | 238 | ||
| 238 | template <typename T> | 239 | template <typename T> |
| 239 | size_t ReadBytes(T* data, size_t length) const { | 240 | std::size_t ReadBytes(T* data, std::size_t length) const { |
| 240 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); | 241 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); |
| 241 | return ReadArray(reinterpret_cast<char*>(data), length); | 242 | return ReadArray(reinterpret_cast<char*>(data), length); |
| 242 | } | 243 | } |
| 243 | 244 | ||
| 244 | template <typename T> | 245 | template <typename T> |
| 245 | size_t WriteBytes(const T* data, size_t length) { | 246 | std::size_t WriteBytes(const T* data, std::size_t length) { |
| 246 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); | 247 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); |
| 247 | return WriteArray(reinterpret_cast<const char*>(data), length); | 248 | return WriteArray(reinterpret_cast<const char*>(data), length); |
| 248 | } | 249 | } |
| 249 | 250 | ||
| 250 | template <typename T> | 251 | template <typename T> |
| 251 | size_t WriteObject(const T& object) { | 252 | std::size_t WriteObject(const T& object) { |
| 252 | static_assert(!std::is_pointer_v<T>, "WriteObject arguments must not be a pointer"); | 253 | static_assert(!std::is_pointer_v<T>, "WriteObject arguments must not be a pointer"); |
| 253 | return WriteArray(&object, 1); | 254 | return WriteArray(&object, 1); |
| 254 | } | 255 | } |
| 255 | 256 | ||
| 256 | size_t WriteString(const std::string& str) { | 257 | std::size_t WriteString(const std::string& str) { |
| 257 | return WriteArray(str.c_str(), str.length()); | 258 | return WriteArray(str.c_str(), str.length()); |
| 258 | } | 259 | } |
| 259 | 260 | ||