diff options
Diffstat (limited to 'server/src/templatelist.cc')
| -rw-r--r-- | server/src/templatelist.cc | 82 | 
1 files changed, 18 insertions, 64 deletions
| 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 <sys/types.h> -#include <dirent.h> +#include "templateheaderparser.h"  #include "debug.h" -#include "templateheaderparser.h" -static std::vector<std::string> listdir(std::string path) +TemplateList::TemplateList(std::string path) +  : EntityList(path, "template")  { -  std::vector<std::string> 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<std::string> templates = listdir(templatepath); -  std::vector<std::string>::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 | 
