summaryrefslogtreecommitdiff
path: root/src/saxparser.cc
diff options
context:
space:
mode:
authorAndré Nusser <andre.nusser@googlemail.com>2016-03-22 00:40:15 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2016-03-29 22:43:30 +0200
commit31ac3fd56ce77cfb2e9caddd0bdac0614971d98b (patch)
treeb1d1546af97495e06b9129bcae08cefcfc98897e /src/saxparser.cc
parentefe93864d53f72be4fa4dfe003f0f7578fc558e2 (diff)
Parser refactoring.
* Use new style * Update to C++11 * Use more std::string than char*
Diffstat (limited to 'src/saxparser.cc')
-rw-r--r--src/saxparser.cc150
1 files changed, 73 insertions, 77 deletions
diff --git a/src/saxparser.cc b/src/saxparser.cc
index 1ed3050..e59a1fe 100644
--- a/src/saxparser.cc
+++ b/src/saxparser.cc
@@ -30,106 +30,102 @@
#include <string.h>
#include <hugin.hpp>
-static void character_hndl(void *p, const XML_Char *s, int len)
+SAXParser::SAXParser()
{
- SAXParser *parser = (SAXParser*)XML_GetUserData(p);
- std::string chars;
- chars.append(s, len);
- parser->characterData(chars);
+ p = XML_ParserCreate(nullptr);
+ if(!p) {
+ ERR(sax, "Couldn't allocate memory for parser\n");
+ // throw Exception(...); TODO
+ return;
+ }
+
+ XML_SetUserData(p, this);
+ XML_UseParserAsHandlerArg(p);
+ XML_SetElementHandler(p, start_hndl, end_hndl);
+ XML_SetCharacterDataHandler(p, character_hndl);
}
-static void start_hndl(void *p, const char *el, const char **attr)
+SAXParser::~SAXParser()
{
- SAXParser *parser = (SAXParser*)XML_GetUserData(p);
+ XML_ParserFree(p);
+}
- // Convert to comfy C++ values...
- std::string name = el;
- std::map< std::string, std::string > attributes;
+int SAXParser::parse()
+{
+ DEBUG(sax, "parse()\n");
+
+ std::string buf;
+ int len;
+
+ do {
+ len = readData(buf, 32);
+ if(len <= -1) {
+ parseError("", 0, "Could not read data", 0);
+ return 1;
+ }
+ if(!XML_Parse(p, (char*)buf.c_str(), len, len == 0)) {
+ parseError(buf, len, XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
+ return 1;
+ }
+
+ buf.clear();
+ } while(len);
+
+ return 0;
+}
- while(*attr) {
- std::string at_name = *attr;
- attr++;
- std::string at_value = *attr;
- attr++;
+int SAXParser::parse(const std::string& buffer)
+{
+ DEBUG(sax, "parse(buffer %d bytes)\n", (int)buffer.length());
- attributes.insert(make_pair(at_name, at_value));
- }
+ if(!XML_Parse(p, buffer.c_str(), buffer.length(), true)) {
+ parseError(buffer, buffer.length(),
+ XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
+ return 1;
+ }
- parser->startTag(name, attributes);
+ return 0;
}
-static void end_hndl(void *p, const char *el)
+void SAXParser::parseError(const std::string& buf, std::size_t len, const std::string& error, std::size_t lineno)
{
- SAXParser *parser = (SAXParser*)XML_GetUserData(p);
- std::string name = el;
- parser->endTag(name);
-}
+ fprintf(stderr, "SAXParser error at line %d: %s\n", (int)lineno, error.c_str());
+ fprintf(stderr, "Buffer %u bytes: \n[\n", (int)len);
+ fwrite((char*)buf.c_str(), len, 1, stderr);
-SAXParser::SAXParser()
-{
- p = XML_ParserCreate(NULL);
- if(!p) {
- fprintf(stderr, "Couldn't allocate memory for parser\n");
- // throw Exception(...);
- return;
- }
-
- XML_SetUserData(p, this);
- XML_UseParserAsHandlerArg(p);
- XML_SetElementHandler(p, start_hndl, end_hndl);
- XML_SetCharacterDataHandler(p, character_hndl);
+ fprintf(stderr, "\n]\n");
+ fflush(stderr);
}
-SAXParser::~SAXParser()
+void SAXParser::character_hndl(void* p, const XML_Char* s, int len)
{
- XML_ParserFree(p);
+ SAXParser* parser = (SAXParser*)XML_GetUserData(p);
+ std::string chars(s, len);
+ parser->characterData(chars);
}
-int SAXParser::parse()
+void SAXParser::start_hndl(void* p, const char* el, const char** attr)
{
- DEBUG(sax, "parse()\n");
-
- char buf[32];
- int len;
-
- do {
- len = readData(buf, sizeof(buf) - 1);
- if(len == -1) {
- parseError((char*)"", 0, "Could not read data", 0);
- return 1;
- }
- if(!XML_Parse(p, buf, len, len == 0)) {
- parseError(buf, len, XML_ErrorString(XML_GetErrorCode(p)),
- (int)XML_GetCurrentLineNumber(p));
- return 1;
- }
-
- memset(buf, 0, sizeof(buf));
- } while(len);
-
- return 0;
-}
+ SAXParser* parser = (SAXParser*)XML_GetUserData(p);
-int SAXParser::parse(std::string buffer)
-{
- DEBUG(sax, "parse(buffer %d bytes)\n", (int)buffer.length());
+ // Convert to comfy C++ values...
+ attr_t attributes;
+
+ while(*attr) {
+ std::string at_name = *attr++;
+ std::string at_value = *attr++;
- if(!XML_Parse(p, buffer.c_str(), buffer.length(), true)) {
- parseError((char*)buffer.c_str(), buffer.length(),
- XML_ErrorString(XML_GetErrorCode(p)),
- (int)XML_GetCurrentLineNumber(p));
- return 1;
- }
+ attributes.emplace(at_name, at_value);
+ }
- return 0;
+ parser->startTag(std::string(el), attributes);
}
-void SAXParser::parseError(char *buf, size_t len, std::string error, int lineno)
+void SAXParser::end_hndl(void* p, const char* el)
{
- fprintf(stderr, "SAXParser error at line %d: %s\n", lineno, error.c_str());
- fprintf(stderr, "\tBuffer %u bytes: [", (int)len);
- if(fwrite(buf, len, 1, stderr) != len) {}
- fprintf(stderr, "]\n");
- fflush(stderr);
+ SAXParser* parser = (SAXParser*)XML_GetUserData(p);
+ parser->endTag(std::string(el));
}