summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2012-03-23 15:28:36 +0100
committerJonas Suhr Christensen <jsc@umbraculum.org>2012-03-23 15:28:36 +0100
commit5c3abe7907d7dfcf35da2151cd131eef35bc33ad (patch)
treed9ab072858292e932bbbb1ad9dd412098d12ac3e /src
parent27d602ab23f38c00e76dd5d4b658580424c01702 (diff)
Added files.
Diffstat (limited to 'src')
-rw-r--r--src/xmlparser.cc103
-rw-r--r--src/xmlparser.h61
2 files changed, 164 insertions, 0 deletions
diff --git a/src/xmlparser.cc b/src/xmlparser.cc
new file mode 100644
index 0000000..7bc7e0a
--- /dev/null
+++ b/src/xmlparser.cc
@@ -0,0 +1,103 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * xmlparser.cc
+ *
+ * Wed Mar 21 09:15:51 CET 2012
+ * Copyright 2012 Jonas Suhr Christensen
+ * jsc@umbraculum.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Munia.
+ *
+ * Munia 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.
+ *
+ * Munia 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 Munia; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "xmlparser.h"
+
+#include "stdio.h"
+
+#include "xml_encode_decode.h"
+
+XmlParser::XmlParser(std::string filename) {
+ // open file handler
+ fp = fopen(filename.c_str(), "r");
+ if(!fp) {
+ fprintf(stderr, "Could not open file\n");
+ }
+
+ task = NULL;
+ tasklist.clear();
+
+ open_tagtype = none;
+}
+
+XmlParser::~XmlParser() {
+ // close file handler
+ fclose(fp);
+ if(task) delete task;
+}
+
+void XmlParser::characterData(std::string& data) {
+ characterbuf += data;
+
+ /*
+ switch(open_tagtype) {
+ case title:
+ // task->title += xml_decode(data);
+ break;
+ case desc:
+ // task->desc += xml_decode(data);
+ break;
+ default:
+ break;
+ }
+ */
+}
+
+void XmlParser::startTag(std::string name, attributes_t &attr) {
+ // printf("start: %s\n", name.c_str());
+ characterbuf.clear();
+ if(name == "task") {
+ if(task) delete task;
+
+ task = new task_t();
+ task->id = atoi(xml_decode(attr["id"]).c_str());
+ task->parent_id = atoi(xml_decode(attr["parent_id"]).c_str());
+ }
+
+}
+
+void XmlParser::endTag(std::string name) {
+ // printf("end: %s\n", name.c_str());
+ if (name == "task") {
+ if(task) tasklist.insert(*task);
+ }
+ else if (name == "title") {
+ if(task) task->title = characterbuf;
+ // open_tagtype = title;
+ }
+ else if (name == "desc") {
+ if(task) task->desc = characterbuf;
+ // open_tagtype = desc;
+ }
+ // open_tagtype = none;
+}
+
+int XmlParser::readData(char* data, size_t size) {
+ if(!fp) return 0; // Could not read file
+
+ return fread(data, 1, size, fp);
+}
diff --git a/src/xmlparser.h b/src/xmlparser.h
new file mode 100644
index 0000000..85a0600
--- /dev/null
+++ b/src/xmlparser.h
@@ -0,0 +1,61 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * xmlparser.h
+ *
+ * Wed Mar 21 09:15:51 CET 2012
+ * Copyright 2012 Jonas Suhr Christensen
+ * jsc@umbraculum.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Munia.
+ *
+ * Munia 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.
+ *
+ * Munia 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 Munia; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __MUNIA_XMLPARSER_H__
+#define __MUNIA_XMLPARSER_H__
+
+#include "task.h"
+#include "saxparser.h"
+
+class XmlParser : public SAXParser {
+public:
+ XmlParser(std::string filename);
+ ~XmlParser();
+
+ void startTag(std::string name, attributes_t &attr);
+ void endTag(std::string name);
+ void characterData(std::string& data);
+
+ TaskList tasklist;
+protected:
+ int readData(char* data, size_t size);
+
+private:
+ FILE* fp;
+ task_t* task;
+ std::string characterbuf;
+
+ enum tag_t {
+ title,
+ desc,
+ none
+ };
+
+ tag_t open_tagtype;
+};
+
+#endif/*__MUNIA_XMLPARSER_H__*/