Line data Source code
1 : // file : xsd/cxx/xml/sax/std-input-source.hxx
2 : // copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
3 : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 :
5 : #ifndef XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX
6 : #define XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX
7 :
8 : #include <istream>
9 :
10 : #include <xsd/cxx/xml/string.hxx>
11 :
12 : #include <xercesc/sax/InputSource.hpp>
13 : #include <xercesc/util/BinInputStream.hpp>
14 :
15 : namespace xsd
16 : {
17 : namespace cxx
18 : {
19 : namespace xml
20 : {
21 : namespace sax
22 : {
23 : class std_input_stream: public xercesc::BinInputStream
24 : {
25 : public:
26 0 : std_input_stream (std::istream& is)
27 0 : : is_ (is)
28 : {
29 0 : }
30 :
31 : virtual XMLFilePos
32 0 : curPos () const
33 : {
34 0 : return static_cast<XMLFilePos> (is_.tellg ());
35 : }
36 :
37 : virtual XMLSize_t
38 0 : readBytes (XMLByte* const buf, const XMLSize_t size)
39 : {
40 : // Some implementations don't clear gcount if you
41 : // call read() on a stream that is in the eof state.
42 : //
43 0 : if (is_.eof ())
44 0 : return 0;
45 :
46 : // Unset the exception failbit while we are working
47 : // with the stream.
48 : //
49 0 : std::ios_base::iostate old (is_.exceptions ());
50 0 : is_.exceptions (old & ~std::ios_base::failbit);
51 :
52 0 : is_.read (reinterpret_cast<char*> (buf),
53 : static_cast<std::streamsize> (size));
54 :
55 : // Clear the fail bit if it was caused by eof and restore
56 : // the original exception state. If there are any pending
57 : // errors then the exception will be thrown now.
58 : //
59 0 : if (is_.fail () && is_.eof ())
60 0 : is_.clear (is_.rdstate () & ~std::ios_base::failbit);
61 :
62 0 : is_.exceptions (old);
63 :
64 : // Make sure that if we failed, readBytes won't be called
65 : // again.
66 : //
67 0 : return !is_.fail ()
68 0 : ? static_cast<XMLSize_t> (is_.gcount ())
69 0 : : 0;
70 : }
71 :
72 : virtual const XMLCh*
73 0 : getContentType () const
74 : {
75 0 : return 0;
76 : }
77 :
78 : private:
79 : std::istream& is_;
80 : };
81 :
82 :
83 : class std_input_source: public xercesc::InputSource
84 : {
85 : public:
86 0 : std_input_source (std::istream& is)
87 0 : : is_ (&is)
88 : {
89 0 : }
90 :
91 : template <typename C>
92 : std_input_source (std::istream& is, const C* system_id)
93 : : xercesc::InputSource (xml::string (system_id).c_str ()),
94 : is_ (&is)
95 : {
96 : }
97 :
98 : template <typename C>
99 0 : std_input_source (std::istream& is,
100 : const std::basic_string<C>& system_id)
101 0 : : xercesc::InputSource (xml::string (system_id).c_str ()),
102 0 : is_ (&is)
103 : {
104 0 : }
105 :
106 : template <typename C>
107 : std_input_source (std::istream& is,
108 : const C* system_id,
109 : const C* public_id)
110 : : xercesc::InputSource (xml::string (system_id).c_str (),
111 : xml::string (public_id).c_str ()),
112 : is_ (&is)
113 : {
114 : }
115 :
116 : template <typename C>
117 : std_input_source (std::istream& is,
118 : const std::basic_string<C>& system_id,
119 : const std::basic_string<C>& public_id)
120 : : xercesc::InputSource (xml::string (system_id).c_str (),
121 : xml::string (public_id).c_str ()),
122 : is_ (&is)
123 : {
124 : }
125 :
126 : struct copy {};
127 :
128 : // Throws the copy exception if this function is called more
129 : // than once.
130 : //
131 : virtual xercesc::BinInputStream*
132 0 : makeStream () const
133 : {
134 0 : if (is_ == 0)
135 0 : throw copy ();
136 :
137 0 : std::istream& is (*is_);
138 :
139 0 : is_ = 0;
140 :
141 0 : return new std_input_stream (is);
142 : }
143 :
144 : private:
145 : mutable std::istream* is_;
146 : };
147 : }
148 : }
149 : }
150 : }
151 :
152 : #endif // XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX
|