1
0
Fork 0

DBG: added some functionality in filehelper.cpp + fixed a nice bug in command.cpp (va args was printed but never used)

This commit is contained in:
mrexodia 2015-11-26 02:32:17 +01:00
parent 438650e905
commit 23f5720a2b
3 changed files with 29 additions and 13 deletions

View File

@ -388,7 +388,7 @@ CMDRESULT cmddirectexec(const char* cmd, ...)
_vsnprintf_s(command, _TRUNCATE, cmd, ap);
va_end(ap);
strcpy_s(command, StringUtils::Trim(cmd).c_str());
strcpy_s(command, StringUtils::Trim(command).c_str());
COMMAND* found = cmdfindmain(command);
if(!found || !found->cbCommand)
return STATUS_ERROR;

View File

@ -1,29 +1,43 @@
#include "filehelper.h"
bool FileHelper::ReadAllText(const String & fileName, String & content)
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)
if (hFile == INVALID_HANDLE_VALUE)
return false;
unsigned int filesize = GetFileSize(hFile, 0);
if(!filesize)
if (!filesize)
{
content.clear();
return true;
}
Memory<char*> filedata(filesize + 1, "FileReader::ReadAllText:filedata");
Memory<char*> filedata(filesize + 1, "FileReader::ReadAllData:filedata");
DWORD read = 0;
if(!ReadFile(hFile, filedata(), filesize, &read, 0))
if (!ReadFile(hFile, filedata(), filesize, &read, nullptr))
return false;
content = String(filedata());
content = std::vector<unsigned char>(filedata(), filedata() + filesize);
return true;
}
bool FileHelper::WriteAllData(const String& fileName, const void* data, size_t size)
{
Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
return false;
DWORD written = 0;
return !!WriteFile(hFile, data, DWORD(size), &written, nullptr);
}
bool FileHelper::ReadAllText(const String & fileName, String & content)
{
std::vector<unsigned char> data;
if (!ReadAllData(fileName, data))
return false;
content = String((const char*)data.data());
return true;
}
bool FileHelper::WriteAllText(const String & fileName, const String & content)
{
Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
if(hFile == INVALID_HANDLE_VALUE)
return false;
DWORD written = 0;
return !!WriteFile(hFile, content.c_str(), DWORD(content.length()), &written, nullptr);
}
return WriteAllData(fileName, content.c_str(), content.length());
}

View File

@ -6,6 +6,8 @@
class FileHelper
{
public:
static bool ReadAllData(const String & fileName, std::vector<unsigned char> & content);
static bool WriteAllData(const String & fileName, const void* data, size_t size);
static bool ReadAllText(const String & fileName, String & content);
static bool WriteAllText(const String & fileName, const String & content);
};