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

example_cpp_2.cpp

This is an example of how to use the Pattern class in a C++ program.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_cpp_2.cpp
00003  *
00004  * Purpose:     Implementation file for the example_cpp_2 project.
00005  *
00006  * Created:     28th April 2006
00007  * Updated:     14th May 2006
00008  *
00009  * Status:      Wizard-generated
00010  *
00011  * License:     (Licensed under the Synesis Software Open License)
00012  *
00013  *              Copyright (c) 2006, Synesis Software Pty Ltd.
00014  *              All rights reserved.
00015  *
00016  *              www:        http://www.synesis.com.au/software
00017  *
00018  *              This source code is placed into the public domain 2006
00019  *              by Synesis Software Pty Ltd. There are no restrictions
00020  *              whatsoever to your use of the software. 
00021  *
00022  *              This source code is provided by Synesis Software Pty Ltd "as is"
00023  *              and any warranties, whether expressed or implied, including, but
00024  *              not limited to, the implied warranties of merchantability and
00025  *              fitness for a particular purpose are disclaimed. In no event
00026  *              shall the Synesis Software Pty Ltd be liable for any direct,
00027  *              indirect, incidental, special, exemplary, or consequential
00028  *              damages (including, but not limited to, procurement of
00029  *              substitute goods or services; loss of use, data, or profits; or
00030  *              business interruption) however caused and on any theory of
00031  *              liability, whether in contract, strict liability, or tort
00032  *              (including negligence or otherwise) arising in any way out of
00033  *              the use of this software, even if advised of the possibility of
00034  *              such damage. 
00035  *
00036  *              Neither the name of Synesis Software Pty Ltd nor the names of
00037  *              any subdivisions, employees or agents of Synesis Software Pty
00038  *              Ltd, nor the names of any other contributors to this software
00039  *              may be used to endorse or promote products derived from this
00040  *              software without specific prior written permission. 
00041  *
00042  * ////////////////////////////////////////////////////////////////////////// */
00043 
00044 
00045 /* shwild Header Files */
00046 #include <shwild/shwild.hpp>
00047 #include <shwild/implicit_link.h>
00048 
00049 /* cstring Header Files */
00050 #include <cstring/implicit_link.h>
00051 
00052 
00053 /* Standard C++ Header Files */
00054 #include <exception>
00055 #include <iostream>
00056 
00057 #if !defined(__WATCOMC__) && \
00058     (   !defined(_MSC_VER) || \
00059         _MSC_VER >= 1100)
00060 
00061 using std::cerr;
00062 using std::endl;
00063 
00064 #else /* ? __WATCOMC__ */
00065 namespace std
00066 {
00067     using ::exception;
00068 }
00069 #endif /* __WATCOMC__ */
00070 
00071 /* Standard C Header Files */
00072 #include <assert.h>
00073 #include <stdlib.h>
00074 
00075 #if defined(_MSC_VER) && \
00076     defined(_DEBUG)
00077 # include <crtdbg.h>
00078 #endif /* _MSC_VER) && _DEBUG */
00079 
00080 /* ////////////////////////////////////////////////////////////////////////// */
00081 
00082 static int main_(int /* argc */, char ** /*argv*/)
00083 {
00084     /* Matching literal strings. */
00085     {
00086         const shwild::Pattern   pattern1("abcd");
00087 
00088         assert(pattern1.match("abcd"));
00089         assert(!pattern1.match("ABCD"));
00090 
00091         const shwild::Pattern   pattern2("abcd", SHWILD_F_IGNORE_CASE);
00092 
00093         assert(pattern2.match("ABCD"));
00094     }
00095 
00096     /* Using wildcards. */
00097     {
00098         const shwild::Pattern   pattern("a*c?");
00099 
00100         assert(pattern.match("abcd"));
00101         assert(pattern.match("a*c?"));
00102         assert(pattern.match("abbbbbbbbcd"));
00103         assert(pattern.match("acd"));
00104         assert(!pattern.match("abdc"));
00105         assert(pattern.match("abc?"));
00106     }
00107 
00108     /* Using escaped characters. */
00109     {
00110         const shwild::Pattern   pattern1("a\\*c\\?");
00111 
00112         assert(!pattern1.match("abcd"));
00113         assert(pattern1.match("a*c?"));
00114         assert(!pattern1.match("abbbbbbbbcd"));
00115         assert(!pattern1.match("acd"));
00116         assert(!pattern1.match("abdc"));
00117         assert(!pattern1.match("abc?"));
00118 
00119         /* All of the following search for 'a' followed by '\\' followed by any
00120          * number of any character, following by '\\' followed by one of any
00121          * character.
00122          */
00123         const shwild::Pattern   pattern2("a\\*c\\?", SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE);
00124 
00125         assert(!pattern2.match("abcd"));
00126         assert(pattern2.match("a\\*c\\?"));
00127     }
00128 
00129     /* Matching ranges. */
00130     {
00131         const shwild::Pattern   pattern1("a[bc]c[defghijklm]");
00132 
00133         assert(pattern1.match("abcd"));
00134         assert(!pattern1.match("aacd"));
00135         assert(pattern1.match("accm"));
00136         assert(!pattern1.match("abcn"));
00137         assert(!pattern1.match("a[bc]c[defghijklm]"));
00138 
00139         /* All of the following the given pattern as if it is a
00140          * literal string.
00141          */
00142         const shwild::Pattern   pattern2("a[bc]c[defghijklm]", SHWILD_F_SUPPRESS_RANGE_SUPPORT);
00143 
00144         assert(!pattern2.match("abcd"));
00145         assert(!pattern2.match("aacd"));
00146         assert(!pattern2.match("accm"));
00147         assert(!pattern2.match("abcn"));
00148         assert(pattern2.match("a[bc]c[defghijklm]"));
00149     }
00150 
00151     /* Matching ranges with continuum. */
00152     {
00153         const shwild::Pattern   pattern1("a[b-c]c[d-m]");
00154 
00155         assert(pattern1.match("abcd"));
00156         assert(!pattern1.match("aacd"));
00157         assert(pattern1.match("accm"));
00158         assert(!pattern1.match("abcn"));
00159 
00160         const shwild::Pattern   pattern2("a[b-c]c[d-m]", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT);
00161 
00162         assert(pattern2.match("abcd"));
00163         assert(pattern2.match("a-cd"));
00164         assert(pattern2.match("accd"));
00165         assert(!pattern2.match("aacd"));
00166         assert(pattern2.match("accm"));
00167         assert(!pattern2.match("accl"));
00168         assert(!pattern2.match("abcn"));
00169     }
00170 
00171     /* Matching ranges with high-low continuum. */
00172     {
00173         const shwild::Pattern   pattern1("a[c-b]c[m-d]");
00174 
00175         assert(pattern1.match("abcd"));
00176         assert(!pattern1.match("aacd"));
00177         assert(pattern1.match("accm"));
00178         assert(!pattern1.match("abcn"));
00179 
00180         try
00181         {
00182             const shwild::Pattern   pattern2("a[c-b]c[m-d]", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT);
00183 
00184             assert(!"Should not get here, since the continuum is high->low");
00185 
00186             assert(!pattern2.match("aacd"));
00187             assert(!pattern2.match("abcd"));
00188             assert(!pattern2.match("accd"));
00189             assert(!pattern2.match("accm"));
00190             assert(!pattern2.match("abcn"));
00191         }
00192         catch(shwild::PatternException &)
00193         {
00194         }
00195     }
00196 
00197     /* Matching ranges with cross-case continuum. */
00198     {
00199         const shwild::Pattern   pattern1("a[b-C]c[d-M]");
00200 
00201         assert(pattern1.match("abcd"));
00202         assert(!pattern1.match("aacd"));
00203         assert(pattern1.match("aCcJ"));
00204         assert(!pattern1.match("abcn"));
00205 
00206         try
00207         {
00208             const shwild::Pattern   pattern2("a[b-C]c[d-M]", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT);
00209 
00210             assert(!"Should not get here, since the continuum is cross-case");
00211 
00212             assert(!pattern2.match("abcd"));
00213             assert(!pattern2.match("aacd"));
00214             assert(!pattern2.match("aCcJ"));
00215             assert(!pattern2.match("abcn"));
00216         }
00217         catch(shwild::PatternException &)
00218         {
00219         }
00220     }
00221 
00222     /* Matching ranges with cross-case continuum. */
00223     {
00224         const shwild::Pattern   pattern1("a[*]c[?]");
00225 
00226         assert(!pattern1.match("abcd"));
00227         assert(pattern1.match("a*c?"));
00228         assert(!pattern1.match("abbbbbbbbcd"));
00229         assert(!pattern1.match("acd"));
00230         assert(!pattern1.match("abdc"));
00231         assert(!pattern1.match("abc?"));
00232 
00233         try
00234         {
00235             const shwild::Pattern   pattern2("a[*]c[?]", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT);
00236 
00237             assert(!"Should not get here, since the range contains literal wildcards");
00238 
00239             assert(!pattern2.match("abcd"));
00240             assert(!pattern2.match("a*c?"));
00241             assert(!pattern2.match("abbbbbbbbcd"));
00242             assert(!pattern2.match("acd"));
00243             assert(!pattern2.match("abdc"));
00244             assert(!pattern2.match("abc?"));
00245         }
00246         catch(shwild::PatternException &)
00247         {
00248         }
00249     }
00250 
00251     /* Matching ranges with continuum and leading/trailing hyphens. */
00252     {
00253         const shwild::Pattern   pattern1("a[-a-c]c[d-]");
00254 
00255         assert(pattern1.match("abcd"));
00256         assert(pattern1.match("aacd"));
00257         assert(pattern1.match("acc-"));
00258         assert(pattern1.match("a-c-"));
00259         assert(!pattern1.match("abce"));
00260 
00261         try
00262         {
00263             const shwild::Pattern   pattern2("a[-a-c]c[d-]", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT);
00264 
00265             assert(!"Should not get here, since the range contains leading/trailing hyphens");
00266 
00267             assert(!pattern2.match("abcd"));
00268             assert(!pattern2.match("aacd"));
00269             assert(!pattern2.match("acc-"));
00270             assert(!pattern2.match("a-c-"));
00271             assert(!pattern2.match("abce"));
00272         }
00273         catch(shwild::PatternException &)
00274         {
00275         }
00276     }
00277 
00278     /* Matching ranges with inverse continuum. */
00279     {
00280         const shwild::Pattern   pattern1("a[b-c]c[^d-m]");
00281 
00282         assert(!pattern1.match("abcd"));
00283         assert(!pattern1.match("aacd"));
00284         assert(pattern1.match("abcc"));
00285         assert(!pattern1.match("accm"));
00286         assert(pattern1.match("abcn"));
00287 
00288         try
00289         {
00290             const shwild::Pattern   pattern2("a[b-c]c[^d-m]", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT);
00291 
00292             assert(!"Should not get here, since the range does not support ^");
00293 
00294             assert(pattern2.match("abcd"));
00295             assert(!pattern2.match("aacd"));
00296             assert(!pattern2.match("abcc"));
00297             assert(pattern2.match("accm"));
00298             assert(!pattern2.match("abcn"));
00299         }
00300         catch(shwild::PatternException &)
00301         {
00302         }
00303     }
00304 
00305     return EXIT_SUCCESS;
00306 }
00307 
00308 int main(int argc, char *argv[])
00309 {
00310     int             iRet;
00311 
00312 #if defined(_MSC_VER) && \
00313     defined(_DEBUG)
00314     _CrtMemState    memState;
00315 #endif /* _MSC_VER && _MSC_VER */
00316 
00317 #if defined(_MSC_VER) && \
00318     defined(_DEBUG)
00319     _CrtMemCheckpoint(&memState);
00320 #endif /* _MSC_VER && _MSC_VER */
00321 
00322     try
00323     {
00324         iRet = main_(argc, argv);
00325     }
00326     catch(std::exception &x)
00327     {
00328         cerr << "Unhandled error: " << x.what() << endl;
00329 
00330         iRet = EXIT_FAILURE;
00331     }
00332     catch(...)
00333     {
00334         cerr << "Unhandled unknown error" << endl;
00335 
00336         iRet = EXIT_FAILURE;
00337     }
00338 
00339 #if defined(_MSC_VER) && \
00340     defined(_DEBUG)
00341     _CrtMemDumpAllObjectsSince(&memState);
00342 #endif /* _MSC_VER) && _DEBUG */
00343 
00344     return iRet;
00345 }
00346 
00347 /* ////////////////////////////////////////////////////////////////////////// */

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