Install a pkg-config file.
[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 #ifdef __cplusplus
22 #include <stdexcept>
23 #include <string>
24 #include <vector>
25 #include <regex.h>
26
27 namespace Fides {
28         class regexp {
29                 regex_t comp;
30
31                 public:
32                 static const int EXTENDED = REG_EXTENDED;
33                 static const int ICASE = REG_ICASE;
34                 static const int NOSUB = REG_NOSUB;
35                 static const int NEWLINE = REG_NEWLINE;
36
37                 static const int NOTBOL = REG_NOTBOL;
38                 static const int NOTEOL = REG_NOTEOL;
39
40                 /// Construct a compiled regular expression.
41                 ///
42                 /// @param exp    Regular expression to compile.
43                 /// @param cflags Bitwise OR of options to apply when compiling the regular expression:
44                 ///               - Fides::regexp::EXTENDED
45                 ///                 Use POSIX Extended Regular Expression syntax when interpreting exp.
46                 ///               - Fides::regexp::ICASE
47                 ///                 Make the expression case-insensitive.
48                 ///               - Fides::regexp::NOSUB
49                 ///                 Disable support for substring addressing.
50                 ///               - Fides::regexp::NEWLINE
51                 ///                 Do not treat the newline character as the start or end of a line.
52                 regexp(const std::string &exp, int cflags = 0) {
53                         int err = regcomp(&comp, exp.c_str(), cflags);
54                         if(err)
55                                 throw std::runtime_error("Could not compile regular expression");
56                 }
57
58                 ~regexp() {
59                         regfree(&comp);
60                 }
61
62                 /// Test whether a string matches the regular expression.
63                 ///
64                 /// @param in     String to test.
65                 /// @param eflags Bitwise OR of options to apply when matching the string:
66                 ///               - Fides::regexp::NOTBOL
67                 ///                 Do not treat the start of the string as the start of a line.
68                 ///               - Fides::regexp::NOTEOL
69                 ///                 Do not treat the end of the string as the end of a line.
70                 /// @return True if the string matches the regular expression, false otherwise.
71                 bool match(const std::string &in, int eflags = 0) {
72                         return regexec(&comp, in.c_str(), 0, 0, eflags) == 0;
73                 }
74         };
75
76         std::string b64encode(const std::string &in);
77         std::string b64decode(const std::string &in);
78         std::string hexencode(const std::string &in);
79         std::string hexdecode(const std::string &in);
80
81         std::vector<std::string> dirlist(const std::string &path);
82 }
83
84 extern "C" {
85 #endif
86
87 extern char *fides_b64encode(const char *in);
88 extern char *fides_b64decode(const char *in);
89 extern char *fides_hexencode(const char *in);
90 extern char *fides_hexdecode(const char *in);
91
92 #ifdef __cplusplus
93 }
94 #endif
95
96 #endif