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

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