moved regular expression classes into a separate namespace

This commit is contained in:
mr-alice 2016-09-13 12:05:22 +02:00
parent e9418bb5c6
commit a2e34f9cc6
25 changed files with 439 additions and 426 deletions

View file

@ -1,6 +1,3 @@
#ifndef RS_EXPRESSIONS_H
#define RS_EXPRESSIONS_H
/*
* rs-core/src/rsiface: rsexpr.h
*
@ -26,38 +23,41 @@
*
*/
#pragma once
#include <string>
#include <list>
#include <stdint.h>
#include "retroshare/rstypes.h"
/******************************************************************************************
Enumerations defining the Operators usable in the Boolean search expressions
******************************************************************************************/
namespace RsRegularExpression
{
enum LogicalOperator{
AndOp=0, /* exp AND exp */
OrOp=1, /* exp OR exp */
XorOp=2 /* exp XOR exp */
AndOp=0, /* exp AND exp */
OrOp=1, /* exp OR exp */
XorOp=2 /* exp XOR exp */
};
/*Operators for String Queries*/
/* Operators for String Queries */
enum StringOperator{
ContainsAnyStrings = 0, /* e.g. name contains any of 'conference' 'meeting' 'presentation' */
ContainsAllStrings = 1, /* same as above except that it contains ALL of the strings */
EqualsString = 2 /* exactly equal*/
ContainsAnyStrings = 0, /* e.g. name contains any of 'conference' 'meeting' 'presentation' */
ContainsAllStrings = 1, /* same as above except that it contains ALL of the strings */
EqualsString = 2 /* exactly equal*/
};
/*Relational operators ( >, <, >=, <=, == and InRange )*/
/* Comparison operators ( >, <, >=, <=, == and InRange ) */
enum RelOperator{
Equals = 0,
GreaterEquals = 1,
Greater = 2,
SmallerEquals = 3,
Smaller = 4,
InRange = 5 /* lower limit <= value <= upper limit*/
Equals = 0,
GreaterEquals = 1,
Greater = 2,
SmallerEquals = 3,
Smaller = 4,
InRange = 5 /* lower limit <= value <= upper limit*/
};
/********************************************************************************************
@ -69,26 +69,26 @@ class Expression ;
class LinearizedExpression
{
public:
std::vector<uint8_t> _tokens ;
std::vector<uint32_t> _ints ;
std::vector<std::string> _strings ;
public:
std::vector<uint8_t> _tokens ;
std::vector<uint32_t> _ints ;
std::vector<std::string> _strings ;
typedef enum { EXPR_DATE= 0,
EXPR_POP = 1,
EXPR_SIZE= 2,
EXPR_HASH= 3,
EXPR_NAME= 4,
EXPR_PATH= 5,
EXPR_EXT = 6,
EXPR_COMP= 7,
EXPR_SIZE_MB=8 } token ;
typedef enum { EXPR_DATE = 0,
EXPR_POP = 1,
EXPR_SIZE = 2,
EXPR_HASH = 3,
EXPR_NAME = 4,
EXPR_PATH = 5,
EXPR_EXT = 6,
EXPR_COMP = 7,
EXPR_SIZE_MB = 8 } token ;
static Expression *toExpr(const LinearizedExpression& e) ;
static Expression *toExpr(const LinearizedExpression& e) ;
private:
static Expression *toExpr(const LinearizedExpression& e,int&,int&,int&) ;
static void readStringExpr(const LinearizedExpression& e,int& n_ints,int& n_strings,std::list<std::string>& strings,bool& b,StringOperator& op) ;
private:
static Expression *toExpr(const LinearizedExpression& e,int&,int&,int&) ;
static void readStringExpr(const LinearizedExpression& e,int& n_ints,int& n_strings,std::list<std::string>& strings,bool& b,StringOperator& op) ;
};
@ -96,110 +96,122 @@ class LinearizedExpression
Boolean Search Expression
classes:
Expression: The base class of all expression typest
CompoundExpression: The expression which uses a logical operator to combine
the results of two expressions
StringExpression: An expression which uses some sort of string comparison.
RelExpression: A Relational Expression where > < >= <= == make sense.
e.g. size date etc
Expression: The base class of all expression typest
CompoundExpression: The expression which uses a logical operator to combine
the results of two expressions
StringExpression: An expression which uses some sort of string comparison.
RelExpression: A Relational Expression where > < >= <= == make sense.
e.g. size date etc
******************************************************************************************/
class FileEntry;
/*!
* \brief The ExpFileEntry class
* This is the base class the regular expressions can operate on. Derive it to fit your own needs!
*/
class ExpFileEntry
{
public:
virtual const std::string& file_name() const =0;
virtual uint64_t file_size() const =0;
virtual time_t file_modtime() const =0;
virtual time_t file_popularity() const =0;
virtual const std::string file_parent_path() const =0;
virtual const RsFileHash& file_hash() const =0;
};
class Expression
{
public:
virtual bool eval (FileEntry *file) = 0;
virtual ~Expression() {};
public:
virtual bool eval (const ExpFileEntry& file) = 0;
virtual ~Expression() {};
virtual void linearize(LinearizedExpression& e) const = 0 ;
virtual void linearize(LinearizedExpression& e) const = 0 ;
};
class CompoundExpression : public Expression
{
public:
CompoundExpression( enum LogicalOperator op, Expression * exp1, Expression *exp2)
: Lexp(exp1), Rexp(exp2), Op(op){ }
public:
CompoundExpression( enum LogicalOperator op, Expression * exp1, Expression *exp2)
: Lexp(exp1), Rexp(exp2), Op(op){ }
bool eval (FileEntry *file) {
if (Lexp == NULL or Rexp == NULL) {
return false;
}
switch (Op){
case AndOp:
return Lexp->eval(file) && Rexp->eval(file);
case OrOp:
return Lexp->eval(file) || Rexp->eval(file);
case XorOp:
return Lexp->eval(file) ^ Rexp->eval(file);
default:
return false;
}
}
virtual ~CompoundExpression(){
delete Lexp;
delete Rexp;
}
bool eval (const ExpFileEntry& file) {
if (Lexp == NULL or Rexp == NULL) {
return false;
}
switch (Op){
case AndOp:
return Lexp->eval(file) && Rexp->eval(file);
case OrOp:
return Lexp->eval(file) || Rexp->eval(file);
case XorOp:
return Lexp->eval(file) ^ Rexp->eval(file);
default:
return false;
}
}
virtual ~CompoundExpression(){
delete Lexp;
delete Rexp;
}
virtual void linearize(LinearizedExpression& e) const ;
private:
Expression *Lexp;
Expression *Rexp;
enum LogicalOperator Op;
virtual void linearize(LinearizedExpression& e) const ;
private:
Expression *Lexp;
Expression *Rexp;
enum LogicalOperator Op;
};
class StringExpression: public Expression
{
public:
StringExpression(enum StringOperator op, std::list<std::string> &t, bool ic): Op(op),terms(t), IgnoreCase(ic){}
public:
StringExpression(enum StringOperator op, std::list<std::string> &t, bool ic): Op(op),terms(t), IgnoreCase(ic){}
virtual void linearize(LinearizedExpression& e) const ;
protected:
bool evalStr(const std::string &str);
virtual void linearize(LinearizedExpression& e) const ;
protected:
bool evalStr(const std::string &str);
enum StringOperator Op;
std::list<std::string> terms;
bool IgnoreCase;
enum StringOperator Op;
std::list<std::string> terms;
bool IgnoreCase;
};
template <class T>
class RelExpression: public Expression
{
public:
RelExpression(enum RelOperator op, T lv, T hv): Op(op), LowerValue(lv), HigherValue(hv) {}
public:
RelExpression(enum RelOperator op, T lv, T hv): Op(op), LowerValue(lv), HigherValue(hv) {}
virtual void linearize(LinearizedExpression& e) const ;
protected:
bool evalRel(T val);
virtual void linearize(LinearizedExpression& e) const ;
protected:
bool evalRel(T val);
enum RelOperator Op;
T LowerValue;
T HigherValue;
enum RelOperator Op;
T LowerValue;
T HigherValue;
};
template<> void RelExpression<int>::linearize(LinearizedExpression& e) const ;
template <class T>
bool RelExpression<T>::evalRel(T val) {
switch (Op) {
case Equals:
return LowerValue == val;
case GreaterEquals:
return LowerValue >= val;
case Greater:
return LowerValue > val;
case SmallerEquals:
return LowerValue <= val;
case Smaller:
return LowerValue < val;
case InRange:
return (LowerValue <= val) && (val <= HigherValue);
default:
return false;
}
switch (Op) {
case Equals:
return LowerValue == val;
case GreaterEquals:
return LowerValue >= val;
case Greater:
return LowerValue > val;
case SmallerEquals:
return LowerValue <= val;
case Smaller:
return LowerValue < val;
case InRange:
return (LowerValue <= val) && (val <= HigherValue);
default:
return false;
}
}
@ -212,12 +224,12 @@ Binary Predicate for Case Insensitive search
*Factor locales in the comparison
*/
struct CompareCharIC :
public std::binary_function< char , char , bool> {
public std::binary_function< char , char , bool> {
bool operator () ( char ch1 , char ch2 ) const {
return tolower( static_cast < unsigned char > (ch1) )
== tolower( static_cast < unsigned char > (ch2) );
}
bool operator () ( char ch1 , char ch2 ) const {
return tolower( static_cast < unsigned char > (ch1) )
== tolower( static_cast < unsigned char > (ch2) );
}
};
@ -228,55 +240,55 @@ Some implementations of StringExpressions.
class NameExpression: public StringExpression
{
public:
NameExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
StringExpression(op,t,ic) {}
bool eval(FileEntry *file);
public:
NameExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
StringExpression(op,t,ic) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_NAME) ;
StringExpression::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_NAME) ;
StringExpression::linearize(e) ;
}
};
class PathExpression: public StringExpression {
public:
PathExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
StringExpression(op,t,ic) {}
bool eval(FileEntry *file);
PathExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
StringExpression(op,t,ic) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_PATH) ;
StringExpression::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_PATH) ;
StringExpression::linearize(e) ;
}
};
class ExtExpression: public StringExpression {
public:
ExtExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
StringExpression(op,t,ic) {}
bool eval(FileEntry *file);
ExtExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
StringExpression(op,t,ic) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_EXT) ;
StringExpression::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_EXT) ;
StringExpression::linearize(e) ;
}
};
class HashExpression: public StringExpression {
public:
HashExpression(enum StringOperator op, std::list<std::string> &t):
StringExpression(op,t, true) {}
bool eval(FileEntry *file);
HashExpression(enum StringOperator op, std::list<std::string> &t):
StringExpression(op,t, true) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_HASH) ;
StringExpression::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_HASH) ;
StringExpression::linearize(e) ;
}
};
/******************************************************************************************
@ -286,62 +298,62 @@ Some implementations of Relational Expressions.
class DateExpression: public RelExpression<int>
{
public:
DateExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
DateExpression(enum RelOperator op, int lv, int hv):
RelExpression<int>(op,lv,hv) {}
bool eval(FileEntry *file);
public:
DateExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
DateExpression(enum RelOperator op, int lv, int hv):
RelExpression<int>(op,lv,hv) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_DATE) ;
RelExpression<int>::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_DATE) ;
RelExpression<int>::linearize(e) ;
}
};
class SizeExpression: public RelExpression<int>
{
public:
SizeExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
SizeExpression(enum RelOperator op, int lv, int hv):
RelExpression<int>(op,lv,hv) {}
bool eval(FileEntry *file);
public:
SizeExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
SizeExpression(enum RelOperator op, int lv, int hv):
RelExpression<int>(op,lv,hv) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_SIZE) ;
RelExpression<int>::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_SIZE) ;
RelExpression<int>::linearize(e) ;
}
};
class SizeExpressionMB: public RelExpression<int>
{
public:
SizeExpressionMB(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
SizeExpressionMB(enum RelOperator op, int lv, int hv):
RelExpression<int>(op,lv,hv) {}
bool eval(FileEntry *file);
public:
SizeExpressionMB(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
SizeExpressionMB(enum RelOperator op, int lv, int hv):
RelExpression<int>(op,lv,hv) {}
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_SIZE_MB) ;
RelExpression<int>::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_SIZE_MB) ;
RelExpression<int>::linearize(e) ;
}
};
class PopExpression: public RelExpression<int>
{
public:
PopExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
PopExpression(enum RelOperator op, int lv, int hv): RelExpression<int>(op,lv,hv) {}
PopExpression(const LinearizedExpression& e) ;
bool eval(FileEntry *file);
public:
PopExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
PopExpression(enum RelOperator op, int lv, int hv): RelExpression<int>(op,lv,hv) {}
PopExpression(const LinearizedExpression& e) ;
bool eval(const ExpFileEntry& file);
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_POP) ;
RelExpression<int>::linearize(e) ;
}
virtual void linearize(LinearizedExpression& e) const
{
e._tokens.push_back(LinearizedExpression::EXPR_POP) ;
RelExpression<int>::linearize(e) ;
}
};
}
#endif /* RS_EXPRESSIONS_H */

View file

@ -36,7 +36,7 @@
class RsFiles;
extern RsFiles *rsFiles;
class Expression;
namespace RsRegularExpression { class Expression; }
class CacheStrapper ;
class CacheTransfer;
@ -190,8 +190,8 @@ class RsFiles
virtual int SearchKeywords(std::list<std::string> keywords, std::list<DirDetails> &results,FileSearchFlags flags) = 0;
virtual int SearchKeywords(std::list<std::string> keywords, std::list<DirDetails> &results,FileSearchFlags flags,const RsPeerId& peer_id) = 0;
virtual int SearchBoolExp(Expression * exp, std::list<DirDetails> &results,FileSearchFlags flags) = 0;
virtual int SearchBoolExp(Expression * exp, std::list<DirDetails> &results,FileSearchFlags flags,const RsPeerId& peer_id) = 0;
virtual int SearchBoolExp(RsRegularExpression::Expression * exp, std::list<DirDetails> &results,FileSearchFlags flags) = 0;
virtual int SearchBoolExp(RsRegularExpression::Expression * exp, std::list<DirDetails> &results,FileSearchFlags flags,const RsPeerId& peer_id) = 0;
/***
* Utility Functions.

View file

@ -35,7 +35,7 @@
#include "retroshare/rstypes.h"
class LinearizedExpression ;
namespace RsRegularExpression { class LinearizedExpression ; }
class RsTurtleClientService ;
class RsTurtle;
@ -102,7 +102,7 @@ class RsTurtle
// as they come back.
//
virtual TurtleRequestId turtleSearch(const std::string& match_string) = 0 ;
virtual TurtleRequestId turtleSearch(const LinearizedExpression& expr) = 0 ;
virtual TurtleRequestId turtleSearch(const RsRegularExpression::LinearizedExpression& expr) = 0 ;
// Initiates tunnel handling for the given file hash. tunnels. Launches
// an exception if an error occurs during the initialization process. The