Re: Parsing - is this a sensible idea?
I wrote a class like that a few years ago and it turned out to be quite
useful
Example code:
string Part1, Part2, Key;
parse_str(Line) >> Part1 >> "%" >> Key >> "%" >> Part2 >> "";
if (Key.size() != 0) ...
The class was modelled after the Rexx parse command so it uses some
special strings like
"." for word
"10" to go to position 10 in the string
"+10" to go 10 positions forward in the string
"," to go to the next line in the string etc.
The construction of the class is as follows
parse_str(const string& in_s) ...
Constructor that just saves the string variable internally
//method that picks up integer variable to assign value to and returns the
object to enable
//continuing using >> operators
parse_str& operator>>(int& ival)
{
wordstep();
(this->*m_try_assign)();
m_pvar = (void*)&ival;
m_try_assign = &parse_str::try_assign_int;
m_wordmatch = 1;
return *this;
}
// method that recognizes special strings and search items
parse_str& operator>>(const char* in_psz)...
// method that converts a part of the parse string to an integer
int try_assign_int() ...
// variables
void* m_pvar; // pointer to variable to set value to
const string m_str; // string passed in as argument to constructor
int (t_parse_string::* m_try_assign)(void); function pointer to method
that assigns variable
--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c++/
More information at http://www.talkaboutprogramming.com/faq.html