#include "modelCDMLib.h"
+#include <fstream>
+
#include "CDMUtilities.h"
#include "creaWx.h"
#include "wx/dir.h"
std::string pathFixed(CDMUtilities::fixPath(path));
this->libraries.clear();
- wxDir dir(crea::std2wx((pathFixed).c_str()));
- if (dir.IsOpened())
- {
- wxString fileName;
- bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
- while (cont)
- {
- std::string stdfileName = crea::wx2std(fileName);
-
- modelCDMLibrary* library = new modelCDMLibrary(pathFixed + "/" + stdfileName, this->level + 1);
- this->libraries.push_back(library);
- this->children.push_back(library);
-
- cont = dir.GetNext(&fileName);
- }
-
- }
- this->SortChildren();
-}
-
-modelCDMLib::~modelCDMLib()
-{
- for (int i = 0; i < this->libraries.size(); i++)
+ wxDir dir(crea::std2wx((pathFixed).c_str()));
+ if (dir.IsOpened())
{
- if(this->libraries[i] != NULL)
+ wxString fileName;
+ bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+ while (cont)
{
- delete this->libraries[i];
- this->libraries[i] = NULL;
+ std::string stdfileName = crea::wx2std(fileName);
+
+ modelCDMLibrary* library = new modelCDMLibrary(pathFixed + "/" + stdfileName, this->level + 1);
+ this->libraries.push_back(library);
+ this->children.push_back(library);
+
+ cont = dir.GetNext(&fileName);
}
+
}
+ this->SortChildren();
+}
+
+modelCDMLib::~modelCDMLib()
+{
}
-bool modelCDMLib::CreateLibrary(
+modelCDMIProjectTreeNode* modelCDMLib::CreateLibrary(
const std::string& name,
std::string*& result,
const std::string& path
)
{
- //TODO: implement method
- return true;
+ //copy template library folder with new name
+ std::string copyCommand = "cp -r " + this->path + "/template_lib " + this->path + "/" + name;
+ if(system(copyCommand.c_str()))
+ {
+ result = new std::string("An error occurred while running '" + copyCommand + "'.");
+ return NULL;
+ }
+ //TODO: set name of library in CMakeLists inside copied folder
+ std::string line;
+ std::ifstream in((this->path + "/" + name + "/CMakeLists.txt").c_str());
+ if( !in.is_open())
+ {
+ result = new std::string("CMakeLists.txt file failed to open.");
+ return NULL;
+ }
+ std::ofstream out((this->path + "/" + name + "/CMakeLists.txt.tmp").c_str());
+ while (getline(in, line))
+ {
+ if(line == "SET ( LIBRARY_NAME MyLib )")
+ line = "SET ( LIBRARY_NAME " + name + " )";
+ out << line << std::endl;
+ }
+ in.close();
+ out.close();
+ //delete old file and rename new file
+ std::string renameCommand = "mv " + this->path + "/" + name + "/CMakeLists.txt.tmp " + this->path + "/" + name + "/CMakeLists.txt";
+ if(system(renameCommand.c_str()))
+ {
+ result = new std::string("An error occurred while running '" + renameCommand + "'.");
+ return NULL;
+ }
+
+ //add library to model
+ modelCDMLibrary* library = new modelCDMLibrary(this->path + "/" + name, this->level + 1);
+ this->libraries.push_back(library);
+ this->children.push_back(library);
+
+ result = new std::string(this->path + "/" + name);
+ return library;
}
bool modelCDMLib::OpenCMakeListsFile(std::string*& result)