1 /* parserinputbuffer.cc
2  * this file is part of libxml++
3  *
4  * copyright (C) 2003 by libxml++ developer's team
5  *
6  * this file is covered by the GNU Lesser General Public License,
7  * which should be included with libxml++ as the file COPYING.
8  */
9 
10 #include <libxml++/io/parserinputbuffer.h>
11 #include <libxml++/exceptions/internal_error.h>
12 
13 #include <libxml/globals.h> //Needed by libxml/xmlIO.h
14 #include <libxml/xmlIO.h>
15 
16 namespace xmlpp
17 {
18 
19   struct ParserInputBufferCallback
20   {
on_readxmlpp::ParserInputBufferCallback21     static int on_read(void * context, char * buffer, int len)
22     {
23       auto tmp = static_cast<ParserInputBuffer*>(context);
24       return tmp->do_read(buffer, len);
25     }
26 
on_closexmlpp::ParserInputBufferCallback27     static int on_close(void * context)
28     {
29       auto tmp = static_cast<ParserInputBuffer*>(context);
30       return tmp->do_close();
31     }
32   };
33 
34 
ParserInputBuffer()35   ParserInputBuffer::ParserInputBuffer()
36   {
37     impl_ = xmlParserInputBufferCreateIO(
38         &ParserInputBufferCallback::on_read,
39         &ParserInputBufferCallback::on_close,
40         static_cast<void*>(this),
41         XML_CHAR_ENCODING_NONE);
42     if (!impl_)
43     {
44       throw internal_error("Cannot initialise underlying xmlParserInputBuffer");
45     }
46   }
47 
~ParserInputBuffer()48   ParserInputBuffer::~ParserInputBuffer()
49   {
50   }
51 
on_close()52   bool ParserInputBuffer::on_close()
53   {
54     bool result = do_close();
55     // the underlying structure is being freed by libxml, the pointer will soon be
56     // invalid.
57     impl_ = nullptr;
58 
59     return result;
60   }
61 
on_read(char * buffer,int len)62   int ParserInputBuffer::on_read(
63       char * buffer,
64       int len)
65   {
66     return do_read(buffer, len);
67   }
68 
do_close()69   bool ParserInputBuffer::do_close()
70   {
71     return true;
72   }
73 
cobj()74   _xmlParserInputBuffer* ParserInputBuffer::cobj()
75   {
76     return impl_;
77   }
78 
cobj() const79   const _xmlParserInputBuffer* ParserInputBuffer::cobj() const
80   {
81     return impl_;
82   }
83 
84 }
85