diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-08 00:49:15 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-08 00:50:04 +0100 |
commit | bf24f6fc48b4eebb06cdcd7029d1d31d4c6028dd (patch) | |
tree | b9cb68dc44e002586a1e09aeb60bb42f75258758 /src/FileUtil.cpp | |
parent | 3fc89b69ff3fc937a0cde32c2fae9ce3b3cf1ebc (diff) |
Added loading of project file and file validations
Next up: parse project.conf file
Diffstat (limited to 'src/FileUtil.cpp')
-rw-r--r-- | src/FileUtil.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp new file mode 100644 index 0000000..511a324 --- /dev/null +++ b/src/FileUtil.cpp @@ -0,0 +1,56 @@ +#include "../include/FileUtil.hpp" +#include <cstdio> + +using namespace std; + +namespace sibs +{ + FileType getFileType(const char *path) + { + tinydir_file file; + if(tinydir_file_open(&file, path) == 0) + { + return file.is_dir ? FileType::DIRECTORY : FileType::REGULAR; + } + else + { + return FileType::FILE_NOT_FOUND; + } + } + + void walkDirectory(const char *directory, FileWalkCallbackFunc callbackFunc) + { + tinydir_dir dir; + tinydir_open(&dir, directory); + + while (dir.has_next) + { + tinydir_file file; + tinydir_readfile(&dir, &file); + callbackFunc(&file); + tinydir_next(&dir); + } + + tinydir_close(&dir); + } + + Result<string> getFileContent(const char *filepath) + { + FILE *file = fopen(filepath, "rb"); + if(!file || errno != 0) + { + perror(filepath); + return Result<string>::Err("Failed to open file"); + } + + fseek(file, 0, SEEK_END); + size_t fileSize = ftell(file); + fseek(file, 0, SEEK_SET); + + string result; + result.resize(fileSize); + fread(&result[0], 1, fileSize, file); + fclose(file); + return Result<string>::Ok(std::move(result)); + } +}
\ No newline at end of file |