X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcreaDevManagerLib%2FCDMUtilities.cpp;h=77f5f52cb7b199ec3efb419551f6b7cea640b80a;hb=5a17d994576296f2a5a85f3a01ad5631786a0c56;hp=b4696b1104177d6949398c58a0d5638d631cef95;hpb=35dd4d1ddf73a91d308cc49fc394104169936055;p=crea.git diff --git a/lib/creaDevManagerLib/CDMUtilities.cpp b/lib/creaDevManagerLib/CDMUtilities.cpp index b4696b1..77f5f52 100644 --- a/lib/creaDevManagerLib/CDMUtilities.cpp +++ b/lib/creaDevManagerLib/CDMUtilities.cpp @@ -356,4 +356,154 @@ namespace CDMUtilities return res; } + 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; + } + }