summaryrefslogtreecommitdiff
path: root/Source/Core/Common/File.h
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2017-01-15 21:46:32 +0100
committerJosJuice <josjuice@gmail.com>2017-06-15 21:33:50 +0200
commitf09ceaa735080b8cde5f9102e7f061530d9b474e (patch)
tree5dfc15fa2b363db2165bca24d5b7e97bcdfcb4a8 /Source/Core/Common/File.h
parent67dbbc67c0b3fa09e4f5c94ea67a83190c4e1a5e (diff)
Move IOFile to a separate file
Reduces the number of files that need to be recompiled when making changes to FileUtil.h.
Diffstat (limited to 'Source/Core/Common/File.h')
-rw-r--r--Source/Core/Common/File.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/Source/Core/Common/File.h b/Source/Core/Common/File.h
new file mode 100644
index 0000000000..2b13da6bd6
--- /dev/null
+++ b/Source/Core/Common/File.h
@@ -0,0 +1,93 @@
+// Copyright 2008 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <cstddef>
+#include <cstdio>
+#include <string>
+
+#include "Common/CommonTypes.h"
+#include "Common/NonCopyable.h"
+
+namespace File
+{
+// simple wrapper for cstdlib file functions to
+// hopefully will make error checking easier
+// and make forgetting an fclose() harder
+class IOFile : public NonCopyable
+{
+public:
+ IOFile();
+ IOFile(std::FILE* file);
+ IOFile(const std::string& filename, const char openmode[]);
+
+ ~IOFile();
+
+ IOFile(IOFile&& other) noexcept;
+ IOFile& operator=(IOFile&& other) noexcept;
+
+ void Swap(IOFile& other) noexcept;
+
+ bool Open(const std::string& filename, const char openmode[]);
+ bool Close();
+
+ template <typename T>
+ bool ReadArray(T* data, size_t length, size_t* pReadBytes = nullptr)
+ {
+ size_t read_bytes = 0;
+ if (!IsOpen() || length != (read_bytes = std::fread(data, sizeof(T), length, m_file)))
+ m_good = false;
+
+ if (pReadBytes)
+ *pReadBytes = read_bytes;
+
+ return m_good;
+ }
+
+ template <typename T>
+ bool WriteArray(const T* data, size_t length)
+ {
+ if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file))
+ m_good = false;
+
+ return m_good;
+ }
+
+ bool ReadBytes(void* data, size_t length)
+ {
+ return ReadArray(reinterpret_cast<char*>(data), length);
+ }
+
+ bool WriteBytes(const void* data, size_t length)
+ {
+ return WriteArray(reinterpret_cast<const char*>(data), length);
+ }
+
+ bool IsOpen() const { 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() && IsOpen(); }
+ std::FILE* GetHandle() { return m_file; }
+ void SetHandle(std::FILE* file);
+
+ bool Seek(s64 off, int origin);
+ u64 Tell() const;
+ u64 GetSize();
+ bool Resize(u64 size);
+ bool Flush();
+
+ // clear error state
+ void Clear()
+ {
+ m_good = true;
+ std::clearerr(m_file);
+ }
+
+private:
+ std::FILE* m_file;
+ bool m_good;
+};
+
+} // namespace File