Files
OpenRCT2-Web/src/openrct2/core/FileStream.cpp
T

203 lines
5.0 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
* 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>
#ifndef _WIN32
#include <sys/stat.h>
#else
#include <io.h>
#endif
#ifdef _MSC_VER
#define ftello _ftelli64
#define fseeko _fseeki64
#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)
: 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)
{
const char* mode;
switch (fileMode)
{
2025-03-25 18:23:36 +01:00
case FileMode::open:
mode = "rb";
_canRead = true;
_canWrite = false;
break;
2025-03-25 18:23:36 +01:00
case FileMode::write:
mode = "w+b";
_canRead = true;
_canWrite = true;
break;
2025-03-25 18:23:36 +01:00
case FileMode::append:
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);
_file = _wfopen(pathW.c_str(), modeW.c_str());
#else
2025-03-25 18:23:36 +01:00
if (fileMode == FileMode::open)
{
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));
}
#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);
#endif
_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)
{
if (fread(buffer, 1, static_cast<size_t>(length), _file) == length)
{
return;
}
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)
{
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);
}
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;
}
const void* FileStream::GetData() const
{
return nullptr;
}
} // namespace OpenRCT2