"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/be13_api/beregex.h" (19 Feb 2021, 2586 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "beregex.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.4.5_vs_1.5.0.

    1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
    2 /*
    3  * beregex.h:
    4  * 
    5  * simple cover for regular expression class.
    6  * The class allocates and frees the strings 
    7  */
    8 
    9 #ifndef BEREGEX_H
   10 #define BEREGEX_H
   11 
   12 #ifdef HAVE_TRE_TRE_H
   13 # include <tre/tre.h>
   14 #else
   15 # ifdef HAVE_REGEX_H
   16 #  include <regex.h>
   17 # endif
   18 #endif
   19 
   20 
   21 
   22 #include <string>
   23 #include <iostream>
   24 #include <fstream>
   25 #include <assert.h>
   26 #include <string.h>
   27 #include <vector>
   28 #include <set>
   29 
   30 class beregex {
   31 private:
   32     void compile();
   33     beregex & operator=(const beregex&that); // don't use this, please
   34  public:
   35     /** Bargain-basement detector of things that might be regular expressions. */
   36     static const char *version();
   37     static bool is_regex(const std::string &str);
   38 
   39     std::string  pat;              /* our pattern */
   40     int     flags;
   41     // Note: nreg_ is void* because the compiler will not allow us to define it as "struct regex_t *"
   42     // We could get around this by including regex.h, but that introduces dependencies for programs that include
   43     // beregex.h.
   44     void   *nreg_;
   45     beregex(const beregex &that);
   46     beregex(std::string pat_,int flags_);
   47     ~beregex();
   48     /**
   49      * perform a search for a single hit. If there is a group and something is found,
   50      * set *found to be what was found, *offset to be the starting offset, and *len to be
   51      * the length. Note that this only handles a single group.
   52      */
   53     int search(const std::string &line,std::string *found,size_t *offset,size_t *len) const;
   54     int search(const std::string &line,std::string *matches,int REGMAX) const;
   55     std::string search(const std::string &line) const;
   56 };
   57 typedef std::vector<beregex *> beregex_vector;
   58 
   59 /**
   60  * The regex_list maintains a list of regular expressions.
   61  * The list can be read out of a file.
   62  * check() returns true if the provided string is inside the list
   63  * This should be combined with the word_and_context_list
   64  */
   65 class regex_list {
   66  public:
   67     std::vector<beregex *> patterns;
   68     regex_list():patterns(){}
   69 
   70     size_t size(){
   71         return patterns.size();
   72     }
   73     /**
   74      * Read a file; returns 0 if successful, -1 if failure.
   75      * @param fname - the file to read.
   76      */
   77     virtual ~regex_list(){
   78         for(std::vector<beregex *>::iterator it=patterns.begin(); it != patterns.end(); it++){
   79             delete *it;
   80         }
   81     }
   82     void add_regex(const std::string &pat);
   83     int readfile(std::string fname);
   84     /** check() is threadsafe. */
   85     bool check(const std::string &probe,std::string *found, size_t *offset,size_t *len) const;
   86 };
   87 
   88 
   89 #endif