1
0
Fork 0

DBG: (minimal) performance increase in FileHelper::ReadAllData

This commit is contained in:
mrexodia 2016-06-06 11:20:46 +02:00
parent c800b2f5ba
commit 8ef2234caa
No known key found for this signature in database
GPG Key ID: D72F9A4FAA0073B4
2 changed files with 11 additions and 9 deletions

View File

@ -1,22 +1,21 @@
#include "filehelper.h"
#include "handle.h"
#include "stringutils.h"
bool FileHelper::ReadAllData(const String & fileName, std::vector<unsigned char> & content)
{
Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if(hFile == INVALID_HANDLE_VALUE)
return false;
unsigned int filesize = GetFileSize(hFile, 0);
unsigned int filesize = GetFileSize(hFile, nullptr);
if(!filesize)
{
content.clear();
return true;
}
Memory<char*> filedata(filesize + 1, "FileReader::ReadAllData:filedata");
content.resize(filesize);
DWORD read = 0;
if(!ReadFile(hFile, filedata(), filesize, &read, nullptr))
return false;
content = std::vector<unsigned char>(filedata(), filedata() + filesize);
return true;
return !!ReadFile(hFile, content.data(), filesize, &read, nullptr);
}
bool FileHelper::WriteAllData(const String & fileName, const void* data, size_t size)

View File

@ -33,6 +33,9 @@ String StringUtils::Escape(const String & s)
auto ch = uint8_t(s[i]);
switch(ch)
{
case '\0':
escaped += "\\0";
break;
case '\t':
escaped += "\\t";
break;
@ -93,11 +96,11 @@ String StringUtils::TrimRight(const String & s)
String StringUtils::Utf16ToUtf8(const WString & wstr)
{
String convertedString;
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, 0, 0, 0, 0);
auto requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
if(requiredSize > 0)
{
std::vector<char> buffer(requiredSize);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], requiredSize, 0, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], requiredSize, nullptr, nullptr);
convertedString.assign(buffer.begin(), buffer.end() - 1);
}
return convertedString;
@ -111,7 +114,7 @@ String StringUtils::Utf16ToUtf8(const wchar_t* wstr)
WString StringUtils::Utf8ToUtf16(const String & str)
{
WString convertedString;
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0);
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
if(requiredSize > 0)
{
std::vector<wchar_t> buffer(requiredSize);