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/macrolist.cc | 83 +++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 62 deletions(-) (limited to 'server/src/macrolist.cc') diff --git a/server/src/macrolist.cc b/server/src/macrolist.cc index b67f239..908de1e 100644 --- a/server/src/macrolist.cc +++ b/server/src/macrolist.cc @@ -27,80 +27,39 @@ */ #include "macrolist.h" -#include -#include +#include -#include "debug.h" #include "macroheaderparser.h" -static std::vector listdir(std::string path) -{ - 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); +#include "debug.h" - return files; +MacroList::MacroList(std::string path) + : EntityList(path, "macro") +{ + rescan(); } -MacroList::MacroList(std::string macropath) + +void MacroList::addFile(std::string file) { - this->macropath = macropath; - std::vector macros = listdir(macropath); - std::vector::iterator i = macros.begin(); - while(i != macros.end()) { - MacroHeaderParser parser(macropath + "/" + *i); - try { - parser.parse(); - Macro *macro = parser.getMacro(); - (*this)[macro->attributes["name"]][VersionStr(macro->attributes["version"])] = *i; - } catch(Exception &e) { - PRACRO_WARN(macrolist, "Skipping %s: %s\n", i->c_str(), e.what()); - } - i++; + if(file.substr(file.size() - 4) != ".xml") { + PRACRO_DEBUG(macrolist, "Skipping file: %s\n", file.c_str()); + return; } - { - iterator i = begin(); - while(i != end()) { - MacroListItem::iterator j = i->second.begin(); - while(j != i->second.end()) { - PRACRO_DEBUG(macrolist, "%s - v%s file: %s\n", - i->first.c_str(), - ((std::string)j->first).c_str(), - j->second.c_str()); - j++; - } - i++; - } + PRACRO_DEBUG(macrolist, "Adding file: %s\n", file.c_str()); + MacroHeaderParser parser(file); + try { + parser.parse(); + Macro *macro = parser.getMacro(); + insertEntity(macro->attributes["name"], + macro->attributes["version"], + file); + } catch(Exception &e) { + PRACRO_WARN(macrolist, "Skipping %s: %s\n", file.c_str(), e.what()); } } -std::string MacroList::getLatestVersion(std::string macro) throw(Exception) -{ - if(find(macro) == end()) throw Exception("Macro ["+macro+"] does not exist"); - MacroListItem mli = (*this)[macro]; - if(mli.size() == 0) return ""; - PRACRO_DEBUG(macrolist, "Search for %s - found %s v%s\n", - macro.c_str(), - (macropath + "/" + mli.begin()->second).c_str(), - ((std::string)mli.begin()->first).c_str()); - return macropath + "/" + mli.begin()->second; -} - #ifdef TEST_MACROLIST #define MACRODIR "/home" // We assume this directory exists and does not contain any xml files! -- cgit v1.2.3