Re: Re: XMLPULL in C++

Nick Woolley <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
Hi

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).  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...
};

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...
};


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..
	

}

> we have an initial version of XML tests now in CVS, checkout src/xml/tests
> (i am attaching example below) - those tests eventually should cover whole API.

Thanks, I'll look at this later...

Cheers,

Nick
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.