00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044 #ifndef SHWILD_INCL_SHWILD_HPP_SHWILD
00045 #define SHWILD_INCL_SHWILD_HPP_SHWILD
00046
00047
00048
00049
00050
00051 #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
00052 # define SHWILD_VER_SHWILD_HPP_SHWILD_MAJOR 1
00053 # define SHWILD_VER_SHWILD_HPP_SHWILD_MINOR 0
00054 # define SHWILD_VER_SHWILD_HPP_SHWILD_REVISION 2
00055 # define SHWILD_VER_SHWILD_HPP_SHWILD_EDIT 2
00056 #endif
00057
00058
00059
00060
00061
00062 #include <shwild/shwild.h>
00063 #include <stdexcept>
00064
00065
00066
00067
00068
00069 #if !defined(SHWILD_NO_NAMESPACE)
00070 namespace shwild
00071 {
00072 #endif
00073
00074
00075
00076
00077
00078
00081 class PatternException
00082 : public std::runtime_error
00083 {
00086 public:
00087 typedef std::runtime_error parent_class_type;
00088 typedef PatternException class_type;
00090
00093 public:
00095 PatternException(char const *message, int shwildErrorCode)
00096 : parent_class_type(message)
00097 , m_shwildErrorCode(shwildErrorCode)
00098 {}
00100
00103 public:
00105 virtual char const *what() const throw()
00106 {
00107 return "Pattern Exception";
00108 }
00110 int errorCode() const throw()
00111 {
00112 return m_shwildErrorCode;
00113 }
00115
00118 private:
00119 int m_shwildErrorCode;
00121 };
00122
00125 class Pattern
00126 {
00127 public:
00131 explicit Pattern(char const *pattern, unsigned flags = 0);
00135 explicit Pattern(slice_t const *pattern, unsigned flags = 0);
00139 explicit Pattern(slice_t const &pattern, unsigned flags = 0);
00141 ~Pattern();
00142
00143 public:
00145 bool match(char const *string) const;
00147 bool match(slice_t const *string) const;
00149 bool match(slice_t const &string) const;
00150
00151 private:
00152 static shwild_handle_t init_(char const *pattern, unsigned flags);
00153 static shwild_handle_t init_(slice_t const *pattern, unsigned flags);
00154
00155 private:
00156 shwild_handle_t m_hCompiledPattern;
00157
00158 private:
00159 Pattern(Pattern const &);
00160 Pattern &operator =(Pattern const &);
00161 };
00162
00163
00164
00165
00166
00167 #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
00168
00169 inline shwild_handle_t Pattern::init_(char const *pattern, unsigned flags)
00170 {
00171 shwild_handle_t hCompiledPattern;
00172 int r = shwild_compile_pattern(pattern, flags, &hCompiledPattern);
00173
00174 if(r < 0)
00175 {
00176 hCompiledPattern = NULL;
00177
00178 throw PatternException("Failed to compile pattern", r);
00179 }
00180
00181 return hCompiledPattern;
00182 }
00183
00184 inline shwild_handle_t Pattern::init_(slice_t const *pattern, unsigned flags)
00185 {
00186 shwild_handle_t hCompiledPattern;
00187 int r = shwild_compile_pattern_s(pattern, flags, &hCompiledPattern);
00188
00189 if(r < 0)
00190 {
00191 hCompiledPattern = NULL;
00192
00193 throw PatternException("Failed to compile pattern", r);
00194 }
00195
00196 return hCompiledPattern;
00197 }
00198
00199 inline Pattern::Pattern(char const *pattern, unsigned flags)
00200 : m_hCompiledPattern(init_(pattern, flags))
00201 {}
00202
00203 inline Pattern::Pattern(slice_t const *pattern, unsigned flags)
00204 : m_hCompiledPattern(init_(pattern, flags))
00205 {}
00206
00207 inline Pattern::Pattern(slice_t const &pattern, unsigned flags)
00208 : m_hCompiledPattern(init_(&pattern, flags))
00209 {}
00210
00211 inline Pattern::~Pattern()
00212 {
00213 shwild_destroy_pattern(m_hCompiledPattern);
00214 }
00215
00216 inline bool Pattern::match(char const *string) const
00217 {
00218 int r = shwild_match_pattern(m_hCompiledPattern, string);
00219
00220 if(r < 0)
00221 {
00222 throw PatternException("Match failed", r);
00223 }
00224
00225 return 0 == r;
00226 }
00227
00228 inline bool Pattern::match(slice_t const *string) const
00229 {
00230 int r = shwild_match_pattern_s(m_hCompiledPattern, string);
00231
00232 if(r < 0)
00233 {
00234 throw PatternException("Match failed", r);
00235 }
00236
00237 return 0 == r;
00238 }
00239
00240 inline bool Pattern::match(slice_t const &string) const
00241 {
00242 int r = shwild_match_pattern_s(m_hCompiledPattern, &string);
00243
00244 if(r < 0)
00245 {
00246 throw PatternException("Match failed", r);
00247 }
00248
00249 return 0 == r;
00250 }
00251
00252 #endif
00253
00254
00255
00256
00257
00258 #if !defined(SHWILD_NO_NAMESPACE)
00259 }
00260
00261 #endif
00262
00263
00264
00265 #endif
00266
00267