#include <fstream>
#include <sstream>
#include <algorithm>
+#include <boost/regex.hpp>
#include "creaWx.h"
#include "wx/dir.h"
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())
+ std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+
+ boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+\\s+ON");
+ std::string::const_iterator start, end;
+ start = CMfile.begin();
+ end = CMfile.end();
+ boost::match_results<std::string::const_iterator> what;
+ boost::match_flag_type flags = boost::match_default;
+ while(boost::regex_search(start, end, what, expression, flags))
{
- std::string line;
- while(!CMFile.eof())
+ //std::cout << what[0].str() << std::endl;
+ boost::regex expression1 = boost::regex("\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+");
+ std::string::const_iterator start1, end1;
+ start1 = what[0].first;
+ end1 = what[0].second;
+ boost::match_results<std::string::const_iterator> what1;
+ if(boost::regex_search(start1, end1, what1, expression1, flags))
{
- 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")
- {
- std::getline(CMFile, line, ')');
- CDMUtilities::splitter::split(lineSeg,line,"\t\n",CDMUtilities::splitter::no_empties);
- if(lineSeg.size() > 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())
- {
- res[correspondence[lineSeg[0]]] = true;
- }
- }
- }
+ std::string dete = what1.str();
+ CDMUtilities::normalizeStr(dete);
+ //std::cout << dete << std::endl;
+ if(correspondence.find(dete) != correspondence.end())
+ res[correspondence[dete]] = true;
}
- CMFile.close();
+ start = what[0].second;
}
}
return res;
bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
const bool& toInclude)
{
+ std::map<std::string, std::string> correspondence;
+
+ correspondence["VTK"] = "_USE_VTK";
+ correspondence["ITK"] = "_USE_ITK";
+ correspondence["GDCM"] = "_USE_GDCM";
+ correspondence["GDCM_VTK"] = "_USE_GDCM_VTK";
+ correspondence["GSMIS"] = "_USE_GSMIS";
+ correspondence["WxWidgets"] = "_USE_WXWIDGETS";
+ correspondence["KWWidgets"] = "_USE_KWWIDGETS";
+
+ if (correspondence.find(library_name) != correspondence.end())
+ {
+ std::string library_command = correspondence[library_name];
+// std::cout << "found correspondence " << library_command << std::endl;
+// std::cout.flush();
+ if (this->HasCMakeLists())
+ {
+ std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+ std::string resCMfile = "";
+ bool found = false;
+
+// std::cout << "found cmakefile: " << CMfile << std::endl;
+// std::cout.flush();
+
+ try {
+// std::cout << "first regex" << std::endl;
+// std::cout.flush();
+ boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
+
+ std::string::const_iterator start, end;
+ start = CMfile.begin();
+ end = CMfile.end();
+ boost::match_results<std::string::const_iterator> what;
+ boost::match_flag_type flags = boost::match_default;
+ if(boost::regex_search(start, end, what, expression, flags))
+ {
+// std::cout << "found " << what.str() << std::endl;
+// std::cout.flush();
+ found = true;
+ resCMfile += what.prefix().str();
+ if (toInclude)
+ resCMfile += what.str();
+ else
+ resCMfile += "#" + what.str();
+ resCMfile += what.suffix().str();
+
+ return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+ }
+ else
+ {
+// std::cout << "second regex" << std::endl;
+// std::cout.flush();
+ boost::regex expression("^\\h*#\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
+ if(boost::regex_search(start, end, what, expression, flags))
+ {
+ found = true;
+ resCMfile += what.prefix().str();
+ if(toInclude)
+ {
+ std::string dete = what[0].str();
+ for (int i = 0; i < dete.size(); ++i) {
+ if (dete[i] != '#')
+ resCMfile.push_back(dete[i]);
+ if (dete[i] == 'S')
+ {
+ resCMfile += dete.substr(i+1);
+ break;
+ }
+ }
+ }
+ else
+ resCMfile += what.str();
+
+ resCMfile += what.suffix().str();
+ return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+ }
+ else
+ {
+// std::cout << "third regex" << std::endl;
+// std::cout.flush();
+ boost::regex expression("^\\h*#\\h*UNCOMMENT EACH LIBRARY NEEDED \\(WILL BE FOUND AND USED AUTOMATICALLY\\)[^\\n]*\\n");
+ if(boost::regex_search(start, end, what, expression, flags))
+ {
+ found = true;
+ resCMfile += what.prefix().str();
+ resCMfile += what.str();
+ if(toInclude)
+ {
+ resCMfile += "SET(${BBTK_PACKAGE_NAME}"+ library_command +" ON)\n";
+ }
+ resCMfile += what.suffix().str();
+ return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+ }
+ }
+ }
+ } catch (boost::bad_expression& e) {
+ std::cout << "bad regex: " << e.what() << std::endl;
+ std::cout.flush();
+ }
+ }
+ }
return false;
}