223c08ab2854ef8b21f6cfa635b3b16a2edb370f
[platform/upstream/libxml++.git] / libxml++ / io / parserinputbuffer.cc
1 /* document.h
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   {
21     static int on_read(void * context, char * buffer, int len)
22     {
23       ParserInputBuffer * tmp = static_cast<ParserInputBuffer*>(context);
24       return tmp->do_read(buffer, len);
25     }
26
27     static int on_close(void * context)
28     {
29       ParserInputBuffer * tmp = static_cast<ParserInputBuffer*>(context);
30       return tmp->do_close();
31     }
32   };
33
34
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_ == 0)
43     {
44       throw internal_error("Cannot initialise underlying xmlParserInputBuffer");
45     }
46   }
47
48   ParserInputBuffer::~ParserInputBuffer()
49   {
50   }
51
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_ = 0;
58
59     return result;
60   }
61
62   int ParserInputBuffer::on_read(
63       char * buffer,
64       int len)
65   {
66     return do_read(buffer, len);
67   }
68
69   bool ParserInputBuffer::do_close()
70   {
71     return true;
72   }
73
74   _xmlParserInputBuffer* ParserInputBuffer::cobj()
75   {
76     return impl_;
77   }
78
79   const _xmlParserInputBuffer* ParserInputBuffer::cobj() const
80   {
81     return impl_;
82   }
83
84 }