diff --git a/main.cpp b/main.cpp index dcf5af4..5266ff1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -65,61 +66,71 @@ char *getBufferFromFile(const char* filename) return buffer; } -#define IGNOREWHITESPACE while (*p!=0 && *p <= 32) { p++; } -void loadLagueirtoFile() +static inline void trim(std::string &s) { + while (!s.empty() && isspace(s.front())) s.erase(s.begin()); + while (!s.empty() && isspace(s.back())) s.pop_back(); +} + +static inline std::string trim_copy(std::string s) { + trim(s); + return s; +} + +bool loadLagueirtoFile(const std::string §ion_to_load) { - char *buffer = getBufferFromFile("lagueirtofile"); - char *p = buffer; - char token[255]; - while (*p!=0) + std::ifstream f("lagueirtofile"); + if (!f) { std::cerr << "Cannot open lagueirtofile\n"; exit(1); } + + std::string line; + std::string current_section {"@none@"}; + bool found_any_section = false; + bool found_default = false; + bool active = false; + + while (std::getline(f, line)) { - IGNOREWHITESPACE; // Ignore whitespace - int i=0; - while (*p>32 && *p!='=') { token[i]=*p; i++; p++; } - IGNOREWHITESPACE - if (*p=='=') + // Quitar espacios al inicio y final + trim(line); + + if (line.empty() || line[0] == '#') + continue; + + // Detectar sección + if (line.front() == '[') { - token[i]=0; - int tokennum = -1; - for (int j=0;j getIncludes(std::string filename) -{ - char *buffer = getBufferFromFile(filename.c_str()); +std::vector getIncludes(const std::string &filename) { + std::ifstream f(filename); + if (!f) return {}; std::vector includes; + std::string line; - char *p = buffer; - while (*p != 0) - { - // Ignore commented lines - if ( (*p=='/') && (*(p+1)!=0) && (*(p+1)=='/')) while( (*p != 0) && (*p != 13) ) p++; + while (std::getline(f, line)) { + // Quitar comentarios de línea + auto pos = line.find("//"); + if (pos != std::string::npos) + line.resize(pos); - // Ignore comment blocks - if ( (*p=='/') && (*(p+1)!=0) && (*(p+1)=='*')) { - p+=2; - while( (*p != 0) && (*p != '*') && (*(p+1)!=0) && (*(p+1)!='/') ) p++; - } + // Buscar #include + pos = line.find("#include"); + if (pos == std::string::npos) + continue; - if (*p=='#') - { - if (textFound(p, "#include")) - { - p+=8; - while(strchr(" \t", *p)) p++; // Ignore whitespace (spaces and tabs) - if (*p == '"') - { - p++; - char *p2 = p; - while (*p2!='"' && *p2!=0 ) p2++; - if (*p2!=0) { - *p2=0; - includes.push_back(std::string(p)); - p = p2+1; - } - } - } - } - p++; + // Avanzar tras "#include" + pos += 8; + + // Saltar espacios + while (pos < line.size() && (line[pos] == ' ' || line[pos] == '\t')) + pos++; + + // Solo queremos includes con comillas + if (pos >= line.size() || line[pos] != '"') + continue; + + pos++; // saltar la primera comilla + size_t start = pos; + + // Buscar la comilla de cierre + while (pos < line.size() && line[pos] != '"') + pos++; + + if (pos < line.size()) + includes.push_back(line.substr(start, pos - start)); } - - free(buffer); + return includes; } -bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type object_file_write_time) -{ +bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type object_file_write_time) { auto include_files = getIncludes(file); - for (auto include : include_files) - { + for (auto include : include_files) { std::filesystem::path fullpath(file); auto path_without_filename = fullpath.remove_filename(); std::string src_path = path_without_filename.string(); std::string include_file = src_path + include; - if (!std::filesystem::exists(include_file)) - { + if (!std::filesystem::exists(include_file)) { std::cout << "WARNING: Include file '" << include_file << "' not found." << std::endl; - } - else - { + } else { auto include_file_write_time = std::filesystem::last_write_time(include_file); - if (include_file_write_time > object_file_write_time) - { + if (include_file_write_time > object_file_write_time) { return true; - } - else - { + } else { if (HeadersNeedRecompile(include_file, object_file_write_time)) return true; } } @@ -228,39 +230,29 @@ bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type obje return false; } -bool MustRecompile(std::string source_file) -{ +bool MustRecompile(std::string source_file) { std::string object_file = build_path + folder_char + getFileNameWithoutExtension(source_file)+".o"; // si el objecte no existeix, fa falta recompilar - if (!std::filesystem::exists(object_file)) - { + if (!std::filesystem::exists(object_file)) { return true; - } - else - { + } else { // Si sí que existeix, agafem la data de modificació auto object_file_write_time = std::filesystem::last_write_time(object_file); // Si la data de modificació del cpp es major que la del objecte, fa falta recompilar auto source_file_write_time = std::filesystem::last_write_time(source_file); - if (source_file_write_time > object_file_write_time) - { + if (source_file_write_time > object_file_write_time) { return true; - } - else - { - if (HeadersNeedRecompile(source_file, object_file_write_time)) - return true; + } else { + if (HeadersNeedRecompile(source_file, object_file_write_time)) return true; } } return false; } -void MaybeRecompile(std::string source_file) -{ - if (MustRecompile(source_file)) - { +void MaybeRecompile(std::string source_file) { + if (MustRecompile(source_file)) { std::string object_file = build_path + folder_char + getFileNameWithoutExtension(source_file)+".o"; must_link = true; std::string command = "g++ " + source_file + " " + cppflags + " -c -o " + object_file; @@ -269,116 +261,94 @@ void MaybeRecompile(std::string source_file) std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); system(command.c_str()); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + float t = float(std::chrono::duration_cast(end - begin).count())/1000000; std::cout << "(" << t << " seconds)" << std::endl; - } - else - { + } else { //std::cout << object_file << " està actualitzat" << std::endl; } } +void processCommand(std::string arg) { + // Do nothing for now +} + int main(int argc, char *argv[]) { - if (argc>1) { - for (int i=1; i(end_all - begin_all).count())/1000000; std::cout << "(" << t << " seconds)" << std::endl; - } - else - { + } else { std::cout << "Everything is up to date. Nothing to do." << std::endl; } } \ No newline at end of file