Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

shwild/shwild.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:    shwild/shwild.hpp
00003  *
00004  * Purpose: C++ root file for the shwild C-API
00005  *
00006  * Created: 17th June 2005
00007  * Updated: 8th March 2008
00008  *
00009  * Home:    http://shwild.org/
00010  *
00011  * Copyright (c) 2005-2008, Matthew Wilson and Sean Kelly
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the names of Matthew Wilson and Sean Kelly nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * ////////////////////////////////////////////////////////////////////////// */
00039 
00040 
00044 #ifndef SHWILD_INCL_SHWILD_HPP_SHWILD
00045 #define SHWILD_INCL_SHWILD_HPP_SHWILD
00046 
00047 /* /////////////////////////////////////////////////////////////////////////////
00048  * Version information
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 /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
00057 
00058 /* /////////////////////////////////////////////////////////////////////////////
00059  * Includes
00060  */
00061 
00062 #ifndef SHWILD_INCL_SHWILD_H_SHWILD
00063 # include <shwild/shwild.h>
00064 #endif /* !SHWILD_INCL_SHWILD_H_SHWILD */
00065 
00066 #include <stdexcept>
00067 
00068 /* /////////////////////////////////////////////////////////////////////////////
00069  * Compiler warnings
00070  */
00071 
00072 #ifdef __BORLANDC__
00073 # pragma warn -8026 /* Suppresses "Functions with exception specifications are not expanded inline" */
00074 #endif /* compiler */
00075 
00076 /* /////////////////////////////////////////////////////////////////////////////
00077  * Namespace
00078  */
00079 
00080 #if !defined(SHWILD_NO_NAMESPACE)
00081 namespace shwild
00082 {
00083 #endif /* !SHWILD_NO_NAMESPACE */
00084 
00085 /* /////////////////////////////////////////////////////////////////////////////
00086  * Classes
00087  */
00088 
00089 // TODO: Flesh this out to be a full and complete exception class
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  * Implementation
00189  */
00190 
00191 #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
00192 
00193 inline /* static */ 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 /* static */ 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 /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
00279 
00280 /* /////////////////////////////////////////////////////////////////////////////
00281  * Namespace
00282  */
00283 
00284 #if !defined(SHWILD_NO_NAMESPACE)
00285 } // namespace shwild
00286 
00287 #endif /* !SHWILD_NO_NAMESPACE */
00288 
00289 /* ////////////////////////////////////////////////////////////////////////// */
00290 
00291 #endif /* !SHWILD_INCL_SHWILD_HPP_SHWILD */
00292 
00293 /* ////////////////////////////////////////////////////////////////////////// */

b64 Library documentation © Synesis Software Pty Ltd, 2004-2007