⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 levelparser.cpp

📁 RGA: Biowaste Game Example This C++ application demonstrates how to create a 2D mobile game for S60
💻 CPP
字号:

#include "LevelParser.h"

CLevelParser::CLevelParser()
	{

	}

CLevelParser::~CLevelParser()
	{
	if (iFileBuffer)
		{
		delete [] iFileBuffer;
		iFileBuffer = NULL;
		}
	}

TInt CLevelParser::Init(const TDesC& aFilename)
	{
	TInt error;

	// connect to file server
	RFs fs;
	error = fs.Connect();
	if (error != KErrNone)
		{
		return error;
		}
	
	// open the file for reading
	RFile file;
	error = file.Open(fs, aFilename, EFileStream | EFileRead);
	if (error != KErrNone)
		{
		fs.Close();
		return error;
		}

	// read the entire file into the buffer
	TInt filesize;
	file.Size(filesize);
	iFileBufferSize = filesize;
	
	if (iFileBufferSize == 0)
		{
		file.Close();
		fs.Close();
		return KErrArgument;
		}
	
	iFileBuffer = new TUint8[iFileBufferSize];
	if (iFileBuffer == NULL)
		{
		file.Close();
		fs.Close();
		return KErrNoMemory;
		}
	
	TPtr8 ptr(iFileBuffer, iFileBufferSize);
	file.Read(ptr);
	
	file.Close();
	fs.Close();
	
	// go thru buffer and replace all comments with whitespaces
	TUint32 i;
	for (i=0; i<iFileBufferSize-1; i++)
		{
		if (iFileBuffer[i] == '/' &&
			iFileBuffer[i+1] == '/')
			{
			// this is a start of a comment, replace rest of the
			// line with whitespaces
			while ( i < iFileBufferSize &&
					iFileBuffer[i] != '\r' &&
					iFileBuffer[i] != '\n' )
				{
				iFileBuffer[i++] = ' ';
				} 
			}
		}
	
	return KErrNone;
	}


TBool CLevelParser::FindNextTag(const TDesC8& aTag)
	{
	TInt pos = iPosition;
	TPtrC8 ptr(iFileBuffer + pos, iFileBufferSize - pos);
	
	TInt startpos = ptr.Find(aTag);
	if (startpos == KErrNotFound)
		{
		return EFalse;
		}
	
	// jump over the tag we just found
	pos += startpos + aTag.Length() + 1;
	
	// find the closing tag
	TPtrC8 ptr2(iFileBuffer + pos, iFileBufferSize - pos);
	TInt endpos = ptr2.Find(aTag);
	if (endpos == KErrNotFound)
		{
		return EFalse;
		}
	endpos += pos;

	iDataPosStart = pos;
	iDataPosEnd = pos + (endpos - pos - 2);
	
	iPosition = endpos + aTag.Length() + 1;	
	iValuePos = iDataPosStart;
	return ETrue;
	}

TInt CLevelParser::GetTagData(TDes8& aBuffer) const
	{
	if (iDataPosStart && iDataPosEnd)
		{
		const TInt length = iDataPosEnd - iDataPosStart;
		if (length > aBuffer.MaxLength())
			{
			return KErrUnderflow;
			}
		
		TPtrC8 ptrdata(iFileBuffer + iDataPosStart, length);
		aBuffer = ptrdata;
		return KErrNone;
		}
	else
		{
		return KErrNotFound;
		}
	}


TBool CLevelParser::GetNextTagInt(TInt& aValue)
	{
	if (iDataPosStart && iDataPosEnd)
		{
//		const TInt length = iDataPosEnd - iDataPosStart;
//		TPtrC8 ptrdata(iFileBuffer + iDataPosStart, length);
		
		// move to beginning of next value
		while (	iFileBuffer[iValuePos] < '0' ||
				iFileBuffer[iValuePos] > '9')
			{
			iValuePos++;

			// check end of data
			if (iValuePos >= iDataPosEnd)
				{
				return EFalse;
				}
			}
		
		TUint32 valueend = iValuePos;
		while (	valueend < iDataPosEnd &&
				iFileBuffer[valueend] >= '0' &&
				iFileBuffer[valueend] <= '9')
			{
			valueend++;
			}
		
		// convert text to number
		TPtrC8 ptrvalue(iFileBuffer + iValuePos, valueend - iValuePos);
		TLex8 lex(ptrvalue);

		iValuePos = valueend + 1;
		
		if (lex.Val(aValue) == KErrNone)
			{
			return ETrue;
			}
		}
	return EFalse;
	}



TUint32 CLevelParser::Position() const
	{
	return iPosition;
	}

void CLevelParser::SetPosition(TUint32 aPosition)
	{
	if (aPosition < iFileBufferSize)
		{
		iPosition = aPosition;
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -