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

142 lines
3.9 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
{
2018-06-22 22:58:39 +02:00
bool Exists(const std::string& path)
2017-02-09 21:48:44 +00:00
{
return Platform::FileExists(path);
2017-02-09 21:48:44 +00:00
}
2018-06-22 22:58:39 +02:00
bool Copy(const std::string& srcPath, const std::string& dstPath, bool overwrite)
2016-12-05 16:58:07 +00:00
{
2017-01-05 18:13:48 +00:00
return platform_file_copy(srcPath.c_str(), dstPath.c_str(), overwrite);
2016-12-05 16:58:07 +00:00
}
2018-06-22 22:58:39 +02:00
bool Delete(const std::string& path)
2016-12-05 16:58:07 +00:00
{
2017-01-05 18:13:48 +00:00
return platform_file_delete(path.c_str());
2016-12-05 16:58:07 +00:00
}
2018-06-22 22:58:39 +02:00
bool Move(const std::string& srcPath, const std::string& dstPath)
2016-12-07 12:00:02 +00:00
{
2017-01-05 18:13:48 +00:00
return platform_file_move(srcPath.c_str(), dstPath.c_str());
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
2021-01-07 01:22:22 +09:00
void WriteAllBytes(const std::string& path, const void* buffer, size_t length)
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_WRITE);
fs.Write(buffer, length);
}
2018-06-22 22:58:39 +02:00
uint64_t GetLastModified(const std::string& 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
2017-02-09 21:14:48 +00:00
2018-06-22 22:58:39 +02:00
bool writeentirefile(const utf8* path, const void* buffer, size_t length)
2018-02-01 18:49:14 +01:00
{
try
{
File::WriteAllBytes(String::ToStd(path), buffer, length);
return true;
}
2018-06-22 22:58:39 +02:00
catch (const std::exception&)
2018-02-01 18:49:14 +01:00
{
return false;
}
}