X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcreaDevManagerLib%2FCDMUtilities.cpp;h=ae37bb57aa195aa63480aa8a06abd330f14d329c;hb=de76ec742d17da9747b691dd168a8d832a64168e;hp=09e25f31c06d8a64f1ea9896d214a145f94e1a4d;hpb=327c33758d25e6ff1f90f9ab74ea219eaed934a9;p=crea.git diff --git a/lib/creaDevManagerLib/CDMUtilities.cpp b/lib/creaDevManagerLib/CDMUtilities.cpp index 09e25f3..ae37bb5 100644 --- a/lib/creaDevManagerLib/CDMUtilities.cpp +++ b/lib/creaDevManagerLib/CDMUtilities.cpp @@ -41,44 +41,16 @@ #include #include +#include +#include + namespace CDMUtilities { - template - Container& splitter::split - ( - Container& result, - const typename Container::value_type& s, - const typename Container::value_type& delimiters, - empties_t empties - ) - { - result.clear(); - size_t current; - size_t next = -1; - do - { - if (empties == no_empties) - { - next = s.find_first_not_of(delimiters, next + 1); - if (next == Container::value_type::npos) - { - break; - } - next -= 1; - } - current = next + 1; - next = s.find_first_of(delimiters, current); - result.push_back(s.substr(current, next - current)); - } - while (next != Container::value_type::npos); - return result; - } - const std::string fixPath(const std::string& path) { std::string pathFixed = ""; -#if(_WIN32) +#ifdef _WIN32 // ------ Windows std::vector pathSplit; @@ -109,46 +81,72 @@ namespace CDMUtilities int openTextEditor(const std::string& file) { - std::string command = TEXT_EDITOR; +#ifdef _WIN32 + wxConfigBase* pConfig = wxConfigBase::Get(); + std::string command = "start " + crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR))); if(file != "") command += " \"" + file + "\""; - command += " &"; +#else + wxConfigBase* pConfig = wxConfigBase::Get(); + std::string command = crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR))); + if(file != "") + command += " \"" + file + "\""; + command += " &"; +#endif return system(command.c_str()); } int openFileExplorer(const std::string& file) { - std::string command = FILE_EXPLORER; +#ifdef _WIN32 + wxConfigBase* pConfig = wxConfigBase::Get(); + std::string command = "start " + crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER))); if(file != "") command += " \"" + file + "\""; - command += " &"; +#else + wxConfigBase* pConfig = wxConfigBase::Get(); + std::string command = crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER))); + if(file != "") + command += " \"" + file + "\""; + command += " &"; +#endif return system(command.c_str()); } - int openFileWithCommand(const std::string& file, const std::string& command) + int openFileWithCommand(const std::string& file, const std::string& command, const std::string& parameters) { +#ifdef _WIN32 + std::string comm = "start " + command; + if(file != "") + comm += " \"" + file + "\" " + parameters; +#else std::string comm = command; if(file != "") - comm += " \"" + file + "\""; + comm += " \"" + file + "\" " + parameters; comm += " &"; - +#endif + std::cout << "executing: " << comm << std::endl; return system(comm.c_str()); } int openBBEditor() { - std::string comm = "bbEditor &"; +#ifdef _WIN32 + std::string comm = "start bbEditor"; +#else + std::string comm = "bbEditor &"; +#endif return system(comm.c_str()); } int openCreaToolsTools() { #ifdef _WIN32 - std::string comm = "creaTools &"; + std::string comm = "start creaTools"; #else std::string comm = "creaTools.sh &"; #endif @@ -158,7 +156,8 @@ namespace CDMUtilities int openTerminal(const std::string& command) { - std::string comm = TERMINAL; + wxConfigBase* pConfig = wxConfigBase::Get(); + std::string comm = crea::wx2std(pConfig->Read(wxT("TERMINAl"), crea::std2wx(CDMUtilities::TERMINAL))); if (command != "") comm += + " " + command; comm += " &"; @@ -316,7 +315,7 @@ namespace CDMUtilities std::string stringify(const std::string& line) { std::string res; - for (int i = 0; i < line.size(); i++) + for (int i = 0; i < (int)(line.size()); i++) { if(line[i] == '\\') res.push_back('\\'); @@ -327,4 +326,196 @@ namespace CDMUtilities return res; } + std::string readFile(const std::string& file_path) + { + std::string res; + std::ifstream file(file_path.c_str()); + if (file.is_open()) + { + char ch = file.get(); + while (!file.eof()) + { + res.push_back(ch); + ch = file.get(); + } + file.close(); + } + return res; + } + + bool writeFile(const std::string& file_path, const std::string& st) + { + std::ofstream file(file_path.c_str()); + if (file.is_open()) + { + file << st; + file.close(); + return true; + } + return false; + + } + + CMLFile readCMLFile(const std::string& file_path) + { + CMLFile res; + + std::ifstream file(file_path.c_str()); + if (file.is_open()) + { + char ch = file.get(); + while (!file.eof()) + { + syntaxElement element; + if (isspace(ch)) + { + //std::cout << "space" << std::endl; + element.first = "space"; + element.second.push_back(std::string(1,ch)); + } + else if (ch == '#') + { + //std::cout << "comment" << std::endl; + element.first = "comment"; + std::string commentValue; + while (ch != '\n') + { + commentValue.push_back(ch); + + ch = file.get(); + if (file.eof()) + break; + }; + if (!file.eof()) + commentValue.push_back('\n'); + element.second.push_back(commentValue); + } + else + { + //std::cout << "command" << std::endl; + element.first = "command"; + std::string commandValue; + while (true) + { + //std::cout << ch; + //std::cout.flush(); + while(!isspace(ch) && ch != '(' && ch != ')' && ch != '#') + { + if(ch == '"') + { + if (commandValue.size()) { + element.second.push_back(commandValue); + commandValue.clear(); + } + commandValue.push_back(ch); + ch = file.get(); + while(!file.eof() && ch != '"') + { + if(ch == '\\') + { + commandValue.push_back(ch); + ch = file.get(); + if(!file.eof()) + commandValue.push_back(ch); + } + else + { + commandValue.push_back(ch); + } + ch = file.get(); + } + if(!file.eof()) + commandValue.push_back(ch); + element.second.push_back(commandValue); + commandValue.clear(); + } + else + commandValue.push_back(ch); + + ch = file.get(); + } + + if (!file.eof() && (isspace(ch) || ch == '(' || ch == ')' || ch == '#')) + { + if (commandValue.size()) { + element.second.push_back(commandValue); + commandValue.clear(); + } + commandValue.push_back(ch); + if (ch == '#') { + while (ch != '\n') { + ch = file.get(); + if (file.eof()) + break; + commandValue.push_back(ch); + }; + } + element.second.push_back(commandValue); + if (commandValue == ")") + { + commandValue.clear(); + break; + } + commandValue.clear(); + } + + ch = file.get(); + if (file.eof()) + break; + } + } + res.push_back(element); + + ch = file.get(); + if (file.eof()) + break; + } + + file.close(); + } + +/* + std::cout << "CMakeLists: " << file_path << std::endl; + for (int i = 0; i < res.size(); ++i) { + if (res[i].first == "command") + std::cout << "@"; + for (int j = 0; j < res[i].second.size(); ++j) { + std::cout << "~" << res[i].second[j]; + } + if (res[i].first == "command") + std::cout << "@"; + } + std::cout << "End of file" << std::endl; +*/ + return res; + } + + bool writeCMLFile(const std::string& file_path, const CMLFile& data) + { + std::ofstream file(file_path.c_str()); + if(file.is_open()) + { + for (int i = 0; i < data.size(); ++i) { + for (int j = 0; j < data[i].second.size(); ++j) { + file << data[i].second[j]; + } + } + file.close(); + return true; + } + return false; + } + + + + void + normalizeStr(std::string& st) + { + while(st.size() && isspace(st[0])) + st.erase(0,1); + while(st.size() && isspace(st[st.size()-1])) + st.erase(st.size()-1,1); + return; + } + }