diff options
author | David Tardon <dtardon@redhat.com> | 2015-07-20 18:23:46 +0200 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2015-07-20 18:23:46 +0200 |
commit | 5b75213bedc88d6df1d95611521a913ad97a4bdc (patch) | |
tree | 1afdcb7607bf4e2c9d27da402692aa1378a158dc | |
parent | fcddf7e158245164b3e58b52bbd358eb1b10f07a (diff) |
move libxml2-related functions to a separate file
Change-Id: I41a37b02f72e5c8d42e636b54627716add60e70f
-rw-r--r-- | src/lib/Makefile.am | 2 | ||||
-rw-r--r-- | src/lib/VDXParser.cpp | 1 | ||||
-rw-r--r-- | src/lib/VSDXMLHelper.cpp | 172 | ||||
-rw-r--r-- | src/lib/VSDXMLHelper.h | 21 | ||||
-rw-r--r-- | src/lib/VSDXMLParserBase.cpp | 1 | ||||
-rw-r--r-- | src/lib/VSDXMetaData.cpp | 1 | ||||
-rw-r--r-- | src/lib/VSDXParser.cpp | 1 | ||||
-rw-r--r-- | src/lib/VSDXTheme.cpp | 1 | ||||
-rw-r--r-- | src/lib/VisioDocument.cpp | 1 | ||||
-rw-r--r-- | src/lib/libvisio_xml.cpp | 189 | ||||
-rw-r--r-- | src/lib/libvisio_xml.h | 48 |
11 files changed, 246 insertions, 192 deletions
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index f07c5ac..f5968fd 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -27,6 +27,7 @@ libvisio_@VSD_MAJOR_VERSION@_@VSD_MINOR_VERSION@_la_DEPENDENCIES = @LIBVISIO_WIN libvisio_@VSD_MAJOR_VERSION@_@VSD_MINOR_VERSION@_la_LDFLAGS = $(version_info) -export-dynamic -no-undefined libvisio_@VSD_MAJOR_VERSION@_@VSD_MINOR_VERSION@_la_SOURCES = \ libvisio_utils.cpp \ + libvisio_xml.cpp \ VisioDocument.cpp \ VSD5Parser.cpp \ VSD6Parser.cpp \ @@ -46,6 +47,7 @@ libvisio_@VSD_MAJOR_VERSION@_@VSD_MINOR_VERSION@_la_SOURCES = \ VSDStylesCollector.cpp \ VSDXMLHelper.cpp \ libvisio_utils.h \ + libvisio_xml.h \ VSD5Parser.h \ VSD6Parser.h \ VSDInternalStream.h \ diff --git a/src/lib/VDXParser.cpp b/src/lib/VDXParser.cpp index 1776de2..a64c5fc 100644 --- a/src/lib/VDXParser.cpp +++ b/src/lib/VDXParser.cpp @@ -15,6 +15,7 @@ #include <boost/shared_ptr.hpp> #include "VDXParser.h" #include "libvisio_utils.h" +#include "libvisio_xml.h" #include "VSDContentCollector.h" #include "VSDStylesCollector.h" #include "VSDXMLHelper.h" diff --git a/src/lib/VSDXMLHelper.cpp b/src/lib/VSDXMLHelper.cpp index b42b093..5bd40f0 100644 --- a/src/lib/VSDXMLHelper.cpp +++ b/src/lib/VSDXMLHelper.cpp @@ -17,180 +17,10 @@ #include <boost/algorithm/string.hpp> #include <boost/lexical_cast.hpp> #include <boost/shared_ptr.hpp> -#include <libxml/xmlIO.h> -#include <libxml/xmlstring.h> #include <librevenge-stream/librevenge-stream.h> #include "VSDXMLHelper.h" #include "libvisio_utils.h" - - -namespace -{ - -extern "C" { - - static int vsdxInputCloseFunc(void *) - { - return 0; - } - - static int vsdxInputReadFunc(void *context, char *buffer, int len) - { - librevenge::RVNGInputStream *input = (librevenge::RVNGInputStream *)context; - - if ((!input) || (!buffer) || (len < 0)) - return -1; - - if (input->isEnd()) - return 0; - - unsigned long tmpNumBytesRead = 0; - const unsigned char *tmpBuffer = input->read(len, tmpNumBytesRead); - - if (tmpBuffer && tmpNumBytesRead) - memcpy(buffer, tmpBuffer, tmpNumBytesRead); - return tmpNumBytesRead; - } - -#ifdef DEBUG - static void vsdxReaderErrorFunc(void *, const char *message, xmlParserSeverities severity, xmlTextReaderLocatorPtr) -#else - static void vsdxReaderErrorFunc(void *, const char *, xmlParserSeverities severity, xmlTextReaderLocatorPtr) -#endif - { - switch (severity) - { - case XML_PARSER_SEVERITY_VALIDITY_WARNING: - VSD_DEBUG_MSG(("Found xml parser severity validity warning %s\n", message)); - break; - case XML_PARSER_SEVERITY_VALIDITY_ERROR: - VSD_DEBUG_MSG(("Found xml parser severity validity error %s\n", message)); - break; - case XML_PARSER_SEVERITY_WARNING: - VSD_DEBUG_MSG(("Found xml parser severity warning %s\n", message)); - break; - case XML_PARSER_SEVERITY_ERROR: - VSD_DEBUG_MSG(("Found xml parser severity error %s\n", message)); - break; - default: - break; - } - } - -} // extern "C" - -} // anonymous namespace - -// xmlTextReader helper function - -xmlTextReaderPtr libvisio::xmlReaderForStream(librevenge::RVNGInputStream *input, const char *URL, const char *encoding, int options) -{ - xmlTextReaderPtr reader = xmlReaderForIO(vsdxInputReadFunc, vsdxInputCloseFunc, (void *)input, URL, encoding, options); - xmlTextReaderSetErrorHandler(reader, vsdxReaderErrorFunc, 0); - return reader; -} - -libvisio::Colour libvisio::xmlStringToColour(const xmlChar *s) -{ - if (xmlStrEqual(s, BAD_CAST("Themed"))) - return libvisio::Colour(); - std::string str((const char *)s); - if (str[0] == '#') - { - if (str.length() != 7) - { - VSD_DEBUG_MSG(("Throwing XmlParserException\n")); - throw XmlParserException(); - } - else - str.erase(str.begin()); - } - else - { - if (str.length() != 6) - { - VSD_DEBUG_MSG(("Throwing XmlParserException\n")); - throw XmlParserException(); - } - } - - std::istringstream istr(str); - unsigned val = 0; - istr >> std::hex >> val; - - return Colour((val & 0xff0000) >> 16, (val & 0xff00) >> 8, val & 0xff, 0); -} - -libvisio::Colour libvisio::xmlStringToColour(const boost::shared_ptr<xmlChar> &s) -{ - return xmlStringToColour(s.get()); -} - -long libvisio::xmlStringToLong(const xmlChar *s) -{ - using boost::lexical_cast; - using boost::bad_lexical_cast; - if (xmlStrEqual(s, BAD_CAST("Themed"))) - return 0; - - try - { - return boost::lexical_cast<long, const char *>((const char *)s); - } - catch (const boost::bad_lexical_cast &) - { - VSD_DEBUG_MSG(("Throwing XmlParserException\n")); - throw XmlParserException(); - } - return 0; -} - -long libvisio::xmlStringToLong(const boost::shared_ptr<xmlChar> &s) -{ - return xmlStringToLong(s.get()); -} - -double libvisio::xmlStringToDouble(const xmlChar *s) try -{ - if (xmlStrEqual(s, BAD_CAST("Themed"))) - return 0.0; - - return boost::lexical_cast<double, const char *>((const char *)s); -} -catch (const boost::bad_lexical_cast &) -{ - VSD_DEBUG_MSG(("Throwing XmlParserException\n")); - throw XmlParserException(); -} - -double libvisio::xmlStringToDouble(const boost::shared_ptr<xmlChar> &s) -{ - return xmlStringToDouble(s.get()); -} - -bool libvisio::xmlStringToBool(const xmlChar *s) -{ - if (xmlStrEqual(s, BAD_CAST("Themed"))) - return 0; - - bool value = false; - if (xmlStrEqual(s, BAD_CAST("true")) || xmlStrEqual(s, BAD_CAST("1"))) - value = true; - else if (xmlStrEqual(s, BAD_CAST("false")) || xmlStrEqual(s, BAD_CAST("0"))) - value = false; - else - { - VSD_DEBUG_MSG(("Throwing XmlParserException\n")); - throw XmlParserException(); - } - return value; - -} - -bool libvisio::xmlStringToBool(const boost::shared_ptr<xmlChar> &s) -{ - return xmlStringToBool(s.get()); -} +#include "libvisio_xml.h" // VSDXRelationship diff --git a/src/lib/VSDXMLHelper.h b/src/lib/VSDXMLHelper.h index 588ca50..0a46a6a 100644 --- a/src/lib/VSDXMLHelper.h +++ b/src/lib/VSDXMLHelper.h @@ -20,27 +20,6 @@ namespace libvisio { -// create an xmlTextReader pointer from a librevenge::RVNGInputStream pointer -// needs to be freed using xmlTextReaderFree function. - -xmlTextReaderPtr xmlReaderForStream(librevenge::RVNGInputStream *input, - const char *URL, - const char *encoding, - int options); - -Colour xmlStringToColour(const xmlChar *s); -Colour xmlStringToColour(const boost::shared_ptr<xmlChar> &s); - -long xmlStringToLong(const xmlChar *s); -long xmlStringToLong(const boost::shared_ptr<xmlChar> &s); - -double xmlStringToDouble(const xmlChar *s); -double xmlStringToDouble(const boost::shared_ptr<xmlChar> &s); - -bool xmlStringToBool(const xmlChar *s); -bool xmlStringToBool(const boost::shared_ptr<xmlChar> &s); - - class VSDCollector; // Helper classes to properly handle OPC relationships diff --git a/src/lib/VSDXMLParserBase.cpp b/src/lib/VSDXMLParserBase.cpp index 33afca9..76d254e 100644 --- a/src/lib/VSDXMLParserBase.cpp +++ b/src/lib/VSDXMLParserBase.cpp @@ -16,6 +16,7 @@ #include <boost/spirit/include/classic.hpp> #include "VSDXMLParserBase.h" #include "libvisio_utils.h" +#include "libvisio_xml.h" #include "VSDContentCollector.h" #include "VSDStylesCollector.h" #include "VSDXMLHelper.h" diff --git a/src/lib/VSDXMetaData.cpp b/src/lib/VSDXMetaData.cpp index ffcad6e..3724dc9 100644 --- a/src/lib/VSDXMetaData.cpp +++ b/src/lib/VSDXMetaData.cpp @@ -10,6 +10,7 @@ #include "VSDXMetaData.h" #include "VSDXMLTokenMap.h" #include "libvisio_utils.h" +#include "libvisio_xml.h" #include <string> #include <boost/shared_ptr.hpp> diff --git a/src/lib/VSDXParser.cpp b/src/lib/VSDXParser.cpp index 9cb62e6..dfaca93 100644 --- a/src/lib/VSDXParser.cpp +++ b/src/lib/VSDXParser.cpp @@ -15,6 +15,7 @@ #include <boost/shared_ptr.hpp> #include "VSDXParser.h" #include "libvisio_utils.h" +#include "libvisio_xml.h" #include "VSDContentCollector.h" #include "VSDStylesCollector.h" #include "VSDXMLHelper.h" diff --git a/src/lib/VSDXTheme.cpp b/src/lib/VSDXTheme.cpp index 142623f..edf395c 100644 --- a/src/lib/VSDXTheme.cpp +++ b/src/lib/VSDXTheme.cpp @@ -13,6 +13,7 @@ #include "VSDXMLTokenMap.h" #include "libvisio_utils.h" +#include "libvisio_xml.h" using boost::shared_ptr; diff --git a/src/lib/VisioDocument.cpp b/src/lib/VisioDocument.cpp index ee7c758..6304391 100644 --- a/src/lib/VisioDocument.cpp +++ b/src/lib/VisioDocument.cpp @@ -12,6 +12,7 @@ #include <librevenge/librevenge.h> #include <libvisio/libvisio.h> #include "libvisio_utils.h" +#include "libvisio_xml.h" #include "VDXParser.h" #include "VSDParser.h" #include "VSDXParser.h" diff --git a/src/lib/libvisio_xml.cpp b/src/lib/libvisio_xml.cpp new file mode 100644 index 0000000..09ad165 --- /dev/null +++ b/src/lib/libvisio_xml.cpp @@ -0,0 +1,189 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libvisio project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "libvisio_xml.h" + +#include <boost/lexical_cast.hpp> + +#include "VSDTypes.h" +#include "libvisio_utils.h" + +namespace libvisio +{ + +namespace +{ + +extern "C" +{ + + static int vsdxInputCloseFunc(void *) + { + return 0; + } + + static int vsdxInputReadFunc(void *context, char *buffer, int len) + { + librevenge::RVNGInputStream *input = (librevenge::RVNGInputStream *)context; + + if ((!input) || (!buffer) || (len < 0)) + return -1; + + if (input->isEnd()) + return 0; + + unsigned long tmpNumBytesRead = 0; + const unsigned char *tmpBuffer = input->read(len, tmpNumBytesRead); + + if (tmpBuffer && tmpNumBytesRead) + memcpy(buffer, tmpBuffer, tmpNumBytesRead); + return tmpNumBytesRead; + } + +#ifdef DEBUG + static void vsdxReaderErrorFunc(void *, const char *message, xmlParserSeverities severity, xmlTextReaderLocatorPtr) +#else + static void vsdxReaderErrorFunc(void *, const char *, xmlParserSeverities severity, xmlTextReaderLocatorPtr) +#endif + { + switch (severity) + { + case XML_PARSER_SEVERITY_VALIDITY_WARNING: + VSD_DEBUG_MSG(("Found xml parser severity validity warning %s\n", message)); + break; + case XML_PARSER_SEVERITY_VALIDITY_ERROR: + VSD_DEBUG_MSG(("Found xml parser severity validity error %s\n", message)); + break; + case XML_PARSER_SEVERITY_WARNING: + VSD_DEBUG_MSG(("Found xml parser severity warning %s\n", message)); + break; + case XML_PARSER_SEVERITY_ERROR: + VSD_DEBUG_MSG(("Found xml parser severity error %s\n", message)); + break; + default: + break; + } + } + +} // extern "C" + +} // anonymous namespace + +xmlTextReaderPtr xmlReaderForStream(librevenge::RVNGInputStream *input, const char *URL, const char *encoding, int options) +{ + xmlTextReaderPtr reader = xmlReaderForIO(vsdxInputReadFunc, vsdxInputCloseFunc, (void *)input, URL, encoding, options); + xmlTextReaderSetErrorHandler(reader, vsdxReaderErrorFunc, 0); + return reader; +} + +Colour xmlStringToColour(const xmlChar *s) +{ + if (xmlStrEqual(s, BAD_CAST("Themed"))) + return Colour(); + std::string str((const char *)s); + if (str[0] == '#') + { + if (str.length() != 7) + { + VSD_DEBUG_MSG(("Throwing XmlParserException\n")); + throw XmlParserException(); + } + else + str.erase(str.begin()); + } + else + { + if (str.length() != 6) + { + VSD_DEBUG_MSG(("Throwing XmlParserException\n")); + throw XmlParserException(); + } + } + + std::istringstream istr(str); + unsigned val = 0; + istr >> std::hex >> val; + + return Colour((val & 0xff0000) >> 16, (val & 0xff00) >> 8, val & 0xff, 0); +} + +Colour xmlStringToColour(const boost::shared_ptr<xmlChar> &s) +{ + return xmlStringToColour(s.get()); +} + +long xmlStringToLong(const xmlChar *s) +{ + using boost::lexical_cast; + using boost::bad_lexical_cast; + if (xmlStrEqual(s, BAD_CAST("Themed"))) + return 0; + + try + { + return boost::lexical_cast<long, const char *>((const char *)s); + } + catch (const boost::bad_lexical_cast &) + { + VSD_DEBUG_MSG(("Throwing XmlParserException\n")); + throw XmlParserException(); + } + return 0; +} + +long xmlStringToLong(const boost::shared_ptr<xmlChar> &s) +{ + return xmlStringToLong(s.get()); +} + +double xmlStringToDouble(const xmlChar *s) try +{ + if (xmlStrEqual(s, BAD_CAST("Themed"))) + return 0.0; + + return boost::lexical_cast<double, const char *>((const char *)s); +} +catch (const boost::bad_lexical_cast &) +{ + VSD_DEBUG_MSG(("Throwing XmlParserException\n")); + throw XmlParserException(); +} + +double xmlStringToDouble(const boost::shared_ptr<xmlChar> &s) +{ + return xmlStringToDouble(s.get()); +} + +bool xmlStringToBool(const xmlChar *s) +{ + if (xmlStrEqual(s, BAD_CAST("Themed"))) + return 0; + + bool value = false; + if (xmlStrEqual(s, BAD_CAST("true")) || xmlStrEqual(s, BAD_CAST("1"))) + value = true; + else if (xmlStrEqual(s, BAD_CAST("false")) || xmlStrEqual(s, BAD_CAST("0"))) + value = false; + else + { + VSD_DEBUG_MSG(("Throwing XmlParserException\n")); + throw XmlParserException(); + } + return value; + +} + +bool xmlStringToBool(const boost::shared_ptr<xmlChar> &s) +{ + return xmlStringToBool(s.get()); +} + +} // namespace libvisio + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/libvisio_xml.h b/src/lib/libvisio_xml.h new file mode 100644 index 0000000..3ec7524 --- /dev/null +++ b/src/lib/libvisio_xml.h @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libvisio project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef __LIBVISIO_XML_H__ +#define __LIBVISIO_XML_H__ + +#include <boost/shared_ptr.hpp> + +#include <librevenge-stream/librevenge-stream.h> + +#include <libxml/xmlreader.h> + +namespace libvisio +{ + +struct Colour; + +// create an xmlTextReader pointer from a librevenge::RVNGInputStream pointer +// needs to be freed using xmlTextReaderFree function. + +xmlTextReaderPtr xmlReaderForStream(librevenge::RVNGInputStream *input, + const char *URL, + const char *encoding, + int options); + +Colour xmlStringToColour(const xmlChar *s); +Colour xmlStringToColour(const boost::shared_ptr<xmlChar> &s); + +long xmlStringToLong(const xmlChar *s); +long xmlStringToLong(const boost::shared_ptr<xmlChar> &s); + +double xmlStringToDouble(const xmlChar *s); +double xmlStringToDouble(const boost::shared_ptr<xmlChar> &s); + +bool xmlStringToBool(const xmlChar *s); +bool xmlStringToBool(const boost::shared_ptr<xmlChar> &s); + +} // namespace libvisio + +#endif // __LIBVISIO_XML_H__ + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |