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)
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<std::string> 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<std::string> 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<std::string> 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;
}
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<std::string> 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;
bool modelCDMApplication::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude)
{
+ std::map<std::string, std::string> 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<std::string> 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;
}
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<std::string> 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;
{
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<std::string> 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<std::string> 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;
}
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<std::string> 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;
bool modelCDMLibrary::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude)
{
+ std::map<std::string, std::string> 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<std::string> 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;
}
std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
{
std::map<std::string, std::string> 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<std::string, bool> res;
res["VTK"] = false;
res["ITK"] = false;
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<std::string> 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;
bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
const bool& toInclude)
{
+ std::map<std::string, std::string> 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;
}
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<std::string> 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<std::string> 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<std::string> 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;
}