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

160 lines
4.6 KiB
C++
Raw Normal View History

2018-05-02 13:15:26 +01:00
#pragma region Copyright (c) 2014-2018 OpenRCT2 Developers
2016-12-05 03:30:25 +00:00
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2017-08-29 23:25:52 +01:00
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <sys/stat.h>
#endif
2018-05-02 13:15:26 +01:00
#include <fstream>
2016-12-05 03:30:25 +00:00
#include "File.h"
#include "FileStream.hpp"
#include "String.hpp"
2018-03-13 13:14:02 +01:00
#include "../util/Util.h"
2016-12-05 03:30:25 +00:00
#include "../platform/platform.h"
2016-12-05 16:58:07 +00:00
2016-12-05 03:30:25 +00:00
namespace File
{
2017-02-09 21:48:44 +00:00
bool Exists(const std::string &path)
{
return platform_file_exists(path.c_str());
}
2017-01-05 18:13:48 +00: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
}
2017-01-05 18:13:48 +00: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
}
2017-01-05 18:13:48 +00: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
}
2018-05-02 13:15:26 +01:00
std::vector<uint8> ReadAllBytes(const std::string_view& path)
2016-12-05 03:30:25 +00:00
{
2018-05-02 13:15:26 +01:00
std::vector<uint8> 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(path.data());
std::ifstream fs(pathW, std::ios::in | std::ios::binary);
#else
std::ifstream fs(path.data(), std::ios::in | std::ios::binary);
#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)
{
std::string message = String::StdFormat("'%s' exceeds maximum length of %lld bytes.", SIZE_MAX);
throw IOException(message);
}
else
{
2018-05-02 13:15:26 +01:00
result.resize(fsize);
fs.seekg(0);
fs.read((char *)result.data(), result.size());
fs.exceptions(fs.failbit);
2016-12-05 03:30:25 +00:00
}
return result;
}
2017-02-09 21:32:17 +00:00
void WriteAllBytes(const std::string &path, const void * buffer, size_t length)
{
auto fs = FileStream(path, FILE_MODE_WRITE);
fs.Write(buffer, length);
}
2017-05-17 19:11:42 +01:00
std::vector<std::string> ReadAllLines(const std::string &path)
{
std::vector<std::string> lines;
2018-05-02 13:15:26 +01:00
auto data = ReadAllBytes(path);
auto lineStart = (const char *)data.data();
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
uint64 GetLastModified(const std::string &path)
{
uint64 lastModified = 0;
#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)ftWrite.dwHighDateTime << 32ULL) | (uint64)ftWrite.dwLowDateTime;
}
CloseHandle(hFile);
}
free(pathW);
#else
struct stat statInfo;
if (stat(path.c_str(), &statInfo) == 0)
{
lastModified = statInfo.st_mtime;
}
#endif
return lastModified;
}
2018-05-04 22:40:09 +02:00
} // namespace File
2017-02-09 21:14:48 +00:00
2018-02-01 18:49:14 +01:00
bool writeentirefile(const utf8 * path, const void * buffer, size_t length)
{
try
{
File::WriteAllBytes(String::ToStd(path), buffer, length);
return true;
}
catch (const std::exception &)
{
return false;
}
}