Back
1 //------------------------------------------------------------------------------
2 // Module: Regex.cpp //
3 // //
4 // Class which encapsulates a very simple regular expression parser //
5 // //
6 // Copyright (c) 2004 by Lars Haendel //
7 // Home: http://www.newty.de //
8 // //
9 // This program is free software; you can redistribute it and/or modify //
10 // it under the terms of the GNU General Public License as published by //
11 // the Free Software Foundation as version 2 of the License. //
12 // //
13 // This program is distributed in the hope that it will be useful, //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
16 // GNU General Public License for more details. //
17 // //
18 // You should have received a copy of the GNU General Public License //
19 // along with this program; if not, write to the Free Software //
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
21 // //
22 //------------------------------------------------------------------------------
23
24 #ifndef RegexH
25 #define RegexH
26
27 #define MAX_REGEX_LEN 64
28
29 //----------------------------------------------------------------------------------------------------------------------
30 // Examples:
31 // "[\4\D]" for sections in ini files like "[Options]"
32 // "#\4\4\4" for strings in Delphi like "#123" or "#012"
33 // "/*\8*/" for C/C++ comments like "/* This is *a* comment */"
34 //
35 // Codes:
36 // Cardinality | Alpha | Number | Whitespace | Code | Explanation
37 // ---------------------------------------------------------------------------------------------------------------------
38 // one | | | | \0 | one arbitrary character
39 // one | no | no | yes | \1 | one whitespace
40 // one | no | yes | no | \2 | one number
41 // one | no | yes | yes | \3 | one number or whitespace
42 // one | yes | no | no | \4 | one alpha
43 // one | yes | no | yes | \5 | one alpha or whitespace
44 // one | yes | yes | no | \6 | one alpha or number
45 // one | yes | yes | yes | \7 | one alpha or number or whitespace
46 // arbitrary | | | | \8 | arbitrary number of arbitrary characters
47 // arbitrary | no | no | yes | \9 | arbitrary number of whitespace
48 // arbitrary | no | yes | no | \A | arbitrary number of numbers
49 // arbitrary | no | yes | yes | \B | arbitrary number of numbers or whitespace
50 // arbitrary | yes | no | no | \C | arbitrary number of alpha
51 // arbitrary | yes | no | yes | \D | arbitrary number of alpha or whitespace
52 // arbitrary | yes | yes | no | \E | arbitrary number of alpha or numbers
53 // arbitrary | yes | yes | yes | \F | arbitrary number of alpha or numbers or whitespace
54 //
55 // Notes: Arbitrary means any number including zero. It's not allowed to have two arbitrary codes after eachother.
56
57 // function pointer, used for the elements a regex is splitted into
58 typedef int (*TFuncPtr)(const char*const& szString, const int& pos, const char& cChar, void* func);
59
60
61 //----------------------------------------------------------------------------------------------------------------------
62 class TRegex
63 {
64 public:
65 // constructor/destructor
66 TRegex();
67 ~TRegex();
68
69 // set regex
70 void SetRegex(const char*const& _szRegex);
71
72 // returns # of matching characters if passed string starts with stored regular expression, else returns 0
73 int StartsWithRegex(const char*const& szString) const;
74
75 const char* GetRegexString() const { return szRegex; };
76
77 private:
78
79 int nFuncs; // number of elements regex was splitted into
80 TFuncPtr* func; // regex elements
81 char* para; //
82 bool* f_Arbitrary; // flag: regex element is of type 'arbitrary'
83
84 char szRegex[MAX_REGEX_LEN];
85 };
86 #endif
Top |