summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/debug.cc253
-rw-r--r--src/debug.h133
-rw-r--r--src/log.cc43
-rw-r--r--src/log.h38
-rw-r--r--src/saxparser.cc412
-rw-r--r--src/saxparser.h152
-rw-r--r--src/xml_encode_decode.cc103
-rw-r--r--src/xml_encode_decode.h35
8 files changed, 1169 insertions, 0 deletions
diff --git a/src/debug.cc b/src/debug.cc
new file mode 100644
index 0000000..c68ce2d
--- /dev/null
+++ b/src/debug.cc
@@ -0,0 +1,253 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * debug.cc
+ *
+ * Wed Feb 11 11:22:12 CET 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 "debug.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include <string>
+
+#include "log.h"
+
+static FILE *logfp = stderr;
+
+#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+struct __debug_channel
+{
+ char name[32];
+ unsigned flags;
+};
+
+static const char * const debug_class_str[] =
+ { "fixme", "err", "warn", "info", "debug" };
+
+#define __DEBUG_CHANNEL_MAX 256
+
+static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX];
+static unsigned n_debug_channel = 0;
+static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme);
+
+static int __debug_enabled(const enum __debug_class cl, const char *ch)
+{
+ unsigned i;
+ for(i = 0; i < n_debug_channel; i++) {
+ if(!strcmp(ch, debug_channel[i].name)) {
+ return (debug_channel[i].flags & (1 << cl)) != 0;
+ }
+ }
+ return debug_flags & (1 << cl);
+}
+
+
+#ifdef WITH_DEBUG
+int __debug(const char *func, const int line,
+ const enum __debug_class cl,
+ const char *ch, const char *fmt, ...)
+{
+ int ret = 0;
+ if(__debug_enabled(cl, ch)) {
+ if((unsigned)cl < NELEM(debug_class_str))
+ ret += fprintf(logfp, "%s:%s:%s:%d ",
+ debug_class_str[(unsigned)cl], ch, func, line);
+ if(fmt) {
+ va_list va;
+ va_start(va, fmt);
+ ret += vfprintf(logfp, fmt, va);
+ va_end(va);
+ }
+ }
+ if(ret){
+ fflush(logfp);
+ }
+ return ret;
+}
+
+int __debug_va(const char *func, const int line,
+ const enum __debug_class cl,
+ const char *ch, const char *fmt, va_list va)
+{
+ int ret = 0;
+ if(__debug_enabled(cl, ch)) {
+ if((unsigned)cl < NELEM(debug_class_str))
+ ret += fprintf(logfp, "%s:%s:%s:%d ",
+ debug_class_str[(unsigned)cl], ch, func, line);
+ if(fmt)
+ ret += vfprintf(logfp, fmt, va);
+ }
+ if(ret) {
+ fflush(logfp);
+ }
+ return ret;
+}
+
+#endif
+
+int __log(const char *func, const int line, const enum __debug_class cl,
+ const char *ch, const char *fmt, ...)
+{
+ std::string logmsg;
+ char str[8];
+
+#ifdef WITH_DEBUG
+ int ret = 0;
+#endif
+ if(__debug_enabled(cl, ch)) {
+ if((unsigned)cl < NELEM(debug_class_str))
+ if((unsigned)cl < NELEM(debug_class_str))
+#ifdef WITH_DEBUG
+ ret = fprintf(logfp, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line);
+#endif
+ sprintf(str, "%d", line);
+ logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str;
+ if(fmt) {
+ va_list va;
+ va_start(va, fmt);
+#ifdef WITH_DEBUG
+ ret += vfprintf(logfp, fmt, va);
+#endif
+ char* ptr;
+ if(vasprintf(&ptr, fmt, va) == -1) {}
+ logmsg += ptr;
+ va_end(va);
+ }
+ }
+#ifdef WITH_DEBUG
+ if(ret) {
+ fprintf(logfp, "\n");
+ fflush(logfp);
+ }
+#endif
+ log(logmsg);
+ return logmsg.size();
+}
+
+int __log_va(const char *func, const int line, const enum __debug_class cl,
+ const char *ch, const char *fmt, va_list va)
+{
+ std::string logmsg;
+ char str[8];
+#ifdef WITH_DEBUG
+ int ret = 0;
+#endif
+ if(__debug_enabled(cl, ch)) {
+ if((unsigned)cl < NELEM(debug_class_str))
+#ifdef WITH_DEBUG
+ ret = fprintf(logfp, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line);
+#endif
+ sprintf(str, "%d", line);
+ logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str;
+ if(fmt) {
+#ifdef WITH_DEBUG
+ ret += vfprintf(logfp, fmt, va);
+#endif
+ char* ptr;
+ if(vasprintf(&ptr, fmt, va) == -1) {}
+ logmsg += ptr;
+ }
+ }
+#ifdef WITH_DEBUG
+ if(ret) {
+ fprintf(logfp, "\n");
+ fflush(logfp);
+ }
+#endif
+ return logmsg.size();
+}
+
+void debug_init(FILE *fp)
+{
+ logfp = fp;
+}
+
+
+/*
+ * fmt := [set[,set]*]*
+ * set := [+-]channel
+ * | class[+-]channel
+ * | [+-]all
+ */
+void debug_parse(const char *fmt)
+{
+ char *s;
+ char *next;
+ char *opt;
+
+ if(!(s = strdup(fmt))) return;
+
+ for(opt = s; opt; opt = next) {
+ int set = 0;
+ int clr = 0;
+ unsigned i;
+ if((next = strchr(opt, ','))) *next++ = '\0';
+ char *p = opt + strcspn(opt, "+-");
+ if(!*p) p = opt; // All chars -> a channel name
+ if(p > opt) {
+ // we have a class
+ for(i = 0; i < NELEM(debug_class_str); i++) {
+ int n = strlen(debug_class_str[i]);
+ if(n != (p - opt)) continue;
+ if(!memcmp(opt, debug_class_str[i], n)) {
+ // Found the class
+ if(*p == '+')
+ set = 1 << i;
+ else
+ clr = 1 << i;
+ break;
+ }
+ }
+ if(i == NELEM(debug_class_str)) continue;
+ } else {
+ if(*p == '-')
+ clr = ~0;
+ else
+ set = ~0;
+ }
+ if(*p == '+' || *p == '-') p++;
+ if(!*p) continue;
+ if(!strcmp("all", p)) {
+ debug_flags = (debug_flags & ~clr) | set;
+ } else {
+ if(strlen(p) >= sizeof(debug_channel[0].name)) continue;
+ for(i = 0; i < n_debug_channel; i++) {
+ if(!strcmp(p, debug_channel[i].name)) {
+ debug_channel[i].flags = (debug_channel[i].flags & ~clr) | set;
+ break;
+ }
+ }
+ if(i == n_debug_channel && n_debug_channel < __DEBUG_CHANNEL_MAX) {
+ strcpy(debug_channel[i].name, p);
+ debug_channel[i].flags = (debug_flags & ~clr) | set;
+ n_debug_channel++;
+ }
+ }
+ }
+ free(s);
+}
+
diff --git a/src/debug.h b/src/debug.h
new file mode 100644
index 0000000..a5f199d
--- /dev/null
+++ b/src/debug.h
@@ -0,0 +1,133 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * debug.h
+ *
+ * Wed Feb 11 11:22:12 CET 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.
+ *
+ * Pentominos 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 Pentominos; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __PENTOMINOS_DEBUG_H__
+#define __PENTOMINOS_DEBUG_H__
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#ifdef HAVE_CONFIG_H
+// For USE_EFENCE
+#include <config.h>
+#ifdef USE_EFENCE
+#include <new>
+#include <stdlib.h>
+#include <efencepp.h>
+#include <efence.h>
+
+// Lazy static alocations makes efence freak out.
+// Use this to use hardcoded values instead.
+// Currently it disables:
+// - gethostbyname
+// - getsockname
+// - getpeername
+// - iconv
+//#define BYPASS_STATICALLOCATIONS
+
+#endif/*USE_EFENCE*/
+#endif/*HAVE_CONFIG*/
+
+void debug_init(FILE *fp);
+void debug_parse(const char *fmt);
+
+enum __debug_class
+{
+ __class_fixme = 0,
+ __class_err = 1,
+ __class_warn = 2,
+ __class_info = 3,
+ __class_debug = 4
+};
+
+#ifdef WITH_DEBUG
+int __debug(const char *func, const int line, enum __debug_class cl,
+ const char *ch, const char *fmt, ...)
+ __attribute__((format (printf,5,6)));
+
+int __debug_va(const char *func, const int line, enum __debug_class cl,
+ const char *ch, const char *fmt, va_list va);
+
+#define __DEBUG_PRINT(cl, ch, fmt...) \
+ do { __debug(__func__, __LINE__, cl, ch, fmt); } while(0)
+#define __DEBUG_PRINT_VA(cl, ch, fmt, a) \
+ do { __debug_va(__func__, __LINE__, cl, ch, fmt, a); } while(0)
+#define __DEBUG(cl, ch, fmt...) \
+ __DEBUG_PRINT(__class##cl, #ch, fmt)
+#define __DEBUG_VA(cl, ch, fmt, a) \
+ __DEBUG_PRINT_VA(__class##cl, #ch, fmt, a)
+
+#define FIXME(ch, fmt...) __DEBUG(_fixme, ch, fmt)
+#define ERR(ch, fmt...) __DEBUG(_err, ch, fmt)
+#define WARN(ch, fmt...) __DEBUG(_warn, ch, fmt)
+#define INFO(ch, fmt...) __DEBUG(_info, ch, fmt)
+#define DEBUG(ch, fmt...) __DEBUG(_debug, ch, fmt)
+
+#define FIXME_VA(ch, fmt, a) __DEBUG_VA(_fixme, ch, fmt, a)
+#define ERR_VA(ch, fmt, a) __DEBUG_VA(_err, ch, fmt, a)
+#define WARN_VA(ch, fmt, a) __DEBUG_VA(_warn, ch, fmt, a)
+#define INFO_VA(ch, fmt, a) __DEBUG_VA(_info, ch, fmt, a)
+#define DEBUG_VA(ch, fmt, a) __DEBUG_VA(_debug, ch, fmt, a)
+
+#else
+
+// If we compile without debug support, we want them all to go away
+#define FIXME(ch, fmt...)
+#define INFO(ch, fmt...)
+#define WARN(ch, fmt...)
+#define ERR(ch, fmt...)
+#define DEBUG(ch, fmt...)
+#define FIXME_VA(ch, fmt...)
+#define INFO_VA(ch, fmt...)
+#define WARN_VA(ch, fmt...)
+#define ERR_VA(ch, fmt...)
+#define DEBUG_VA(ch, fmt...)
+
+#endif/*WITH_DEBUG*/
+
+int __log(const char *func, const int line,
+ enum __debug_class cl, const char *ch, const char *fmt, ...)
+ __attribute__((format (printf,5,6)));
+
+int __log_va(const char *func, const int line, enum __debug_class cl,
+ const char *ch, const char *fmt, va_list va);
+
+#define __LOG_PRINT(cl, ch, fmt...) \
+ do { __log(__func__, __LINE__, cl, ch, fmt); } while(0)
+#define __LOG_PRINT_VA(cl, ch, fmt, a) \
+ do { __log_va(__func__, __LINE__, cl, ch, fmt, a); } while(0)
+#define __LOG(cl, ch, fmt...) __LOG_PRINT(__class##cl, #ch, fmt)
+#define __LOG_VA(cl, ch, fmt, a) __LOG_PRINT_VA(__class##cl, #ch, fmt, a)
+
+#define INFO_LOG(ch, fmt...) __LOG(_info, ch, fmt)
+#define WARN_LOG(ch, fmt...) __LOG(_warn, ch, fmt)
+#define ERR_LOG(ch, fmt...) __LOG(_err, ch, fmt)
+#define INFO_LOG_VA(ch, fmt, a) __LOG_VA(_info, ch, fmt, a)
+#define WARN_LOG_VA(ch, fmt, a) __LOG_VA(_warn, ch, fmt, a)
+#define ERR_LOG_VA(ch, fmt, a) __LOG_VA(_err, ch, fmt, a)
+
+#endif/*__PENTOMINOS_DEBUG_H__*/
diff --git a/src/log.cc b/src/log.cc
new file mode 100644
index 0000000..fe72422
--- /dev/null
+++ b/src/log.cc
@@ -0,0 +1,43 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * log.cc
+ *
+ * Tue Oct 24 17:44:47 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Artefact.
+ *
+ * Artefact 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.
+ *
+ * Artefact 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 Artefact; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "log.h"
+
+#include "debug.h"
+
+#include <syslog.h>
+
+/*
+ void openlog(const char *ident, int option, int facility); // Optional
+ void syslog(int priority, const char *format, ...);
+ void closelog(void); // Optional
+*/
+
+void log(std::string message)
+{
+ syslog(LOG_CONS, // Write to console if error sending to system logger.
+ "%s", message.c_str());
+}
diff --git a/src/log.h b/src/log.h
new file mode 100644
index 0000000..14a5585
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,38 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * log.h
+ *
+ * Tue Oct 24 17:44:46 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Artefact.
+ *
+ * Artefact 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.
+ *
+ * Artefact 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 Artefact; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __ARTEFACT_LOG_H__
+#define __ARTEFACT_LOG_H__
+
+#include <string>
+
+/**
+ * log appends a message to the syslog queue.\n
+ * @param message An STL string containing the string to be appended.
+ */
+void log(std::string message);
+
+#endif/*__ARTEFACT_LOG_H__*/
diff --git a/src/saxparser.cc b/src/saxparser.cc
new file mode 100644
index 0000000..14f204c
--- /dev/null
+++ b/src/saxparser.cc
@@ -0,0 +1,412 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * saxparser.cc
+ *
+ * Mon Mar 24 14:40:15 CET 2008
+ * Copyright 2008 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 "saxparser.h"
+#include "debug.h"
+
+#include <string.h>
+#include <stdio.h>
+
+static void character_hndl(void *p, const XML_Char *s, int len)
+{
+ SAXParser *parser = (SAXParser*)XML_GetUserData(p);
+ std::string chars;
+ chars.append(s, len);
+ parser->characterData(chars);
+}
+
+static void start_hndl(void *p, const char *el, const char **attr)
+{
+ SAXParser *parser = (SAXParser*)XML_GetUserData(p);
+
+ // Convert to comfy C++ values...
+ std::string name = el;
+ std::map< std::string, std::string > attributes;
+
+ while(*attr) {
+ std::string at_name = *attr;
+ attr++;
+ std::string at_value = *attr;
+ attr++;
+
+ attributes.insert(make_pair(at_name, at_value));
+ }
+
+ if(parser->outertag == "") parser->outertag = name;
+
+ parser->startTag(name, attributes);
+}
+
+static void end_hndl(void *p, const char *el)
+{
+ SAXParser *parser = (SAXParser*)XML_GetUserData(p);
+ std::string name = el;
+
+ if(name == parser->outertag) parser->done = true;
+
+ parser->endTag(name);
+}
+
+
+SAXParser::SAXParser()
+{
+ p = XML_ParserCreate(NULL);
+ if(!p) {
+ ERR(sax, "Couldn't allocate memory for parser\n");
+ // throw Exception(...);
+ return;
+ }
+
+ // XML_SetEncoding(p, "UTF-8");
+ XML_SetUserData(p, this);
+ XML_UseParserAsHandlerArg(p);
+ XML_SetElementHandler(p, start_hndl, end_hndl);
+ XML_SetCharacterDataHandler(p, character_hndl);
+
+ bufferbytes = 0;
+ totalbytes = 0;
+ done = false;
+
+}
+
+SAXParser::~SAXParser()
+{
+ if(p) XML_ParserFree(p);
+}
+
+int SAXParser::parse()
+{
+ char buf[32];
+ int len;
+
+ do {
+ len = readData(buf, sizeof(buf) - 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;
+}
+
+static bool iswhitespace(const char *buf, size_t size)
+{
+ for(size_t i = 0; i < size; i++)
+ if(buf[i] != ' ' && buf[i] != '\n' && buf[i] != '\t' && buf[i] != '\r')
+ return false;
+ return true;
+}
+
+bool SAXParser::parse(const char *data, size_t size)
+{
+ std::string xml;
+ xml.append(data, size);
+ DEBUG(sax, "parse %d bytes [%s]\n", size, xml.c_str());
+
+ if(data == NULL || size == 0) return done;
+
+ bufferbytes = size;
+ totalbytes += bufferbytes;
+
+ if(! XML_Parse(p, data, size, false) ) {
+ if(XML_GetErrorCode(p) == XML_ERROR_JUNK_AFTER_DOC_ELEMENT) return true;
+ if(XML_GetErrorCode(p) == XML_ERROR_FINISHED &&
+ iswhitespace(data, size)) return true;
+ if(done && XML_GetErrorCode(p) == XML_ERROR_UNCLOSED_TOKEN) return true;
+ parseError(data, size, XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
+ return false;
+ }
+
+ if(done) {
+ if(! XML_Parse(p, data, 0, true) ) {
+ if(XML_GetErrorCode(p) == XML_ERROR_JUNK_AFTER_DOC_ELEMENT) return true;
+ if(XML_GetErrorCode(p) == XML_ERROR_FINISHED &&
+ iswhitespace(data, size)) return true;
+ if(XML_GetErrorCode(p) == XML_ERROR_UNCLOSED_TOKEN) return true;
+ parseError(data, 0, XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
+ return false;
+ }
+ }
+
+ if(done) {
+ DEBUG(sax, "Got END_OF_DOCUMENT [%s] at %ld\n",
+ outertag.c_str(), XML_GetCurrentByteIndex(p));
+ }
+
+ return done;
+}
+
+void SAXParser::parseError(const char *buf, size_t len,
+ std::string error, int lineno)
+{
+ fprintf(stderr, "SAXParser error at line %d: %s\n", lineno, error.c_str());
+ fprintf(stderr, "\tBuffer %u bytes: [", len);
+ if(fwrite(buf, len, 1, stderr) != len) {}
+ fprintf(stderr, "]\n");
+ fflush(stderr);
+}
+
+unsigned int SAXParser::usedBytes()
+{
+ return bufferbytes + (XML_GetCurrentByteIndex(p) - totalbytes);
+}
+
+int SAXParser::readData(char *, size_t)
+{
+ return 0;
+}
+
+void SAXParser::endTag(std::string)
+{
+}
+
+void SAXParser::startTag(std::string, attributes_t &)
+{
+}
+
+void SAXParser::characterData(std::string &)
+{
+}
+
+#ifdef TEST_SAXPARSER
+//deps: log.cc debug.cc exception.cc
+//cflags: -I..
+//libs: -lexpat
+#include <test.h>
+
+#define XMLFILE "/tmp/saxparsertest.xml"
+
+#include "exception.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#include <memory.h>
+
+static char xml[] =
+"<?xml version='1.0' encoding='UTF-8'?>\n"
+"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n"
+" <commit version=\"\" macro=\"referral\" template=\"amd_forunders\">\n"
+" <field value=\"Some docs\" name=\"referral.doctor\"/>\n"
+" <field value=\"DIMS\" name=\"referral.diagnosecode\"/>\n"
+" <field value=\"Avs\" name=\"referral.diagnose\"/>\n"
+" </commit>\n"
+"</pracro>\n \t\n\r"
+ ;
+
+static char xml_notrailingwhitespace[] =
+"<?xml version='1.0' encoding='UTF-8'?>\n"
+"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n"
+" <commit version=\"\" macro=\"referral\" template=\"amd_forunders\">\n"
+" <field value=\"Some docs\" name=\"referral.doctor\"/>\n"
+" <field value=\"DIMS\" name=\"referral.diagnosecode\"/>\n"
+" <field value=\"Avs\" name=\"referral.diagnose\"/>\n"
+" </commit>\n"
+"</pracro>"
+ ;
+
+static char xml_fail[] =
+"<?xml version='1.0' encoding='UTF-8'?>\n"
+"<pracro version=\"1.0\" user\"testuser\" cpr=\"1505050505\">\n"
+" <request macro=\"test\" template=\"test\"/>\n"
+"</pracro>\n"
+ ;
+
+static char xml_fail2[] =
+"<?xml version='1.0' encoding='UTF-8'?>\n"
+"<pracro version=\"1.0\" user\"testuser\" cpr=\"1505050505\">\n"
+" <request macro=\"test\" template=\"test\"/>\n"
+"</pracro>\n"
+"this is junk\n"
+ ;
+
+class MyFileParser :public SAXParser {
+public:
+ MyFileParser(const char *file) {
+ fd = open(file, O_RDONLY);
+ }
+
+ int readData(char *data, size_t size) {
+ return read(fd, data, size);
+ }
+
+ void startTag(std::string name, attributes_t &attributes)
+ {
+ //printf("<%s>\n", name.c_str());
+ }
+
+ void parseError(const char *buf, size_t len, std::string error, int lineno)
+ {
+ throw Exception(error);
+ }
+
+private:
+ int fd;
+};
+
+class MyBufferParser :public SAXParser {
+public:
+ void startTag(std::string name, attributes_t &attributes)
+ {
+ //printf("<%s>\n", name.c_str());
+ }
+
+ void parseError(char *buf, size_t len, std::string error, int lineno)
+ {
+ throw Exception(error);
+ }
+};
+
+TEST_BEGIN;
+
+FILE *fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml);
+fclose(fp);
+
+TEST_MSG("Test callback parser.");
+{
+ MyFileParser parser(XMLFILE);
+ parser.parse();
+}
+
+TEST_MSG("Test buffer parser.");
+for(size_t sz = 1; sz < 1000; sz++) {
+ bool test = false;
+ MyBufferParser parser;
+ std::string buf = xml;
+ size_t pos = 0;
+ while(pos < buf.length()) {
+ std::string substr = buf.substr(pos, sz);
+
+ try {
+ test |= parser.parse((char*)substr.c_str(), substr.length());
+ } catch(Exception &e) {
+ TEST_TRUE(true, "Buffer parser failed on size %d: %s [%s]",
+ sz, e.what(), substr.c_str());
+ }
+ pos += sz;
+ }
+
+ TEST_TRUE(test, "Test buffer parser on %d bytes", sz);
+ }
+
+fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml_notrailingwhitespace);
+fprintf(fp, "%s", xml_notrailingwhitespace);
+fclose(fp);
+
+TEST_MSG("Test buffer parser with multiple documents in the same buffer.");
+{
+ fp = fopen(XMLFILE, "r");
+ TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be read.");
+ if(!fp) TEST_FATAL("Could not read from "XMLFILE);
+
+ for(size_t sz = 1; sz < 1000; sz++) {
+ MyBufferParser *parser = NULL;
+ rewind(fp);
+ size_t numdocs = 0;
+ char *buf = new char[sz + 1];
+ memset(buf, 0, sz + 1);
+ size_t size;
+ while( (size = fread(buf, 1, sz, fp)) > 0) {
+ while(size) {
+ if(parser == NULL) {
+ parser = new MyBufferParser();
+ }
+ if(parser->parse(buf, size)) {
+
+ // Got one
+ numdocs++;
+
+ size = size - parser->usedBytes();
+ strcpy(buf, buf + parser->usedBytes());
+ delete parser; parser = NULL;
+ } else {
+ size = 0;
+ memset(buf, 0, sz + 1);
+ }
+ }
+ }
+ TEST_EQUAL(numdocs, 2, "Test if 2 documents were parsed on docsize %d.", sz);
+ if(parser) delete parser; parser = NULL;
+ delete[] buf;
+ }
+ fclose(fp);
+}
+
+fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml_fail);
+fclose(fp);
+
+TEST_MSG("Test failure");
+{
+ MyFileParser parser(XMLFILE);
+ try {
+ parser.parse();
+ } catch(Exception &e) {
+ goto goon;
+ }
+ TEST_TRUE(false, "This test should fail...\n");
+}
+goon:
+
+fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml_fail2);
+fclose(fp);
+
+// Test failure
+{
+ MyFileParser parser(XMLFILE);
+ try {
+ parser.parse();
+ } catch(Exception &e) {
+ goto goonagain;
+ }
+ TEST_TRUE(false, "This test should fail...\n");
+}
+goonagain:
+
+unlink(XMLFILE);
+
+TEST_END;
+
+#endif/*TEST_SAXPARSER*/
diff --git a/src/saxparser.h b/src/saxparser.h
new file mode 100644
index 0000000..c303d41
--- /dev/null
+++ b/src/saxparser.h
@@ -0,0 +1,152 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * saxparser.h
+ *
+ * Mon Mar 24 14:40:15 CET 2008
+ * Copyright 2008 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.
+ */
+#ifndef __PRACRO_SAXPARSER_H__
+#define __PRACRO_SAXPARSER_H__
+
+#include <string>
+#include <map>
+#include <expat.h>
+
+typedef std::map< std::string, std::string> attributes_t;
+
+/**
+ * This class implements a SAX Parser, utilising the eXpat XML parser library.
+ * It uses virtual methods for the callbacks, and transforms tagnames and
+ * attributes into C++ values (std::string and std::vector).
+ */
+class SAXParser {
+public:
+ /**
+ * Constructor.
+ * It initialises the eXpat library.
+ */
+ SAXParser();
+
+ /**
+ * Destructor.
+ * It frees the eXpat library resources.
+ */
+ virtual ~SAXParser();
+
+ /**
+ * Call this method to use the reimplemented readData method for input.
+ * The entire document is parsed through this single call.
+ * @return An integer wityh value 0 on success, or 1 on failure.
+ * @see int readData(char *data, size_t size)
+ */
+ int parse();
+
+ /**
+ * Character data callback method.
+ * Reimplement this to get character callbacks.
+ * This callback might be called several times, if a character block is big.
+ * In that cae it might be nessecary to buffer to received bytes.
+ * @param data A std::string containing the character data.
+ */
+ virtual void characterData(std::string &data);
+
+ /**
+ * Start tag callback mehtod.
+ * Reimplement this to get start tag callbacks.
+ * It is called each time a new start tag is seen.
+ * @param name A std::string containing the tag name.
+ * @param attributes A std::map of std::string to std::string containing all
+ * attributes for the tag.
+ */
+ virtual void startTag(std::string name, attributes_t &attr);
+
+ /**
+ * End tag callback mehtod.
+ * Reimplement this to get end tag callbacks.
+ * It is called each time an end tag is seen.
+ * @param name A std::string containing the tag name.
+ */
+ virtual void endTag(std::string name);
+
+ /**
+ * Error callback method.
+ * Reimplement this to handle error messages.
+ * A default implementation prints out the current buffer, linenumber and
+ * error message to the screen.
+ * @param buf A char* containing the current buffer being parsed.
+ * @param len A size_t containing the length of the current buffer being
+ * parsed.
+ * @param error A std::string containing the error message.
+ * @param lineno An integer containing the line number on which the error
+ * occurred.
+ */
+ virtual void parseError(const char *buf, size_t len, std::string error,
+ int lineno);
+
+ /**
+ * Buffer parse method.
+ * Use this method to parse an external buffer with xml data.
+ * This method can be called several times (ie. in a read loop).
+ * @param buf A char* containing the buffer to parse.
+ * @param size A size_t comntaining the size of the buffer to parse.
+ * @return A boolean with the value true if a complete document has been seen.
+ * false otherwise.
+ * @see bool parse(char *buf, size_t size)
+ */
+ bool parse(const char *buf, size_t size);
+
+ /**
+ * Get the number of bytes used from the last buffer.
+ * If the buffer parse method is used, and the buffer comes from a stream of
+ * xml doxuments, this method can be used to figure out how many bytes from
+ * the stream should be replayed, to another parser.
+ * @return an integer containing the number of bytes used from the last
+ * buffer.
+ * @see bool parse(char *buf, size_t size)
+ */
+ unsigned int usedBytes();
+
+ // private stuff that needs to be public!
+ std::string outertag;
+ bool done;
+
+protected:
+ /**
+ * Read data callback method.
+ * This method is used when the parse() method is used.
+ * It can be used to connect the parser with eg. a file.
+ * @param data A char* containing the buffer to be filled.
+ * @param size A size_t containing the maximum number of bytes to be filled
+ * (ie. the size of data)
+ * @return An integer contaning the actual number of bytes filled. 0 if no
+ * more bytes are available.
+ * @see int parse()
+ */
+ virtual int readData(char *data, size_t size);
+
+ XML_Parser p;
+
+ unsigned int bufferbytes;
+ unsigned int totalbytes;
+};
+
+#endif/*__PRACRO_SAXPARSER_H__*/
diff --git a/src/xml_encode_decode.cc b/src/xml_encode_decode.cc
new file mode 100644
index 0000000..1c0e377
--- /dev/null
+++ b/src/xml_encode_decode.cc
@@ -0,0 +1,103 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * xml_encode_decode.cc
+ *
+ * Mon Jun 9 10:19:33 CEST 2008
+ * Copyright 2008 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 "xml_encode_decode.h"
+#include <string.h>
+/*
+char xml_map[][2][16] =
+ {
+ { "&", "&amp;" }, // & must be first
+ { "\'", "&apos;" },
+ { "\"", "&qout;" },
+ { ">", "&gt;" },
+ { "<", "&lt;" },
+ { "", "" } // End marker
+ };
+*/
+
+char xml_map[][2][16] =
+ {
+ { "&", "&#38;" }, // & must be first
+ { "\'", "&#39;" },
+ { "\"", "&#34;" },
+ { ">", "&#62;" },
+ { "<", "&#60;" },
+ { "", "" } // End marker
+ };
+
+#define MAX_MAPS 5
+
+std::string xml_encode(std::string str)
+{
+ size_t pos;
+
+ for( int map = 0; map < MAX_MAPS; map++ ) {
+ pos = 0;
+ while( ( pos = str.find(xml_map[map][0], pos) ) != std::string::npos) {
+ str.replace(pos, strlen(xml_map[map][0]), xml_map[map][1]);
+ pos += strlen(xml_map[map][1]);
+ }
+ }
+
+ return str;
+}
+
+std::string xml_decode(std::string str)
+{
+ size_t pos;
+
+ // Traverse backwards, to handle '&' last.
+ for( int map = MAX_MAPS - 1; map > -1; map-- ) {
+ pos = 0;
+ while( ( pos = str.find(xml_map[map][1], pos) ) != std::string::npos) {
+ str.replace(pos, strlen(xml_map[map][1]), xml_map[map][0]);
+ pos += strlen(xml_map[map][0]);
+ }
+ }
+
+ return str;
+}
+
+#ifdef TEST_XML_ENCODE_DECODE
+//deps:
+//cflags:
+//libs:
+#include <test.h>
+
+TEST_BEGIN;
+
+std::string in = "&A<B>C\"D\'<>\"&amp;E<>";
+std::string enc = xml_encode(in);
+std::string denc = xml_encode(enc);
+std::string dec = xml_decode(denc);
+std::string ddec = xml_decode(dec);
+
+TEST_EQUAL_STR(in, ddec, "compare");
+TEST_EQUAL_STR(enc, dec, "compare");
+
+TEST_END;
+
+#endif/*TEST_XML_ENCODE_DECODE*/
diff --git a/src/xml_encode_decode.h b/src/xml_encode_decode.h
new file mode 100644
index 0000000..39b1407
--- /dev/null
+++ b/src/xml_encode_decode.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * xml_encode_decode.h
+ *
+ * Mon Jun 9 10:19:33 CEST 2008
+ * Copyright 2008 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.
+ */
+#ifndef __PRACRO_XML_ENCODE_DECODE_H__
+#define __PRACRO_XML_ENCODE_DECODE_H__
+
+#include <string>
+
+std::string xml_encode(std::string str);
+std::string xml_decode(std::string str);
+
+#endif/*__PRACRO_XML_ENCODE_DECODE_H__*/