From 7d3c9ec7368a5d25235e818c2c147ba7e89f6567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Sun, 27 Feb 2022 14:06:15 +0100 Subject: [+] Added base project --- src/main.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/main.cpp (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..333a841 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,108 @@ +#include "../inc/FileAssembler.h" +#include +#include +#include +#include +#include + + +using namespace std; +namespace fs = filesystem; + +void write_file(string path, string content){ + fstream f; + f.open(path, ios::out); + if (!f) { + cerr << "Error: swg: File '" << path << "' could not be created!" << endl; + exit(6); + }else{ + f << content; + f.close(); + } +} + +void recursive_directory_copy(const fs::path& src, const fs::path& dst) noexcept +{ +} + +int generateWebsite(string arg, string config){ + // cout << arg << " " << config << endl; + if(config[config.length()-1] != '/') + config += "/"; + FileAssembler *fa = new FileAssembler(config); + /* TODO + * clean or don't clean*/ + map pages = fa->get_pages(); + map posts = fa->get_posts(); + string target = fa->get_target(); + string index = fa->get_index(); + + if(!fs::create_directory(target)){ + cerr << "Error: swg: Directory '" << target << "' could not be created!" << endl; + exit(6); + } + if(!fs::create_directory(target+"/posts")){ + cerr << "Error: swg: Directory '" << target << "/posts' could not be created!" << endl; + exit(6); + } + map::iterator it = pages.begin(); + while(it != pages.end()){ + if(it->first == index){ + write_file(target+"/index.html", it->second); + }else{ + write_file(target+"/"+FileAssembler::lowercase(it->first)+".html", it->second); + } + it++; + } + + it = posts.begin(); + while(it != posts.end()){ + write_file(target+"/posts/"+FileAssembler::lowercase(it->first)+".html", it->second); + it++; + } + + if (false){ // Only copying the cached resources vs all of them + /** list resources = fa->get_cached_resources(); + + list::iterator res = resources.begin(); + while(res != resources.end()){ + error_code ec; + fs::copy(config+"resources/"+*res, target+"/"+*res,ec); + if(ec.value() != 0){ + cerr << "Error: swg: Resource file '" << *res << "' could not be copied!" << endl; + exit(6); + } + res ++; + } + **/ + }else{ + // Recursively copies all files and folders from src to target and overwrites existing files in target. + try{ + fs::copy(config+"resources/", target, fs::copy_options::overwrite_existing | fs::copy_options::recursive); + }catch (std::exception& e){ + std::cout << e.what(); // TODO better print + } + } + + return 0; +} + +int main(int argc, char * argv[]){ + for(int i = 1; i < argc; i++){ + if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") ){ + cout << "Usage: swg \nOptions are:" << endl; + cout << "Option list goes here" << endl; + exit(0); + }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "-g") || !strcmp(argv[i], "-cg")){ + }else{ //TODO - MAKE IT CORRECTLY - ACCEPT TWO PATH IF G IS SPECIFIED + if(i == argc-1){ //passes if not actual options specified because i == argc-1 + return generateWebsite(argv[i-1], argv[i]); + } + cerr << "ERROR: Invalid Command Line Option Found: \"" << argv[i] << "\"." << endl; + return 1; + } + } + + cerr << "ERROR: No Command Line Option Found. Type in --help or -h" << endl; + return 1; +} -- cgit v1.2.3