| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 15 окт. 2020 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib 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 Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IO_PATHPATTERN_H_ |
| 23 |
#define LSP_PLUG_IN_IO_PATHPATTERN_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/io/Path.h> |
| 28 |
#include <lsp-plug.in/common/status.h> |
| 29 |
#include <lsp-plug.in/lltl/parray.h> |
| 30 |
#include <lsp-plug.in/lltl/darray.h> |
| 31 |
|
| 32 |
namespace lsp |
| 33 |
{ |
| 34 |
namespace io |
| 35 |
{ |
| 36 |
// |
| 37 |
// The pattern for the file name. |
| 38 |
// |
| 39 |
// Syntax: |
| 40 |
// * - Any character sequence except '/' or '\' |
| 41 |
// ? - Any character except '/' or '\' |
| 42 |
// / or \ - Path separator |
| 43 |
// `/ - Escaped character '/' (Available for escaping: '*', '?', '`', '/', '\', '&', '|', '(', ')' ) |
| 44 |
// name.ext - Strict match of characters to 'name.ext' |
| 45 |
// ... & ... - Conjunction of two conditions |
| 46 |
// ... | ... - Disjunction of two conditions |
| 47 |
// ! ... - Negotation of match |
| 48 |
// **/ or **\ - Any path |
| 49 |
// ( ... ) - Pattern group |
| 50 |
// |
| 51 |
// Example: |
| 52 |
// **/((*.c|*.h)&(test-*)) - matches any source/header C files for any subdirectory starting with 'test-' prefix |
| 53 |
class PathPattern |
| 54 |
{ |
| 55 |
public: |
| 56 |
enum flags |
| 57 |
{ |
| 58 |
INVERSE = 1 << 0, |
| 59 |
MATCH_CASE = 1 << 1, |
| 60 |
FULL_PATH = 1 << 2, |
| 61 |
|
| 62 |
NONE = 0 |
| 63 |
}; |
| 64 |
|
| 65 |
protected: |
| 66 |
enum command_t |
| 67 |
{ |
| 68 |
CMD_SEQUENCE, // cmd1 cmd2 ... |
| 69 |
CMD_AND, // ... & ... |
| 70 |
CMD_OR, // ... | ... |
| 71 |
CMD_PATTERN, // ... text ... |
| 72 |
CMD_ANY, // ... * ... |
| 73 |
CMD_ANYPATH, // ... **/ ... |
| 74 |
}; |
| 75 |
|
| 76 |
enum matcher_type_t |
| 77 |
{ |
| 78 |
M_PATTERN, |
| 79 |
M_ANY, |
| 80 |
M_ANYPATH, |
| 81 |
M_BOOL, |
| 82 |
M_SEQUENCE, |
| 83 |
M_BRUTE |
| 84 |
}; |
| 85 |
|
| 86 |
enum token_type_t |
| 87 |
{ |
| 88 |
T_GROUP_START, // ( |
| 89 |
T_IGROUP_START, // !( |
| 90 |
T_GROUP_END, // ) |
| 91 |
T_OR, // | |
| 92 |
T_AND, // & |
| 93 |
T_NOT, // ! |
| 94 |
T_TEXT, // text |
| 95 |
T_ANY, // * |
| 96 |
T_ANYPATH, // **/ |
| 97 |
T_EOF // <eof> |
| 98 |
}; |
| 99 |
|
| 100 |
typedef struct cmd_t |
| 101 |
{ |
| 102 |
command_t nCommand; // Command |
| 103 |
size_t nStart; // Start of character sequence |
| 104 |
size_t nLength; // Length of character sequence |
| 105 |
ssize_t nChars; // Actual length in characters (without escaping) |
| 106 |
bool bInverse; // Inverse condition flag |
| 107 |
lltl::parray<cmd_t> sChildren; |
| 108 |
} cmd_t; |
| 109 |
|
| 110 |
typedef struct tokenizer_t |
| 111 |
{ |
| 112 |
ssize_t nToken; |
| 113 |
|
| 114 |
const LSPString *pMask; |
| 115 |
size_t nPosition; |
| 116 |
size_t nStart; // Start position of token data |
| 117 |
size_t nLength; // Length of character sequence |
| 118 |
ssize_t nChars; // Actual length in characters (without escaping) |
| 119 |
} token_t; |
| 120 |
|
| 121 |
protected: |
| 122 |
struct matcher_t; |
| 123 |
|
| 124 |
typedef struct pos_t |
| 125 |
{ |
| 126 |
size_t start; |
| 127 |
size_t count; |
| 128 |
} pos_t; |
| 129 |
|
| 130 |
typedef bool (*match_match_t)(matcher_t *m, size_t start, size_t count); |
| 131 |
|
| 132 |
typedef struct matcher_t |
| 133 |
{ |
| 134 |
matcher_type_t type; // Type of matcher |
| 135 |
match_match_t match; |
| 136 |
|
| 137 |
const cmd_t *cmd; // Command |
| 138 |
const LSPString *pat; // Pattern data |
| 139 |
const LSPString *str; // Destination string to match |
| 140 |
size_t flags; // Flags |
| 141 |
} matcher_t; |
| 142 |
|
| 143 |
typedef struct any_matcher_t : public matcher_t |
| 144 |
{ |
| 145 |
ssize_t bad; // Index of last bad character |
| 146 |
ssize_t good; // Index of last good match |
| 147 |
} any_matcher_t; |
| 148 |
|
| 149 |
typedef struct bool_matcher_t : public matcher_t |
| 150 |
{ |
| 151 |
lltl::parray<matcher_t> cond; // List of additionally checked conditions |
| 152 |
} bool_matcher_t; |
| 153 |
|
| 154 |
typedef struct mregion_t |
| 155 |
{ |
| 156 |
size_t start; // Start of the region |
| 157 |
matcher_t *matcher; // Matcher assigned to the region |
| 158 |
const cmd_t *cmd; // Command |
| 159 |
} mregion_t; |
| 160 |
|
| 161 |
typedef struct sequence_matcher_t: public matcher_t |
| 162 |
{ |
| 163 |
size_t prefix; // Number of fixed prefixes |
| 164 |
size_t postfix; // Number of fixed postfixes |
| 165 |
lltl::darray<mregion_t> fixed; // Fixed text regions |
| 166 |
lltl::parray<matcher_t> var; // Variable text regions |
| 167 |
} sequence_matcher_t; |
| 168 |
|
| 169 |
typedef struct brute_matcher_t: public matcher_t |
| 170 |
{ |
| 171 |
lltl::darray<mregion_t> items; // Matching regions |
| 172 |
} brute_matcher_t; |
| 173 |
|
| 174 |
protected: |
| 175 |
LSPString sMask; |
| 176 |
cmd_t *pRoot; |
| 177 |
size_t nFlags; |
| 178 |
|
| 179 |
protected: |
| 180 |
status_t parse(const LSPString *pattern, size_t flags = NONE); |
| 181 |
bool match_full(const LSPString *path) const; |
| 182 |
|
| 183 |
static ssize_t get_token(tokenizer_t *it); |
| 184 |
static inline void next_token(tokenizer_t *it); |
| 185 |
static status_t parse_not(cmd_t **dst, tokenizer_t *it); |
| 186 |
static status_t parse_and(cmd_t **dst, tokenizer_t *it); |
| 187 |
static status_t parse_or(cmd_t **dst, tokenizer_t *it); |
| 188 |
static status_t parse_sequence(cmd_t **dst, tokenizer_t *it); |
| 189 |
static void destroy_cmd(cmd_t *cmd); |
| 190 |
static status_t merge_simple(cmd_t **out, command_t type, command_t cmd, tokenizer_t *it); |
| 191 |
static status_t merge_step(cmd_t **out, cmd_t *next, command_t type); |
| 192 |
static status_t merge_last(cmd_t **dst, cmd_t *out, cmd_t *next, ssize_t tok); |
| 193 |
|
| 194 |
static matcher_t *create_matcher(const matcher_t *src, const cmd_t *cmd); |
| 195 |
static bool add_range_matcher(sequence_matcher_t *sm, const pos_t *pos); |
| 196 |
static void destroy_matcher(matcher_t *match); |
| 197 |
|
| 198 |
static bool pattern_matcher_match(matcher_t *m, size_t start, size_t count); |
| 199 |
static bool any_matcher_match(matcher_t *m, size_t start, size_t count); |
| 200 |
static bool anypath_matcher_match(matcher_t *m, size_t start, size_t count); |
| 201 |
static bool and_matcher_match(matcher_t *m, size_t start, size_t count); |
| 202 |
static bool or_matcher_match(matcher_t *m, size_t start, size_t count); |
| 203 |
static bool sequence_matcher_match(matcher_t *m, size_t start, size_t count); |
| 204 |
static bool brute_matcher_match(matcher_t *m, size_t start, size_t count); |
| 205 |
|
| 206 |
static bool check_pattern_case(const lsp_wchar_t *pat, const lsp_wchar_t *s, size_t len); |
| 207 |
static bool check_pattern_nocase(const lsp_wchar_t *pat, const lsp_wchar_t *s, size_t len); |
| 208 |
static ssize_t seek_pattern_case(const lsp_wchar_t *pat, const lsp_wchar_t *s, size_t len, ssize_t rounds); |
| 209 |
static ssize_t seek_pattern_nocase(const lsp_wchar_t *pat, const lsp_wchar_t *s, size_t len, ssize_t rounds); |
| 210 |
|
| 211 |
static ssize_t sequence_check_prefix(sequence_matcher_t *sm, size_t start, size_t count); |
| 212 |
static ssize_t sequence_check_postfix(sequence_matcher_t *sm, size_t start, size_t count); |
| 213 |
static bool sequence_alloc_fixed(sequence_matcher_t *sm, size_t idx, size_t start, size_t count); |
| 214 |
static bool sequence_next_fixed(sequence_matcher_t *sm, size_t start, size_t count); |
| 215 |
static bool sequence_match_variable(sequence_matcher_t *sm, size_t start, size_t count); |
| 216 |
|
| 217 |
static bool brute_match_variable(brute_matcher_t *bm, size_t start, size_t count); |
| 218 |
static bool brute_next_variable(brute_matcher_t *bm, size_t start, size_t count); |
| 219 |
|
| 220 |
public: |
| 221 |
explicit PathPattern(); |
| 222 |
PathPattern(const PathPattern &) = delete; |
| 223 |
PathPattern(PathPattern &&) = delete; |
| 224 |
~PathPattern(); |
| 225 |
|
| 226 |
PathPattern & operator = (const PathPattern &) = delete; |
| 227 |
PathPattern & operator = (PathPattern &&) = delete; |
| 228 |
|
| 229 |
public: |
| 230 |
status_t set(const PathPattern *src) { return parse(&src->sMask, src->nFlags); } |
| 231 |
status_t set(const Path *pattern, size_t flags = NONE) { return parse(pattern->as_string(), flags);} |
| 232 |
status_t set(const LSPString *pattern, size_t flags = NONE) { return parse(pattern, flags); } |
| 233 |
status_t set(const char *pattern, size_t flags = NONE); |
| 234 |
|
| 235 |
inline const char *get_utf8() const { return sMask.get_utf8(); } |
| 236 |
inline const LSPString *get() const { return &sMask; } |
| 237 |
inline status_t get(LSPString *mask) const { return (mask != NULL) ? mask->set(&sMask) : STATUS_BAD_ARGUMENTS; } |
| 238 |
inline status_t get(Path *mask) const { return (mask != NULL) ? mask->set(&sMask) : STATUS_BAD_ARGUMENTS; } |
| 239 |
|
| 240 |
inline size_t flags() const { return nFlags; } |
| 241 |
size_t set_flags(size_t flags); |
| 242 |
|
| 243 |
inline status_t set_pattern(const PathPattern *src) { return set(&src->sMask, nFlags); } |
| 244 |
inline status_t set_pattern(const LSPString *pattern) { return set(pattern, nFlags); } |
| 245 |
inline status_t set_pattern(const Path *pattern) { return set(pattern, nFlags); } |
| 246 |
inline status_t set_pattern(const char *pattern) { return set(pattern, nFlags); } |
| 247 |
|
| 248 |
bool test(const char *path) const; |
| 249 |
bool test(const LSPString *path) const; |
| 250 |
bool test(const Path *path) const; |
| 251 |
|
| 252 |
void swap(PathPattern *dst); |
| 253 |
inline void swap(PathPattern &dst) { swap(&dst); } |
| 254 |
}; |
| 255 |
} /* namespace io */ |
| 256 |
} /* namespace lsp */ |
| 257 |
|
| 258 |
#endif /* LSP_PLUG_IN_IO_PATHPATTERN_H_ */ |