Clean up the namespace.
[fides] / lib / utility.h
1 /* fides.h - Light-weight, decentralised trust and authorisation management
2    Copyright (C) 2008-2009  Guus Sliepen <guus@tinc-vpn.org>
3   
4    Fides is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 2.1 of
7    the License, or (at your option) any later version.
8   
9    Fides is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU Lesser General Public License for more details.
13   
14    You should have received a copy of the GNU Lesser General Public
15    License along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef __FIDES_UTILITY_H__
19 #define __FIDES_UTILITY_H__
20
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 #include <regex.h>
25
26 namespace Fides {
27         class regexp {
28                 regex_t comp;
29
30                 public:
31                 static const int EXTENDED = REG_EXTENDED;
32                 static const int ICASE = REG_ICASE;
33                 static const int NOSUB = REG_NOSUB;
34                 static const int NEWLINE = REG_NEWLINE;
35
36                 static const int NOTBOL = REG_NOTBOL;
37                 static const int NOTEOL = REG_NOTEOL;
38
39                 /// Construct a compiled regular expression.
40                 ///
41                 /// @param exp    Regular expression to compile.
42                 /// @param cflags Bitwise OR of options to apply when compiling the regular expression:
43                 ///               - Fides::regexp::EXTENDED
44                 ///                 Use POSIX Extended Regular Expression syntax when interpreting exp.
45                 ///               - Fides::regexp::ICASE
46                 ///                 Make the expression case-insensitive.
47                 ///               - Fides::regexp::NOSUB
48                 ///                 Disable support for substring addressing.
49                 ///               - Fides::regexp::NEWLINE
50                 ///                 Do not treat the newline character as the start or end of a line.
51                 regexp(const std::string &exp, int cflags = 0) {
52                         int err = regcomp(&comp, exp.c_str(), cflags);
53                         if(err)
54                                 throw std::runtime_error("Could not compile regular expression");
55                 }
56
57                 ~regexp() {
58                         regfree(&comp);
59                 }
60
61                 /// Test whether a string matches the regular expression.
62                 ///
63                 /// @param in     String to test.
64                 /// @param eflags Bitwise OR of options to apply when matching the string:
65                 ///               - Fides::regexp::NOTBOL
66                 ///                 Do not treat the start of the string as the start of a line.
67                 ///               - Fides::regexp::NOTEOL
68                 ///                 Do not treat the end of the string as the end of a line.
69                 /// @return True if the string matches the regular expression, false otherwise.
70                 bool match(const std::string &in, int eflags = 0) {
71                         return regexec(&comp, in.c_str(), 0, 0, eflags) == 0;
72                 }
73         };
74
75         std::string b64encode(const std::string &in);
76         std::string b64decode(const std::string &in);
77         std::string hexencode(const std::string &in);
78         std::string hexdecode(const std::string &in);
79
80         std::vector<std::string> dirlist(const std::string &path);
81 }
82
83 #endif