Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / MFC

A simple XML Parser

Rate me:
Please Sign up or sign in to vote.
4.69/5 (13 votes)
28 Jan 2002CPOL 362.8K   5.8K   70  
A class to read and write non validated XML files
// CMabString.h : header file
//
// CMabString Header
//
// Portions Written by Michael A. Barnhart <mabtech@swbell.net>
// Copyright (c) MAB Tech 1995,1996,2000,2001
//
// Portions Written by Zafir Anjum
// Copyright (c) 1998
// http://www.codeguru.com
//
// Portions Written by Paul E. Bible <pbible@littlefishsoftware.com>
// Copyright (c) 2000. All Rights Reserved.
// http://www.codeproject.com
//
// This class is a merging of the code from the above authors.
// My original code had numeric conversion in c functions, 
// which handled a controled field length and switch formats for maximum 
// accuracy.
// Zafir's code added enhancements to the CString class which enhanced
// the parsing of numeric fields in a single line. 
// Paul's coding style was very inline with Zafir's.
// This allowed a very easy merging of the two classes and thus the 
// dependancy on CString to be removed. 
// This eliminated problems with the migration of the CString class out
// of the MFC class in MFC version 7.
// Mixed into all of this was some evolution of functions as needed.
//
// My thanks to both of the above for sharing there work. Mike
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// HISTORY
// 12 Feb 2001 - Initial code merging.
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(Mab_STRING_H__12Feb2001__INCLUDED_)
#define Mab_STRING_H__12Feb2001__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// To support BSTR's we shall use the ATL functions./
// We can remove this dependancy if we remove BSTR support.
#include <atlbase.h>

// &&&&&&&& The following may be changed to user preference
#define DefStrLen  8	// Default string length to use for number conversion
#define DefExpChr 69	// Default Exponential character in scientific format

class CMabString  
{
public: 
	CMabString();
	CMabString(CMabString& szString);
	CMabString(LPCTSTR pszString);
	CMabString(LPCTSTR pszString,int leng);
	CMabString(BSTR bstrString);
	CMabString(TCHAR ch, int nRepeat);
	virtual ~CMabString();
	
	// Assignment Operations
	const CMabString& operator=(CMabString& strSrc);
	const CMabString& operator=(LPCTSTR lpsz);
	const CMabString& operator=(BSTR bstrStr);
	operator LPCTSTR() const	{ return m_pszString; }
	TCHAR*	GetString()			{ return m_pszString; }
	BSTR	AllocSysString();
	LPTSTR GetBuffer(int minlen);
	void ReleaseBuffer(int minlen=0);
	void operator=(double val);
	void operator=(float val);
	void operator=(int val);
	void operator=(long val);
	void PutLong(long i, int len=DefStrLen);
	void PutInt(int i, int len=DefStrLen);
	void PutFloat(float value, int len=DefStrLen, char ExpChar=DefExpChr);
	void PutDouble(double value, int len=DefStrLen, char ExpChar=DefExpChr);
	void PutFloatS(float value, int nsignificands=DefStrLen, char ExpChar=DefExpChr);
	void PutDoubleS(double value, int nsignificands=DefStrLen, char ExpChar=DefExpChr);

	CMabString& Insert(int pos, LPCTSTR s);
	CMabString& Insert(int pos, TCHAR c);

	CMabString& Delete(int pos, int len);
	CMabString& Replace(int pos, int len, LPCTSTR s);

	// Concatenation
	const CMabString& operator+=(CMabString& strSrc);
	const CMabString& operator+=(LPCTSTR lpsz);
	const CMabString& operator+=(BSTR bstrStr);
	const CMabString& operator+=(TCHAR ch);
	friend CMabString operator+(CMabString& strSrc1, CMabString& strSrc2);
	friend CMabString operator+(CMabString& strSrc, LPCTSTR lpsz);
	friend CMabString operator+(LPCTSTR lpsz, CMabString& strSrc);
	friend CMabString operator+(CMabString& strSrc, BSTR bstrStr);
	friend CMabString operator+(BSTR bstrStr, CMabString& strSrc);

	// Accessors for the String as an Array
	int		GetLength() const;
	bool	IsEmpty() const;
	void	Empty();
	TCHAR	GetAt(int nIndex);
	void	SetAt(int nIndex, TCHAR ch);
	TCHAR	operator[] (int nIndex);

	// Conversions
	void	MakeUpper();
	void	MakeLower();
	void	MakeReverse();
	void	TrimLeft();
	void	TrimRight();

	// Searching
	int	Find(TCHAR ch) const;
	int	Find(TCHAR ch, int nStart) const;
	int	Find(LPCTSTR lpszSub);
	int	Find(LPCTSTR lpszSub, int nStart);
	int	FindOneOf(LPCTSTR lpszCharSet) const;
	int	ReverseFind(TCHAR ch) const;
	int Find( LPCTSTR lpszSub, int startpos = 0 ) const;
	int FindNoCase( TCHAR ch, int startpos = 0 ) const;
	int FindNoCase( LPCTSTR lpszSub, int startpos = 0 ) const;
	int FindReplace( LPCTSTR lpszSub, LPCTSTR lpszReplaceWith, BOOL bGlobal = TRUE );
	int FindReplaceNoCase( LPCTSTR lpszSub, LPCTSTR lpszReplaceWith,
				BOOL bGlobal = TRUE );
	int ReverseFind( LPCTSTR lpszSub, int startpos = -1 ) const;
	int ReverseFindNoCase( TCHAR ch, int startpos = -1  ) const;
	int ReverseFindNoCase( LPCTSTR lpszSub, int startpos = -1 ) const;

	// Extraction
	CMabString Mid(int nFirst) const;
	CMabString Mid(int nFirst, int nCount) const;
	CMabString Left(int nCount) const;
	CMabString Right(int nCount) const;
	CMabString SpanIncluding(LPCTSTR lpszCharSet) const;
	CMabString SpanExcluding(LPCTSTR lpszCharSet) const;
	CMabString GetField( LPCTSTR delim, int fieldnum);
	CMabString GetField( TCHAR delim, int fieldnum);
	int GetFieldCount( LPCTSTR delim );
	int GetFieldCount( TCHAR delim );
	CMabString StepField( TCHAR delim);
	CMabString NextField( TCHAR delim);
	CMabString GetDelimitedField( LPCTSTR delimStart, LPCTSTR delimEnd,
				int fieldnum = 0);
	BOOL IsInt();
	BOOL IsFloat();
	int GetInt();
	long GetLong();
	float GetFloat();
	double GetDouble();

	// Comparison
	int Compare(CMabString& str) const;
	int Compare(LPCTSTR lpsz) const;
	int CompareNoCase(CMabString& str) const;
	int CompareNoCase(LPCTSTR lpsz) const;
	int Collate(CMabString& str) const;
	int Collate(LPCTSTR lpsz) const;
	int CollateNoCase(CMabString& str) const;
	int CollateNoCase(LPCTSTR lpsz) const;

	// Formatting
	void Format(LPCTSTR pszCharSet, ...);
	void Accuracy(int acc,int ExpAscii);
	void Remove(char c);
	void NoDoubleSpace();
	void NoControlChar();
	void Fill(int len, char c=' ');
	void Trim();
	void UsCur();

	// Replacing
	int Replace(TCHAR chOld, TCHAR chNew);
	int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);
	
protected:
	LPTSTR m_pszString;
	LPTSTR m_pszField;
	long m_iStrLen;
	void	StringCopy(CMabString& str, int nLen, int nIndex, int nExtra) const;
	void	StringCopy(int nSrcLen, LPCTSTR lpszSrcData);
	void	ConcatCopy(LPCTSTR lpszData);
	void	ConcatCopy(TCHAR ch);
	void	ConcatCopy(LPCTSTR lpszData1, LPCTSTR lpszData2);
	void	AllocString(int nLen);
	void	ReAllocString(int nLen);

/////////////////////////////////////////////////////////
private:
	static CMabString m_csNAN;
	static CMabString m_csNINF;
	static CMabString m_csPINF;
	//
	void TestExp();
	inline double atodn(char alpha[],int n);
	inline float atofn(char *alpha,int n);
	inline int atoin(char *alpha,int n);
	inline long int atoln(char alpha[],int n);
	inline void dtoan(char *alpha, double val, int len);
	inline void itoan(char alpha[],int n,int l);
	inline BOOL ltoan(char alpha[],long int n,int l);
	// for stepping through the fields
//	LPTSTR m_lpsz;
//	LPTSTR m_lpszRemainder;
//	LPTSTR m_lpszret;

};	


// Compare operations
bool operator==(const CMabString& s1, const CMabString& s2);
bool operator==(const CMabString& s1, LPCTSTR s2);
bool operator==(LPCTSTR s1, const CMabString& s2);
bool operator!=(const CMabString& s1, const CMabString& s2);
bool operator!=(const CMabString& s1, LPCTSTR s2);
bool operator!=(LPCTSTR s1, const CMabString& s2);
bool operator<=(const CMabString& s1, const CMabString& s2);
bool operator>=(const CMabString& s1, const CMabString& s2);
bool operator<(const CMabString& s1, const CMabString& s2);
bool operator>(const CMabString& s1, const CMabString& s2);

// Compare implementations
inline bool operator==(const CMabString& s1, const CMabString& s2)
	{ return s1.Compare(s2) == 0; }
inline bool operator==(const CMabString& s1, LPCTSTR s2)
	{ return s1.Compare(s2) == 0; }
inline bool operator==(LPCTSTR s1, const CMabString& s2)
	{ return s2.Compare(s1) == 0; }
inline bool operator!=(const CMabString& s1, const CMabString& s2)
	{ return s1.Compare(s2) != 0; }
inline bool operator!=(const CMabString& s1, LPCTSTR s2)
	{ return s1.Compare(s2) != 0; }
inline bool operator!=(LPCTSTR s1, const CMabString& s2)
	{ return s2.Compare(s1) != 0; }
inline bool operator<=(const CMabString& s1, const CMabString& s2)
	{
	if(s1.Compare(s2) <= 0)
		return TRUE;
	return FALSE;
	}
inline bool operator>=(const CMabString& s1, const CMabString& s2)
	{
	if(s1.Compare(s2) >= 0)
		return TRUE;
	return FALSE;
	}
inline bool operator<(const CMabString& s1, const CMabString& s2)
	{
	if(s1.Compare(s2) < 0)
		return TRUE;
	return FALSE;
	}
inline bool operator>(const CMabString& s1, const CMabString& s2)
	{
	if(s1.Compare(s2) > 0)
		return TRUE;
	return FALSE;
	}

#endif // !defined(Mab_STRING_H__12Feb2001__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United States United States
Began programming in 1968 on a Wang 720. Move to Fortran and began developing FEM (finite element model) applications on an IBM 360 in 1973. Developed custom FEM editors for most of my career until 1995.

Comments and Discussions