/**
 * StringTokenizer.h
 * Author: Daniel Karrels dan@karrels.com
 * Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 *
 * $Id: StringTokenizer.h 2156 2008-06-10 00:46:03Z sirvulcan $
 */

#ifndef __STRINGTOKENIZER_H
#define __STRINGTOKENIZER_H "$Id: StringTokenizer.h 2156 2008-06-10 00:46:03Z sirvulcan $"

#include	<vector>
#include	<string>
#include	<iostream>

using std::string ;
using std::vector ;

/**
 * This class provides a clean mechanism for parsing
 * C++ strings based on a given delimiter.
 * It overloads operator[], and does not affect the
 * original string passed as argument to the constructor.
 * The tokens maintained by this class are zero indexed.
 * This class is immutable.
 */
class StringTokenizer
{

private:
	/**
	 * This is the type that will be used to store
	 * the tokens in the StringTokenizer object.
	 */
	typedef vector< string > vectorType ;

public:

	/**
	 * Constructor receives the string to be
	 * tokenized, and the delimiter by which
	 * tokens will be generated.
	 */
	StringTokenizer( const string& = string(), char = ' ' ) ;

	/**
	 * The destructor is a NOOP because no streams have been
	 * opened, and no memory dynamically explicitly allocated.
	 */
	virtual ~StringTokenizer() ;

	/**
	 * This is the type of the variable used for
	 * representing the size (number of tokens) of
	 * the StringTokenizer object.
	 */
	typedef vectorType::size_type size_type ;

	/**
	 * Retrieve a const reference to a given token, zero-indexed.
	 * This method will assert(false) if the requested index is
	 * out of bounds.  This is for debugging, and may be conditionally
	 * compiled at a later time to throw an exception while
	 * in a production environment.
	 */
	const string&		getToken( const size_type& ) const ;

	/**
	 * This method allows StringTokenizer objects to be used like
	 * arrays.  This method just calls the getToken() method, and
	 * its semantics are the same.
	 */
	inline const string&	operator[]( const size_type& sub ) const
		{ return getToken( sub ) ; }

	/**
	 * Return a const reference to the original C++ string before
	 * tokenization.
	 * A copy of the original string is *not* kept because, in
	 * the setting in which this class was designed, getOriginal()
	 * is never called.  This will free up some processing time
	 * spent copying the original string.
	 */
	inline const string	getOriginal() const
		{ return assemble() ; }

	/**
	 * Return the number of tokens in this StringTokenizer object.
	 */
	inline size_type	size() const
		{ return array.size() ; }

	/**
	 * Return true if the StringTokenizer holds no tokens,
	 * false otherwise.
	 * This is equivalent to (size() == 0).
	 */
	inline bool		empty() const
		{ return array.empty() ; }

	/**
	 * Determine if the subscript argument is within the
	 * bounds [0,size()).
	 * Return true if so, false otherwise.
	 * This method works also even if the StringTokenizer is empty.
	 */
	inline bool		validSubscript( const size_type& sub ) const
		{ return (sub < size()) ; }

	/**
	 * This method builds and returns a C++ string starting at the given
	 * index, and continuing until the last token, placing the
	 * appropriate delimiter between each token.
	 * With no argument supplied, assemble() will return the entire
	 * original string, delimiters included.
	 */
	string			assemble( const size_type& = 0 ) const ;

	/**
	 * The immutable iterator type to use for walking through
	 * this object's tokens.
	 */
	typedef vectorType::const_iterator const_iterator ;

	/**
	 * Retrieve an immutable iterator to the beginning of this
	 * object's token structure.
	 */
	inline const_iterator	begin() const
		{ return array.begin() ; }

	/**
	 * Retrieve an immutable iterator to the end of this object's
	 * token structure.
	 */
	inline const_iterator	end() const
		{ return array.end() ; }

	/**
	 * Return the total number of characters for all tokens,
	 * including the delimiters.
	 */
	inline size_type	totalChars() const ;

	/**
	 * Convenience method for debugging purposes.
	 */
	friend std::ostream& operator<<( std::ostream& out,
		const StringTokenizer& rhs )
		{
		for( size_type i = 0, end = rhs.size() ; i < end ; ++i )
			{
			out << rhs.array[ i ] ;
			if( i < (rhs.size() - 1) )
				{
				out << rhs.delimiter ;
				}
			}
		return out ;
		}

protected:

	/**
	 * Disable the copy constructor.  This method is declared
	 * but NOT defined.
	 */
	StringTokenizer( const StringTokenizer& ) ;

	/**
	 * Protected method called internally by the constructor once
	 * at object instantiation to tokenize the given C++ string.
	 */
	virtual void		Tokenize( const string& ) ;

	/**
	 * The delimiter by which the (original) string is tokenized.
	 */
	const char		delimiter ;

	/**
	 * The structure for holding the tokens.
	 */
	vectorType		array ;

} ;

#endif /* __STRINGTOKENIZER_H */
