5 // Statically link pcre on Windows
6 #if defined(TARGET_OS_WINDOWS) || defined(TARGET_OS_DOS)
14 // Do we still need to include sys/types.h?
15 #include <sys/types.h>
20 void *compile_pattern(const char *pattern
, bool ignore_case
= false);
21 void free_compiled_pattern(void *cp
);
22 bool pattern_match(void *compiled_pattern
, const char *text
, int length
);
24 // Globs are always available.
25 void *compile_glob_pattern(const char *pattern
, bool ignore_case
= false);
26 void free_compiled_glob_pattern(void *cp
);
27 bool glob_pattern_match(void *compiled_pattern
, const char *text
, int length
);
29 typedef void *(*p_compile
)(const char *pattern
, bool ignore_case
);
30 typedef void (*p_free
)(void *cp
);
31 typedef bool (*p_match
)(void *compiled_pattern
, const char *text
, int length
);
36 virtual ~base_pattern() { }
38 virtual bool valid() const = 0;
39 virtual bool matches(const std::string
&s
) const = 0;
42 template <p_compile pcomp
, p_free pfree
, p_match pmatch
>
43 class basic_text_pattern
: public base_pattern
46 basic_text_pattern(const std::string
&s
, bool icase
= false)
47 : pattern(s
), compiled_pattern(NULL
),
48 isvalid(true), ignore_case(icase
)
53 : pattern(), compiled_pattern(NULL
),
54 isvalid(false), ignore_case(false)
58 basic_text_pattern(const basic_text_pattern
&tp
)
61 compiled_pattern(NULL
),
63 ignore_case(tp
.ignore_case
)
70 pfree(compiled_pattern
);
73 const basic_text_pattern
&operator= (const basic_text_pattern
&tp
)
79 pfree(compiled_pattern
);
81 compiled_pattern
= NULL
;
83 ignore_case
= tp
.ignore_case
;
87 const basic_text_pattern
&operator= (const std::string
&spattern
)
89 if (pattern
== spattern
)
93 pfree(compiled_pattern
);
95 compiled_pattern
= NULL
;
97 // We don't change ignore_case
104 !!(compiled_pattern
= pcomp(pattern
.c_str(), ignore_case
))
110 return !pattern
.length();
116 && (compiled_pattern
|| (isvalid
= compile()));
119 bool matches(const char *s
, int length
) const
121 return valid() && pmatch(compiled_pattern
, s
, length
);
124 bool matches(const char *s
) const
126 return matches(std::string(s
));
129 bool matches(const std::string
&s
) const
131 return matches(s
.c_str(), s
.length());
134 const std::string
&tostring() const
141 mutable void *compiled_pattern
;
142 mutable bool isvalid
;
147 basic_text_pattern
<compile_pattern
,
148 free_compiled_pattern
, pattern_match
> text_pattern
;
151 basic_text_pattern
<compile_glob_pattern
,
152 free_compiled_glob_pattern
,
153 glob_pattern_match
> glob_pattern
;