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
00041
00045 #ifndef SHWILD_INCL_SHWILD_HPP_SHWILD
00046 #define SHWILD_INCL_SHWILD_HPP_SHWILD
00047
00048
00049
00050
00051
00052 #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
00053 # define SHWILD_VER_SHWILD_HPP_SHWILD_MAJOR 1
00054 # define SHWILD_VER_SHWILD_HPP_SHWILD_MINOR 1
00055 # define SHWILD_VER_SHWILD_HPP_SHWILD_REVISION 4
00056 # define SHWILD_VER_SHWILD_HPP_SHWILD_EDIT 7
00057 #endif
00058
00059
00060
00061
00062
00063 #ifndef SHWILD_INCL_SHWILD_H_SHWILD
00064 # include <shwild/shwild.h>
00065 #endif
00066
00067 #include <stdexcept>
00068
00069
00070
00071
00072
00073 #ifdef __BORLANDC__
00074 # pragma warn -8026
00075 #endif
00076
00077
00078
00079
00080
00081 #if !defined(SHWILD_NO_NAMESPACE)
00082 namespace shwild
00083 {
00084 #endif
00085
00086
00087
00088
00089
00090
00093 class PatternException
00094 : public std::runtime_error
00095 {
00098 public:
00099 typedef std::runtime_error parent_class_type;
00100 typedef PatternException class_type;
00102
00105 public:
00107 PatternException(char const* message, int shwildErrorCode)
00108 : parent_class_type(message)
00109 , m_shwildErrorCode(shwildErrorCode)
00110 {}
00112
00115 public:
00117 virtual char const* what() const throw()
00118 {
00119 return "Pattern Exception";
00120 }
00122 int errorCode() const throw()
00123 {
00124 return m_shwildErrorCode;
00125 }
00127
00130 private:
00131 int m_shwildErrorCode;
00133 };
00134
00137 class Pattern
00138 {
00139 public:
00143 explicit Pattern(char const* pattern, unsigned flags = 0);
00147 explicit Pattern(slice_t const* pattern, unsigned flags = 0);
00151 explicit Pattern(slice_t const& pattern, unsigned flags = 0);
00153 ~Pattern();
00154
00155 public:
00157 bool match(char const* string) const;
00159 bool match(slice_t const* string) const;
00161 bool match(slice_t const& string) const;
00162
00163 public:
00170 int numMatched() const
00171 {
00172 return m_numMatches;
00173 }
00174
00175 private:
00176 static shwild_handle_t init_(char const* pattern, unsigned flags, int &numMatches);
00177 static shwild_handle_t init_(slice_t const* pattern, unsigned flags, int &numMatches);
00178
00179 private:
00180 shwild_handle_t m_hCompiledPattern;
00181 int m_numMatches;
00182
00183 private:
00184 Pattern(Pattern const&);
00185 Pattern &operator =(Pattern const&);
00186 };
00187
00188
00189
00190
00191
00192 #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
00193
00194 inline shwild_handle_t Pattern::init_(char const* pattern, unsigned flags, int &numMatches)
00195 {
00196 shwild_handle_t hCompiledPattern;
00197
00198 numMatches = shwild_compile_pattern(pattern, flags, &hCompiledPattern);
00199
00200 if(numMatches < 0)
00201 {
00202 hCompiledPattern = NULL;
00203
00204 throw PatternException("Failed to compile pattern", numMatches);
00205 }
00206
00207 return hCompiledPattern;
00208 }
00209
00210 inline shwild_handle_t Pattern::init_(slice_t const* pattern, unsigned flags, int &numMatches)
00211 {
00212 shwild_handle_t hCompiledPattern;
00213
00214 numMatches = shwild_compile_pattern_s(pattern, flags, &hCompiledPattern);
00215
00216 if(numMatches < 0)
00217 {
00218 hCompiledPattern = NULL;
00219
00220 throw PatternException("Failed to compile pattern", numMatches);
00221 }
00222
00223 return hCompiledPattern;
00224 }
00225
00226 inline Pattern::Pattern(char const* pattern, unsigned flags)
00227 : m_hCompiledPattern(init_(pattern, flags, m_numMatches))
00228 {}
00229
00230 inline Pattern::Pattern(slice_t const* pattern, unsigned flags)
00231 : m_hCompiledPattern(init_(pattern, flags, m_numMatches))
00232 {}
00233
00234 inline Pattern::Pattern(slice_t const& pattern, unsigned flags)
00235 : m_hCompiledPattern(init_(&pattern, flags, m_numMatches))
00236 {}
00237
00238 inline Pattern::~Pattern()
00239 {
00240 shwild_destroy_pattern(m_hCompiledPattern);
00241 }
00242
00243 inline bool Pattern::match(char const* string) const
00244 {
00245 int r = shwild_match_pattern(m_hCompiledPattern, string);
00246
00247 if(r < 0)
00248 {
00249 throw PatternException("Match failed", r);
00250 }
00251
00252 return 0 == r;
00253 }
00254
00255 inline bool Pattern::match(slice_t const* string) const
00256 {
00257 int r = shwild_match_pattern_s(m_hCompiledPattern, string);
00258
00259 if(r < 0)
00260 {
00261 throw PatternException("Match failed", r);
00262 }
00263
00264 return 0 == r;
00265 }
00266
00267 inline bool Pattern::match(slice_t const& string) const
00268 {
00269 int r = shwild_match_pattern_s(m_hCompiledPattern, &string);
00270
00271 if(r < 0)
00272 {
00273 throw PatternException("Match failed", r);
00274 }
00275
00276 return 0 == r;
00277 }
00278
00279 #endif
00280
00281
00282
00283
00284
00285 #if !defined(SHWILD_NO_NAMESPACE)
00286 }
00287
00288 #endif
00289
00290
00291
00292 #endif
00293
00294