From 2615896faa646e5830abf2c269150e1165c66515 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- libxsd/libxsd/cxx/xml/sax/std-input-source.hxx | 151 +++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 libxsd/libxsd/cxx/xml/sax/std-input-source.hxx (limited to 'libxsd/libxsd/cxx/xml/sax/std-input-source.hxx') diff --git a/libxsd/libxsd/cxx/xml/sax/std-input-source.hxx b/libxsd/libxsd/cxx/xml/sax/std-input-source.hxx new file mode 100644 index 0000000..c51577f --- /dev/null +++ b/libxsd/libxsd/cxx/xml/sax/std-input-source.hxx @@ -0,0 +1,151 @@ +// file : libxsd/cxx/xml/sax/std-input-source.hxx +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef LIBXSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX +#define LIBXSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX + +#include + +#include + +#include +#include + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace sax + { + class std_input_stream: public xercesc::BinInputStream + { + public: + std_input_stream (std::istream& is) + : is_ (is) + { + } + + virtual XMLFilePos + curPos () const + { + return static_cast (is_.tellg ()); + } + + virtual XMLSize_t + readBytes (XMLByte* const buf, const XMLSize_t size) + { + // Some implementations don't clear gcount if you + // call read() on a stream that is in the eof state. + // + if (is_.eof ()) + return 0; + + // Unset the exception failbit while we are working + // with the stream. + // + std::ios_base::iostate old (is_.exceptions ()); + is_.exceptions (old & ~std::ios_base::failbit); + + is_.read (reinterpret_cast (buf), + static_cast (size)); + + // Clear the fail bit if it was caused by eof and restore + // the original exception state. If there are any pending + // errors then the exception will be thrown now. + // + if (is_.fail () && is_.eof ()) + is_.clear (is_.rdstate () & ~std::ios_base::failbit); + + is_.exceptions (old); + + // Make sure that if we failed, readBytes won't be called + // again. + // + return !is_.fail () + ? static_cast (is_.gcount ()) + : 0; + } + + virtual const XMLCh* + getContentType () const + { + return 0; + } + + private: + std::istream& is_; + }; + + + class std_input_source: public xercesc::InputSource + { + public: + std_input_source (std::istream& is) + : is_ (&is) + { + } + + template + std_input_source (std::istream& is, const C* system_id) + : xercesc::InputSource (xml::string (system_id).c_str ()), + is_ (&is) + { + } + + template + std_input_source (std::istream& is, + const std::basic_string& system_id) + : xercesc::InputSource (xml::string (system_id).c_str ()), + is_ (&is) + { + } + + template + std_input_source (std::istream& is, + const C* system_id, + const C* public_id) + : xercesc::InputSource (xml::string (system_id).c_str (), + xml::string (public_id).c_str ()), + is_ (&is) + { + } + + template + std_input_source (std::istream& is, + const std::basic_string& system_id, + const std::basic_string& public_id) + : xercesc::InputSource (xml::string (system_id).c_str (), + xml::string (public_id).c_str ()), + is_ (&is) + { + } + + struct copy {}; + + // Throws the copy exception if this function is called more + // than once. + // + virtual xercesc::BinInputStream* + makeStream () const + { + if (is_ == 0) + throw copy (); + + std::istream& is (*is_); + + is_ = 0; + + return new std_input_stream (is); + } + + private: + mutable std::istream* is_; + }; + } + } + } +} + +#endif // LIBXSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX -- cgit v1.1