"Fossies" - the Fresh Open Source Software Archive

Member "postal-0.76/expand.h" (17 Apr 2001, 2846 Bytes) of package /linux/privat/postal-0.76.tgz:


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 "expand.h" see the Fossies "Dox" file reference documentation.

    1 #ifndef EXPAND_H
    2 #define EXPAND_H
    3 
    4 using namespace std;
    5 #include <string>
    6 #include <sys/types.h>
    7 #include <regex.h>
    8 
    9 class NamePattern;
   10 
   11 // This class stores a set of patterns to match and the replacement data for
   12 // each pattern.
   13 class NameExpand
   14 {
   15 public:
   16   // Read the file with the specified name and parse it as a list of
   17   // expansions to perform.
   18   NameExpand(const char *filename);
   19 
   20   // expand the input to a random output based on our replacement patterns.
   21   int expand(string &output, const string &input, bool sequential = false);
   22 
   23 private:
   24   // past the end of the sequence
   25   bool m_no_seq;
   26   NamePattern *m_names;
   27 
   28   NameExpand(const NameExpand&);
   29   NameExpand & operator=(const NameExpand&);
   30 };
   31 
   32 // This class stores a pattern to match and the replacement data.
   33 // It also can have a pointer to another NamePattern, if so it will search
   34 // recursively if it's pattern is not matched.
   35 class NamePattern
   36 {
   37 public:
   38   // match is a regular expression that input string should match.
   39   // replace is the pattern for replacing.  An unescaped (escape is back-slash
   40   // '.' will mean not to change that character.  Otherwise it's either a
   41   // single character to replace it with, or a list of characters inside
   42   // '[' and ']'.  Ranges can be denoted with a hyphen as usual for globbing.
   43   NamePattern(const char *match, const char *replace);
   44   ~NamePattern();
   45 
   46   // expand the input to a random output based on our replacement pattern (if
   47   // our regex is matched).  If not then call recursively if we have a next
   48   // NamePattern.
   49   int expand(string &output, const string &input, bool sequential = false);
   50 
   51   // Add the next pattern to the list.
   52   void AddNext(NamePattern *np) { m_next = np; }
   53 
   54 private:
   55   // The pattern we are matching.  Not actually used after startup.
   56   const char *m_match;
   57 
   58   // The pattern we replace with.  Again not used other than diagnostics
   59   // after startup.
   60   const char *m_replace;
   61 
   62   // The number of elements in the arrays == characters to replace
   63   int m_conv_len;
   64 
   65   // The compiled regular expression for matching.
   66   regex_t m_match_regex;
   67 
   68   // The array of strings used for conversion.  For every character position
   69   // that is to be changed there will be a pointer in this array (NULL means
   70   // no change).  Each character in each string is a random character to be
   71   // used for replacement.  If a character happens to occur twice then it will
   72   // be twice as likely to occur in output.
   73   char **m_convert;
   74 
   75   // The length of the strings above.  Saves lots of strlen() calls.
   76   int *m_conv_item_len;
   77 
   78   // The next number to be used for each element
   79   int *m_next_value;
   80 
   81   // past the end of the sequence
   82   bool m_no_seq;
   83 
   84   // Pointer to the next pattern (or NULL).
   85   NamePattern *m_next;
   86 
   87   NamePattern(const NamePattern&);
   88   NamePattern & operator=(const NamePattern&);
   89 };
   90 
   91 #endif