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

include/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: 30th April 2006
00008  *
00009  * Home:    http://shwild.org/
00010  *
00011  * Copyright (c) 2005-2006, 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     0
00054 # define SHWILD_VER_SHWILD_HPP_SHWILD_REVISION  2
00055 # define SHWILD_VER_SHWILD_HPP_SHWILD_EDIT      2
00056 #endif /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
00057 
00058 /* /////////////////////////////////////////////////////////////////////////////
00059  * Includes
00060  */
00061 
00062 #include <shwild/shwild.h>
00063 #include <stdexcept>
00064 
00065 /* /////////////////////////////////////////////////////////////////////////////
00066  * Namespace
00067  */
00068 
00069 #if !defined(SHWILD_NO_NAMESPACE)
00070 namespace shwild
00071 {
00072 #endif /* !SHWILD_NO_NAMESPACE */
00073 
00074 /* /////////////////////////////////////////////////////////////////////////////
00075  * Classes
00076  */
00077 
00078 // TODO: Flesh this out to be a full and complete exception class
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  * Implementation
00165  */
00166 
00167 #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
00168 
00169 inline /* static */ 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 /* static */ 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 /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
00253 
00254 /* /////////////////////////////////////////////////////////////////////////////
00255  * Namespace
00256  */
00257 
00258 #if !defined(SHWILD_NO_NAMESPACE)
00259 } // namespace shwild
00260 
00261 #endif /* !SHWILD_NO_NAMESPACE */
00262 
00263 /* ////////////////////////////////////////////////////////////////////////// */
00264 
00265 #endif /* !SHWILD_INCL_SHWILD_HPP_SHWILD */
00266 
00267 /* ////////////////////////////////////////////////////////////////////////// */

shwild Library documentation © Matthew Wilson and Sean Kelly, 2004-2006 SourceForge.net Logo