Try this code and you can avoid using Boost.Tokenizer and Boost.Spirit libs
#include <vector>
#include <string>
#include <iostream>
const char Separators[] = { ' ', 9 };
bool Str_IsSeparator( const char Ch )
{
for ( size_t i = 0; i != sizeof( Separators ); i++ )
{
if ( Separators[i] == Ch ) { return true; }
}
return false;
}
void SplitLine( size_t FromToken, size_t ToToken, const std::string& Str, std::vector<std::string>& Components )
{
size_t TokenNum = 0;
size_t Offset = FromToken - 1;
const char* CStr = Str.c_str();
const char* CStrj = Str.c_str();
while ( *CStr )
{
while ( *CStr && Str_IsSeparator( *CStr ) ) { CStr++; }
if ( !*CStr ) { return; }
bool InsideQuotes = ( *CStr == '\"' );
if ( InsideQuotes )
{
for ( CStrj = ++CStr; *CStrj && *CStrj != '\"'; CStrj++ );
}
else
{
for ( CStrj = CStr; *CStrj && !Str_IsSeparator( *CStrj ); CStrj++ );
}
if ( CStr != CStrj )
{
TokenNum++;
if ( TokenNum >= FromToken )
{
Components[ TokenNum-Offset ].assign( CStr, CStrj );
if ( TokenNum >= ToToken ) { return; }
}
CStr = CStrj;
if ( *CStr ) { CStr++; }
}
}
}
int main()
{
std::string test = "1st 2nd \"3rd with some comment\" 4th";
std::vector<std::string> Out;
Out.resize(5);
SplitLine(1, 4, test, Out);
for(size_t j = 0 ; j != Out.size() ; j++) { std::cout << Out[j] << std::endl; }
return 0;
}
It uses a pre-allocated array of strings (it is not based on zero, but is easily fixed), and it is quite simple.
source
share