2017-01-15 20:46:32 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-01-15 20:46:32 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "Common/IOFile.h"
|
|
|
|
|
2017-01-15 20:46:32 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
#include "Common/CommonFuncs.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2020-06-26 15:52:31 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
|
|
|
|
namespace File
|
|
|
|
{
|
|
|
|
IOFile::IOFile() : m_file(nullptr), m_good(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IOFile::IOFile(std::FILE* file) : m_file(file), m_good(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-24 16:25:23 +00:00
|
|
|
IOFile::IOFile(const std::string& filename, const char openmode[], SharedAccess sh)
|
|
|
|
: m_file(nullptr), m_good(true)
|
2017-01-15 20:46:32 +00:00
|
|
|
{
|
2022-09-24 16:25:23 +00:00
|
|
|
Open(filename, openmode, sh);
|
2017-01-15 20:46:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IOFile::~IOFile()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
IOFile::IOFile(IOFile&& other) noexcept : m_file(nullptr), m_good(true)
|
|
|
|
{
|
|
|
|
Swap(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOFile& IOFile::operator=(IOFile&& other) noexcept
|
|
|
|
{
|
|
|
|
Swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOFile::Swap(IOFile& other) noexcept
|
|
|
|
{
|
|
|
|
std::swap(m_file, other.m_file);
|
|
|
|
std::swap(m_good, other.m_good);
|
|
|
|
}
|
|
|
|
|
2022-09-24 16:25:23 +00:00
|
|
|
bool IOFile::Open(const std::string& filename, const char openmode[],
|
|
|
|
[[maybe_unused]] SharedAccess sh)
|
2017-01-15 20:46:32 +00:00
|
|
|
{
|
|
|
|
Close();
|
2020-11-04 19:59:39 +00:00
|
|
|
|
2017-01-15 20:46:32 +00:00
|
|
|
#ifdef _WIN32
|
2022-09-24 16:25:23 +00:00
|
|
|
if (sh == SharedAccess::Default)
|
|
|
|
{
|
|
|
|
m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0;
|
|
|
|
}
|
|
|
|
else if (sh == SharedAccess::Read)
|
|
|
|
{
|
|
|
|
m_file = _tfsopen(UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str(), SH_DENYWR);
|
|
|
|
m_good = m_file != nullptr;
|
|
|
|
}
|
2017-01-15 20:46:32 +00:00
|
|
|
#else
|
2020-06-26 15:52:31 +00:00
|
|
|
#ifdef ANDROID
|
2020-11-04 19:59:39 +00:00
|
|
|
if (IsPathAndroidContent(filename))
|
|
|
|
m_file = fdopen(OpenAndroidContent(filename, OpenModeToAndroid(openmode)), openmode);
|
2020-06-26 15:52:31 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
m_file = std::fopen(filename.c_str(), openmode);
|
2020-11-04 19:59:39 +00:00
|
|
|
|
2017-06-17 01:16:58 +00:00
|
|
|
m_good = m_file != nullptr;
|
2017-01-15 20:46:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return m_good;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOFile::Close()
|
|
|
|
{
|
|
|
|
if (!IsOpen() || 0 != std::fclose(m_file))
|
|
|
|
m_good = false;
|
|
|
|
|
|
|
|
m_file = nullptr;
|
|
|
|
return m_good;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOFile::SetHandle(std::FILE* file)
|
|
|
|
{
|
|
|
|
Close();
|
2022-01-29 05:01:03 +00:00
|
|
|
ClearError();
|
2017-01-15 20:46:32 +00:00
|
|
|
m_file = file;
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:57:43 +00:00
|
|
|
u64 IOFile::GetSize() const
|
2017-01-15 20:46:32 +00:00
|
|
|
{
|
|
|
|
if (IsOpen())
|
|
|
|
return File::GetSize(m_file);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:58:31 +00:00
|
|
|
bool IOFile::Seek(s64 offset, SeekOrigin origin)
|
|
|
|
{
|
|
|
|
int fseek_origin;
|
|
|
|
switch (origin)
|
|
|
|
{
|
|
|
|
case SeekOrigin::Begin:
|
|
|
|
fseek_origin = SEEK_SET;
|
|
|
|
break;
|
|
|
|
case SeekOrigin::Current:
|
|
|
|
fseek_origin = SEEK_CUR;
|
|
|
|
break;
|
|
|
|
case SeekOrigin::End:
|
|
|
|
fseek_origin = SEEK_END;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsOpen() || 0 != fseeko(m_file, offset, fseek_origin))
|
2017-01-15 20:46:32 +00:00
|
|
|
m_good = false;
|
|
|
|
|
|
|
|
return m_good;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 IOFile::Tell() const
|
|
|
|
{
|
|
|
|
if (IsOpen())
|
|
|
|
return ftello(m_file);
|
|
|
|
else
|
|
|
|
return UINT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOFile::Flush()
|
|
|
|
{
|
|
|
|
if (!IsOpen() || 0 != std::fflush(m_file))
|
|
|
|
m_good = false;
|
|
|
|
|
|
|
|
return m_good;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOFile::Resize(u64 size)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!IsOpen() || 0 != _chsize_s(_fileno(m_file), size))
|
|
|
|
#else
|
|
|
|
// TODO: handle 64bit and growing
|
|
|
|
if (!IsOpen() || 0 != ftruncate(fileno(m_file), size))
|
|
|
|
#endif
|
|
|
|
m_good = false;
|
|
|
|
|
|
|
|
return m_good;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace File
|