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

164 lines
4.6 KiB
C++
Raw Normal View History

2016-12-05 03:30:25 +00:00
/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 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
# define WIN32_LEAN_AND_MEAN
# 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
2018-06-22 22:58:39 +02:00
#include "../platform/platform.h"
#include "../util/Util.h"
2016-12-05 03:30:25 +00:00
#include "File.h"
#include "FileStream.hpp"
#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_file_exists(path.c_str());
}
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
}
std::vector<uint8_t> ReadAllBytes(const std::string_view& path)
2016-12-05 03:30:25 +00:00
{
std::vector<uint8_t> result;
2016-12-05 03:30:25 +00:00
2018-05-02 13:15:26 +01:00
#if defined(_WIN32) && !defined(__MINGW32__)
auto pathW = String::ToUtf16(std::string(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())
{
throw IOException("Unable to open " + std::string(path.data()));
}
fs.seekg(0, std::ios::end);
auto fsize = (size_t)fs.tellg();
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);
fs.seekg(0);
2018-06-22 22:58:39 +02:00
fs.read((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
std::string ReadAllText(const 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;
}
2018-06-22 22:58:39 +02:00
void WriteAllBytes(const std::string& path, const void* buffer, size_t length)
2017-02-09 21:32:17 +00:00
{
auto fs = FileStream(path, FILE_MODE_WRITE);
fs.Write(buffer, length);
}
2017-05-17 19:11:42 +01:00
2018-06-22 22:58:39 +02:00
std::vector<std::string> ReadAllLines(const std::string& 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);
2018-06-22 22:58:39 +02:00
auto lineStart = (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
2018-06-22 22:58:39 +02:00
uint64_t GetLastModified(const std::string& path)
2017-08-29 23:25:52 +01:00
{
uint64_t lastModified = 0;
2017-08-29 23:25:52 +01:00
#ifdef _WIN32
auto pathW = utf8_to_widechar(path.c_str());
auto hFile = CreateFileW(pathW, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
FILETIME ftCreate, ftAccess, ftWrite;
if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
{
lastModified = ((uint64_t)ftWrite.dwHighDateTime << 32ULL) | (uint64_t)ftWrite.dwLowDateTime;
2017-08-29 23:25:52 +01:00
}
CloseHandle(hFile);
}
free(pathW);
#else
2018-06-22 22:58:39 +02:00
struct stat statInfo
{
};
2017-08-29 23:25:52 +01:00
if (stat(path.c_str(), &statInfo) == 0)
{
lastModified = statInfo.st_mtime;
}
#endif
2018-06-22 22:58:39 +02:00
return lastModified;
2017-08-29 23:25:52 +01:00
}
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;
}
}