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

145 lines
4.2 KiB
C++
Raw Normal View History

2016-12-05 03:30:25 +00:00
/*****************************************************************************
* Copyright (c) 2014-2021 OpenRCT2 developers
2016-12-05 03:30:25 +00:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-12-05 03:30:25 +00:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-12-05 03:30:25 +00:00
*****************************************************************************/
2017-08-29 23:25:52 +01:00
#ifdef _WIN32
2018-07-21 16:17:06 +02:00
# include <windows.h>
2017-08-29 23:25:52 +01:00
#else
2018-07-21 16:17:06 +02:00
# include <sys/stat.h>
2017-08-29 23:25:52 +01:00
#endif
#include "../platform/Platform2.h"
2018-06-22 22:58:39 +02:00
#include "../util/Util.h"
2016-12-05 03:30:25 +00:00
#include "File.h"
#include "FileStream.h"
2016-12-05 03:30:25 +00:00
#include "String.hpp"
2018-06-22 22:58:39 +02:00
#include <fstream>
2016-12-05 16:58:07 +00:00
2016-12-05 03:30:25 +00:00
namespace File
{
2022-01-08 13:57:29 +01:00
bool Exists(std::string_view path)
2017-02-09 21:48:44 +00:00
{
fs::path file = u8path(path);
log_verbose("Checking if file exists: %s", std::string(path).c_str());
2022-01-27 00:33:17 +01:00
std::error_code ec;
const auto result = fs::exists(file, ec);
return result && ec.value() == 0;
2017-02-09 21:48:44 +00:00
}
2022-01-08 13:57:29 +01:00
bool Copy(std::string_view srcPath, std::string_view dstPath, bool overwrite)
2016-12-05 16:58:07 +00:00
{
if (!overwrite && Exists(dstPath))
{
log_warning("File::Copy(): Not overwriting %s, because overwrite flag == false", std::string(dstPath).c_str());
return false;
}
2022-01-27 00:33:17 +01:00
std::error_code ec;
const auto result = fs::copy_file(u8path(srcPath), u8path(dstPath), ec);
return result && ec.value() == 0;
2016-12-05 16:58:07 +00:00
}
2022-01-08 13:57:29 +01:00
bool Delete(std::string_view path)
2016-12-05 16:58:07 +00:00
{
2022-01-27 00:33:17 +01:00
std::error_code ec;
const auto result = fs::remove(u8path(path), ec);
return result && ec.value() == 0;
2016-12-05 16:58:07 +00:00
}
2022-01-08 13:57:29 +01:00
bool Move(std::string_view srcPath, std::string_view dstPath)
2016-12-07 12:00:02 +00:00
{
2022-01-27 00:33:17 +01:00
std::error_code ec;
fs::rename(u8path(srcPath), u8path(dstPath), ec);
return ec.value() == 0;
2016-12-07 12:00:02 +00:00
}
2021-01-07 01:22:22 +09:00
std::vector<uint8_t> ReadAllBytes(std::string_view path)
2016-12-05 03:30:25 +00:00
{
2018-05-02 13:15:26 +01:00
#if defined(_WIN32) && !defined(__MINGW32__)
2021-01-07 01:22:22 +09:00
auto pathW = String::ToWideChar(path);
2018-05-02 13:15:26 +01:00
std::ifstream fs(pathW, std::ios::in | std::ios::binary);
#else
std::ifstream fs(std::string(path), std::ios::in | std::ios::binary);
2018-05-02 13:15:26 +01:00
#endif
if (!fs.is_open())
{
2021-01-07 01:22:22 +09:00
throw IOException("Unable to open " + std::string(path));
2018-05-02 13:15:26 +01:00
}
2021-01-07 01:22:22 +09:00
std::vector<uint8_t> result;
auto fsize = Platform::GetFileSize(path);
2016-12-05 03:30:25 +00:00
if (fsize > SIZE_MAX)
{
2018-10-11 06:20:01 +02:00
std::string message = String::StdFormat(
"'%s' exceeds maximum length of %lld bytes.", std::string(path).c_str(), SIZE_MAX);
2016-12-05 03:30:25 +00:00
throw IOException(message);
}
else
{
2018-05-02 13:15:26 +01:00
result.resize(fsize);
2020-04-22 17:09:29 +02:00
fs.read(reinterpret_cast<char*>(result.data()), result.size());
2018-05-02 13:15:26 +01:00
fs.exceptions(fs.failbit);
2016-12-05 03:30:25 +00:00
}
return result;
}
2017-02-09 21:32:17 +00:00
2021-01-07 01:22:22 +09:00
std::string ReadAllText(std::string_view path)
{
auto bytes = ReadAllBytes(path);
// TODO skip BOM
std::string result(bytes.size(), 0);
std::copy(bytes.begin(), bytes.end(), result.begin());
return result;
}
2021-01-07 01:22:22 +09:00
std::vector<std::string> ReadAllLines(std::string_view path)
2017-05-17 19:11:42 +01:00
{
std::vector<std::string> lines;
2018-05-02 13:15:26 +01:00
auto data = ReadAllBytes(path);
2020-04-22 17:09:29 +02:00
auto lineStart = reinterpret_cast<const char*>(data.data());
2018-05-02 13:15:26 +01:00
auto ch = lineStart;
2017-05-17 19:11:42 +01:00
char lastC = 0;
2018-05-02 13:15:26 +01:00
for (size_t i = 0; i < data.size(); i++)
2017-05-17 19:11:42 +01:00
{
char c = *ch;
if (c == '\n' && lastC == '\r')
{
// Ignore \r\n
lineStart = ch + 1;
}
else if (c == '\n' || c == '\r')
{
lines.emplace_back(lineStart, ch - lineStart);
lineStart = ch + 1;
}
lastC = c;
ch++;
}
// Last line
lines.emplace_back(lineStart, ch - lineStart);
return lines;
}
2017-08-29 23:25:52 +01:00
2022-01-08 13:57:29 +01:00
void WriteAllBytes(std::string_view path, const void* buffer, size_t length)
2021-01-07 01:22:22 +09:00
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_WRITE);
fs.Write(buffer, length);
}
2022-01-08 13:57:29 +01:00
uint64_t GetLastModified(std::string_view path)
2017-08-29 23:25:52 +01:00
{
2020-11-23 00:23:07 -03:00
return Platform::GetLastModified(path);
2017-08-29 23:25:52 +01:00
}
uint64_t GetSize(std::string_view path)
{
return Platform::GetFileSize(path);
}
2018-05-04 22:40:09 +02:00
} // namespace File