From 984d1dd9993aa5beffa4270b6421dea05ab04faa Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 14 Jan 2010 13:48:52 +0000 Subject: Made EntityList superclass for the Macro and TemplateList classes. --- server/src/templatelist.cc | 83 ++++++++-------------------------------------- 1 file changed, 13 insertions(+), 70 deletions(-) (limited to 'server/src/templatelist.cc') diff --git a/server/src/templatelist.cc b/server/src/templatelist.cc index bbaefc3..b952467 100644 --- a/server/src/templatelist.cc +++ b/server/src/templatelist.cc @@ -27,84 +27,27 @@ */ #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("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(path); } -TemplateList::TemplateList(std::string templatepath) +void TemplateList::addFile(std::string file) { - MutexAutolock lock(mutex); - - 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++; - } - - { - 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(); + (*this)[templ->attributes["name"]][VersionStr(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) -{ - MutexAutolock lock(mutex); - - 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