This is just a brief question how would i read the attributes from the tokeniser in the same order as declaration occurs in the class declaration, separated by full-colons.

Tokeniser class

#include <iostream>
using namespace std;
#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;
}

Class where i need to do the stuff

Media::Media():_barcode(""),_speed(""){}
 
int Media::read(Tokeniser& tok)
{

_barcode = tok.nextToken();
_speed = tok.nextToken();

return 0;
}

Recommended Answers

All 2 Replies

Like this ??

int Media::read(Tokeniser& tok)
{

string delim = ":";
tok.setDelim(delim);
string in_barcode(_barcode);
Tokeniser t1(in_barcode, ":");
_barcode = tok.nextToken();
string in_speed(_speed);
Tokeniser t2(in_speed, ":");
_speed = tok.nextToken();

return 0;
}
Member Avatar for iamthwee

Whatever works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.