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