From bf24f6fc48b4eebb06cdcd7029d1d31d4c6028dd Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 8 Dec 2017 00:49:15 +0100 Subject: Added loading of project file and file validations Next up: parse project.conf file --- src/Conf.cpp | 18 ++++++++++++++ src/FileUtil.cpp | 56 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 src/Conf.cpp create mode 100644 src/FileUtil.cpp create mode 100644 src/main.cpp (limited to 'src') diff --git a/src/Conf.cpp b/src/Conf.cpp new file mode 100644 index 0000000..d535c60 --- /dev/null +++ b/src/Conf.cpp @@ -0,0 +1,18 @@ +#include "../include/Conf.hpp" +#include "../include/FileUtil.hpp" + +using namespace std; + +namespace sibs +{ + Result readConf(const char *filepath) + { + Result fileContentResult = getFileContent(filepath); + if(fileContentResult.isErr()) + return fileContentResult; + + string configData; + printf("file content:\n%s\n", fileContentResult.unwrap().c_str()); + return Result::Ok(std::move(configData)); + } +} \ No newline at end of file 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 + +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 getFileContent(const char *filepath) + { + FILE *file = fopen(filepath, "rb"); + if(!file || errno != 0) + { + perror(filepath); + return Result::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::Ok(std::move(result)); + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d49ae1f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,76 @@ +#include +#include "../include/FileUtil.hpp" +#include "../include/Conf.hpp" +#include + +using namespace std; +using namespace sibs; + +void usage() +{ + printf("Simple build system for native languages\n"); + printf("usage:\n"); + printf(" sibs \n"); + printf("option:\n"); + printf(" project path: Path containing project.conf\n"); + printf("examples:\n"); + printf(" sibs myProject"); + exit(1); +} + +void validateProjectPath(const char *projectPath) +{ + FileType projectPathFileType = getFileType(projectPath); + if(projectPathFileType == FileType::FILE_NOT_FOUND) + { + perror(projectPath); + exit(2); + } + else if(projectPathFileType == FileType::REGULAR) + { + printf("Expected project path (%s) to be a directory, was a file", projectPath); + exit(3); + } +} + +void validateProjectConfPath(const char *projectConfPath) +{ + FileType projectConfFileType = getFileType(projectConfPath); + if(projectConfFileType == FileType::FILE_NOT_FOUND) + { + perror(projectConfPath); + exit(4); + } + else if(projectConfFileType == FileType::DIRECTORY) + { + printf("Expected project conf (%s) to be a directory, was a file", projectConfPath); + exit(5); + } +} + + +int main(int argc, const char **argv) +{ + if(argc != 2) + usage(); + + const char *projectPath = argv[1]; + validateProjectPath(projectPath); + + string projectConfFilePath = projectPath; + projectConfFilePath += "/project.conf"; + validateProjectConfPath(projectConfFilePath.c_str()); + + auto result = readConf(projectConfFilePath.c_str()); + if(result.isOk()) + { + + } + else + { + printf("Failed to read config: %s\n", result.getErrMsg()); + exit(6); + } + + return 0; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2