summaryrefslogtreecommitdiff
path: root/server/src/macrolist.cc
diff options
context:
space:
mode:
authordeva <deva>2009-07-22 15:00:29 +0000
committerdeva <deva>2009-07-22 15:00:29 +0000
commit7e349e2789a633a6014baea63aeb7932e024c917 (patch)
tree0b7b20adf693e3991f4410368255fd05aa8aab0d /server/src/macrolist.cc
parent8109ada79a24f03e00ebc199ebfdb58e70b054d9 (diff)
Changed the way the macros are looked up in the filesystem (now they are parsed and indexed using version numbers). Updated all unit tests, to compile and run again.
Diffstat (limited to 'server/src/macrolist.cc')
-rw-r--r--server/src/macrolist.cc114
1 files changed, 114 insertions, 0 deletions
diff --git a/server/src/macrolist.cc b/server/src/macrolist.cc
new file mode 100644
index 0000000..8207b06
--- /dev/null
+++ b/server/src/macrolist.cc
@@ -0,0 +1,114 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * macrolist.cc
+ *
+ * Wed Jul 22 10:26:40 CEST 2009
+ * Copyright 2009 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "macrolist.h"
+
+#include <sys/types.h>
+#include <dirent.h>
+
+#include "debug.h"
+#include "macroheaderparser.h"
+
+static std::vector<std::string> listdir(std::string path)
+{
+ 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;
+}
+
+MacroList::MacroList(std::string macropath)
+{
+ this->macropath = macropath;
+ std::vector<std::string> macros = listdir(macropath);
+ std::vector<std::string>::iterator i = macros.begin();
+ while(i != macros.end()) {
+ MacroHeaderParser parser(macropath + "/" + *i);
+ parser.parse();
+ Macro *macro = parser.getMacro();
+ (*this)[macro->attributes["name"]][Version(macro->attributes["version"])] = *i;
+ i++;
+ }
+}
+
+std::string MacroList::getLatestVersion(std::string macro)
+{
+ if(find(macro) == end()) return "";
+ MacroListItem mli = (*this)[macro];
+ if(mli.size() == 0) return "";
+ printf("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
+
+int main()
+{
+ MacroList lst("/home");
+
+ lst["macro1"][Version("1.0")] = "macro1-1.0.xml";
+ lst["macro1"][Version("1.1")] = "macro1-1.1.xml";
+ lst["macro1"][Version("1.1.1")] = "macro1-1.1.1.xml";
+ lst["macro1"][Version("1.2")] = "macro1-1.2.xml";
+ lst["macro2"][Version("1.0")] = "macro2.xml";
+ lst["macro3"][Version("1.0")] = "macro3.xml";
+
+ MacroList::iterator i = lst.begin();
+ while(i != lst.end()) {
+ MacroListItem::iterator j = i->second.begin();
+ while(j != i->second.end()) {
+ printf("%s - v%s file: %s\n",
+ i->first.c_str(),
+ ((std::string)j->first).c_str(),
+ j->second.c_str());
+ j++;
+ }
+ i++;
+ }
+
+ return 0;
+}
+
+#endif/*TEST_MACROLIST*/