[Expat-discuss] Expat and C++ ?

Albrecht Fritzsche ali at mental.com
Tue Jul 4 14:02:29 CEST 2006



On Tue, 4 Jul 2006 expat-discuss-request at libexpat.org wrote:

> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 03 Jul 2006 19:19:21 -0400
> From: hymedia <hymedia at videotron.ca>
> Subject: Re: [Expat-discuss] Expat and C++ ?
> To: expat-discuss at libexpat.org
> Message-ID: <0J1U00FS7OSF8P70 at VL-MO-MR004.ip.videotron.ca>
> Content-Type: text/plain; charset=us-ascii
>
> Hi all!
>
> Anyone have an exemple on how to wrap expat2 in a c++ class witout static or
> global functions?

Maybe something along the following lines...

struct Wrapper {
    XML_Parser m_parser;

    void init(
      const XML_Char* encoding,
      const XML_Char* seperator) {
      m_parser = XML_ParserCreate_MM(
        encoding,
        0,          // memsuite
        seperator); // namespace seperator

     if (!m_parser)
	return;

     // Set the user data used in callbacks
     XML_SetUserData(m_parser, (void *) this);
   }
   void fini() {
    if (m_parser)
        XML_ParserFree(m_parser);
    m_parser = 0;
  }
  bool parse(
    const char* buffer,
    int length,
    bool isFinal) {
    assert(m_parser != NULL);

    // get the length if not specified
    if (length < 0)
        length = strlen(buffer);

    // invoke the parser
    return XML_Parse(m_parser,buffer,length,isFinal)!=0;
  }
  ...
};

Hope it helps,
Ali




More information about the Expat-discuss mailing list