1 |
|
#include "utils/StringUtils.hpp" |
2 |
|
|
3 |
|
using namespace std; |
4 |
– |
using namespace oopse; |
4 |
|
|
5 |
+ |
namespace oopse { |
6 |
|
string UpperCase(const string& S) { |
7 |
|
string uc = S; |
8 |
|
unsigned int n = uc.size(); |
23 |
|
return lc; |
24 |
|
} |
25 |
|
|
26 |
< |
int findBegin(istream theStream, char* startText ){ |
26 |
> |
char* trimSpaces(char *str) { |
27 |
> |
size_t len; |
28 |
> |
char *right, *left; |
29 |
> |
|
30 |
> |
if (strlen(str) == 0) return(str); |
31 |
> |
|
32 |
> |
/* Trim whitespace from left side */ |
33 |
> |
for (left = str; isspace(*left); left++); |
34 |
> |
|
35 |
> |
/* Trim whitespace from right side */ |
36 |
> |
if ((len = strlen(left))) |
37 |
> |
{ |
38 |
> |
right = left + (len - 1); |
39 |
> |
|
40 |
> |
while (isspace(*right)) |
41 |
> |
{ |
42 |
> |
*right = '\0'; |
43 |
> |
right--; |
44 |
> |
} |
45 |
> |
} |
46 |
> |
|
47 |
> |
/* Only do the str copy if there were spaces to the left */ |
48 |
> |
if (left != str) |
49 |
> |
strcpy(str, left); |
50 |
> |
|
51 |
> |
return (str); |
52 |
> |
} |
53 |
> |
|
54 |
> |
int findBegin(istream &theStream, char* startText ){ |
55 |
|
const int MAXLEN = 1024; |
56 |
|
char readLine[MAXLEN]; |
57 |
|
int foundText = 0; |
78 |
|
"stream ended unexpectedly.\n", lineNum); |
79 |
|
return -1; |
80 |
|
} |
81 |
< |
|
81 |
> |
|
82 |
|
the_token = strtok( readLine, " ,;\t" ); |
83 |
< |
|
84 |
< |
if (!strcasecmp("begin", the_token)) { |
85 |
< |
the_token = TrimSpaces(strtok( NULL, " ,;\t" )); |
86 |
< |
foundText = !strcasecmp( startText, the_token ); |
87 |
< |
} |
88 |
< |
|
83 |
> |
if ( the_token != NULL) |
84 |
> |
if (!strcasecmp("begin", the_token)) { |
85 |
> |
the_token = strtok( NULL, " ,;\t" ); |
86 |
> |
if ( the_token != NULL){ |
87 |
> |
foundText = !strcasecmp( startText, the_token ); |
88 |
> |
} |
89 |
> |
} |
90 |
> |
|
91 |
|
if (!foundText) { |
92 |
|
if (!theStream.eof()) { |
93 |
|
theStream.getline(readLine, MAXLEN); |
95 |
|
} else { |
96 |
|
printf( "Error fast forwarding stream at line %d: " |
97 |
|
"stream ended unexpectedly.\n", lineNum); |
98 |
< |
return -1; |
98 |
> |
return -1; |
99 |
|
} |
100 |
|
} |
101 |
|
} |
122 |
|
return(ntokens); |
123 |
|
} |
124 |
|
|
95 |
– |
char* TrimSpaces(char *str) { |
96 |
– |
size_t len; |
97 |
– |
char *right, *left; |
98 |
– |
|
99 |
– |
if (strlen(str) == 0) return(str); |
100 |
– |
|
101 |
– |
/* Trim whitespace from left side */ |
102 |
– |
for (left = str; isspace(*left); left++); |
103 |
– |
|
104 |
– |
/* Trim whitespace from right side */ |
105 |
– |
if ((len = strlen(left))) |
106 |
– |
{ |
107 |
– |
right = left + (len - 1); |
108 |
– |
|
109 |
– |
while (isspace(*right)) |
110 |
– |
{ |
111 |
– |
*right = '\0'; |
112 |
– |
right--; |
113 |
– |
} |
114 |
– |
} |
115 |
– |
|
116 |
– |
/* Only do the str copy if their was spaces to the left */ |
117 |
– |
if (left != str) |
118 |
– |
strcpy(str, left); |
119 |
– |
|
120 |
– |
return (str); |
121 |
– |
} |
122 |
– |
|
125 |
|
int isEndLine(char *line) { |
126 |
|
char *working_line; |
127 |
|
char *foo; |
130 |
|
|
131 |
|
foo = strtok(working_line, " ,;\t"); |
132 |
|
|
133 |
< |
if (!strcasecmp(foo, "end")) return 1; |
133 |
> |
if (foo != NULL) { |
134 |
> |
|
135 |
> |
if (!strcasecmp(foo, "end")) return 1; |
136 |
> |
|
137 |
> |
} |
138 |
|
|
139 |
|
return 0; |
140 |
|
} |
141 |
+ |
} |