Re: Re: XMLPULL in C++

Aleksander Slominski <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
Nick Woolley wrote:

> Aleksander Slominski wrote:
> >
> > fundamental choice is between char_t*/string (UTF-8 internally)
> > or wchar_t*/wstring (UTF-16 internally).
> >
> >>     And what IO streaming do you mean which can't be handled by C++
> >>streams?
> >
> > you need to add support decoding different charsets, basically to have
> > something similar to:
> >     new InputStreamReader(byte_stream_fd, "UTF-8");
>
> Possibly I'm being naive, but wouldn't this be a solution, using the
> ANSI C++ basic_string and basic_stream classes (below).

do they support transcoding like ISO-8859-1 to UTF-8 or UTF-16?

> It doesn't use
> the pattern where InputStreamReader/Parser can cope with either kind of
> input, rather it uses C++'s templating facility construct specific
> classes for each type of encoding.
>
> template<class Ch, class Tr = char_traits<Ch> >
> class XmlPullParser
> {
> public:
>     typedef std::basic_stream<Ch, Tr>  XmlStream;
>     typedef std::basic_string<Ch, Tr> XmlString;
>
> // XML data is handled as streams/strings of whatever kind of
> // character class Ch is specified when the template class is
> // instantiated
>
> // we omit the get/setInputEncoding calls, since the encoding is set
> // when the parser class is instantiated.
>
>     void setInput(XmlStream* instream);
> // pointer (rather than reference) allows us to 'unset' the stream
> // (to 0) if we want to make sure things are disconnected
>
>     const XmlString& getText();
> // (this is assuming we have an internal XmlString we can pass
> // back, else this would have to have a return type of just 'XmlString'
>
> // normal strings can be used for normal uses
>     std::string getPositionDescription() const;
>
> // rest of API etc...
> };

probably XmlString should be also parametrized like XmlString<Ch>

> template<class Ch, Tr = char_traits<Ch> >
> class XmlPullParserImpl : public XmlPullParser<Ch, Tr>
> {
> // implement API here ...
> };
>
> template<class Ch, Tr = char_traits<Ch> >
> class XmlPullParserFactory
> {
> // implement Factory here...
> };

ok.

> Then these would be used like this:
>
> int main()
> {
>      XmlPullParserImpl<char> utf8_parser;
>      XmlPullParserImpl<wchar> utf16_parser;
>
>      // or
>      XmlPullParserFactory<wchar> factory;
>      XmlPullParser<whar>* utf16_parser2 = factory.newPullParser();
>
>      basic_istream<wchar> utf16_stream;
>
>      utf16_parser2->setInput(&utf16_stream);
>
>      utf16_parser.next();
>      basic_string<wchar> text = utf16_parser.getText();
>      // etc..
>
>
> }

hi,

i have done something similar in XPP1 though i have used #define.

however the problem is that utf16_stream needs to have still
source encoding information: even for utf16 on can have LE or UE and need to do
BOM detection and that is even more complex for byte streams as you need to convert
byte stream into UTF-16 or UTF-8 and input can be in ISO-8859-1, UTF8 or even UTF-16 ...

so i think you essentially you need to abstract encode/decode functionality
(i do not think such functionality is part of standard C++ library).

alek
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.