Debian Codegraph @

Source/Core/Common/IOFile.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <cstddef>
8 #include <cstdio>
9 #include <string>
10 #include <string_view>
11
12 #include "Common/CommonTypes.h"
13
14 namespace File
15 {
16 enum class SeekOrigin
17 {
18 Begin,
19 Current,
20 End,
21 };
22
23 enum class SharedAccess
24 {
25 Default,
26 Read,
27 };
28
29 // simple wrapper for cstdlib file functions to
30 // hopefully will make error checking easier
31 // and make forgetting an fclose() harder
32 class IOFile
33 {
34 public:
35 IOFile();
36 IOFile(std::FILE* file);
37 IOFile(const std::string& filename, const char openmode[],
38 SharedAccess sh = SharedAccess::Default);
39
40 ~IOFile();
41
42 IOFile(const IOFile&) = delete;
43 IOFile& operator=(const IOFile&) = delete;
44
45 IOFile(IOFile&& other) noexcept;
46 IOFile& operator=(IOFile&& other) noexcept;
47
48 void Swap(IOFile& other) noexcept;
49
50 bool Open(const std::string& filename, const char openmode[],
51 SharedAccess sh = SharedAccess::Default);
52 bool Close();
53
54 template <typename T>
55 requires(std::is_trivially_copyable_v<T>)
56 bool ReadArray(T* elements, size_t count, size_t* num_read = nullptr)
57 {
58 size_t read_count = 0;
59 if (!IsOpen() || count != (read_count = std::fread(elements, sizeof(T), count, m_file)))
60 m_good = false;
61
62 if (num_read)
63 *num_read = read_count;
64
65 return m_good;
66 }
67
68 template <typename T>
69 requires(std::is_trivially_copyable_v<T>)
70 bool WriteArray(const T* elements, size_t count)
71 {
72 if (!IsOpen() || count != std::fwrite(elements, sizeof(T), count, m_file))
73 m_good = false;
74
75 return m_good;
76 }
77
78 template <typename T, std::size_t N>
79 bool ReadArray(std::array<T, N>* elements, size_t* num_read = nullptr)
80 {
81 return ReadArray(elements->data(), elements->size(), num_read);
82 }
83
84 template <typename T, std::size_t N>
85 bool WriteArray(const std::array<T, N>& elements)
86 {
87 return WriteArray(elements.data(), elements.size());
88 }
89
90 bool ReadBytes(void* data, size_t length) { return ReadArray(static_cast<char*>(data), length); }
91
92 bool WriteBytes(const void* data, size_t length)
93 {
94 return WriteArray(static_cast<const char*>(data), length);
95 }
96
97 bool WriteString(std::string_view str) { return WriteBytes(str.data(), str.size()); }
98
99 bool IsOpen() const { return nullptr != m_file; }
100 // m_good is set to false when a read, write or other function fails
101 bool IsGood() const { return m_good; }
102 explicit operator bool() const { return IsGood() && IsOpen(); }
103 std::FILE* GetHandle() { return m_file; }
104 void SetHandle(std::FILE* file);
105
106 bool Seek(s64 offset, SeekOrigin origin);
107 u64 Tell() const;
108 u64 GetSize() const;
109 bool Resize(u64 size);
110 bool Flush();
111
112 // clear error state
113 void ClearError()
114 {
115 m_good = true;
116 if (IsOpen())
117 std::clearerr(m_file);
118 }
119
120 private:
121 std::FILE* m_file;
122 bool m_good;
123 };
124
125 } // namespace File