Commit 81202ee4 authored by mr-ti's avatar mr-ti
Browse files

No commit message

No commit message
parent 6074152e
//
// C++ Implementation: cregex
//
// Description:
//
//
// Author: Emeric Verschuur <ct-info@mr-ti.com>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "cregex.h"
#include <iostream>
CRegex::CRegex(const string &expr, int flags)
{
errNo=regcomp(&regex,expr.c_str(),flags);
if(errNo==0){
state=NOSTATE;
}else{
state=ERROR;
}
nmatch = 1 /*regex.re_nsub*/;
pmatch = (regmatch_t*)malloc (sizeof (regmatch_t) * nmatch);
}
CRegex::~CRegex()
{
free(pmatch);
regfree(&regex);
}
int CRegex::getErrorNb(){
return errNo;
}
string CRegex::getErrorMsg(){
size_t tmpLen=regerror(errNo, &regex, NULL, 0);
char *tmp=(char*)malloc(tmpLen+1);
regerror(errNo, &regex, tmp, tmpLen);
string errStr(tmp);
free(tmp);
return errStr;
}
CRegex::State CRegex::getState(){
return state;
}
bool CRegex::exec(int flags){
int match = regexec (&regex, buffer->c_str()+offset, nmatch, pmatch, flags);
if(match==0){
state=MATCH;
return true;
}else if (match == REG_NOMATCH){
state=NOMATCH;
return false;
}else{
state=ERROR;
return false;
}
}
list<string> CRegex::getMatchs(){
list<string> lst;
if(state!=ERROR){
while(exec()){
lst.push_back(buffer->substr(pmatch[0].rm_so+offset,pmatch[0].rm_eo - pmatch[0].rm_so));
offset+=pmatch[0].rm_eo+1;
}
offset=0;
}
return lst;
}
void CRegex::setBuffer(string *buffer){
offset=0;
this->buffer=buffer;
}
//
// C++ Interface: cregex
//
// Description:
//
//
// Author: Emeric Verschuur <ct-info@mr-ti.com>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef CREGEX_H
#define CREGEX_H
#include <string>
#include <list>
#include <regex.h>
using namespace std;
class CRegex{
public:
typedef enum State{
NOSTATE,
MATCH,
NOMATCH,
ERROR
};
CRegex(const string &expr, int flags=REG_EXTENDED);
~CRegex();
void setBuffer(string *buffer);
bool exec(int flags=0);
State getState();
list<string> getMatchs();
int getErrorNb();
string getErrorMsg();
private:
State state;
regex_t regex;
size_t nmatch;
regmatch_t *pmatch;
int errNo;
string *buffer;
unsigned long offset;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment