diff --git a/libretroshare/src/_rsiface/rsexpr.cc b/libretroshare/src/_rsiface/rsexpr.cc new file mode 100644 index 000000000..224d8e309 --- /dev/null +++ b/libretroshare/src/_rsiface/rsexpr.cc @@ -0,0 +1,255 @@ +#include "rsexpr.h" +#include "dbase/findex.h" +#include "rsexpr.h" +#include +#include + + +CompoundExpression::CompoundExpression( enum LogicalOperator op, Expression * exp1, Expression *exp2): + Lexp(exp1), + Rexp(exp2), + Op(op) +{ +} + +bool CompoundExpression::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; + } +} + +CompoundExpression::~CompoundExpression() +{ + delete Lexp; + delete Rexp; +} + + +StringExpression::StringExpression(enum StringOperator op, std::list &t, bool ic) : + Op(op),terms(t), + IgnoreCase(ic) +{ +} + +/****************************************************************************************** +eval functions of relational expressions. + +******************************************************************************************/ + +//******** Releation expressions Constructors - Begin + +DateExpression::DateExpression(enum RelOperator op, int v): + RelExpression(op,v,v) +{ +} + +DateExpression::DateExpression(enum RelOperator op, int lv, int hv): + RelExpression(op,lv,hv) +{ +} + +SizeExpression::SizeExpression(enum RelOperator op, int v): + RelExpression(op,v,v) +{ +} + +SizeExpression::SizeExpression(enum RelOperator op, int lv, int hv): + RelExpression(op,lv,hv) +{ +} + +SizeExpression::PopExpression(enum RelOperator op, int v): + RelExpression(op,v,v) +{ +} + +SizeExpression::PopExpression(enum RelOperator op, int lv, int hv): + RelExpression(op,lv,hv) +{ +} +//******** Releation expressions Constructors - End + +bool DateExpression::eval(FileEntry *file) +{ + return evalRel(file->modtime); +} + +bool SizeExpression::eval(FileEntry *file) +{ + return evalRel(file->size); +} + +bool PopExpression::eval(FileEntry *file) +{ + return evalRel(file->pop); +} + +/****************************************************************************************** +Code for evaluating string expressions + +******************************************************************************************/ + +//******** String expressions Constructors - Begin +NameExpression::NameExpression(enum StringOperator op, std::list &t, bool ic): + StringExpression(op,t,ic) +{ +} + +PathExpression::PathExpression(enum StringOperator op, std::list &t, bool ic): + StringExpression(op,t,ic) +{ +} + +ExtExpression::ExtExpression(enum StringOperator op, std::list &t, bool ic): + StringExpression(op,t,ic) +{ +} + +HashExpression::HashExpression(enum StringOperator op, std::list &t): + StringExpression(op,t, true) +{ +} +//******** String expressions Constructors - End + + +bool NameExpression::eval(FileEntry *file) +{ + return evalStr(file->name); +} + +bool PathExpression::eval(FileEntry *file){ + std::string path; + /*Construct the path of this file*/ + DirEntry * curr = file->parent; + while ( curr != NULL ){ + path = curr->name+"/"+ path; + curr = curr->parent; + } + return evalStr(path); +} + +bool ExtExpression::eval(FileEntry *file){ + std::string ext; + /*Get the part of the string after the last instance of . in the filename */ + unsigned int index = file->name.find_last_of('.'); + if (index != std::string::npos) { + ext = file->name.substr(index+1); + if (ext != "" ){ + return evalStr(ext); + } + } + return false; +} + +bool HashExpression::eval(FileEntry *file){ + return evalStr(file->hash); +} + +/*Check whether two strings are 'equal' to each other*/ +static bool StrEquals(const std::string & str1, const std::string & str2, + bool IgnoreCase ){ + if ( str1.size() != str2.size() ){ + return false; + } else if (IgnoreCase) { + std::equal( str1.begin(), str1.end(), + str2.begin(), CompareCharIC() ); + } + return std::equal( str1.begin(), str1.end(), + str2.begin()); +} + +/*Check whether one string contains the other*/ +static bool StrContains( std::string & str1, std::string & str2, + bool IgnoreCase){ + + std::string::const_iterator iter ; + if (IgnoreCase) { + iter = std::search( str1.begin(), str1.end(), + str2.begin(), str2.end(), CompareCharIC() ); + } else { + iter = std::search( str1.begin(), str1.end(), + str2.begin(), str2.end()); + } + + return ( iter != str1.end() ); +} + + +bool StringExpression :: evalStr ( std::string &str ){ + std::list::iterator iter; + switch (Op) { + case ContainsAllStrings: + for ( iter = terms.begin(); iter != terms.end(); iter++ ) { + if ( StrContains (str, *iter, IgnoreCase) == false ){ + return false; + } + } + return true; + break; + case ContainsAnyStrings: + for ( iter = terms.begin(); iter != terms.end(); iter++ ) { + if ( StrContains (str,*iter, IgnoreCase) == true ) { + return true; + } + } + break; + case EqualsString: + for ( iter = terms.begin(); iter != terms.end(); iter++ ) { + if ( StrEquals (str,*iter, IgnoreCase) == true ) { + return true; + } + } + break; + default: + return false; + } + return false; +} + +template +RelExpression::RelExpression(enum RelOperator op, T lv, T hv): + Op(op), + LowerValue(lv), + HigherValue(hv) +{ +} + +template +bool RelExpression::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; + } +} + +bool CompareCharIC::operator () ( char ch1 , char ch2 ) const +{ + return tolower( static_cast < unsigned char > (ch1) ) + == tolower( static_cast < unsigned char > (ch2) ); +} + + diff --git a/libretroshare/src/_rsiface/rsexpr.h b/libretroshare/src/_rsiface/rsexpr.h new file mode 100644 index 000000000..ea5436aa9 --- /dev/null +++ b/libretroshare/src/_rsiface/rsexpr.h @@ -0,0 +1,200 @@ +#ifndef RS_EXPRESSIONS_H +#define RS_EXPRESSIONS_H + +/* + * rs-core/src/rsiface: rsexpr.h + * + * RetroShare C++ Interface. + * + * Copyright 2007-2008 by Kashif Kaleem. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + + +#include +#include + +/****************************************************************************************** +Enumerations defining the Operators usable in the Boolean search expressions +******************************************************************************************/ + + +enum LogicalOperator{ + AndOp=0, /* exp AND exp */ + OrOp, /* exp OR exp */ + XorOp /* exp XOR exp */ +}; + + +/*Operators for String Queries*/ +enum StringOperator{ + ContainsAnyStrings = 0, /* e.g. name contains any of 'conference' 'meeting' 'presentation' */ + ContainsAllStrings, /* same as above except that it contains ALL of the strings */ + EqualsString /* exactly equal*/ +}; + +/*Relational operators ( >, <, >=, <=, == and InRange )*/ +enum RelOperator{ + Equals = 0, + GreaterEquals, + Greater, + SmallerEquals, + Smaller, + InRange /* lower limit <= value <= upper limit*/ +}; + +/****************************************************************************************** +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 + +******************************************************************************************/ + +class FileEntry; + +class Expression +{ +public: + virtual bool eval (FileEntry *file) = 0; + virtual ~Expression(); +}; + + +class CompoundExpression : public Expression +{ +public: + CompoundExpression( enum LogicalOperator op, Expression * exp1, Expression *exp2); + + bool eval (FileEntry *file); + virtual ~CompoundExpression(); +private: + Expression *Lexp; + Expression *Rexp; + enum LogicalOperator Op; + +}; + +class StringExpression: public Expression +{ +public: + StringExpression(enum StringOperator op, std::list &t, bool ic); +protected: + bool evalStr(std::string &str); +private: + enum StringOperator Op; + std::list terms; + bool IgnoreCase; +}; + +template +class RelExpression: public Expression { +public: + RelExpression(enum RelOperator op, T lv, T hv): + Op(op), LowerValue(lv), HigherValue(hv) {} +protected: + bool evalRel(T val); +private: + enum RelOperator Op; + T LowerValue; + T HigherValue; +}; + + + +/****************************************************************************************** +Binary Predicate for Case Insensitive search + +******************************************************************************************/ +/*Binary predicate for case insensitive character comparison.*/ +/*TODOS: + *Factor locales in the comparison + */ +struct CompareCharIC : public std::binary_function< char , char , bool> +{ + bool operator () ( char ch1 , char ch2 ) const; +}; + +/****************************************************************************************** +Some implementations of StringExpressions. + +******************************************************************************************/ + +class NameExpression: public StringExpression +{ +public: + NameExpression(enum StringOperator op, std::list &t, bool ic); + bool eval(FileEntry *file); +}; + +class PathExpression: public StringExpression +{ +public: + PathExpression(enum StringOperator op, std::list &t, bool ic); + bool eval(FileEntry *file); +}; + +class ExtExpression: public StringExpression { +public: + ExtExpression(enum StringOperator op, std::list &t, bool ic); + bool eval(FileEntry *file); +}; + +class HashExpression: public StringExpression { +public: + HashExpression(enum StringOperator op, std::list &t); + bool eval(FileEntry *file); +}; + +/****************************************************************************************** +Some implementations of Relational Expressions. + +******************************************************************************************/ + +class DateExpression: public RelExpression +{ +public: + DateExpression(enum RelOperator op, int v); + DateExpression(enum RelOperator op, int lv, int hv); + bool eval(FileEntry *file); +}; + +class SizeExpression: public RelExpression +{ +public: + SizeExpression(enum RelOperator op, int v); + SizeExpression(enum RelOperator op, int lv, int hv); + + bool eval(FileEntry *file); +}; + +class PopExpression: public RelExpression { +public: + PopExpression(enum RelOperator op, int v); + PopExpression(enum RelOperator op, int lv, int hv); + + bool eval(FileEntry *file); +}; + +#endif /* RS_EXPRESSIONS_H */ diff --git a/libretroshare/src/_rsiface/rsfiles.h b/libretroshare/src/_rsiface/rsfiles.h new file mode 100644 index 000000000..fadda2163 --- /dev/null +++ b/libretroshare/src/_rsiface/rsfiles.h @@ -0,0 +1,164 @@ +#ifndef RSFILES_H +#define RSFILES_H + +/* + * libretroshare/src/rsiface: rsfiles.h + * + * RetroShare C++ Interface. + * + * Copyright 2008 by Robert Fernie. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + + +#include +#include +#include + +#include "rstypes.h" + +class Expression; + +/* These are used mainly by ftController at the moment */ +const uint32_t RS_FILE_CTRL_PAUSE = 0x00000100; +const uint32_t RS_FILE_CTRL_START = 0x00000200; + +const uint32_t RS_FILE_RATE_TRICKLE = 0x00000001; +const uint32_t RS_FILE_RATE_SLOW = 0x00000002; +const uint32_t RS_FILE_RATE_STANDARD = 0x00000003; +const uint32_t RS_FILE_RATE_FAST = 0x00000004; +const uint32_t RS_FILE_RATE_STREAM_AUDIO = 0x00000005; +const uint32_t RS_FILE_RATE_STREAM_VIDEO = 0x00000006; + +const uint32_t RS_FILE_PEER_ONLINE = 0x00001000; +const uint32_t RS_FILE_PEER_OFFLINE = 0x00002000; + +/************************************ + * Used To indicate where to search. + * + * The Order of these is very important, + * it specifies the search order too. + * + */ + +const uint32_t RS_FILE_HINTS_MASK = 0x00ffffff; + +const uint32_t RS_FILE_HINTS_CACHE = 0x00000001; +const uint32_t RS_FILE_HINTS_EXTRA = 0x00000002; +const uint32_t RS_FILE_HINTS_LOCAL = 0x00000004; +const uint32_t RS_FILE_HINTS_REMOTE = 0x00000008; +const uint32_t RS_FILE_HINTS_DOWNLOAD= 0x00000010; +const uint32_t RS_FILE_HINTS_UPLOAD = 0x00000020; +const uint32_t RS_FILE_HINTS_TURTLE = 0x00000040; + + +const uint32_t RS_FILE_HINTS_SPEC_ONLY = 0x01000000; +const uint32_t RS_FILE_HINTS_NO_SEARCH = 0x02000000; + +/* Callback Codes */ +//const uint32_t RS_FILE_HINTS_CACHE = 0x00000001; // ALREADY EXISTS +const uint32_t RS_FILE_HINTS_MEDIA = 0x00001000; + +const uint32_t RS_FILE_HINTS_BACKGROUND = 0x00002000; // To download slowly. + +const uint32_t RS_FILE_EXTRA_DELETE = 0x0010; + +const uint32_t CB_CODE_CACHE = 0x0001; +const uint32_t CB_CODE_EXTRA = 0x0002; +const uint32_t CB_CODE_MEDIA = 0x0004; + + +class RsFiles +{ +public: + +// RsFiles is pure virtual. It deon't need this: +// RsFiles() { return; } +// virtual ~RsFiles() { return; } + RsFiles(); + virtual ~RsFiles(); + +/****************************************/ + /* download */ + + + /*** + * Control of Downloads. + ***/ + virtual bool FileRequest(std::string fname, std::string hash, uint64_t size, + std::string dest, uint32_t flags, std::list srcIds) = 0; + virtual bool FileCancel(std::string hash) = 0; + virtual bool FileControl(std::string hash, uint32_t flags) = 0; + virtual bool FileClearCompleted() = 0; + + /*** + * Download / Upload Details. + ***/ + virtual bool FileDownloads(std::list &hashs) = 0; + virtual bool FileUploads(std::list &hashs) = 0; + virtual bool FileDetails(std::string hash, uint32_t hintflags, FileInfo &info) = 0; + + + /*** + * Extra List Access + ***/ + virtual bool ExtraFileAdd(std::string fname, std::string hash, uint64_t size, + uint32_t period, uint32_t flags) = 0; + virtual bool ExtraFileRemove(std::string hash, uint32_t flags) = 0; + virtual bool ExtraFileHash(std::string localpath, + uint32_t period, uint32_t flags) = 0; + virtual bool ExtraFileStatus(std::string localpath, FileInfo &info) = 0; + virtual bool ExtraFileMove(std::string fname, std::string hash, uint64_t size, + std::string destpath) = 0; + + /*** + * Directory Listing / Search Interface + */ + virtual int RequestDirDetails(std::string uid, std::string path, DirDetails &details) = 0; + virtual int RequestDirDetails(void *ref, DirDetails &details, uint32_t flags) = 0; + + virtual int SearchKeywords(std::list keywords, std::list &results,uint32_t flags) = 0; + virtual int SearchBoolExp(Expression * exp, std::list &results) = 0; + + /*** + * Utility Functions. + ***/ + virtual bool ConvertSharedFilePath(std::string path, std::string &fullpath) = 0; + virtual void ForceDirectoryCheck() = 0; + virtual bool InDirectoryCheck() = 0; + + /*** + * Directory Control + ***/ + virtual void setDownloadDirectory(std::string path) = 0; + virtual void setPartialsDirectory(std::string path) = 0; + virtual std::string getDownloadDirectory() = 0; + virtual std::string getPartialsDirectory() = 0; + + virtual bool getSharedDirectories(std::list &dirs) = 0; + virtual bool addSharedDirectory(std::string dir) = 0; + virtual bool removeSharedDirectory(std::string dir) = 0; + +}; + + +// Extern +RsFiles *rsFiles; + +#endif diff --git a/libretroshare/src/_rsiface/rsiface.pri b/libretroshare/src/_rsiface/rsiface.pri index 86af28561..fd8391b32 100644 --- a/libretroshare/src/_rsiface/rsiface.pri +++ b/libretroshare/src/_rsiface/rsiface.pri @@ -6,10 +6,13 @@ SOURCES = $$PWP/rsinit.cc \ $$PWP/rscontrol.cc \ $$PWP/notifybase.cc \ $$PWP/rsifacereal.cc \ - $$PWP/rstypes.cc + $$PWP/rstypes.cc \ + rsexpr.cc HEADERS = $$PWP/rsinit.h \ $$PWP/rsiface.h \ $$PWP/rscontrol.h \ $$PWP/notifybase.h \ $$PWP/rsifacereal.h \ - $$PWP/rstypes.h + $$PWP/rstypes.h \ + $$PWP/rsfiles.h \ + $$PWP/rsexpr.h