From 74a28aa7125be6a603128ad600c98c4882f3b5c2 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 1 Jun 2010 12:58:32 +0000 Subject: From new_protocol branch. --- server/src/templatelist.cc | 82 ++++++++++------------------------------------ 1 file changed, 18 insertions(+), 64 deletions(-) (limited to 'server/src/templatelist.cc') diff --git a/server/src/templatelist.cc b/server/src/templatelist.cc index cdf2b4b..af03586 100644 --- a/server/src/templatelist.cc +++ b/server/src/templatelist.cc @@ -27,80 +27,34 @@ */ #include "templatelist.h" -#include -#include +#include "templateheaderparser.h" #include "debug.h" -#include "templateheaderparser.h" -static std::vector listdir(std::string path) +TemplateList::TemplateList(std::string path) + : EntityList(path, "template") { - std::vector files; - - DIR* dir = opendir(path.c_str()); - if(!dir) { - PRACRO_ERR(dump, "Could not open directory: %s\n", path.c_str()); - return files; - } - - struct dirent *d; - while((d = readdir(dir)) != 0) { - //if(d->d_type == DT_DIR) { - std::string name = d->d_name; - if(name.length() >= 4 && name.substr(name.length() - 4) == ".xml") - files.push_back(name); - //} - } - closedir(dir); - - return files; + rescan(); } -TemplateList::TemplateList(std::string templatepath) +void TemplateList::addFile(std::string file) { - this->templatepath = templatepath; - std::vector templates = listdir(templatepath); - std::vector::iterator i = templates.begin(); - while(i != templates.end()) { - TemplateHeaderParser parser(templatepath + "/" + *i); - try { - parser.parse(); - Template *templ = parser.getTemplate(); - (*this)[templ->attributes["name"]][VersionStr(templ->attributes["version"])] = *i; - } catch(Exception &e) { - PRACRO_WARN(templatelist, "Skipping %s: %s\n", i->c_str(), e.what()); - } - - i++; + if(file.substr(file.size() - 4) != ".xml") { + PRACRO_DEBUG(templatelist, "Skipping file: %s\n", file.c_str()); + return; } - { - iterator i = begin(); - while(i != end()) { - TemplateListItem::iterator j = i->second.begin(); - while(j != i->second.end()) { - PRACRO_DEBUG(templatelist, "%s - v%s file: %s\n", - i->first.c_str(), - ((std::string)j->first).c_str(), - j->second.c_str()); - j++; - } - i++; - } + PRACRO_DEBUG(templatelist, "Adding file: %s\n", file.c_str()); + TemplateHeaderParser parser(file); + try { + parser.parse(); + Template *templ = parser.getTemplate(); + insertEntity(templ->attributes["name"], + templ->attributes["version"], + file); + } catch(Exception &e) { + PRACRO_WARN(templatelist, "Skipping %s: %s\n", file.c_str(), e.what()); } - -} - -std::string TemplateList::getLatestVersion(std::string templ) throw(Exception) -{ - if(find(templ) == end()) throw Exception("Template ["+templ+"] does not exist"); - TemplateListItem mli = (*this)[templ]; - if(mli.size() == 0) return ""; - PRACRO_DEBUG(templatelist, "Search for %s - found %s v%s\n", - templ.c_str(), - (templatepath + "/" + mli.begin()->second).c_str(), - ((std::string)mli.begin()->first).c_str()); - return templatepath + "/" + mli.begin()->second; } #ifdef TEST_TEMPLATELIST -- cgit v1.2.3