diff --git a/CompilerFileList.txt b/CompilerFileList.txt new file mode 100644 index 0000000..b2789fd --- /dev/null +++ b/CompilerFileList.txt @@ -0,0 +1,9 @@ +# main prog +main.cpp + +# std version +-std=c++17 + +# warnings +-Wall +-Wextra diff --git a/compiler b/compiler new file mode 100755 index 0000000..0430733 Binary files /dev/null and b/compiler differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d426f12 --- /dev/null +++ b/main.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + +#include + +// Functions decllaration +bool read_file(std::ifstream &compilerfilelist, std::vector &includesfilesnames); +void generate_command(const std::vector &includesfilesnames, std::string &command); +std::string color(int r, int g, int b); +std::string color(); +std::string error_msg(); + +// Main +int main(const int argc, const char *argv[]) +{ + if(argc != 2) + { + std::cerr << error_msg() << "Vous n'avez pas entré le bon nombre d'arguments.\n"; + return 1; + } + + std::string nomfichiersortie {argv[1]}; + + if(std::empty(nomfichiersortie)) + { + std::cerr << error_msg() << "Vous devez indiquer un nom de sortie.\n"; + return 1; + } + + for(const char character : nomfichiersortie) + { + if((character < 65 || character > 90) && (character < 97 || character > 122) && character != '.' && character != '/' && character != '_' && (character < 48 && character > 57)) + { + std::cerr << error_msg() << "Nom de fichier de sortie non valide : \"" << nomfichiersortie << "\"\n"; + return 1; + } + } + + std::ifstream compilerfilelist {"CompilerFileList.txt"}; + if(!compilerfilelist) + { + std::cerr << error_msg() << "Le fichier \"CompilerFileList.txt\" n'existe pas." << std::endl; + return 1; + } + + std::vector includesfilesnames {}; + bool error {read_file(compilerfilelist, includesfilesnames)}; + if(error == true) + { + std::cerr << '\n' << error_msg() << "Impossible de lancer la creation de la commande de compilation, certain noms de fichier contenus dans \"CompilerFileList.txt\" ne sont pas corrects.\n"; + return 1; + } + + if(std::empty(includesfilesnames)) + { + std::cerr << '\n' << error_msg() << "Le fichier \"CompilerFileList.txt\" ne contient aucun nom de fichier."; + return 1; + } + + std::string command {"g++ -o " + nomfichiersortie + " "}; + generate_command(includesfilesnames, command); + + std::cout << "\nVoici la commande qui vas être executée :\n\"" << command << "\"\n\n"; + + system(command.c_str()); +/* + std::cout << "\nLe programme vas être executé...\n\n"; + + command = "./" + nomfichiersortie; + system(command.c_str()); + + std::cout << "\nFin du programme.\n"; +*/ + return 0; +} + +// Functions definition +bool read_file(std::ifstream &compilerfilelist, std::vector &includesfilesnames) +{ + std::string includefilename {}; + + unsigned int line_num {0}; + bool error {false}; + + while(std::getline(compilerfilelist, includefilename)) + { + line_num++; + + if(includefilename.front() == '#' && includefilename.size() > 1) + { + if(line_num != 1) + std::cout << '\n'; + std::string category {std::begin(includefilename) + 1, std::end(includefilename)}; + std::cout << "-- " << color(59, 80, 255) << "Catégorie" << color() << category << '\n'; + } + else if(!(std::empty(includefilename)) && includefilename.front() != '#') + { + for(const char &character : includefilename) + { + if((character < 65 || character > 90) && (character < 97 || character > 122) && character != '.' && character != '/' && character != '_' && character != '-' && (character < 48 && character > 57) && character != '$' && character != '(' && character != ')') + { + std::cout << error_msg() << "Ligne " << line_num << " dans \"CompilerFileList.txt\". Nom d'argument invalide : \"" << includefilename << "\"\n"; + error = true; + break; + } + } + + if(error == false) + { + includesfilesnames.push_back(includefilename); + std::cout << color(37, 155, 36) << "Réussite" << color() << " : Argument \"" << includefilename << "\" ajouté à la liste des arguments à donner au compilateur.\n"; + } + } + } + + return error; +} + +void generate_command(const std::vector &includesfilesnames, std::string &command) +{ + for(const std::string &includefilename : includesfilesnames) + { + command += includefilename + " "; + } + + command.pop_back(); +} + +std::string color(int r, int g, int b) +{ + assert(r < 256); + assert(g < 256); + assert(b < 256); + assert(r >= 0); + assert(g >= 0); + assert(b >= 0); + + std::string colorstr {"\e[38;2;"}; + colorstr += std::to_string(r); + colorstr += ';'; + colorstr += std::to_string(g); + colorstr += ';'; + colorstr += std::to_string(b); + colorstr += 'm'; + + return colorstr; +} + +std::string color() +{ + return "\e[m"; +} + +std::string error_msg() +{ + return color(208, 23, 22) + "Erreur" + color() + ": "; +}