mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-06-21 07:25:39 -04:00
Common: Error/FileSystem backports
This commit is contained in:
@ -1,182 +1,140 @@
|
||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
#include "error.h"
|
||||
#include "string_util.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
|
||||
// Platform-specific includes
|
||||
#include "fmt/format.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include "windows_headers.h"
|
||||
static_assert(std::is_same<DWORD, unsigned long>::value, "DWORD is unsigned long");
|
||||
static_assert(std::is_same<HRESULT, long>::value, "HRESULT is long");
|
||||
#endif
|
||||
|
||||
namespace Common {
|
||||
Error::Error() = default;
|
||||
|
||||
Error::Error() : m_type(Type::None)
|
||||
{
|
||||
m_error.none = 0;
|
||||
}
|
||||
Error::Error(const Error& c) = default;
|
||||
|
||||
Error::Error(const Error& c)
|
||||
{
|
||||
m_type = c.m_type;
|
||||
std::memcpy(&m_error, &c.m_error, sizeof(m_error));
|
||||
m_code_string.AppendString(c.m_code_string);
|
||||
m_message.AppendString(c.m_message);
|
||||
}
|
||||
Error::Error(Error&& e) = default;
|
||||
|
||||
Error::~Error() = default;
|
||||
|
||||
void Error::Clear()
|
||||
{
|
||||
m_type = Type::None;
|
||||
m_error.none = 0;
|
||||
m_code_string.Clear();
|
||||
m_message.Clear();
|
||||
m_description = {};
|
||||
}
|
||||
|
||||
void Error::SetErrno(int err)
|
||||
{
|
||||
m_type = Type::Errno;
|
||||
m_error.errno_f = err;
|
||||
|
||||
m_code_string.Format("%i", err);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
strerror_s(m_message.GetWriteableCharArray(), m_message.GetBufferSize(), err);
|
||||
m_message.UpdateSize();
|
||||
#else
|
||||
const char* message = std::strerror(err);
|
||||
if (message)
|
||||
m_message = message;
|
||||
char buf[128];
|
||||
if (strerror_s(buf, sizeof(buf), err) != 0)
|
||||
m_description = fmt::format("errno {}: {}", err, buf);
|
||||
else
|
||||
m_message = StaticString("<Could not get error message>");
|
||||
#endif
|
||||
}
|
||||
|
||||
void Error::SetSocket(int err)
|
||||
{
|
||||
// Socket errors are win32 errors on windows
|
||||
#ifdef _WIN32
|
||||
SetWin32(err);
|
||||
m_description = fmt::format("errno {}: <Could not get error message>", err);
|
||||
#else
|
||||
SetErrno(err);
|
||||
const char* buf = std::strerror(err);
|
||||
if (buf)
|
||||
m_description = fmt::format("errno {}: {}", err, buf);
|
||||
else
|
||||
m_description = fmt::format("errno {}: <Could not get error message>", err);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Error::SetMessage(const char* msg)
|
||||
void Error::SetErrno(Error* errptr, int err)
|
||||
{
|
||||
m_type = Type::User;
|
||||
m_error.user = 0;
|
||||
m_code_string.Clear();
|
||||
m_message = msg;
|
||||
if (errptr)
|
||||
errptr->SetErrno(err);
|
||||
}
|
||||
|
||||
void Error::SetUser(int err, const char* msg)
|
||||
void Error::SetString(std::string description)
|
||||
{
|
||||
m_type = Type::User;
|
||||
m_error.user = err;
|
||||
m_code_string.Format("%d", err);
|
||||
m_message = msg;
|
||||
m_description = std::move(description);
|
||||
}
|
||||
|
||||
void Error::SetUser(const char* code, const char* message)
|
||||
void Error::SetString(Error* errptr, std::string description)
|
||||
{
|
||||
m_type = Type::User;
|
||||
m_error.user = 0;
|
||||
m_code_string = code;
|
||||
m_message = message;
|
||||
}
|
||||
|
||||
void Error::SetUserFormatted(int err, const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
m_type = Type::User;
|
||||
m_error.user = err;
|
||||
m_code_string.Format("%d", err);
|
||||
m_message.FormatVA(format, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void Error::SetUserFormatted(const char* code, const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
m_type = Type::User;
|
||||
m_error.user = 0;
|
||||
m_code_string = code;
|
||||
m_message.FormatVA(format, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void Error::SetFormattedMessage(const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
m_type = Type::User;
|
||||
m_error.user = 0;
|
||||
m_code_string.Clear();
|
||||
m_message.FormatVA(format, ap);
|
||||
va_end(ap);
|
||||
if (errptr)
|
||||
errptr->SetString(std::move(description));
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
void Error::SetWin32(unsigned long err)
|
||||
{
|
||||
m_type = Type::Win32;
|
||||
m_error.win32 = err;
|
||||
m_code_string.Format("%u", static_cast<u32>(err));
|
||||
m_message.Clear();
|
||||
|
||||
const DWORD r = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, m_error.win32, 0, m_message.GetWriteableCharArray(),
|
||||
m_message.GetWritableBufferSize(), NULL);
|
||||
WCHAR buf[128];
|
||||
const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf,
|
||||
static_cast<DWORD>(std::size(buf)), nullptr);
|
||||
if (r > 0)
|
||||
{
|
||||
m_message.Resize(r);
|
||||
m_message.RStrip();
|
||||
m_description =
|
||||
fmt::format("Win32 Error {}: {}", err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_message = "<Could not resolve system error ID>";
|
||||
m_description = fmt::format("Win32 Error {}: <Could not resolve system error ID>", err);
|
||||
}
|
||||
}
|
||||
|
||||
void Error::SetWin32(Error* errptr, unsigned long err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetWin32(err);
|
||||
}
|
||||
|
||||
void Error::SetHResult(long err)
|
||||
{
|
||||
m_type = Type::HResult;
|
||||
m_error.win32 = err;
|
||||
m_code_string.Format("%08X", static_cast<u32>(err));
|
||||
m_message.Clear();
|
||||
|
||||
const DWORD r = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, m_error.win32, 0, m_message.GetWriteableCharArray(),
|
||||
m_message.GetWritableBufferSize(), NULL);
|
||||
WCHAR buf[128];
|
||||
const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf,
|
||||
static_cast<DWORD>(std::size(buf)), nullptr);
|
||||
if (r > 0)
|
||||
{
|
||||
m_message.Resize(r);
|
||||
m_message.RStrip();
|
||||
m_description =
|
||||
fmt::format("HRESULT {:08X}: {}", err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_message = "<Could not resolve system error ID>";
|
||||
m_description = fmt::format("HRESULT {:08X}: <Could not resolve system error ID>", err);
|
||||
}
|
||||
}
|
||||
|
||||
void Error::SetHResult(Error* errptr, long err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetHResult(err);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// constructors
|
||||
void Error::SetSocket(int err)
|
||||
{
|
||||
// Socket errors are win32 errors on windows
|
||||
#ifdef _WIN32
|
||||
SetWin32(err);
|
||||
#else
|
||||
SetErrno(err);
|
||||
#endif
|
||||
m_type = Type::Socket;
|
||||
}
|
||||
|
||||
void Error::SetSocket(Error* errptr, int err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetSocket(err);
|
||||
}
|
||||
|
||||
Error Error::CreateNone()
|
||||
{
|
||||
Error ret;
|
||||
ret.Clear();
|
||||
return ret;
|
||||
return Error();
|
||||
}
|
||||
|
||||
Error Error::CreateErrno(int err)
|
||||
@ -193,70 +151,10 @@ Error Error::CreateSocket(int err)
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error Error::CreateMessage(const char* msg)
|
||||
Error Error::CreateString(std::string description)
|
||||
{
|
||||
Error ret;
|
||||
ret.SetMessage(msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error Error::CreateUser(int err, const char* msg)
|
||||
{
|
||||
Error ret;
|
||||
ret.SetUser(err, msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error Error::CreateUser(const char* code, const char* message)
|
||||
{
|
||||
Error ret;
|
||||
ret.SetUser(code, message);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error Error::CreateMessageFormatted(const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
Error ret;
|
||||
ret.m_type = Type::User;
|
||||
ret.m_message.FormatVA(format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error Error::CreateUserFormatted(int err, const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
Error ret;
|
||||
ret.m_type = Type::User;
|
||||
ret.m_error.user = err;
|
||||
ret.m_code_string.Format("%d", err);
|
||||
ret.m_message.FormatVA(format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error Error::CreateUserFormatted(const char* code, const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
Error ret;
|
||||
ret.m_type = Type::User;
|
||||
ret.m_error.user = 0;
|
||||
ret.m_code_string = code;
|
||||
ret.m_message.FormatVA(format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
ret.SetString(std::move(description));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -277,86 +175,16 @@ Error Error::CreateHResult(long err)
|
||||
|
||||
#endif
|
||||
|
||||
Error& Error::operator=(const Error& e)
|
||||
{
|
||||
m_type = e.m_type;
|
||||
std::memcpy(&m_error, &e.m_error, sizeof(m_error));
|
||||
m_code_string.Clear();
|
||||
m_code_string.AppendString(e.m_code_string);
|
||||
m_message.Clear();
|
||||
m_message.AppendString(e.m_message);
|
||||
return *this;
|
||||
}
|
||||
Error& Error::operator=(const Error& e) = default;
|
||||
|
||||
Error& Error::operator=(Error&& e) = default;
|
||||
|
||||
bool Error::operator==(const Error& e) const
|
||||
{
|
||||
switch (m_type)
|
||||
{
|
||||
case Type::None:
|
||||
return true;
|
||||
|
||||
case Type::Errno:
|
||||
return m_error.errno_f == e.m_error.errno_f;
|
||||
|
||||
case Type::Socket:
|
||||
return m_error.socketerr == e.m_error.socketerr;
|
||||
|
||||
case Type::User:
|
||||
return m_error.user == e.m_error.user;
|
||||
|
||||
#ifdef _WIN32
|
||||
case Type::Win32:
|
||||
return m_error.win32 == e.m_error.win32;
|
||||
|
||||
case Type::HResult:
|
||||
return m_error.hresult == e.m_error.hresult;
|
||||
#endif
|
||||
}
|
||||
|
||||
return false;
|
||||
return (m_type == e.m_type && m_description == e.m_description);
|
||||
}
|
||||
|
||||
bool Error::operator!=(const Error& e) const
|
||||
{
|
||||
switch (m_type)
|
||||
{
|
||||
case Type::None:
|
||||
return false;
|
||||
|
||||
case Type::Errno:
|
||||
return m_error.errno_f != e.m_error.errno_f;
|
||||
|
||||
case Type::Socket:
|
||||
return m_error.socketerr != e.m_error.socketerr;
|
||||
|
||||
case Type::User:
|
||||
return m_error.user != e.m_error.user;
|
||||
|
||||
#ifdef _WIN32
|
||||
case Type::Win32:
|
||||
return m_error.win32 != e.m_error.win32;
|
||||
|
||||
case Type::HResult:
|
||||
return m_error.hresult != e.m_error.hresult;
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SmallString Error::GetCodeAndMessage() const
|
||||
{
|
||||
SmallString ret;
|
||||
GetCodeAndMessage(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Error::GetCodeAndMessage(String& dest) const
|
||||
{
|
||||
if (m_code_string.IsEmpty())
|
||||
dest.Assign(m_message);
|
||||
else
|
||||
dest.Format("[%s]: %s", m_code_string.GetCharArray(), m_message.GetCharArray());
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
return (m_type != e.m_type || m_description != e.m_description);
|
||||
}
|
@ -1,100 +1,75 @@
|
||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
#pragma once
|
||||
#include "string.h"
|
||||
|
||||
#include "types.h"
|
||||
|
||||
namespace Common {
|
||||
#include <string>
|
||||
|
||||
// this class provides enough storage room for all of these types
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
Error();
|
||||
Error(const Error& e);
|
||||
Error(Error&& e);
|
||||
~Error();
|
||||
|
||||
enum class Type
|
||||
{
|
||||
None = 0, // Set by default constructor, returns 'No Error'.
|
||||
Errno = 1, // Error that is set by system functions, such as open().
|
||||
Socket = 2, // Error that is set by socket functions, such as socket(). On Unix this is the same as errno.
|
||||
User = 3, // When translated, will return 'User Error %u' if no message is specified.
|
||||
Win32 = 4, // Error that is returned by some Win32 functions, such as RegOpenKeyEx. Also used by other APIs through
|
||||
// GetLastError().
|
||||
HResult = 5, // Error that is returned by Win32 COM methods, e.g. S_OK.
|
||||
None = 0,
|
||||
Errno = 1,
|
||||
Socket = 2,
|
||||
User = 3,
|
||||
Win32 = 4,
|
||||
HResult = 5,
|
||||
};
|
||||
|
||||
ALWAYS_INLINE Type GetType() const { return m_type; }
|
||||
ALWAYS_INLINE int GetErrnoCode() const { return m_error.errno_f; }
|
||||
ALWAYS_INLINE int GetSocketCode() const { return m_error.socketerr; }
|
||||
ALWAYS_INLINE int GetUserCode() const { return m_error.user; }
|
||||
#ifdef _WIN32
|
||||
ALWAYS_INLINE unsigned long GetWin32Code() const { return m_error.win32; }
|
||||
ALWAYS_INLINE long GetHResultCode() const { return m_error.hresult; }
|
||||
#endif
|
||||
ALWAYS_INLINE const std::string& GetDescription() const { return m_description; }
|
||||
|
||||
// get code, e.g. "0x00000002"
|
||||
ALWAYS_INLINE const String& GetCodeString() const { return m_code_string; }
|
||||
|
||||
// get description, e.g. "File not Found"
|
||||
ALWAYS_INLINE const String& GetMessage() const { return m_message; }
|
||||
|
||||
// setter functions
|
||||
void Clear();
|
||||
|
||||
/// Error that is set by system functions, such as open().
|
||||
void SetErrno(int err);
|
||||
|
||||
/// Error that is set by socket functions, such as socket(). On Unix this is the same as errno.
|
||||
void SetSocket(int err);
|
||||
void SetMessage(const char* msg);
|
||||
void SetFormattedMessage(const char* format, ...) printflike(2, 3);
|
||||
void SetUser(int err, const char* msg);
|
||||
void SetUser(const char* code, const char* message);
|
||||
void SetUserFormatted(int err, const char* format, ...) printflike(3, 4);
|
||||
void SetUserFormatted(const char* code, const char* format, ...) printflike(3, 4);
|
||||
|
||||
/// Set both description and message.
|
||||
void SetString(std::string description);
|
||||
|
||||
#ifdef _WIN32
|
||||
/// Error that is returned by some Win32 functions, such as RegOpenKeyEx. Also used by other APIs through
|
||||
/// GetLastError().
|
||||
void SetWin32(unsigned long err);
|
||||
|
||||
/// Error that is returned by Win32 COM methods, e.g. S_OK.
|
||||
void SetHResult(long err);
|
||||
#endif
|
||||
|
||||
// constructors
|
||||
static Error CreateNone();
|
||||
static Error CreateErrno(int err);
|
||||
static Error CreateSocket(int err);
|
||||
static Error CreateMessage(const char* msg);
|
||||
static Error CreateMessageFormatted(const char* format, ...) printflike(1, 2);
|
||||
static Error CreateUser(int err, const char* msg);
|
||||
static Error CreateUser(const char* code, const char* message);
|
||||
static Error CreateUserFormatted(int err, const char* format, ...) printflike(2, 3);
|
||||
static Error CreateUserFormatted(const char* code, const char* format, ...) printflike(2, 3);
|
||||
static Error CreateString(std::string description);
|
||||
#ifdef _WIN32
|
||||
static Error CreateWin32(unsigned long err);
|
||||
static Error CreateHResult(long err);
|
||||
#endif
|
||||
|
||||
// get code and description, e.g. "[0x00000002]: File not Found"
|
||||
SmallString GetCodeAndMessage() const;
|
||||
void GetCodeAndMessage(String& dest) const;
|
||||
// helpers for setting
|
||||
static void SetErrno(Error* errptr, int err);
|
||||
static void SetSocket(Error* errptr, int err);
|
||||
static void SetString(Error* errptr, std::string description);
|
||||
static void SetWin32(Error* errptr, unsigned long err);
|
||||
static void SetHResult(Error* errptr, long err);
|
||||
|
||||
// operators
|
||||
Error& operator=(const Error& e);
|
||||
Error& operator=(Error&& e);
|
||||
bool operator==(const Error& e) const;
|
||||
bool operator!=(const Error& e) const;
|
||||
|
||||
private:
|
||||
Type m_type = Type::None;
|
||||
union
|
||||
{
|
||||
int none;
|
||||
int errno_f; // renamed from errno to avoid conflicts with #define'd errnos.
|
||||
int socketerr;
|
||||
int user;
|
||||
#ifdef _WIN32
|
||||
unsigned long win32;
|
||||
long hresult;
|
||||
#endif
|
||||
} m_error{};
|
||||
StackString<16> m_code_string;
|
||||
TinyString m_message;
|
||||
std::string m_description;
|
||||
};
|
||||
|
||||
} // namespace Common
|
||||
|
@ -3,9 +3,11 @@
|
||||
|
||||
#include "file_system.h"
|
||||
#include "assert.h"
|
||||
#include "error.h"
|
||||
#include "log.h"
|
||||
#include "path.h"
|
||||
#include "string_util.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@ -587,7 +589,7 @@ std::string Path::Combine(const std::string_view& base, const std::string_view&
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::FILE* FileSystem::OpenCFile(const char* filename, const char* mode)
|
||||
std::FILE* FileSystem::OpenCFile(const char* filename, const char* mode, Error* error)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const std::wstring wfilename(StringUtil::UTF8StringToWideString(filename));
|
||||
@ -595,23 +597,34 @@ std::FILE* FileSystem::OpenCFile(const char* filename, const char* mode)
|
||||
if (!wfilename.empty() && !wmode.empty())
|
||||
{
|
||||
std::FILE* fp;
|
||||
if (_wfopen_s(&fp, wfilename.c_str(), wmode.c_str()) != 0)
|
||||
const errno_t err = _wfopen_s(&fp, wfilename.c_str(), wmode.c_str());
|
||||
if (err != 0)
|
||||
{
|
||||
Error::SetErrno(error, err);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fp;
|
||||
}
|
||||
|
||||
std::FILE* fp;
|
||||
if (fopen_s(&fp, filename, mode) != 0)
|
||||
const errno_t err = fopen_s(&fp, filename, mode);
|
||||
if (err != 0)
|
||||
{
|
||||
Error::SetErrno(error, err);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fp;
|
||||
#else
|
||||
return std::fopen(filename, mode);
|
||||
std::FILE* fp = std::fopen(filename, mode);
|
||||
if (!fp)
|
||||
Error::SetErrno(error, errno);
|
||||
return fp;
|
||||
#endif
|
||||
}
|
||||
|
||||
int FileSystem::OpenFDFile(const char* filename, int flags, int mode)
|
||||
int FileSystem::OpenFDFile(const char* filename, int flags, int mode, Error* error)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const std::wstring wfilename(StringUtil::UTF8StringToWideString(filename));
|
||||
@ -620,18 +633,21 @@ int FileSystem::OpenFDFile(const char* filename, int flags, int mode)
|
||||
|
||||
return -1;
|
||||
#else
|
||||
return open(filename, flags, mode);
|
||||
const int fd = open(filename, flags, mode);
|
||||
if (fd < 0)
|
||||
Error::SetErrno(error, errno);
|
||||
return fd;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
FileSystem::ManagedCFilePtr FileSystem::OpenManagedCFile(const char* filename, const char* mode)
|
||||
FileSystem::ManagedCFilePtr FileSystem::OpenManagedCFile(const char* filename, const char* mode, Error* error)
|
||||
{
|
||||
return ManagedCFilePtr(OpenCFile(filename, mode), [](std::FILE* fp) { std::fclose(fp); });
|
||||
return ManagedCFilePtr(OpenCFile(filename, mode, error));
|
||||
}
|
||||
|
||||
std::FILE* FileSystem::OpenSharedCFile(const char* filename, const char* mode, FileShareMode share_mode)
|
||||
std::FILE* FileSystem::OpenSharedCFile(const char* filename, const char* mode, FileShareMode share_mode, Error* error)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const std::wstring wfilename(StringUtil::UTF8StringToWideString(filename));
|
||||
@ -661,16 +677,20 @@ std::FILE* FileSystem::OpenSharedCFile(const char* filename, const char* mode, F
|
||||
if (fp)
|
||||
return fp;
|
||||
|
||||
Error::SetErrno(error, errno);
|
||||
return nullptr;
|
||||
#else
|
||||
return std::fopen(filename, mode);
|
||||
std::FILE* fp = std::fopen(filename, mode);
|
||||
if (!fp)
|
||||
Error::SetErrno(error, errno);
|
||||
return fp;
|
||||
#endif
|
||||
}
|
||||
|
||||
FileSystem::ManagedCFilePtr FileSystem::OpenManagedSharedCFile(const char* filename, const char* mode,
|
||||
FileShareMode share_mode)
|
||||
FileShareMode share_mode, Error* error)
|
||||
{
|
||||
return ManagedCFilePtr(OpenSharedCFile(filename, mode, share_mode), [](std::FILE* fp) { std::fclose(fp); });
|
||||
return ManagedCFilePtr(OpenSharedCFile(filename, mode, share_mode, error));
|
||||
}
|
||||
|
||||
int FileSystem::FSeek64(std::FILE* fp, s64 offset, int whence)
|
||||
|
@ -1,8 +1,10 @@
|
||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
@ -11,6 +13,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <vector>
|
||||
|
||||
class Error;
|
||||
|
||||
#ifdef _WIN32
|
||||
#define FS_OSPATH_SEPARATOR_CHARACTER '\\'
|
||||
#define FS_OSPATH_SEPARATOR_STR "\\"
|
||||
@ -87,15 +91,25 @@ bool DeleteFile(const char* path);
|
||||
/// Rename file
|
||||
bool RenamePath(const char* OldPath, const char* NewPath);
|
||||
|
||||
/// Deleter functor for managed file pointers
|
||||
struct FileDeleter
|
||||
{
|
||||
ALWAYS_INLINE void operator()(std::FILE* fp)
|
||||
{
|
||||
if (fp)
|
||||
std::fclose(fp);
|
||||
}
|
||||
};
|
||||
|
||||
/// open files
|
||||
using ManagedCFilePtr = std::unique_ptr<std::FILE, void (*)(std::FILE*)>;
|
||||
ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode);
|
||||
std::FILE* OpenCFile(const char* filename, const char* mode);
|
||||
using ManagedCFilePtr = std::unique_ptr<std::FILE, FileDeleter>;
|
||||
ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode, Error* error = nullptr);
|
||||
std::FILE* OpenCFile(const char* filename, const char* mode, Error* error = nullptr);
|
||||
int FSeek64(std::FILE* fp, s64 offset, int whence);
|
||||
s64 FTell64(std::FILE* fp);
|
||||
s64 FSize64(std::FILE* fp);
|
||||
|
||||
int OpenFDFile(const char* filename, int flags, int mode);
|
||||
int OpenFDFile(const char* filename, int flags, int mode, Error* error = nullptr);
|
||||
|
||||
/// Sharing modes for OpenSharedCFile().
|
||||
enum class FileShareMode
|
||||
@ -108,8 +122,9 @@ enum class FileShareMode
|
||||
|
||||
/// Opens a file in shareable mode (where other processes can access it concurrently).
|
||||
/// Only has an effect on Windows systems.
|
||||
ManagedCFilePtr OpenManagedSharedCFile(const char* filename, const char* mode, FileShareMode share_mode);
|
||||
std::FILE* OpenSharedCFile(const char* filename, const char* mode, FileShareMode share_mode);
|
||||
ManagedCFilePtr OpenManagedSharedCFile(const char* filename, const char* mode, FileShareMode share_mode,
|
||||
Error* error = nullptr);
|
||||
std::FILE* OpenSharedCFile(const char* filename, const char* mode, FileShareMode share_mode, Error* error = nullptr);
|
||||
|
||||
/// Abstracts a POSIX file lock.
|
||||
#ifndef _WIN32
|
||||
|
Reference in New Issue
Block a user