We all have written such a class to use Xerces in an easier way. :)

Here is my version. I think it is even easier to use and it has small
footprint and overhead. But it is NOT designed as a stringclass. Look at the
examples for the two types of cases it is designed for.

Usage examples:

  // This handles all neccesary transcoding on the fly
  std::string attr = X( doc.getAttribute( X("attributename") ) );

or

  // This only transcodes the string "myvalue" once (and only if it is
actually needed).
  X value("myvalue");
  DOMNode* node = elem.getFirstChild();
  while ( node != null ) {
    node.setNodeValue(value);
    node = node.getNextSibling();
  }

Regards

Erik Rydgren
Mandarinen systems AB
Sweden

- .H file ---------------------------------------------

//
//
****************************************************************************
*****************
// cXML4CTranscode
//
****************************************************************************
*****************
//
class cXML4CTranscode
{
  mutable XMLCh*        m_pzXMLStr;
  mutable bool          m_bDelXMLStr;
  mutable int           m_nLength;

  mutable char*         m_pzStr;
  mutable bool          m_bDelStr;

  // This is a stack-only class
  void* operator new (size_t size) { ASSERT(false); }

public:

  cXML4CTranscode(const XMLCh* pzStr);
  cXML4CTranscode(const XMLCh* pzStr, const unsigned int length);
  cXML4CTranscode(const char* pzStr);
  cXML4CTranscode(const cXML4CTranscode& master);
  ~cXML4CTranscode();

  operator const char* () const;
  operator const XMLCh*() const;
};

typedef cXML4CTranscode X;

- .CPP file ---------------------------------------------

//
//
****************************************************************************
*****************
// cXML4CTranscode
//
****************************************************************************
*****************
//
cXML4CTranscode::cXML4CTranscode(const XMLCh* pzStr)
{
  m_pzXMLStr = (XMLCh*) pzStr;
  m_bDelXMLStr = false;
  m_nLength = -1;

  m_pzStr = 0;
  m_bDelStr = true;
}

cXML4CTranscode::cXML4CTranscode(const cXML4CTranscode& master)
{
  m_pzXMLStr = master.m_pzXMLStr;
  m_bDelXMLStr = false;
  m_nLength = master.m_nLength;
  m_pzStr = master.m_pzStr;
  m_bDelStr = false;
}

cXML4CTranscode::cXML4CTranscode(const XMLCh* pzStr, const unsigned int
length)
{
  m_pzXMLStr = (XMLCh*) pzStr;
  m_bDelXMLStr = false;
  m_nLength = (int) length;

  m_pzStr = 0;
  m_bDelStr = true;
}

cXML4CTranscode::cXML4CTranscode(const char* pzStr)
{
  m_pzStr = (char*) pzStr;
  m_bDelStr = false;

  m_pzXMLStr = 0;
  m_bDelXMLStr = true;
  m_nLength = -1;
}

cXML4CTranscode::~cXML4CTranscode()
{
  if (m_bDelStr) delete m_pzStr;
  if (m_bDelXMLStr) delete m_pzXMLStr;
}


cXML4CTranscode::operator const char* () const
{
  if (!m_pzStr) {
    if (m_nLength >= 0) {
      XMLCh* tmp = new XMLCh[m_nLength+1];
      memcpy(tmp, m_pzXMLStr, sizeof(XMLCh) * m_nLength);
      tmp[m_nLength] = 0;
      m_pzStr = XMLString::transcode(tmp);
      delete tmp;
    }
    else {
      m_pzStr = XMLString::transcode(m_pzXMLStr);
    }
    m_bDelStr = true;
  }
  return m_pzStr;
}

cXML4CTranscode::operator const XMLCh*() const
{
  if (!m_pzXMLStr) {
    m_pzXMLStr = XMLString::transcode(m_pzStr);
    m_bDelXMLStr = true;
  }
  return m_pzXMLStr;
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to