Click here to Skip to main content
15,886,137 members
Articles / Desktop Programming / MFC

SolidGraph CAD System

Rate me:
Please Sign up or sign in to vote.
4.97/5 (78 votes)
12 Sep 20062 min read 375.3K   29.8K   209  
A SolidGraph CAD system source code.
/* ==========================================================================
	Class :			CTokenizer

	Date :			06/18/04

	Purpose :		"CTokenizer" is a very simple class to tokenize a string 
					into a string array.	

	Description :	The string is chopped up and put into an internal 
					"CStringArray". With "GetAt", different types of data can 
					be read from the different elements.

	Usage :			Create an instance of "CTokenizer" on the stack. Set 
					the string to parse in the "ctor", and the delimiter if 
					not a comma. The "ctor" automatically tokenize the input
					string.

					Call "GetSize" to get the number of 
					tokens, and "GetAt" to get a specific token.
	
					You can also reuse the tokenizer by calling "Init".
   ========================================================================
	Changes :		28/8  2004	Changed a char to TCHAR to allow UNICODE 
								building (Enrico Detoma)
   ========================================================================*/

#include "stdafx.h"
#include "Tokenizer.h"


////////////////////////////////////////////////////////////////////
// Public functions
//
CTokenizer::CTokenizer( CString strInput, const CString & strDelimiter )
/* ============================================================
	Function :		CTokenizer::CTokenizer
	Description :	Constructor.
	Access :		Public
					
	Return :		void
	Parameters :	CString strInput				-	String to 
														tokenize
					const CString & strDelimiter	-	Delimiter, 
														defaults to
														comma.
	Usage :			Should normally be created on the stack.

   ============================================================*/
{

	Init(strInput, strDelimiter);

}

void CTokenizer::Init( const CString & strInput, const CString & strDelimiter )
/* ============================================================
	Function :		CTokenizer::Init
	Description :	Reinitializes and tokenizes the tokenizer 
					with "strInput". "strDelimiter" is the 
					delimiter to use.
	Access :		Public
					
	Return :		void
	Parameters :	const CString & strInput		-	New string
					const CString & strDelimiter	-	Delimiter,
														defaults to 
														comma

	Usage :			Call to reinitialize the tokenizer.

   ============================================================*/
{

	CString copy( strInput );
	m_stra.RemoveAll();
	int nFound = copy.Find( strDelimiter );

	while(nFound != -1)
	{
		CString strLeft;
		strLeft = copy.Left( nFound );
		copy = copy.Right( copy.GetLength() - ( nFound + 1 ) );

		m_stra.Add( strLeft );
		nFound = copy.Find( strDelimiter );
	}

	m_stra.Add( copy );

}

int CTokenizer::GetSize(  ) const
/* ============================================================
	Function :		CTokenizer::GetSize
	Description :	Gets the number of tokens in the tokenizer.
	Access :		Public
					
	Return :		int	-	Number of tokens.
	Parameters :	none

	Usage :			Call to get the number of tokens in the 
					tokenizer.

   ============================================================*/
{

	return m_stra.GetSize();

}

void CTokenizer::GetAt( int nIndex, CString & str ) const
/* ============================================================
	Function :		CTokenizer::GetAt
	Description :	Get the token at "nIndex" and put it in 
					"str".
	Access :		Public
					
	Return :		void
	Parameters :	int nIndex		- Index to get token from
					CString & str	- Result

	Usage :			Call to get the value of the token at 
					"index".

   ============================================================*/
{

		if( nIndex < m_stra.GetSize() )
			str = m_stra.GetAt( nIndex );
		else
			str = _T( "" );

}

void CTokenizer::GetAt( int nIndex, int & var ) const
/* ============================================================
	Function :		CTokenizer::GetAt
	Description :	Get the token at "nIndex" and put it in 
					"var".
	Access :		Public
					
	Return :		void
	Parameters :	int nIndex	- Index to get token from
					int & var	- Result

	Usage :			Call to get the value of the token at 
					"index".

   ============================================================*/
{

		if( nIndex < m_stra.GetSize() )
			var = _ttoi( m_stra.GetAt( nIndex ) );
		else
			var = 0;

}

void CTokenizer::GetAt( int nIndex, WORD & var ) const
/* ============================================================
	Function :		CTokenizer::GetAt
	Description :	Get the token at "nIndex" and put it in 
					"var".
	Access :		Public
					
	Return :		void
	Parameters :	int nIndex	- Index to get token from
					WORD & var	- Result

	Usage :			Call to get the value of the token at 
					"index".

   ============================================================*/
{

		if( nIndex < m_stra.GetSize() )
			var = static_cast< WORD >( _ttoi( m_stra.GetAt( nIndex ) ) );
		else
			var = 0;

}

void CTokenizer::GetAt( int nIndex, double & var ) const
/* ============================================================
	Function :		CTokenizer::GetAt
	Description :	Get the token at "nIndex" and put it in 
					"var".
	Access :		Public
					
	Return :		void
	Parameters :	int nIndex		- Index to get token from
					double & var	- Result

	Usage :			Call to get the value of the token at 
					"index".

   ============================================================*/
{

		TCHAR   *stop;
		if( nIndex < m_stra.GetSize() )
			var = _tcstod( m_stra.GetAt( nIndex ), &stop );
		else
			var = 0.0;

}

void CTokenizer::GetAt( int nIndex, DWORD & var ) const
/* ============================================================
	Function :		CTokenizer::GetAt
	Description :	Get the token at "nIndex" and put it in 
					"var".
	Access :		Public
					
	Return :		void
	Parameters :	int nIndex	- Index to get token from
					DWORD & var	- Result

	Usage :			Call to get the value of the token at 
					"index".

   ============================================================*/
{

		if( nIndex < m_stra.GetSize() )
			var = static_cast< DWORD >( _ttoi( m_stra.GetAt( nIndex ) ) );
		else
			var = 0;

}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions