1 /* fides.h - Light-weight, decentralised trust and authorisation management
2 Copyright (C) 2008-2009 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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/>.
18 #ifndef __FIDES_UTILITY_H__
19 #define __FIDES_UTILITY_H__
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;
36 static const int NOTBOL = REG_NOTBOL;
37 static const int NOTEOL = REG_NOTEOL;
39 /// Construct a compiled regular expression.
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);
54 throw std::runtime_error("Could not compile regular expression");
61 /// Test whether a string matches the regular expression.
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;
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);
80 std::vector<std::string> dirlist(const std::string &path);