| 1 |
#include "protoString.h" |
| 2 |
#include "protoDebug.h" |
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <ctype.h> // for isspace() |
| 7 |
|
| 8 |
ProtoTokenator::ProtoTokenator(const char* text, |
| 9 |
char delimiter, |
| 10 |
bool stripWhiteSpace, |
| 11 |
unsigned int maxCount, |
| 12 |
bool reverseOrder, |
| 13 |
bool stripTokens) |
| 14 |
: token(delimiter), strip_whitespace(stripWhiteSpace), strip_tokens(stripTokens), |
| 15 |
reverse(reverseOrder), max_count(maxCount), remain(maxCount), |
| 16 |
text_ptr(text), prev_item(NULL) |
| 17 |
{ |
| 18 |
Reset(); |
| 19 |
} |
| 20 |
|
| 21 |
ProtoTokenator::~ProtoTokenator() |
| 22 |
{ |
| 23 |
if (NULL != prev_item) |
| 24 |
{ |
| 25 |
delete[] prev_item; |
| 26 |
prev_item = NULL; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
bool ProtoTokenator::TokenMatch(char c) const |
| 31 |
{ |
| 32 |
return ((c == token) || (isspace(token) && isspace(c))); |
| 33 |
} // end ProtoTokenator::TokenMatch() |
| 34 |
|
| 35 |
void ProtoTokenator::Reset(const char* text, char delimiter) |
| 36 |
{ |
| 37 |
remain = max_count; |
| 38 |
if (NULL != text) text_ptr = text; |
| 39 |
if ('\0' != delimiter) token = delimiter; |
| 40 |
if (reverse) |
| 41 |
{ |
| 42 |
next_ptr = text_ptr; |
| 43 |
if (NULL != next_ptr) |
| 44 |
next_ptr += strlen(next_ptr) - 1; |
| 45 |
if (isspace(token)) |
| 46 |
{ |
| 47 |
// Advance to start of any trailing white space |
| 48 |
while (NULL != next_ptr) |
| 49 |
{ |
| 50 |
if (isspace(*next_ptr)) |
| 51 |
{ |
| 52 |
if (text_ptr == next_ptr) |
| 53 |
next_ptr = NULL; |
| 54 |
else |
| 55 |
next_ptr--; |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
next_ptr = text_ptr; |
| 67 |
if (isspace(token)) |
| 68 |
{ |
| 69 |
// advance to end of any leading white space |
| 70 |
while (NULL != next_ptr) |
| 71 |
{ |
| 72 |
if ('\0' == *next_ptr) |
| 73 |
next_ptr = NULL; |
| 74 |
else if (isspace(*next_ptr)) |
| 75 |
next_ptr++; |
| 76 |
else |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} // end ProtoTokenator::Reset() |
| 82 |
|
| 83 |
const char* const ProtoTokenator::GetNextItem(bool detach) |
| 84 |
{ |
| 85 |
if (reverse) |
| 86 |
{ |
| 87 |
if (strip_whitespace) |
| 88 |
{ |
| 89 |
// Strip any trailing white space, if required |
| 90 |
while (NULL != next_ptr) |
| 91 |
{ |
| 92 |
if (isspace(*next_ptr)) |
| 93 |
{ |
| 94 |
if (text_ptr == next_ptr) |
| 95 |
next_ptr = NULL; |
| 96 |
else |
| 97 |
next_ptr--; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
if (NULL == next_ptr) return NULL; |
| 106 |
|
| 107 |
if ((0 != max_count) && (0 == remain)) |
| 108 |
{ |
| 109 |
// Count was limited, so return remainder as last item |
| 110 |
// (strip whitespace from head if required) |
| 111 |
long itemLen = next_ptr - text_ptr + 1; |
| 112 |
const char* headPtr = text_ptr; |
| 113 |
if (strip_whitespace) |
| 114 |
{ |
| 115 |
while ((0 != itemLen) && (isspace(*headPtr))) |
| 116 |
{ |
| 117 |
headPtr++; |
| 118 |
itemLen--; |
| 119 |
} |
| 120 |
} |
| 121 |
// (strip extraneous token(s) from tail if required) |
| 122 |
if (strip_tokens) |
| 123 |
{ |
| 124 |
const char* tailPtr = headPtr + itemLen - 1; |
| 125 |
while ((0 != itemLen) && TokenMatch(*tailPtr)) |
| 126 |
{ |
| 127 |
tailPtr--; |
| 128 |
itemLen--; |
| 129 |
} |
| 130 |
} |
| 131 |
if (NULL != prev_item) delete[] prev_item; |
| 132 |
if (NULL == (prev_item = new char[itemLen+1])) |
| 133 |
{ |
| 134 |
PLOG(PL_ERROR, "ProtoTokenator::GetNextItem() new char[] error: %s\n", GetErrorString()); |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
strncpy(prev_item, headPtr, itemLen); |
| 138 |
prev_item[itemLen] = '\0'; |
| 139 |
next_ptr = NULL; |
| 140 |
if (detach) |
| 141 |
{ |
| 142 |
const char* item = prev_item; |
| 143 |
prev_item = NULL; |
| 144 |
return item; |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
return prev_item; |
| 149 |
} |
| 150 |
} |
| 151 |
const char* ptr = next_ptr; |
| 152 |
// Advance to next token |
| 153 |
while (text_ptr != ptr) |
| 154 |
{ |
| 155 |
if (TokenMatch(*ptr)) |
| 156 |
break; |
| 157 |
else |
| 158 |
ptr--; |
| 159 |
} |
| 160 |
size_t itemLen = next_ptr - ptr; |
| 161 |
bool firstItem = false; |
| 162 |
if (text_ptr == ptr) |
| 163 |
{ |
| 164 |
if (TokenMatch(*ptr)) |
| 165 |
firstItem = false; |
| 166 |
else |
| 167 |
firstItem = true; |
| 168 |
} |
| 169 |
const char* headPtr = ptr; |
| 170 |
if (firstItem) |
| 171 |
itemLen += 1; |
| 172 |
else |
| 173 |
headPtr += 1; |
| 174 |
// Strip any leading whitespace, if required |
| 175 |
if (strip_whitespace) |
| 176 |
{ |
| 177 |
while ((0 != itemLen) && isspace(*headPtr)) |
| 178 |
{ |
| 179 |
headPtr++; |
| 180 |
itemLen--; |
| 181 |
} |
| 182 |
} |
| 183 |
if (NULL != prev_item) delete[] prev_item; |
| 184 |
if (NULL == (prev_item = new char[itemLen+1])) |
| 185 |
{ |
| 186 |
PLOG(PL_ERROR, "ProtoTokenator::GetNextItem() new char[] error: %s\n", GetErrorString()); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
strncpy(prev_item, headPtr, itemLen); |
| 190 |
prev_item[itemLen] = '\0'; |
| 191 |
if (text_ptr == ptr--) |
| 192 |
{ |
| 193 |
ptr = NULL; |
| 194 |
} |
| 195 |
else if (isspace(token)) |
| 196 |
{ |
| 197 |
// Advance to start of white space |
| 198 |
while (NULL != ptr) |
| 199 |
{ |
| 200 |
if (isspace(*ptr)) |
| 201 |
{ |
| 202 |
if (text_ptr == ptr) |
| 203 |
ptr = NULL; |
| 204 |
else |
| 205 |
ptr--; |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
next_ptr = ptr; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
// Strip any leading white space, if required |
| 218 |
if (strip_whitespace) |
| 219 |
{ |
| 220 |
while (NULL != next_ptr) |
| 221 |
{ |
| 222 |
if ('\0' == *next_ptr) |
| 223 |
next_ptr = NULL; |
| 224 |
else if (isspace(*next_ptr)) |
| 225 |
next_ptr++; |
| 226 |
else |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
if (NULL == next_ptr) return NULL; |
| 231 |
if ((0 != max_count) && (0 == remain)) |
| 232 |
{ |
| 233 |
// Count was limited, so return remainder as last item |
| 234 |
// (strip trailing whitespace if required) |
| 235 |
size_t itemLen = strlen(next_ptr); |
| 236 |
if (strip_whitespace) |
| 237 |
{ |
| 238 |
const char* tailPtr = next_ptr + itemLen - 1; |
| 239 |
while ((0 != itemLen) && (isspace(*tailPtr))) |
| 240 |
{ |
| 241 |
tailPtr--; |
| 242 |
itemLen--; |
| 243 |
} |
| 244 |
} |
| 245 |
// (strip extraneous leading token(s) if required) |
| 246 |
if (strip_tokens) |
| 247 |
{ |
| 248 |
while ((0 != itemLen) && TokenMatch(*next_ptr)) |
| 249 |
{ |
| 250 |
next_ptr++; |
| 251 |
itemLen--; |
| 252 |
} |
| 253 |
} |
| 254 |
if (NULL != prev_item) delete[] prev_item; |
| 255 |
if (NULL == (prev_item = new char[itemLen+1])) |
| 256 |
{ |
| 257 |
PLOG(PL_ERROR, "ProtoTokenator::GetNextItem() new char[] error: %s\n", GetErrorString()); |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
strncpy(prev_item, next_ptr, itemLen + 1); |
| 261 |
next_ptr = NULL; |
| 262 |
if (detach) |
| 263 |
{ |
| 264 |
const char* item = prev_item; |
| 265 |
prev_item = NULL; |
| 266 |
return item; |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
return prev_item; |
| 271 |
} |
| 272 |
} |
| 273 |
const char* ptr; |
| 274 |
if (isspace(token)) |
| 275 |
{ |
| 276 |
// Advance start of whitespace |
| 277 |
ptr = next_ptr; |
| 278 |
while (NULL != ptr) |
| 279 |
{ |
| 280 |
if ('\0' == *ptr) |
| 281 |
ptr = NULL; |
| 282 |
else if (isspace(*ptr)) |
| 283 |
break; |
| 284 |
else |
| 285 |
ptr++; |
| 286 |
} |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
ptr = strchr(next_ptr, token); |
| 291 |
} |
| 292 |
size_t itemLen = (NULL == ptr) ? strlen(next_ptr) : (ptr++ - next_ptr); |
| 293 |
// Strip trailing whitespace, if required |
| 294 |
if (strip_whitespace) |
| 295 |
{ |
| 296 |
const char* tailPtr = next_ptr + itemLen - 1; |
| 297 |
while ((0 != itemLen) && (isspace(*tailPtr--))) |
| 298 |
itemLen--; |
| 299 |
} |
| 300 |
if (NULL != prev_item) delete[] prev_item; |
| 301 |
if (NULL == (prev_item = new char[itemLen+1])) |
| 302 |
{ |
| 303 |
PLOG(PL_ERROR, "ProtoTokenator::GetNextItem() new char[] error: %s\n", GetErrorString()); |
| 304 |
return NULL; |
| 305 |
} |
| 306 |
strncpy(prev_item, next_ptr, itemLen); |
| 307 |
prev_item[itemLen] = '\0'; |
| 308 |
if (strip_whitespace || isspace(token)) |
| 309 |
{ |
| 310 |
// advance to end of white space |
| 311 |
while (NULL != ptr) |
| 312 |
{ |
| 313 |
if ('\0' == *ptr) |
| 314 |
ptr = NULL; |
| 315 |
else if (isspace(*ptr)) |
| 316 |
ptr++; |
| 317 |
else |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
next_ptr = ptr; |
| 322 |
} |
| 323 |
if (0 != max_count) remain -= 1; |
| 324 |
if (detach) |
| 325 |
{ |
| 326 |
const char* item = prev_item; |
| 327 |
prev_item = NULL; |
| 328 |
return item; |
| 329 |
} |
| 330 |
else |
| 331 |
{ |
| 332 |
return prev_item; |
| 333 |
} |
| 334 |
} // end ProtoTokenator::GetNextItem() |