Click here to Skip to main content
12,699,214 members (24,405 online)
Click here to Skip to main content

Stats

22K views
150 downloads
20 bookmarked
Posted

How to build an in-memory state engine at runtime

, 28 Oct 2007 CPOL
A C++ template for an efficient in-memory state engine.
cp
Debug
TagTree.exe
sources
algorithm
container
general
mpl
tag_tree.pdf
//	Copyright � 2007, solosTec
//  All rights reserved.
//---------------------------------------------------------------------
//	License for redistribution is the GNU General Public License v2.
//	See the included readme.txt for details.
//---------------------------------------------------------------------
//	developed by solosTec
//	http://www.solostec.de
//---------------------------------------------------------------------
//
//	$Author: $
//	$Revision: $
/*	$Log: $
*/
//---------------------------------------------------------------------

#include "main.h"


int main(int argc, char* argv[])
{
	Source	source_;
	Scanner	scanner_;
	scanner_.run( source_ );

	return 0;
}

Source::Source()
	: content_ ( "This string contains some keywords that we have to find fast and in a reliable manner. The keywords are: keyword, string and and." )
	, pos_( const_cast<char*> (content_ ))
{}

char Source::next()
{
	const char c ( *pos_ );
	if (c == '\0')
	{
		return EOF;	//	-1
	}
	pos_++;
//	std::cout << "next: " << c << std::endl;
	return c;
}

size_t Source::position() const
{
	return pos_ - content_;
}

Scanner::Scanner()
{
	tagTree_.insertKeyWord( "keyword", KEY_KEYWORD );
	tagTree_.insertKeyWord( "string", KEY_STRING );
	tagTree_.insertKeyWord( "and", KEY_AND );
}

void Scanner::run( Source& source )
{
	for (;;)
	{
		switch( consume( source ))
		{
		case Scanner::KEY_KEYWORD:
			std::cout 
				<< "KEYWORD at " 
				<< source.position() 
				<< std::endl;
			break;
		case Scanner::KEY_STRING:
			std::cout 
				<< "STRING at " 
				<< source.position() 
				<< std::endl;
			break;
		case Scanner::KEY_AND:
			std::cout 
				<< "AND at " 
				<< source.position() 
				<< std::endl;
			break;
		case Scanner::KEY_EOS:
			std::cout 
				<< "End of String at " 
				<< source.position() 
				<< std::endl;
			return;
		default:
			break;
		}
	}

}

Scanner::KEY Scanner::consume( Source& source )
{
	TagTreePtr_ prev = &tagTree_;
	for (;;)
	{
		const char c( source.next());
		if (c == EOF)
		{
			return KEY_EOS;
		}
		TagTreePtr_ p = prev->findNode( c );
		if (p == NULL)
		{
			break;
		}
		prev = p;
	}
	return (prev != NULL)
		? prev->getValue()
		: KEY_UNKNOWN;
}

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)

Share

About the Author

osy
Software Developer (Senior)
Switzerland Switzerland
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 28 Oct 2007
Article Copyright 2007 by osy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid