expanded parse() to compile all tokens, ready to match them

This commit is contained in:
Josh Holtrop 2010-05-07 13:07:21 -04:00
parent 8cc4edfca3
commit 67c051c866
2 changed files with 45 additions and 6 deletions

View File

@ -10,6 +10,11 @@ using namespace std;
namespace I_NAMESPACE { namespace I_NAMESPACE {
#endif #endif
I_CLASSNAME::I_CLASSNAME()
: m_errstr(NULL)
{
}
static void read_istream(istream & i, vector<char> & buff, int & size) static void read_istream(istream & i, vector<char> & buff, int & size)
{ {
size = 0; size = 0;
@ -27,19 +32,50 @@ static void read_istream(istream & i, vector<char> & buff, int & size)
bool I_CLASSNAME::parse(istream & i) bool I_CLASSNAME::parse(istream & i)
{ {
struct { char * name; char * definition; } tokens[] = { struct { char * name; char * definition; pcre * re; } tokens[] = {
I_TOKENLIST I_TOKENLIST
}; };
vector<char> buff; if (sizeof(tokens)/sizeof(tokens[0]) == 0)
int size; {
read_istream(i, buff, size); m_errstr = "No tokens defined";
if (size <= 0)
return false; return false;
}
vector<char> buff;
int buff_size;
read_istream(i, buff, buff_size);
if (buff_size <= 0)
{
m_errstr = "0-length input string";
return false;
}
/* append trailing NUL byte for pcre functions */ /* append trailing NUL byte for pcre functions */
buff.push_back('\0'); buff.push_back('\0');
/* compile all token regular expressions */
for (int i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++)
{
char * errptr;
int erroffset;
tokens[i].re = pcre_compile(tokens[i].definition, PCRE_DOTALL,
&errptr, &erroffset, NULL);
if (tokens[i].re == NULL)
{
cerr << "Error compiling token '" << tokens[i].name
<< "' regular expression at position " << erroffset
<< ": " << errptr << endl;
m_errstr = "Error in token regular expression";
return false;
}
}
int buff_pos = 0;
while (buff_pos < buff_size)
{
}
} }
#ifdef I_NAMESPACE #ifdef I_NAMESPACE

View File

@ -11,9 +11,12 @@ namespace I_NAMESPACE {
class I_CLASSNAME class I_CLASSNAME
{ {
public: public:
I_CLASSNAME();
bool parse(std::istream & in); bool parse(std::istream & in);
char * getError(); { return m_errstr; }
protected: protected:
char * m_errstr;
}; };
#ifdef I_NAMESPACE #ifdef I_NAMESPACE