diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-04 20:21:45 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-06 07:39:58 +0200 |
commit | f1c219b6322427fd2d5d97df17fe684fbfe45afa (patch) | |
tree | 379da26a9a06144cbe054dd21a465a39f3f17e9c /src | |
parent | 444b5725b125e5154a571a1542cbb5a7ca58e29b (diff) |
Return a string for a getFileData
Diffstat (limited to 'src')
-rw-r--r-- | src/Conf.cpp | 13 | ||||
-rw-r--r-- | src/FileUtil.cpp | 19 | ||||
-rw-r--r-- | src/GlobalLib.cpp | 20 | ||||
-rw-r--r-- | src/main.cpp | 16 |
4 files changed, 39 insertions, 29 deletions
diff --git a/src/Conf.cpp b/src/Conf.cpp index dc9c406..74c311c 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -463,19 +463,20 @@ namespace sibs Result<bool> Config::readFromFile(const _tinydir_char_t *filepath, SibsConfig &config) { - Result<StringView> fileContentResult = getFileContent(filepath); + Result<std::string> fileContentResult = getFileContent(filepath); if(fileContentResult.isErr()) return Result<bool>::Err(fileContentResult.getErrMsg()); - const char *code = fileContentResult.unwrap().data; - if(!utf8::is_valid(code, code + fileContentResult.unwrap().size)) + const std::string &code = fileContentResult.unwrap(); + if(!utf8::is_valid(code.data(), code.data() + code.size())) return Result<bool>::Err("File is not in valid utf8 format"); - if(fileContentResult.unwrap().size >= 3 && utf8::is_bom(code)) - code += 3; + const char *code_ptr = code.data(); + if(code.size() >= 3 && utf8::is_bom(code_ptr)) + code_ptr += 3; // Do not free file content (fileContentResult) on purpose, since we are using the data and sibs is short lived - Result<bool> parseResult = Parser::parse(code, config); + Result<bool> parseResult = Parser::parse(code_ptr, config); if(!parseResult) { string errMsg = "Failed while parsing project.conf for project "; 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) diff --git a/src/GlobalLib.cpp b/src/GlobalLib.cpp index df66727..8346a83 100644 --- a/src/GlobalLib.cpp +++ b/src/GlobalLib.cpp @@ -60,7 +60,22 @@ namespace sibs FileString libArchivedFilePath = libPathResult.unwrap(); libArchivedFilePath += TINYDIR_STRING("/.cache/sibs/archive/"); libArchivedFilePath += toFileString(name); - if(getFileType(libArchivedFilePath.c_str()) != FileType::FILE_NOT_FOUND) + FileType archive_path_file_type = getFileType(libArchivedFilePath.c_str()); + + if(archive_path_file_type == FileType::FILE_NOT_FOUND) + return Result<bool>::Ok(true); + + if(archive_path_file_type != FileType::DIRECTORY) + return Result<bool>::Err("A previous download of package is corrupt, attempting to redownload..."); + + bool isEmpty = true; + walkDir(libArchivedFilePath.c_str(), [&isEmpty](tinydir_file *file) + { + isEmpty = false; + return false; + }); + + if(!isEmpty) return Result<bool>::Err("A previous download of package is corrupt, attempting to redownload..."); return Result<bool>::Ok(true); @@ -314,6 +329,7 @@ namespace sibs if(!createArchiveDirResult) return createArchiveDirResult; + FileString libArchivedDir = libArchivedFilePath; libArchivedFilePath += TINYDIR_STRING("/"); libArchivedFilePath += toFileString(package.version.toString()); Result<bool> downloadResult = curl::downloadFile(package.urls[0].c_str(), libArchivedFilePath.c_str()); @@ -329,8 +345,10 @@ namespace sibs // We have extracted the archive, we dont need to cache it. If remove fails, it doesn't really matter, user can remove it himself #if OS_FAMILY == OS_FAMILY_POSIX remove(libArchivedFilePath.c_str()); + remove(libArchivedDir.c_str()); #else _wremove(libArchivedFilePath.c_str()); + _wremove(libArchivedDir.c_str()); #endif return archiveExtractResult; } diff --git a/src/main.cpp b/src/main.cpp index 3d11fd5..0cb55e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -793,26 +793,24 @@ static Result<ExecResult> gitInitProject(const FileString &projectPath) static bool gitIgnoreContainsSibs(const FileString &gitIgnoreFilePath) { - Result<StringView> fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); + Result<std::string> fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); if(!fileContentResult) return false; - StringView fileContent = fileContentResult.unwrap(); - const char *fileContentEnd = fileContent.data + fileContent.size; - auto it = std::search(fileContent.data, fileContentEnd, SIBS_GITIGNORE_HEADER.begin(), SIBS_GITIGNORE_HEADER.end()); + const std::string &fileContent = fileContentResult.unwrap(); + const char *fileContentEnd = fileContent.data() + fileContent.size(); + auto it = std::search(fileContent.data(), fileContentEnd, SIBS_GITIGNORE_HEADER.begin(), SIBS_GITIGNORE_HEADER.end()); bool containsSibs = it != fileContentEnd; - free((void*)fileContent.data); return containsSibs; } static void gitIgnoreAppendSibs(const FileString &gitIgnoreFilePath) { - Result<StringView> fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); + Result<std::string> fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); string fileContentNew; if(fileContentResult) { - StringView fileContent = fileContentResult.unwrap(); - fileContentNew.append(fileContent.data, fileContent.data + fileContent.size); + const std::string &fileContent = fileContentResult.unwrap(); + fileContentNew += fileContent; fileContentNew += "\n\n"; - free((void*)fileContent.data); } fileContentNew += SIBS_GITIGNORE_HEADER; fileContentNew += "\n"; |