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;
}

Dani AI

Generated

A few practical clarifications that build on 's attempts and 's reply.

If the piece you need to split (barcode and speed) is a single colon-separated substring, parse that substring once and fill fields in declaration order. A common mistake is creating temporary Tokeniser objects and then still calling the original tokenizer, or not checking whether the tokenizer is positioned on the correct substring. Another point: the nextToken(delim) overload in the posted Tokeniser ignores its delim argument; either make it respect the passed delimiter or remove the unused parameter to avoid confusion.

A simple, robust alternative is to use a stringstream to split a colon-delimited substring (assume you already have that substring in attrs):

#include <sstream>

std::string attrs = /* the colon-separated substring, e.g. "12345:200" */;
std::istringstream iss(attrs);
std::string barcode, speed;
std::getline(iss, barcode, ':');
std::getline(iss, speed, ':');

_barcode = barcode;
_speed   = speed;

Robustness tips: always check that you actually received enough tokens (use hasMoreTokens() on the outer tokenizer or test getline return values). Consider disabling the Tokeniser exception temporarily (setOption(Tokeniser::THROW_EXCEPTION, false)) if you prefer empty strings over exceptions. Trim whitespace on each field and validate conversions (use std::stoi inside a try/catch when converting speed to numeric). If you keep using your Tokeniser class, fix the nextToken(delim) implementation to use the passed delim, and prefer substr(_currentPos) when find returns npos for clearer code.

In short: extract the colon-separated chunk once, parse it with a small, focused parser (stringstream or a correctly-behaving Tokeniser), validate fields, and avoid creating unused temporary tokenisers as seen in the second post.

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 Member #46692

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.