2020-11-07 13:42:04 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2020-11-07 13:42:04 +01:00
|
|
|
*
|
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "FileStream.h"
|
|
|
|
|
|
|
|
|
|
#include "Path.hpp"
|
|
|
|
|
#include "String.hpp"
|
|
|
|
|
|
2021-09-30 13:02:54 +01:00
|
|
|
#include <string_view>
|
2020-11-07 13:42:04 +01:00
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <sys/stat.h>
|
2021-05-21 17:51:26 +02:00
|
|
|
#else
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <io.h>
|
2020-11-07 13:42:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2024-11-29 12:04:56 +09:00
|
|
|
#define ftello _ftelli64
|
|
|
|
|
#define fseeko _fseeki64
|
2020-11-07 13:42:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace OpenRCT2
|
|
|
|
|
{
|
2025-03-25 18:23:36 +01:00
|
|
|
FileStream::FileStream(const fs::path& path, FileMode fileMode)
|
2021-01-17 15:21:17 +00:00
|
|
|
: FileStream(path.u8string(), fileMode)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 18:23:36 +01:00
|
|
|
FileStream::FileStream(const std::string& path, FileMode fileMode)
|
2020-11-07 13:42:04 +01:00
|
|
|
: FileStream(path.c_str(), fileMode)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 18:23:36 +01:00
|
|
|
FileStream::FileStream(std::string_view path, FileMode fileMode)
|
2021-09-30 13:02:54 +01:00
|
|
|
: FileStream(std::string(path), fileMode)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 18:23:36 +01:00
|
|
|
FileStream::FileStream(const utf8* path, FileMode fileMode)
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
|
|
|
|
const char* mode;
|
|
|
|
|
switch (fileMode)
|
|
|
|
|
{
|
2025-03-25 18:23:36 +01:00
|
|
|
case FileMode::open:
|
2020-11-07 13:42:04 +01:00
|
|
|
mode = "rb";
|
|
|
|
|
_canRead = true;
|
|
|
|
|
_canWrite = false;
|
|
|
|
|
break;
|
2025-03-25 18:23:36 +01:00
|
|
|
case FileMode::write:
|
2020-11-07 13:42:04 +01:00
|
|
|
mode = "w+b";
|
|
|
|
|
_canRead = true;
|
|
|
|
|
_canWrite = true;
|
|
|
|
|
break;
|
2025-03-25 18:23:36 +01:00
|
|
|
case FileMode::append:
|
2020-11-07 13:42:04 +01:00
|
|
|
mode = "a";
|
|
|
|
|
_canRead = false;
|
|
|
|
|
_canWrite = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the directory exists before writing to a file inside it
|
|
|
|
|
if (_canWrite)
|
|
|
|
|
{
|
|
|
|
|
std::string directory = Path::GetDirectory(path);
|
|
|
|
|
if (!Path::DirectoryExists(directory))
|
|
|
|
|
{
|
|
|
|
|
Path::CreateDirectory(directory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2024-12-11 22:28:31 +01:00
|
|
|
auto pathW = String::toWideChar(path);
|
|
|
|
|
auto modeW = String::toWideChar(mode);
|
2020-11-07 13:42:04 +01:00
|
|
|
_file = _wfopen(pathW.c_str(), modeW.c_str());
|
|
|
|
|
#else
|
2025-03-25 18:23:36 +01:00
|
|
|
if (fileMode == FileMode::open)
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
|
|
|
|
struct stat fileStat;
|
|
|
|
|
// Only allow regular files to be opened as its possible to open directories.
|
|
|
|
|
if (stat(path, &fileStat) == 0 && S_ISREG(fileStat.st_mode))
|
|
|
|
|
{
|
|
|
|
|
_file = fopen(path, mode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_file = fopen(path, mode);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (_file == nullptr)
|
|
|
|
|
{
|
2024-12-11 22:28:31 +01:00
|
|
|
throw IOException(String::stdFormat("Unable to open '%s'", path));
|
2020-11-07 13:42:04 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-21 17:51:26 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
_fileSize = _filelengthi64(_fileno(_file));
|
|
|
|
|
#else
|
|
|
|
|
std::error_code ec;
|
2022-03-18 23:21:23 +01:00
|
|
|
_fileSize = fs::file_size(fs::u8path(path), ec);
|
2021-05-21 17:51:26 +02:00
|
|
|
#endif
|
2020-11-07 13:42:04 +01:00
|
|
|
|
|
|
|
|
_ownsFilePtr = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileStream::~FileStream()
|
|
|
|
|
{
|
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
_disposed = true;
|
|
|
|
|
if (_ownsFilePtr)
|
|
|
|
|
{
|
|
|
|
|
fclose(_file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStream::CanRead() const
|
|
|
|
|
{
|
|
|
|
|
return _canRead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStream::CanWrite() const
|
|
|
|
|
{
|
|
|
|
|
return _canWrite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t FileStream::GetLength() const
|
|
|
|
|
{
|
|
|
|
|
return _fileSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t FileStream::GetPosition() const
|
|
|
|
|
{
|
|
|
|
|
return ftello(_file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStream::SetPosition(uint64_t position)
|
|
|
|
|
{
|
|
|
|
|
Seek(position, STREAM_SEEK_BEGIN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStream::Seek(int64_t offset, int32_t origin)
|
|
|
|
|
{
|
|
|
|
|
switch (origin)
|
|
|
|
|
{
|
|
|
|
|
case STREAM_SEEK_BEGIN:
|
|
|
|
|
fseeko(_file, offset, SEEK_SET);
|
|
|
|
|
break;
|
|
|
|
|
case STREAM_SEEK_CURRENT:
|
|
|
|
|
fseeko(_file, offset, SEEK_CUR);
|
|
|
|
|
break;
|
|
|
|
|
case STREAM_SEEK_END:
|
|
|
|
|
fseeko(_file, offset, SEEK_END);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStream::Read(void* buffer, uint64_t length)
|
|
|
|
|
{
|
2021-05-21 18:51:00 +02:00
|
|
|
if (fread(buffer, 1, static_cast<size_t>(length), _file) == length)
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
2021-05-21 18:51:00 +02:00
|
|
|
return;
|
2020-11-07 13:42:04 +01:00
|
|
|
}
|
|
|
|
|
throw IOException("Attempted to read past end of file.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStream::Write(const void* buffer, uint64_t length)
|
|
|
|
|
{
|
2020-12-06 18:13:03 +00:00
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-02 08:29:40 +02:00
|
|
|
if (auto count = fwrite(buffer, static_cast<size_t>(length), 1, _file); count != 1)
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
2022-08-02 08:29:40 +02:00
|
|
|
std::string error = "Unable to write " + std::to_string(length) + " bytes to file. Count = " + std::to_string(count)
|
|
|
|
|
+ ", errno = " + std::to_string(errno);
|
|
|
|
|
throw IOException(error);
|
2020-11-07 13:42:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t position = GetPosition();
|
|
|
|
|
_fileSize = std::max(_fileSize, position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t FileStream::TryRead(void* buffer, uint64_t length)
|
|
|
|
|
{
|
|
|
|
|
size_t readBytes = fread(buffer, 1, static_cast<size_t>(length), _file);
|
|
|
|
|
return readBytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace OpenRCT2
|