diff options
-rw-r--r-- | src/xmlparser.cc | 103 | ||||
-rw-r--r-- | src/xmlparser.h | 61 |
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__*/ |