Re: Help with "Text Contains"
Posted: 30 Nov 2010, 22:46
Hi John,
I'd certainly welcome a strstr() function, or some equivalent to the PERL split() function. I spent an (enjoyable) afternoon coding a simple text line parser that could parse lines of configuration data from a file (in the format index=x,y). The resulting code looked ugly, but without a strstr() function seemed the best I could come up with.
I'm sure you code hand-tune this to be tighter!
Regards,
Mark
I'd certainly welcome a strstr() function, or some equivalent to the PERL split() function. I spent an (enjoyable) afternoon coding a simple text line parser that could parse lines of configuration data from a file (in the format index=x,y). The resulting code looked ugly, but without a strstr() function seemed the best I could come up with.
Code: Select all
// Parse the text line and return the x,y coordinates
// Return ERR if the line fails to parse, else return SUCCESS
// Line format is index=x,y<newline>
// Does not tolerate whitespace around numbers
int parseLine(string line, int &index, int &x, int &y) {
int pos, len;
int equals, comma; // index of the equals and comma chars in the string
equals = comma = -1;
len = strlen(line);
// Locate the = sign
for(pos=0; pos < len; pos++) {
if(StrIndex(line, pos) == EQUAL) {
equals = pos;
break;
}
}
if(equals == -1) {
// no equals
return ERR;
}
index = StrToNum(SubStr(line, 0, equals+1));
// Now locate the comma
for( ; pos < len; pos++) {
if(StrIndex(line, pos) == COMMA) {
comma = pos;
break;
}
}
if(comma == -1) {
// no comma
return ERR;
}
x = StrToNum(SubStr(line, equals+1, (comma - equals) - 1));
// Whatever is after the comma is the y value
y = StrToNum(SubStr(line, comma+1, (len - comma) - 1));
return SUCCESS;
}
Regards,
Mark