Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / C++

Tokenizer and analyzer package supporting precedence prioritized rules

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
1 Jan 20023 min read 181.3K   2.8K   54  
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
// ctkSerializable.h: interface for the ctkSerializable class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CTKSERIALIZABLE_H__D93BEE27_F124_4065_992B_F5B7A39D4D24__INCLUDED_)
#define AFX_CTKSERIALIZABLE_H__D93BEE27_F124_4065_992B_F5B7A39D4D24__INCLUDED_

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

class ctkArchiveException
{
public:
	ctkArchiveException(int nErrorType_, unsigned long data1_, unsigned long data2_)
		{
		nErrorType	=nErrorType_;
		data1		=data1_;
		data2		=data2_;
		}

public:
	int				nErrorType;
	unsigned long	data1;
	unsigned long	data2;

public:
	enum
		{
		readerror_win,
		writeerror_win,
		readerror_size,
		writeerror_size
		};
};

class ctkArchive;

class ctkSerializable
{
public:
	// Due to a problem with the compiler from VC 6.0, 
	// the function must not be pure (otherwise it won't
	// compile the class ctkArchive)
	virtual void	vSerialize(ctkArchive&)
		{ ASSERT(FALSE); };
};

class ctkArchive
{
public:
	virtual bool	fIsStoring() const = 0;
	virtual	bool	fIsReading() const { return !fIsStoring(); };

	template<class T>
	void			vWrite(const T& ref)
		{
		vWriteBinary((const void*)&ref, sizeof(T));
		}

	template<class T>
	void			vWriteNum(const T* ptr, int nCnt)
		{
		vWriteBinary((const void*)ptr, sizeof(T) * nCnt);
		}

	template<class T>
	void			vRead(T& ref)
		{
		vReadBinary((void *)&ref, sizeof(T));
		}

	template<class T>
	void			vReadNum(T* ptr, int nCnt)
		{
		vReadBinary((void *)ptr, sizeof(T) * nCnt);
		}

	template<ctkSerializable>
	void			vWrite(ctkSerializable& ref)
		{
		ref.vSerialize(*this);
		}

	template<ctkSerializable>
	void			vRead(ctkSerializable& ref)
		{
		ref.vSerialize(*this);
		}

	virtual void	vWriteBinary(const void *ptr, unsigned long size) = 0;
	virtual	void	vReadBinary(void *ptr, unsigned long size) = 0;
};

class ctkWinFileArchive : public ctkArchive
{
public:
	ctkWinFileArchive(bool fOutput, HANDLE hFile)
		{
		m_fOutput = fOutput;
		m_hFile = hFile;
		}

public:
	bool	m_fOutput;
	HANDLE	m_hFile;

public:
	virtual void	vWriteBinary(const void *ptr, unsigned long size)
		{
		ASSERT(m_fOutput);
		DWORD dwWritten = 0;
		if(!::WriteFile(m_hFile,ptr,size,&dwWritten,NULL))
			{
			throw ctkArchiveException(
				ctkArchiveException::writeerror_win,
				::GetLastError(),0);
			}

		if(dwWritten!=size)
			throw ctkArchiveException(
					ctkArchiveException::writeerror_size,
					dwWritten,size);
		}

	virtual void	vReadBinary(void *ptr, unsigned long size)
		{
		ASSERT(!m_fOutput);
		DWORD dwRead = 0;
		if(!::ReadFile(m_hFile,ptr,size,&dwRead,NULL))
			{
			throw ctkArchiveException(
				ctkArchiveException::readerror_win,
				::GetLastError(),0);
			}

		if(dwRead != size)
			throw ctkArchiveException(
					ctkArchiveException::readerror_size,
					dwRead,size);
		}

};

#endif // !defined(AFX_CTKSERIALIZABLE_H__D93BEE27_F124_4065_992B_F5B7A39D4D24__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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions