mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-10-05 07:39:01 +00:00
# Conflicts: # CMakeLists.txt # CMakeSettings.json # Data/Sys/GameSettings/GLR.ini # Data/Sys/GameSettings/HA9.ini # Data/Sys/GameSettings/MB3.ini # Data/Sys/GameSettings/MBA.ini # Data/Sys/GameSettings/MCV.ini # Data/Sys/GameSettings/MCY.ini # Data/Sys/GameSettings/NAK.ini # Data/Sys/GameSettings/NAL.ini # Data/Sys/GameSettings/NAT.ini # Data/Sys/GameSettings/R8P.ini # Data/Sys/GameSettings/R9I.ini # Data/Sys/GameSettings/REF.ini # Data/Sys/GameSettings/RES.ini # Data/Sys/GameSettings/RMHE08.ini # Data/Sys/GameSettings/RMHP08.ini # Data/Sys/GameSettings/SE2.ini # Data/Sys/GameSettings/WW2.ini # Data/Sys/GameSettings/WW3.ini # Data/Sys/GameSettings/WWI.ini # Externals/Bochs_disasm/Bochs_disasm.vcxproj # Externals/FreeSurround/FreeSurround.vcxproj # Externals/LZO/LZO.vcxproj # Externals/SFML/build/vc2010/SFML_Network.vcxproj # Externals/bzip2/bzip2.vcxproj # Externals/cpp-optparse/cpp-optparse.vcxproj # Externals/cubeb/msvc/cubeb.vcxproj # Externals/curl/curl.vcxproj # Externals/curl/lib/CMakeLists.txt # Externals/discord-rpc/src/discord-rpc.vcxproj # Externals/ed25519/ed25519.vcxproj # Externals/enet/enet.vcxproj # Externals/glslang/glslang.vcxproj # Externals/imgui/imgui.vcxproj # Externals/liblzma/liblzma.vcxproj # Externals/libpng/png/png.vcxproj # Externals/libusb/libusb_static_2013.vcxproj # Externals/mbedtls/mbedTLS.vcxproj # Externals/miniupnpc/miniupnpc.vcxproj # Externals/minizip/minizip.vcxproj # Externals/picojson/picojson.vcxproj # Externals/pugixml/pugixml.vcxproj # Externals/soundtouch/SoundTouch.vcxproj # Externals/xxhash/xxhash.vcxproj # Externals/zlib/zlib.vcxproj # Externals/zstd/zstd.vcxproj # Languages/Languages.vcxproj # Source/Android/jni/MainAndroid.cpp # Source/Core/AudioCommon/AudioCommon.vcxproj # Source/Core/Common/Common.vcxproj # Source/Core/Common/Common.vcxproj.filters # Source/Core/Common/Logging/Log.h # Source/Core/Common/StringUtil.cpp # Source/Core/Core/CMakeLists.txt # Source/Core/Core/ConfigManager.cpp # Source/Core/Core/ConfigManager.h # Source/Core/Core/Core.vcxproj # Source/Core/Core/Core.vcxproj.filters # Source/Core/Core/HotkeyManager.cpp # Source/Core/Core/State.cpp # Source/Core/DiscIO/DiscIO.vcxproj # Source/Core/DolphinNoGUI/DolphinNoGUI.vcxproj # Source/Core/DolphinQt/Config/Graphics/GeneralWidget.cpp # Source/Core/DolphinQt/DolphinQt.vcxproj # Source/Core/InputCommon/InputCommon.vcxproj # Source/Core/InputCommon/InputCommon.vcxproj.filters # Source/Core/UICommon/UICommon.vcxproj # Source/Core/UpdaterCommon/UpdaterCommon.vcxproj # Source/Core/VideoBackends/D3D/D3D.vcxproj # Source/Core/VideoBackends/D3D12/D3D12.vcxproj # Source/Core/VideoBackends/D3DCommon/D3DCommon.vcxproj # Source/Core/VideoBackends/Null/Null.vcxproj # Source/Core/VideoBackends/OGL/OGL.vcxproj # Source/Core/VideoBackends/Software/Software.vcxproj # Source/Core/VideoBackends/Vulkan/Vulkan.vcxproj # Source/Core/VideoCommon/OnScreenDisplay.cpp # Source/Core/VideoCommon/OnScreenDisplay.h # Source/Core/VideoCommon/VideoCommon.vcxproj # Source/Core/WinUpdater/WinUpdater.vcxproj # Source/DSPTool/DSPTool.vcxproj # Source/PCH/pch.vcxproj # Source/UnitTests/UnitTests.vcxproj
102 lines
2.4 KiB
C++
102 lines
2.4 KiB
C++
// Copyright 2008 Dolphin Emulator Project
|
|
// Licensed under GPLv2+
|
|
// Refer to the license.txt file included.
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "Common/CommonTypes.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:
|
|
IOFile();
|
|
IOFile(std::FILE* file);
|
|
IOFile(const std::string& filename, const char openmode[]);
|
|
IOFile(const std::string& filename, const char openmode[], int shflag);
|
|
|
|
~IOFile();
|
|
|
|
IOFile(const IOFile&) = delete;
|
|
IOFile& operator=(const IOFile&) = delete;
|
|
|
|
IOFile(IOFile&& other) noexcept;
|
|
IOFile& operator=(IOFile&& other) noexcept;
|
|
|
|
void Swap(IOFile& other) noexcept;
|
|
|
|
bool Open(const std::string& filename, const char openmode[]);
|
|
|
|
bool OpenShared(const std::string& filename, const char openmode[], int shflag);
|
|
|
|
bool Close();
|
|
|
|
template <typename T>
|
|
bool ReadArray(T* elements, size_t count, size_t* num_read = nullptr)
|
|
{
|
|
size_t read_count = 0;
|
|
if (!IsOpen() || count != (read_count = std::fread(elements, sizeof(T), count, m_file)))
|
|
m_good = false;
|
|
|
|
if (num_read)
|
|
*num_read = read_count;
|
|
|
|
return m_good;
|
|
}
|
|
|
|
template <typename T>
|
|
bool WriteArray(const T* elements, size_t count)
|
|
{
|
|
if (!IsOpen() || count != std::fwrite(elements, sizeof(T), count, 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 WriteString(std::string_view str) { return WriteBytes(str.data(), str.size()); }
|
|
|
|
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() const;
|
|
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
|