www.pudn.com > uoth_src.zip > RuneBook.cpp


//----------------------------------------------------------------------------- 
//  
// @doc 
// 
// @module	RuneBook.cpp - RuneBook information class | 
// 
// This module contains the RuneBook information class. 
// 
// Copyright (c) 2002 - Descartes Systems Sciences, Inc. 
// 
// All rights reserved. 
// 
// Redistribution and use in source and binary forms, with or without  
// modification, are permitted provided that the following conditions are  
// met: 
//  
// 1. Redistributions of source code must retain the above copyright notice,  
//    this list of conditions and the following disclaimer.  
// 2. Neither the name of Descartes Systems Sciences, Inc nor the names of  
//    its contributors may be used to endorse or promote products derived  
//    from this software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR  
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING  
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS  
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
// 
// @end 
// 
// $History: Cnf.cpp $ 
//       
//----------------------------------------------------------------------------- 
 
#include "stdafx.h" 
#include "RuneBook.h" 
#include "DataParser.h" 
 
// 
// Debug NEW 
// 
 
#if defined (_DEBUG) 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  constructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CRuneBook::CRuneBook () 
{ 
	Init (true); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  destructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CRuneBook::~CRuneBook () 
{ 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Initialize the object 
// 
// @parm bool | fConstruct | If true, we are being called from the 
//		the constructor 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBook::Init (bool fConstruct) 
{ 
	m_strName .Empty (); 
	m_nRunes = 0; 
	memset (&m_anRunes, 0, sizeof (m_anRunes)); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Begin the processing of a RuneBook 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char ** | papszAttrs | Attributes 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBook::OnStart (CDataParser *pParser, const XML_Char **papszAttrs) 
{ 
 
	// 
	// No attributes are allowed 
	// 
 
	if (papszAttrs [0] != NULL) 
	{ 
		pParser ->SetError (IDS_ERR_XML_NO_ATTRS); 
		return; 
	} 
 
	// 
	// Initialize the RuneBook 
	// 
 
	Init (false); 
 
	// 
	// Move to the new state 
	// 
 
	pParser ->SetState (CDataParser::InRuneBook); 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Begin the processing of an XML element 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char * | pszName | Name of the element 
// 
// @parm const XML_Char ** | papszAttrs | Attributes 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBook::OnStartElement (CDataParser *pParser,  
	const XML_Char *pszName, const XML_Char **papszAttrs) 
{ 
	// 
	// Get the element 
	// 
 
	_Element nElement = GetElement (pszName); 
	if (nElement == Unknown) 
	{ 
		pParser ->SetError (IDS_ERR_XML_UNEXPECTED_ELEMENT); 
		return; 
	} 
 
	// 
	// No attributes are allowed 
	// 
 
	if (papszAttrs [0] != NULL) 
	{ 
		pParser ->SetError (IDS_ERR_XML_NO_ATTRS); 
		return; 
	} 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc End an XML element 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char * | pszName | Name of the element 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBook::OnEndElement (CDataParser *pParser, const XML_Char *pszName) 
{ 
 
	// 
	// Get the element 
	// 
 
	_Element nElement = GetElement (pszName); 
	if (nElement == Unknown) 
	{ 
		pParser ->SetError (IDS_ERR_XML_UNEXPECTED_ELEMENT); 
		return; 
	} 
 
	// 
	// Switch based on the element 
	// 
 
	switch (nElement) 
	{ 
		 
		// 
		// If we are ending the RuneBook 
		// 
 
		case RuneBook: 
			pParser ->SetState (CDataParser::InRuneLibrary); 
			pParser ->AddRuneBookToLibrary (); 
			break; 
 
		// 
		// If this is the name 
		// 
 
		case Name: 
			m_strName = pParser ->GetText (false); 
			break; 
 
		// 
		// If this rune list 
		// 
 
		case Treasures: 
			XML_Char *psz = pParser ->GetText (true); 
			while (psz && *psz) 
			{ 
				XML_Char *pszComma = strchr (psz, ','); 
				if (pszComma) 
					*pszComma++ = 0; 
				int nRune = atol (psz); 
				if (m_nRunes < Max_Runes) 
					m_anRunes [m_nRunes++] = nRune; 
				else 
				{ 
					pParser ->SetError (IDS_ERR_XML_TOO_MANY_RUNES); 
					return; 
				} 
				psz = pszComma; 
			} 
			break; 
	} 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Get the element number 
// 
// @parm const XML_Char * | pszName | Element name 
// 
// @rdesc Element id. 
// 
//----------------------------------------------------------------------------- 
 
CRuneBook::_Element CRuneBook::GetElement (const XML_Char *pszName) 
{ 
	if (stricmp (pszName, "RuneBook") == 0) 
		return RuneBook; 
	else if (stricmp (pszName, "Name") == 0) 
		return Name; 
	else if (stricmp (pszName, "Treasures") == 0) 
		return Treasures; 
	else 
		return Unknown; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Write to a stream 
// 
// @parm FILE * | fp | Output file 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBook::Write (FILE *fp) 
{ 
	fprintf (fp, "\t\t\n"); 
	fprintf (fp, "\t\t\t%s\n", (LPCTSTR) m_strName); 
	fprintf (fp, "\t\t\t"); 
	for (int i = 0; i < m_nRunes; ++i) 
	{ 
		if (i != 0) 
			fputc (',', fp); 
		fprintf (fp, "%d", m_anRunes [i]); 
	} 
	fprintf (fp, "\n"); 
	fprintf (fp, "\t\t\n"); 
	return; 
}