]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMLib.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMLib.cpp
index 38cd30df188cdf8a06faf7c326f9272d7ad5380c..3dcf9b8786766ce64d0681718d4dc22f16a65f6b 100644 (file)
@@ -410,29 +410,25 @@ void modelCDMLib::CheckStructure(std::map<std::string, bool>& 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<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;
@@ -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<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;
 }