diff options
Diffstat (limited to 'src/FileUtil.cpp')
-rw-r--r-- | src/FileUtil.cpp | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp index 1fd86d6..e1142ec 100644 --- a/src/FileUtil.cpp +++ b/src/FileUtil.cpp @@ -301,7 +301,7 @@ namespace sibs return true; } - Result<StringView> getFileContent(const _tinydir_char_t *filepath) + Result<std::string> getFileContent(const _tinydir_char_t *filepath) { #if OS_FAMILY == OS_FAMILY_POSIX FILE *file = fopen(filepath, "rb"); @@ -315,25 +315,18 @@ namespace sibs errMsg += toUtf8(filepath); errMsg += "; reason: "; errMsg += strerror(error); - return Result<StringView>::Err(errMsg); + return Result<std::string>::Err(errMsg); } fseek(file, 0, SEEK_END); size_t fileSize = ftell(file); fseek(file, 0, SEEK_SET); - // TODO: Change this to string so it can be deallocated and use std::move to prevent copies - char *result = (char*)malloc(fileSize + 1); - if(!result) - { - std::string errMsg = "Failed to load file content from file: "; - errMsg += toUtf8(filepath); - throw std::runtime_error(errMsg); - } - result[fileSize] = '\0'; - fread(result, 1, fileSize, file); + std::string result; + result.resize(fileSize); + fread(&result[0], 1, fileSize, file); fclose(file); - return Result<StringView>::Ok(StringView(result, fileSize)); + return Result<std::string>::Ok(std::move(result)); } Result<bool> fileWrite(const _tinydir_char_t *filepath, StringView data) |