1 |
|
#include "utils/StringUtils.hpp" |
2 |
|
|
3 |
+ |
using namespace std; |
4 |
+ |
using namespace oopse; |
5 |
+ |
|
6 |
|
string UpperCase(const string& S) { |
7 |
|
string uc = S; |
8 |
|
unsigned int n = uc.size(); |
22 |
|
} |
23 |
|
return lc; |
24 |
|
} |
25 |
+ |
|
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; |
58 |
+ |
int lineNum; |
59 |
+ |
char* the_token; |
60 |
+ |
char* eof_test; |
61 |
+ |
|
62 |
+ |
// rewind the stream |
63 |
+ |
theStream.seekg (0, ios::beg); |
64 |
+ |
lineNum = 0; |
65 |
+ |
|
66 |
+ |
if (!theStream.eof()) { |
67 |
+ |
theStream.getline(readLine, MAXLEN); |
68 |
+ |
lineNum++; |
69 |
+ |
} else { |
70 |
+ |
printf( "Error fast forwarding stream: stream is empty.\n"); |
71 |
+ |
return -1; |
72 |
+ |
} |
73 |
+ |
|
74 |
+ |
while ( !foundText ) { |
75 |
+ |
|
76 |
+ |
if (theStream.eof()) { |
77 |
+ |
printf("Error fast forwarding stream at line %d: " |
78 |
+ |
"stream ended unexpectedly.\n", lineNum); |
79 |
+ |
return -1; |
80 |
+ |
} |
81 |
+ |
|
82 |
+ |
the_token = strtok( readLine, " ,;\t" ); |
83 |
+ |
if ( the_token != NULL) |
84 |
+ |
if (!strcmp("begin", the_token)) { |
85 |
+ |
the_token = strtok( NULL, " ,;\t" ); |
86 |
+ |
if ( the_token != NULL){ |
87 |
+ |
foundText = !strcmp( startText, the_token ); |
88 |
+ |
} |
89 |
+ |
} |
90 |
+ |
|
91 |
+ |
if (!foundText) { |
92 |
+ |
if (!theStream.eof()) { |
93 |
+ |
theStream.getline(readLine, MAXLEN); |
94 |
+ |
lineNum++; |
95 |
+ |
} else { |
96 |
+ |
printf( "Error fast forwarding stream at line %d: " |
97 |
+ |
"stream ended unexpectedly.\n", lineNum); |
98 |
+ |
return -1; |
99 |
+ |
} |
100 |
+ |
} |
101 |
+ |
} |
102 |
+ |
return lineNum; |
103 |
+ |
} |
104 |
+ |
|
105 |
+ |
int countTokens(char *line, char *delimiters) { |
106 |
+ |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
107 |
+ |
|
108 |
+ |
char *working_line; /* WORKING COPY OF LINE. */ |
109 |
+ |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
110 |
+ |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
111 |
+ |
|
112 |
+ |
strtok_ptr= working_line= strdup(line); |
113 |
+ |
|
114 |
+ |
ntokens=0; |
115 |
+ |
while (strtok(strtok_ptr,delimiters)!=NULL) |
116 |
+ |
{ |
117 |
+ |
ntokens++; |
118 |
+ |
strtok_ptr=NULL; |
119 |
+ |
} |
120 |
+ |
|
121 |
+ |
free(working_line); |
122 |
+ |
return(ntokens); |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
int isEndLine(char *line) { |
126 |
+ |
char *working_line; |
127 |
+ |
char *foo; |
128 |
+ |
|
129 |
+ |
working_line = strdup(line); |
130 |
+ |
|
131 |
+ |
foo = strtok(working_line, " ,;\t"); |
132 |
+ |
|
133 |
+ |
if (!strcasecmp(foo, "end")) return 1; |
134 |
+ |
|
135 |
+ |
return 0; |
136 |
+ |
} |