summaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/xml/sax/std-input-source.hxx
blob: bc2a84fa83f431d6ca1b129f464dfb08afa84197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// file      : xsd/cxx/xml/sax/std-input-source.hxx
// copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX
#define XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX

#include <istream>

#include <xsd/cxx/xml/string.hxx>

#include <xercesc/sax/InputSource.hpp>
#include <xercesc/util/BinInputStream.hpp>

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<XMLFilePos> (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<char*> (buf),
                      static_cast<std::streamsize> (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<XMLSize_t> (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 <typename C>
          std_input_source (std::istream& is, const C* system_id)
              : xercesc::InputSource (xml::string (system_id).c_str ()),
                is_ (&is)
          {
          }

          template <typename C>
          std_input_source (std::istream& is,
                            const std::basic_string<C>& system_id)
              : xercesc::InputSource (xml::string (system_id).c_str ()),
                is_ (&is)
          {
          }

          template <typename C>
          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 <typename C>
          std_input_source (std::istream& is,
                            const std::basic_string<C>& system_id,
                            const std::basic_string<C>& 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  // XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX