File: expand.h

package info (click to toggle)
postal 0.70
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 540 kB
  • ctags: 525
  • sloc: cpp: 4,131; sh: 289; ansic: 258; makefile: 134
file content (91 lines) | stat: -rw-r--r-- 2,846 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef EXPAND_H
#define EXPAND_H

using namespace std;
#include <string>
#include <sys/types.h>
#include <regex.h>

class NamePattern;

// This class stores a set of patterns to match and the replacement data for
// each pattern.
class NameExpand
{
public:
  // Read the file with the specified name and parse it as a list of
  // expansions to perform.
  NameExpand(const char *filename);

  // expand the input to a random output based on our replacement patterns.
  int expand(string &output, const string &input, bool sequential = false);

private:
  // past the end of the sequence
  bool m_no_seq;
  NamePattern *m_names;

  NameExpand(const NameExpand&);
  NameExpand & operator=(const NameExpand&);
};

// This class stores a pattern to match and the replacement data.
// It also can have a pointer to another NamePattern, if so it will search
// recursively if it's pattern is not matched.
class NamePattern
{
public:
  // match is a regular expression that input string should match.
  // replace is the pattern for replacing.  An unescaped (escape is back-slash
  // '.' will mean not to change that character.  Otherwise it's either a
  // single character to replace it with, or a list of characters inside
  // '[' and ']'.  Ranges can be denoted with a hyphen as usual for globbing.
  NamePattern(const char *match, const char *replace);
  ~NamePattern();

  // expand the input to a random output based on our replacement pattern (if
  // our regex is matched).  If not then call recursively if we have a next
  // NamePattern.
  int expand(string &output, const string &input, bool sequential = false);

  // Add the next pattern to the list.
  void AddNext(NamePattern *np) { m_next = np; }

private:
  // The pattern we are matching.  Not actually used after startup.
  const char *m_match;

  // The pattern we replace with.  Again not used other than diagnostics
  // after startup.
  const char *m_replace;

  // The number of elements in the arrays == characters to replace
  int m_conv_len;

  // The compiled regular expression for matching.
  regex_t m_match_regex;

  // The array of strings used for conversion.  For every character position
  // that is to be changed there will be a pointer in this array (NULL means
  // no change).  Each character in each string is a random character to be
  // used for replacement.  If a character happens to occur twice then it will
  // be twice as likely to occur in output.
  char **m_convert;

  // The length of the strings above.  Saves lots of strlen() calls.
  int *m_conv_item_len;

  // The next number to be used for each element
  int *m_next_value;

  // past the end of the sequence
  bool m_no_seq;

  // Pointer to the next pattern (or NULL).
  NamePattern *m_next;

  NamePattern(const NamePattern&);
  NamePattern & operator=(const NamePattern&);
};

#endif