1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/***************************************************************************
stringtokenizer.cpp - description
-------------------
begin : Sun Jan 28 2001
copyright : (C) 2001 by Martin Bickel
email : bickel@asc-hq.org
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <cctype>
#include <stdio.h>
#include "stringtokenizer.h"
#include "errors.h"
StringTokenizer :: StringTokenizer ( const ASCString& _str, bool includeOperators_ )
: str( _str ), i ( 0 )
{
includeOperators = includeOperators_ ;
// if ( includeOperators_ ) {
delimitter = "=*/+->";
// }
}
StringTokenizer :: StringTokenizer ( const ASCString& _str, const ASCString& delimitter_ )
: str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
{
}
StringTokenizer :: StringTokenizer ( const ASCString& _str, const char* delimitter_ )
: str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
{
}
int StringTokenizer::CharSpace ( char c )
{
if ( c <= ' ' )
return 0;
const char* d = delimitter.c_str();
do {
if( *d == c && !includeOperators )
return 2;
if ( *d == 0 )
return 1;
d++;
} while ( true );
}
void StringTokenizer::skipTill(char endchar )
{
const char* s = str.c_str();
while ( i < str.length() && s[i] != endchar )
++i;
}
ASCString StringTokenizer::getNextToken( )
{
while ( i < str.length() && !CharSpace(str[i]) )
i++;
if ( i == str.length() )
return "";
int begin = i;
int cs = CharSpace( str[i] );
do {
i++;
} while ( i < str.length() && CharSpace( str[i] ) == cs );
return str.substr(begin, i-begin);
}
ASCString StringTokenizer::getRemaining( )
{
return str.substr(i);
}
StringSplit :: StringSplit ( const ASCString& _str, const ASCString& delimitter_ )
: str( _str ), i ( 0 ), delimitter(delimitter_)
{
}
bool StringSplit::isDelimitter ( char c )
{
const char* d = delimitter.c_str();
do {
if( *d == c )
return true;
if ( *d == 0 )
return false;
d++;
} while ( true );
}
ASCString StringSplit::getNextToken( )
{
const char* sp = str.c_str();
while ( i < str.length() && isDelimitter(sp[i]) )
i++;
if ( i == str.length() )
return "";
int begin = i;
while ( i < str.length() && !isDelimitter(sp[i]) )
i++;
return str.substr(begin, i-begin);
}
|