Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tokeniser

Status
Not open for further replies.

FFCH

Programmer
Oct 17, 2006
10
AU
My question is how would i read attributes from the tokeniser in the same order as declaration occurs in the class declaration, separated by full-colons

[cpp]

Entertainment::Entertainment():_rating("NR"),_title(""),_purchaseDate(),_purchaseCost(),_publisher(""){}


int Entertainment::read(Tokeniser& tok)
{

_rating = tok.nextToken();
_title = tok.nextToken();
_purchaseDate = tok.nextToken();
_purchaseCost = tok.nextToken();
_publisher = tok.nextToken();

return 0;


}[/cpp]
 
or

Code:
int Entertainment::read(Tokeniser& tok)
{
	string in_r(_rating);
	Tokeniser  t1(in_r, ":");
	_rating = tok.nextToken();
	string in_t(_title);
	Tokeniser  t2(in_t, ":");
	_title = tok.nextToken();
	...etc
	
	
	
		
	return 0;
	

}
 
Did you write the Tokeniser class, or download it? What does it look like...?
 
This is what it looks like i did not do it it was given to me
Code:
#include "Tokeniser.h"


const int Tokeniser::THROW_EXCEPTION = 0;


Tokeniser::Tokeniser(void): _delim(""), _data(""), _currentPos(0), _throwException(true)
{
}


Tokeniser::Tokeniser(string const& str, string const& delim = ""):
			_delim(delim), _data(str), _currentPos(0), _throwException(true)
{
}


bool Tokeniser::isValidPos(string::size_type pos)
{
	if(pos >= _data.length())
		return false;
	return true;
}


void Tokeniser::setString(string const& str)
{
	_currentPos = 0;
	_data = str;
}


string const& Tokeniser::getString(void)
{
	return _data;
}


void Tokeniser::setDelim(string const& delim)
{
	_delim = delim;
}


string const& Tokeniser::getDelim(void)
{
	return _delim;
}


bool Tokeniser::hasMoreTokens(void)
{
	if(_currentPos < _data.length())
		return true;
	return false;
}


bool Tokeniser::getOption(int option)
{
	if(option == THROW_EXCEPTION) {
		return _throwException;
	}

	return false;
}


void Tokeniser::setOption(int option, bool value)
{
	if(option == THROW_EXCEPTION) {
		_throwException = value;
		return;
	}
}


string Tokeniser::nextToken(void) throw(NoSuchElementException)
{
	return nextToken(_delim);
}


string Tokeniser::nextToken(string const& delim) throw(NoSuchElementException)
{
	string::size_type end;
	string tmp;

	if(!isValidPos(_currentPos))
		if(_throwException)
			throw NoSuchElementException("No such element");
		else
			return "";

	end = _data.find(_delim, _currentPos);

	tmp = _data.substr(_currentPos, end-_currentPos);

	if(end == string::npos)
		_currentPos = _data.length();
	else
		_currentPos = end + _delim.length();

	return tmp;
}
 
Well, assuming the Tokeniser that you pass to your read() function has its delimitor properly set before you call read(), the first way should work fine; otherwise you need to set the delimitor first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top