Advanced Strategic Command
stringtokenizer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  stringtokenizer.cpp - description
3  -------------------
4  begin : Sun Jan 28 2001
5  copyright : (C) 2001 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include <cctype>
19 #include <stdio.h>
20 #include "stringtokenizer.h"
21 #include "errors.h"
22 
23 
24 StringTokenizer :: StringTokenizer ( const ASCString& _str, bool includeOperators_ )
25  : str( _str ), i ( 0 )
26 {
27  includeOperators = includeOperators_ ;
28  // if ( includeOperators_ ) {
29  delimitter = "=*/+->";
30 // }
31 }
32 
33 StringTokenizer :: StringTokenizer ( const ASCString& _str, const ASCString& delimitter_ )
34  : str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
35 {
36 }
37 
38 StringTokenizer :: StringTokenizer ( const ASCString& _str, const char* delimitter_ )
39  : str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
40 {
41 }
42 
43 
44 
45 int StringTokenizer::CharSpace ( char c )
46 {
47  if ( c <= ' ' )
48  return 0;
49 
50  const char* d = delimitter.c_str();
51  do {
52  if( *d == c && !includeOperators )
53  return 2;
54  if ( *d == 0 )
55  return 1;
56  d++;
57  } while ( true );
58 }
59 
60 void StringTokenizer::skipTill(char endchar )
61 {
62  const char* s = str.c_str();
63  while ( i < str.length() && s[i] != endchar )
64  ++i;
65 }
66 
68 {
69  while ( i < str.length() && !CharSpace(str[i]) )
70  i++;
71 
72  if ( i == str.length() )
73  return "";
74 
75  int begin = i;
76  int cs = CharSpace( str[i] );
77  do {
78  i++;
79  } while ( i < str.length() && CharSpace( str[i] ) == cs );
80  return str.substr(begin, i-begin);
81 }
82 
83 
85 {
86  return str.substr(i);
87 }
88 
89 
90 
91 StringSplit :: StringSplit ( const ASCString& _str, const ASCString& delimitter_ )
92  : str( _str ), i ( 0 ), delimitter(delimitter_)
93 {
94 }
95 
96 
97 
98 bool StringSplit::isDelimitter ( char c )
99 {
100  const char* d = delimitter.c_str();
101  do {
102  if( *d == c )
103  return true;
104  if ( *d == 0 )
105  return false;
106  d++;
107  } while ( true );
108 }
109 
110 
112 {
113  const char* sp = str.c_str();
114  while ( i < str.length() && isDelimitter(sp[i]) )
115  i++;
116 
117  if ( i == str.length() )
118  return "";
119 
120  int begin = i;
121  while ( i < str.length() && !isDelimitter(sp[i]) )
122  i++;
123 
124  return str.substr(begin, i-begin);
125 }
126 
The (base-) classes which are thrown as exceptions.
StringTokenizer(const ASCString &_str, bool includeOperators_=false)
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
StringSplit(const ASCString &_str, const ASCString &delimitter_)
A simple string tokenizer.
ASCString getNextToken()
ASCString getNextToken()
ASCString getRemaining()
void skipTill(char endchar)