| 1 | #ifndef GENUTILS_HPP |
| 2 | #define GENUTILS_HPP |
| 3 | |
| 4 | #include "common.hpp" |
| 5 | |
| 6 | #include <fstream> |
| 7 | #include <regex> |
| 8 | #include <set> |
| 9 | #include <sstream> |
| 10 | #include <string> |
| 11 | |
| 12 | class skip : public std::runtime_error |
| 13 | { |
| 14 | public: |
| 15 | static const int TODO = 0; |
| 16 | static const int OK = 0; |
| 17 | static const int INVALID = 1; |
| 18 | static const int IGNORE = 2; |
| 19 | int cause; |
| 20 | |
| 21 | skip(const std::string &reason, int _cause = INVALID) |
| 22 | : std::runtime_error(reason), cause(_cause) |
| 23 | {} |
| 24 | }; |
| 25 | |
| 26 | class ScopeGuard |
| 27 | { |
| 28 | private: |
| 29 | std::function<void()> cleanup_; |
| 30 | |
| 31 | public: |
| 32 | ScopeGuard(std::function<void()> &&cleanup) : cleanup_(std::move(cleanup)) {} |
| 33 | |
| 34 | ~ScopeGuard() noexcept(false) |
| 35 | { |
| 36 | #if __cplusplus >= 201703L |
| 37 | auto pending = std::uncaught_exceptions(); |
| 38 | #else |
| 39 | auto pending = std::uncaught_exception(); |
| 40 | #endif |
| 41 | try { |
| 42 | cleanup_(); |
| 43 | } catch (...) { |
| 44 | if (!pending) |
| 45 | throw; |
| 46 | } |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | class NamespaceGuard |
| 51 | { |
| 52 | typedef std::vector<std::string> ns_list; |
| 53 | |
| 54 | private: |
| 55 | ns_list ns_; |
| 56 | std::ostream &out_; |
| 57 | |
| 58 | void push(const ns_list &ns) |
| 59 | { |
| 60 | for (auto &&v : ns) { |
| 61 | out_ << "namespace " << v << " {" << std::endl << std::endl; |
| 62 | ns_.push_back(v); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public: |
| 67 | NamespaceGuard(std::ostream &_out) : out_(_out) {} |
| 68 | |
| 69 | void push(const std::string &ns, bool autodetect = true) |
| 70 | { |
| 71 | if (ns_.empty() && autodetect) { |
| 72 | ns_list l{GI_NS, GI_REPOSITORY_NS}; |
| 73 | if (ns == GI_NS) |
| 74 | l.pop_back(); |
| 75 | else if (ns != GI_REPOSITORY_NS) |
| 76 | l.push_back(ns); |
| 77 | push(l); |
| 78 | } else { |
| 79 | push(ns_list{ns}); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void pop(int count = -1) |
| 84 | { |
| 85 | while (!ns_.empty() && (count > 0 || count < 0)) { |
| 86 | out_ << "} // namespace"; |
| 87 | if (ns_.back().size()) |
| 88 | out_ << ' ' << ns_.back(); |
| 89 | out_ << std::endl << std::endl; |
| 90 | ns_.pop_back(); |
| 91 | --count; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ~NamespaceGuard() { pop(); } |
| 96 | }; |
| 97 | |
| 98 | class Matcher |
| 99 | { |
| 100 | std::regex pattern_; |
| 101 | |
| 102 | static std::vector<std::string> readfile(std::istream &input) |
| 103 | { |
| 104 | std::vector<std::string> result; |
| 105 | for (std::string line; std::getline(input, line);) { |
| 106 | result.emplace_back(line); |
| 107 | } |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | public: |
| 112 | Matcher(const std::vector<std::string> &paths, |
| 113 | const std::string &patterns = {}, |
| 114 | const std::function<void(const std::string &)> &custom = {}) |
| 115 | { |
| 116 | std::set<std::string> lines; |
| 117 | for (auto &fpath : paths) { |
| 118 | if (fpath.size()) { |
| 119 | std::ifstream input(fpath); |
| 120 | auto flines = readfile(input); |
| 121 | logger(Log::DEBUG, "read {} lines from {}", flines.size(), fpath); |
| 122 | std::copy( |
| 123 | flines.begin(), flines.end(), std::inserter(lines, lines.begin())); |
| 124 | } |
| 125 | } |
| 126 | if (patterns.size()) { |
| 127 | std::istringstream iss(patterns); |
| 128 | auto flines = readfile(iss); |
| 129 | logger(Log::DEBUG, "read {} data lines", flines.size()); |
| 130 | std::copy( |
| 131 | flines.begin(), flines.end(), std::inserter(lines, lines.begin())); |
| 132 | } |
| 133 | |
| 134 | std::string lpatterns; |
| 135 | for (auto &&l : lines) { |
| 136 | if (!l.empty() && l[0] != '#') { |
| 137 | // combine all expressions into 1 expression |
| 138 | // so we only need to call once to check for matching |
| 139 | lpatterns += (lpatterns.size() ? "|" : "") + l; |
| 140 | } else if (custom && l.size() > 2 && l[0] == '#' && l[1] == '!') { |
| 141 | custom(l); |
| 142 | } |
| 143 | } |
| 144 | if (lpatterns.size()) |
| 145 | pattern_ = std::regex(lpatterns, std::regex::optimize); |
| 146 | } |
| 147 | |
| 148 | static std::string format(const std::string &first, const std::string &second, |
| 149 | const std::string name) |
| 150 | { |
| 151 | return first + ":" + second + ":" + name; |
| 152 | } |
| 153 | |
| 154 | bool matches(const std::string &first, const std::string &second, |
| 155 | const std::vector<std::string> &options) const |
| 156 | { |
| 157 | for (auto &&c : options) { |
| 158 | // assemble string to match |
| 159 | auto find = format(first, second, c); |
| 160 | if (std::regex_match(find, pattern_)) |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | inline bool |
| 168 | is_const(const std::string &ctype) |
| 169 | { |
| 170 | return (ctype.find("const ") != ctype.npos) || ctype == "gconstpointer"; |
| 171 | } |
| 172 | |
| 173 | inline bool |
| 174 | is_volatile(const std::string &ctype) |
| 175 | { |
| 176 | return ctype.find("volatile ") != ctype.npos; |
| 177 | } |
| 178 | |
| 179 | inline int |
| 180 | get_pointer_depth(const std::string &ctype) |
| 181 | { |
| 182 | int pointer = ctype.find("gpointer") != ctype.npos || |
| 183 | ctype.find("gconstpointer") != ctype.npos; |
| 184 | // note that things like const gchar* const* are also possible |
| 185 | return std::count(ctype.begin(), ctype.end(), GI_PTR) + !!pointer; |
| 186 | } |
| 187 | |
| 188 | // check if @s is somehow special |
| 189 | // and mangle it a bit if so |
| 190 | std::string unreserve(const std::string &s, bool force = false); |
| 191 | |
| 192 | #endif // GENUTILS_HPP |