From ef630179c0efdb37f43994958b727f585cf2c3ec Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Tue, 9 Apr 2013 18:39:56 +0200 Subject: [PATCH] Feature #1711 CreaDevManager application implementation Feature: Now including and excluding libraries in CMakeLists files for: Applications, Libraries. Feature: Read included libraries in CMakeLists files for Packages. (Not writing, just reading) --- lib/creaDevManagerLib/modelCDMAppli.cpp | 162 ++++++++---------- lib/creaDevManagerLib/modelCDMApplication.cpp | 160 ++++++++++++----- lib/creaDevManagerLib/modelCDMLib.cpp | 141 ++++++--------- lib/creaDevManagerLib/modelCDMLibrary.cpp | 161 ++++++++++++----- lib/creaDevManagerLib/modelCDMPackage.cpp | 156 +++++++++++++---- lib/creaDevManagerLib/modelCDMProject.cpp | 141 +++++++-------- 6 files changed, 545 insertions(+), 376 deletions(-) diff --git a/lib/creaDevManagerLib/modelCDMAppli.cpp b/lib/creaDevManagerLib/modelCDMAppli.cpp index 7bcc4a2..c66cbb8 100644 --- a/lib/creaDevManagerLib/modelCDMAppli.cpp +++ b/lib/creaDevManagerLib/modelCDMAppli.cpp @@ -486,118 +486,92 @@ void modelCDMAppli::CheckStructure(std::map& properties) bool modelCDMAppli::IsApplicationIncluded(const std::string& application_name) { - if(this->HasCMakeLists()) - { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) - { - std::string line; - while(!CMFile.eof()) - { - std::getline(CMFile, line); - while(line[0]==' ') - line.erase(0,1); - if(line[0] != '#') - { - std::vector lineSeg; - CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties); - if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == application_name) - { - CMFile.close(); - return true; - } - } - } - CMFile.close(); - } - } - return false; + if (this->HasCMakeLists()) + { + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + for (int i = 0; i < cmlFile.size(); ++i) + { + if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY") + { + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#') + { + if (application_name == cmlFile[i].second[pos]) + return true; + break; + } + pos++; + } + } + } + } + return false; } bool modelCDMAppli::SetApplicationInclude(const std::string& application_name, const bool& toInclude) { if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + + bool found = false; + + for (int i = 0; i < cmlFile.size(); ++i) { - std::stringstream outs; - std::string line; - bool found = false; - while(!CMFile.eof()) + if(toInclude && cmlFile[i].first == "comment") { - std::getline(CMFile, line); - if (CMFile.eof()) { - break; - } - if(line != "") + std::vector segments; + std::string line = cmlFile[i].second[0]; + while(line[0] == '#') + line.erase(0,1); + + CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties); + if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == application_name) { - std::vector segs; - CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties); - //is comment - if(segs.size() > 0 && segs[0][0] == '#') - { - if(toInclude) - { - CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == application_name) - { - found = true; - outs << "ADD_SUBDIRECTORY(" << application_name << ")\n"; - } - else - outs << line << "\n"; - } - else - { - outs << line << "\n"; - } - } - //is not comment - else + found = true; + while(cmlFile[i].second[0][0] == '#') + cmlFile[i].second[0].erase(0,1); + } + } + else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY") + { + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#') { - if (segs.size() > 0 && !toInclude) - { - CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == application_name) - { - outs << "#" << line << "\n"; - } - else - { - outs << line << "\n"; - } - } - else + if (application_name == cmlFile[i].second[pos]) { - CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == application_name) + found = true; + if (!toInclude) { - found = true; + cmlFile[i].first = "comment"; + cmlFile[i].second[0] = "#" + cmlFile[i].second[0]; + while (cmlFile[i].second.size() > 1) + { + cmlFile[i].second[0] += cmlFile[i].second[1]; + cmlFile[i].second.erase(cmlFile[i].second.begin()+1); + } + } - outs << line << "\n"; } + break; } - } - else - { - outs << "\n"; + pos++; } } - - CMFile.close(); - - if(!found && toInclude) - outs << "ADD_SUBDIRECTORY(" << application_name << ")\n"; - - std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str()); - if (CMFileOut.is_open()) - { - CMFileOut << outs.rdbuf(); - CMFileOut.close(); - return true; - } } + if (!found && toInclude) + { + CDMUtilities::syntaxElement element; + element.first = "command"; + element.second.push_back("ADD_SUBDIRECTORY(" + application_name + ")"); + cmlFile.push_back(element); + } + + return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile); } return false; } diff --git a/lib/creaDevManagerLib/modelCDMApplication.cpp b/lib/creaDevManagerLib/modelCDMApplication.cpp index c15bedf..0ee18a9 100644 --- a/lib/creaDevManagerLib/modelCDMApplication.cpp +++ b/lib/creaDevManagerLib/modelCDMApplication.cpp @@ -534,57 +534,41 @@ std::map modelCDMApplication::Get3rdPartyLibraries() res["GDCM"] = false; res["Boost"] = false; - if(this->HasCMakeLists()) + if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + // look at every syntax element + for (int i = 0; i < cmlFile.size(); ++i) { - std::string line; - while(!CMFile.eof()) + // if the element is a command and is a SET command + if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET") { - std::getline(CMFile, line, '('); - - std::vector lineSeg; - CDMUtilities::splitter::split(lineSeg,line,"\n ",CDMUtilities::splitter::no_empties); - - if(lineSeg.size() > 0 && lineSeg[lineSeg.size()-1] == "SET") + // search first parameter + int pos = 1; + while (pos < cmlFile[i].second.size()) { - //std::cout << "found set" << std::endl; - std::getline(CMFile, line, ')'); - //std::cout << line << std::endl; - CDMUtilities::splitter::split(lineSeg,line,"\n",CDMUtilities::splitter::no_empties); - if(lineSeg.size() > 0) + // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES + if (cmlFile[i].second[pos] == "${EXE_NAME}_LINK_LIBRARIES") { - //std::cout << lineSeg[0] << std::endl; - for(int i = 0; i < lineSeg[0].size(); ++i) - if (lineSeg[0][i] == ' ') - { - lineSeg[0].erase(i,1); - --i; - } - //std::cout << lineSeg[0] << std::endl; - if (lineSeg[0] == "${EXE_NAME}_LINK_LIBRARIES") + pos++; + // look for all the third party libraries included + while (pos < cmlFile[i].second.size()) { - //std::cout << "link" << std::endl; - for (int l = 1; l < lineSeg.size(); ++l) + if (cmlFile[i].second[pos][0] == '$' && correspondence.find(cmlFile[i].second[pos]) != correspondence.end()) { - for(int i = 0;i < lineSeg[l].size(); i++) - if (lineSeg[l][i] == ' ') - { - lineSeg[l].erase(i,1); - i--; - } - if(lineSeg[l].size() > 0 && lineSeg[l][0] == '$') - { - //std::cout << "found " << lineSeg[l] << std::endl; - res[correspondence[lineSeg[l]]] = true; - } + res[correspondence[cmlFile[i].second[pos]]] = true; } + pos++; } } + // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command + else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') + { + break; + } + pos++; } } - CMFile.close(); } } return res; @@ -592,6 +576,104 @@ std::map modelCDMApplication::Get3rdPartyLibraries() bool modelCDMApplication::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude) { + std::map correspondence; + + correspondence["Crea"] = "${crea_LIBRARIES}"; + correspondence["WxWidgets"] = "${WXWIDGETS_LIBRARIES}"; + correspondence["KWWidgets"] = "${KWWidgets_LIBRARIES}"; + correspondence["VTK"] = "${VTK_LIBRARIES}"; + correspondence["ITK"] = "${ITK_LIBRARIES}"; + correspondence["GDCM"] = "${GDCM_LIBRARIES}"; + correspondence["Boost"] = "${BOOST_LIBRARIES}"; + + if (correspondence.find(library_name) != correspondence.end()) + { + std::string library_command = correspondence[library_name]; + if (this->HasCMakeLists()) + { + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath()); + + // look at every syntax element + for (int i = 0; i < cmlFile.size(); ++i) + { + // if the element is a command and is a SET command + if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET") + { + // search first parameter + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES + if (cmlFile[i].second[pos] == "${EXE_NAME}_LINK_LIBRARIES") + { + bool found = false; + pos++; + // look for all the third party libraries included + while (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] != ")") + { + // if is library_command then found + if (cmlFile[i].second[pos] == library_command) + { + found = true; + //if toInclude is false the make it a comment + if (!toInclude) + { + cmlFile[i].second[pos] = "#" + cmlFile[i].second[pos]; + std::cout << "library commented: " << library_name << std::endl; + } + break; + } + else if (toInclude && cmlFile[i].second[pos][0] == '#') + { + std::vector segments; + CDMUtilities::splitter::split(segments, cmlFile[i].second[pos], "#", CDMUtilities::splitter::no_empties); + if(segments.size()) + { + for (int j = 0; j < segments[0].size(); ++j) + { + if(isspace(segments[0][j])) + { + segments[0].erase(j,1); + j--; + } + } + + if(segments[0].size() && segments[0] == library_command) + { + found = true; + while(cmlFile[i].second[pos][0] == '#') + { + cmlFile[i].second[pos].erase(0,1); + } + std::cout << "library uncommented: " << library_name << std::endl; + break; + } + } + } + pos++; + } + + //if the library was not found and is an inclusion then include it + if (!found && toInclude && pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == ")") + { + cmlFile[i].second.insert(cmlFile[i].second.begin()+pos, library_command); + cmlFile[i].second.insert(cmlFile[i].second.begin()+pos+1, "\n"); + std::cout << "library included: " << library_name << std::endl; + } + } + // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command + else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') + { + break; + } + pos++; + } + } + } + + return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath(), cmlFile); + } + } return false; } diff --git a/lib/creaDevManagerLib/modelCDMLib.cpp b/lib/creaDevManagerLib/modelCDMLib.cpp index 38cd30d..3dcf9b8 100644 --- a/lib/creaDevManagerLib/modelCDMLib.cpp +++ b/lib/creaDevManagerLib/modelCDMLib.cpp @@ -410,29 +410,25 @@ void modelCDMLib::CheckStructure(std::map& properties) bool modelCDMLib::IsLibraryIncluded(const std::string& library_name) { - if(this->HasCMakeLists()) + if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + for (int i = 0; i < cmlFile.size(); ++i) { - std::string line; - while(!CMFile.eof()) + if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY") { - std::getline(CMFile, line); - while(line[0]==' ') - line.erase(0,1); - if(line[0] != '#') + int pos = 1; + while (pos < cmlFile[i].second.size()) { - std::vector lineSeg; - CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties); - if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == library_name) + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#') { - CMFile.close(); - return true; + if (library_name == cmlFile[i].second[pos]) + return true; + break; } + pos++; } } - CMFile.close(); } } return false; @@ -442,87 +438,64 @@ bool modelCDMLib::SetLibraryInclude(const std::string& library_name, const bool& { if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + + bool found = false; + + for (int i = 0; i < cmlFile.size(); ++i) { - std::stringstream outs; - std::string line; - bool found = false; - while(!CMFile.eof()) + if(toInclude && cmlFile[i].first == "comment") { - std::getline(CMFile, line); - if (CMFile.eof()) { - break; - } - if(line != "") + std::vector segments; + std::string line = cmlFile[i].second[0]; + while(line[0] == '#') + line.erase(0,1); + + CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties); + if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == library_name) { - std::vector segs; - CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties); - //is comment - if(segs.size() > 0 && segs[0][0] == '#') - { - if(toInclude) - { - CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name) - { - found = true; - outs << "ADD_SUBDIRECTORY(" << library_name << ")\n"; - } - else - outs << line << "\n"; - } - else - { - outs << line << "\n"; - } - } - //is not comment - else + found = true; + while(cmlFile[i].second[0][0] == '#') + cmlFile[i].second[0].erase(0,1); + } + } + else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY") + { + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#') { - if (segs.size() > 0 && !toInclude) + if (library_name == cmlFile[i].second[pos]) { - CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name) + found = true; + if (!toInclude) { - outs << "#" << line << "\n"; - } - else - { - outs << line << "\n"; - } - } - else - { - CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name) - { - found = true; + cmlFile[i].first = "comment"; + cmlFile[i].second[0] = "#" + cmlFile[i].second[0]; + while (cmlFile[i].second.size() > 1) + { + cmlFile[i].second[0] += cmlFile[i].second[1]; + cmlFile[i].second.erase(cmlFile[i].second.begin()+1); + } + } - outs << line << "\n"; } + break; } + pos++; } - else - { - outs << "\n"; - } - - } - - CMFile.close(); - - if(!found && toInclude) - outs << "ADD_SUBDIRECTORY(" << library_name << ")\n"; - - std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str()); - if (CMFileOut.is_open()) - { - CMFileOut << outs.rdbuf(); - CMFileOut.close(); - return true; } } + if (!found && toInclude) + { + CDMUtilities::syntaxElement element; + element.first = "command"; + element.second.push_back("ADD_SUBDIRECTORY(" + library_name + ")"); + cmlFile.push_back(element); + } + + return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile); } return false; } diff --git a/lib/creaDevManagerLib/modelCDMLibrary.cpp b/lib/creaDevManagerLib/modelCDMLibrary.cpp index 9bac184..fd0e084 100644 --- a/lib/creaDevManagerLib/modelCDMLibrary.cpp +++ b/lib/creaDevManagerLib/modelCDMLibrary.cpp @@ -453,58 +453,41 @@ std::map modelCDMLibrary::Get3rdPartyLibraries() res["GDCM"] = false; res["Boost"] = false; - if(this->HasCMakeLists()) + if (this->HasCMakeLists()) { - CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + // look at every syntax element + for (int i = 0; i < cmlFile.size(); ++i) { - std::string line; - while(!CMFile.eof()) + // if the element is a command and is a SET command + if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET") { - std::getline(CMFile, line, '('); - - std::vector lineSeg; - CDMUtilities::splitter::split(lineSeg,line,"\n ",CDMUtilities::splitter::no_empties); - - if(lineSeg.size() > 0 && lineSeg[lineSeg.size()-1] == "SET") + // search first parameter + int pos = 1; + while (pos < cmlFile[i].second.size()) { - //std::cout << "found set" << std::endl; - std::getline(CMFile, line, ')'); - //std::cout << line << std::endl; - CDMUtilities::splitter::split(lineSeg,line,"\n",CDMUtilities::splitter::no_empties); - if(lineSeg.size() > 0) + // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES + if (cmlFile[i].second[pos] == "${LIBRARY_NAME}_LINK_LIBRARIES") { - //std::cout << lineSeg[0] << std::endl; - for(int i = 0; i < lineSeg[0].size(); ++i) - if (lineSeg[0][i] == ' ') - { - lineSeg[0].erase(i,1); - --i; - } - //std::cout << lineSeg[0] << std::endl; - if (lineSeg[0] == "${LIBRARY_NAME}_LINK_LIBRARIES") + pos++; + // look for all the third party libraries included + while (pos < cmlFile[i].second.size()) { - //std::cout << "link" << std::endl; - for (int l = 1; l < lineSeg.size(); ++l) + if (cmlFile[i].second[pos][0] == '$' && correspondence.find(cmlFile[i].second[pos]) != correspondence.end()) { - for(int i = 0;i < lineSeg[l].size(); i++) - if (lineSeg[l][i] == ' ') - { - lineSeg[l].erase(i,1); - i--; - } - if(lineSeg[l].size() > 0 && lineSeg[l][0] == '$') - { - //std::cout << "found " << lineSeg[l] << std::endl; - res[correspondence[lineSeg[l]]] = true; - } + res[correspondence[cmlFile[i].second[pos]]] = true; } + pos++; } } + // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command + else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') + { + break; + } + pos++; } } - CMFile.close(); } } return res; @@ -512,6 +495,104 @@ std::map modelCDMLibrary::Get3rdPartyLibraries() bool modelCDMLibrary::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude) { + std::map correspondence; + + correspondence["Crea"] = "${crea_LIBRARIES}"; + correspondence["WxWidgets"] = "${WXWIDGETS_LIBRARIES}"; + correspondence["KWWidgets"] = "${KWWidgets_LIBRARIES}"; + correspondence["VTK"] = "${VTK_LIBRARIES}"; + correspondence["ITK"] = "${ITK_LIBRARIES}"; + correspondence["GDCM"] = "${GDCM_LIBRARIES}"; + correspondence["Boost"] = "${BOOST_LIBRARIES}"; + + if (correspondence.find(library_name) != correspondence.end()) + { + std::string library_command = correspondence[library_name]; + if (this->HasCMakeLists()) + { + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath()); + + // look at every syntax element + for (int i = 0; i < cmlFile.size(); ++i) + { + // if the element is a command and is a SET command + if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET") + { + // search first parameter + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES + if (cmlFile[i].second[pos] == "${LIBRARY_NAME}_LINK_LIBRARIES") + { + bool found = false; + pos++; + // look for all the third party libraries included + while (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] != ")") + { + // if is library_command then found + if (cmlFile[i].second[pos] == library_command) + { + found = true; + //if toInclude is false the make it a comment + if (!toInclude) + { + cmlFile[i].second[pos] = "#" + cmlFile[i].second[pos]; + std::cout << "library commented: " << library_name << std::endl; + } + break; + } + else if (toInclude && cmlFile[i].second[pos][0] == '#') + { + std::vector segments; + CDMUtilities::splitter::split(segments, cmlFile[i].second[pos], "#", CDMUtilities::splitter::no_empties); + if(segments.size()) + { + for (int j = 0; j < segments[0].size(); ++j) + { + if(isspace(segments[0][j])) + { + segments[0].erase(j,1); + j--; + } + } + + if(segments[0].size() && segments[0] == library_command) + { + std::cout << "library uncommented: " << library_name << std::endl; + found = true; + while(cmlFile[i].second[pos][0] == '#') + { + cmlFile[i].second[pos].erase(0,1); + } + break; + } + } + } + pos++; + } + + // if the library was not found and is an inclusion then include it. + if (!found && toInclude && pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == ")") + { + cmlFile[i].second.insert(cmlFile[i].second.begin()+pos, library_command); + cmlFile[i].second.insert(cmlFile[i].second.begin()+pos+1, "\n"); + std::cout << "library included: " << library_name << std::endl; + } + } + // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command + else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') + { + break; + } + pos++; + } + } + } + + return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath(), cmlFile); + } + } return false; } diff --git a/lib/creaDevManagerLib/modelCDMPackage.cpp b/lib/creaDevManagerLib/modelCDMPackage.cpp index 81fb906..c6ea443 100644 --- a/lib/creaDevManagerLib/modelCDMPackage.cpp +++ b/lib/creaDevManagerLib/modelCDMPackage.cpp @@ -693,13 +693,13 @@ void modelCDMPackage::CheckStructure(std::map& properties) std::map modelCDMPackage::Get3rdPartyLibraries() { std::map correspondence; - correspondence["${BBTK_PACKAGE_NAME}_USE_VTKON"] = "VTK"; - correspondence["${BBTK_PACKAGE_NAME}_USE_ITKON"] = "ITK"; - correspondence["${BBTK_PACKAGE_NAME}_USE_GDCMON"] = "GDCM"; - correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTKON"] = "GDCM_VTK"; - correspondence["${BBTK_PACKAGE_NAME}_USE_GSMISON"] = "GSMIS"; - correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETSON"] = "WxWidgets"; - correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETSON"] = "KWWidgets"; + correspondence["${BBTK_PACKAGE_NAME}_USE_VTK"] = "VTK"; + correspondence["${BBTK_PACKAGE_NAME}_USE_ITK"] = "ITK"; + correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM"] = "GDCM"; + correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTK"] = "GDCM_VTK"; + correspondence["${BBTK_PACKAGE_NAME}_USE_GSMIS"] = "GSMIS"; + correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETS"] = "WxWidgets"; + correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETS"] = "KWWidgets"; std::map res; res["VTK"] = false; res["ITK"] = false; @@ -709,45 +709,46 @@ std::map modelCDMPackage::Get3rdPartyLibraries() res["WxWidgets"] = false; res["KWWidgets"] = false; - if(this->HasCMakeLists()) + if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + // look at every syntax element + for (int i = 0; i < cmlFile.size(); ++i) { - std::string line; - while(!CMFile.eof()) + // if the element is a command and is a SET command + if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET") { - std::getline(CMFile, line, '('); - - std::vector lineSeg; - CDMUtilities::splitter::split(lineSeg,line,"\n",CDMUtilities::splitter::no_empties); - for(int i = 0; lineSeg.size() > 0 && i < lineSeg[lineSeg.size()-1].size(); ++i) - if (lineSeg[lineSeg.size()-1][i] == ' ') - { - lineSeg[lineSeg.size()-1].erase(i,1); - --i; - } - if(lineSeg.size() > 0 && lineSeg[lineSeg.size()-1][0]!='#' && lineSeg[lineSeg.size()-1] == "SET") + // search first parameter + int pos = 1; + while (pos < cmlFile[i].second.size()) { - std::getline(CMFile, line, ')'); - CDMUtilities::splitter::split(lineSeg,line,"\t\n",CDMUtilities::splitter::no_empties); - if(lineSeg.size() > 0) + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') { - for(int i = 0; i < lineSeg[0].size(); ++i) - if (lineSeg[0][i] == ' ') - { - lineSeg[0].erase(i,1); - --i; - } - - if(lineSeg[0].size() > 0 && lineSeg[0][0] == '$' && correspondence.find(lineSeg[0]) != correspondence.end()) + break; + } + pos++; + } + // if the first parameter is a third party statement + if (pos < cmlFile[i].second.size() && correspondence.find(cmlFile[i].second[pos]) != correspondence.end()) + { + std::string foundLibrary = cmlFile[i].second[pos]; + // search for second parameter + pos++; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') { - res[correspondence[lineSeg[0]]] = true; + break; } + pos++; + } + // if the second parameter is ON + if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == "ON") + { + res[correspondence[foundLibrary]] = true; } } } - CMFile.close(); } } return res; @@ -756,6 +757,89 @@ std::map modelCDMPackage::Get3rdPartyLibraries() bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude) { + std::map correspondence; + + correspondence["VTK"] = "${BBTK_PACKAGE_NAME}_USE_VTK"; + correspondence["ITK"] = "${BBTK_PACKAGE_NAME}_USE_ITK"; + correspondence["GDCM"] = "${BBTK_PACKAGE_NAME}_USE_GDCM"; + correspondence["GDCM_VTK"] = "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK"; + correspondence["GSMIS"] = "${BBTK_PACKAGE_NAME}_USE_GSMIS"; + correspondence["WxWidgets"] = "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS"; + correspondence["KWWidgets"] = "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS"; +/* + if(correspondence.find(library_name) != correspondence.end()) + { + std::string library_command = correspondence[library_name]; + if (this->HasCMakeLists()) + { + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + bool found = false; + // look at every syntax element + for (int i = 0; i < cmlFile.size(); ++i) + { + // if the element is a command and is a SET command + if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET") + { + // search first parameter + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') + { + break; + } + pos++; + } + // if the first parameter is a third party statement + if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == library_command) + { + std::string foundLibrary = cmlFile[i].second[pos]; + // search for second parameter + pos++; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')') + { + break; + } + pos++; + } + // if the second parameter is ON + if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == "ON") + { + found = true; + if (toInclude) + { + std::cout << "library already included: " << library_name << std::endl; + } + else + { + cmlFile[i].first = "comment"; + cmlFile[i].second[0] = "#" + cmlFile[i].second[0]; + std::cout << "library include commented: " << library_name << std::endl; + } + } + else if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == "OFF") + { + found = true; + if (toInclude) + { + cmlFile[i].second[pos] = "ON"; + std::cout << "library include activated: " << library_name << std::endl; + } + else + { + std::cout << "library include already disabled: " << library_name << std::endl; + } + } + } + } else if (cmlFile[i].first == "comment") { + //TODO: check if it is commented + } + } + } + } +*/ return false; } diff --git a/lib/creaDevManagerLib/modelCDMProject.cpp b/lib/creaDevManagerLib/modelCDMProject.cpp index d26932d..08cafc7 100644 --- a/lib/creaDevManagerLib/modelCDMProject.cpp +++ b/lib/creaDevManagerLib/modelCDMProject.cpp @@ -1012,118 +1012,93 @@ void modelCDMProject::CheckStructure(std::map& properties) bool modelCDMProject::IsPackageIncluded(const std::string& package_name) { - if(this->HasCMakeLists()) + if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + for (int i = 0; i < cmlFile.size(); ++i) { - std::string line; - while(!CMFile.eof()) + if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY") { - std::getline(CMFile, line); - while(line[0]==' ') - line.erase(0,1); - if(line[0] != '#') + int pos = 1; + while (pos < cmlFile[i].second.size()) { - std::vector lineSeg; - CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties); - if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == package_name) + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#') { - CMFile.close(); - return true; + if (package_name == cmlFile[i].second[pos]) + return true; + break; } + pos++; } } - CMFile.close(); } } return false; + } bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude) { if (this->HasCMakeLists()) { - std::ifstream CMFile(this->CMakeLists->GetPath().c_str()); - if (CMFile.is_open()) + CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str()); + + bool found = false; + + for (int i = 0; i < cmlFile.size(); ++i) { - std::stringstream outs; - std::string line; - bool found = false; - while(!CMFile.eof()) + if(toInclude && cmlFile[i].first == "comment") { - std::getline(CMFile, line); - if (CMFile.eof()) { - break; - } - if(line != "") + std::vector segments; + std::string line = cmlFile[i].second[0]; + while(line[0] == '#') + line.erase(0,1); + + CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties); + if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name) { - std::vector segs; - CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties); - //is comment - if(segs.size() > 0 && segs[0][0] == '#') - { - if(toInclude) - { - CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == package_name) - { - found = true; - outs << "ADD_SUBDIRECTORY(" << package_name << ")\n"; - } - else - outs << line << "\n"; - } - else - { - outs << line << "\n"; - } - } - //is not comment - else + found = true; + while(cmlFile[i].second[0][0] == '#') + cmlFile[i].second[0].erase(0,1); + } + } + else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY") + { + int pos = 1; + while (pos < cmlFile[i].second.size()) + { + if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#') { - if (segs.size() > 0 && !toInclude) - { - CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == package_name) - { - outs << "#" << line << "\n"; - } - else - { - outs << line << "\n"; - } - } - else + if (package_name == cmlFile[i].second[pos]) { - CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties); - if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == package_name) + found = true; + if (!toInclude) { - found = true; + cmlFile[i].first = "comment"; + cmlFile[i].second[0] = "#" + cmlFile[i].second[0]; + while (cmlFile[i].second.size() > 1) + { + cmlFile[i].second[0] += cmlFile[i].second[1]; + cmlFile[i].second.erase(cmlFile[i].second.begin()+1); + } + } - outs << line << "\n"; } + break; } - } - else - { - outs << "\n"; + pos++; } } - - CMFile.close(); - - if(!found && toInclude) - outs << "ADD_SUBDIRECTORY(" << package_name << ")\n"; - - std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str()); - if (CMFileOut.is_open()) - { - CMFileOut << outs.rdbuf(); - CMFileOut.close(); - return true; - } } + if (!found && toInclude) + { + CDMUtilities::syntaxElement element; + element.first = "command"; + element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")"); + cmlFile.push_back(element); + } + + return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile); } return false; } -- 2.47.1