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

example_cpp_1.cpp

This is an example of how to use the shwild::match() function in a C++ program.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_cpp_1.cpp
00003  *
00004  * Purpose:     Implementation file for the example_cpp_1 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 char  pattern[]   =   "abcd";
00087 
00088         assert(0 == shwild::match(pattern, "abcd", 0));
00089         assert(0 != shwild::match(pattern, "ABCD", 0));
00090         assert(0 == shwild::match(pattern, "ABCD", SHWILD_F_IGNORE_CASE));
00091 
00092         ((void)pattern);    /* Needed to silence false Borland warnings. */
00093     }
00094 
00095     /* Using wildcards. */
00096     {
00097         const char  pattern[]   =   "a*c?";
00098 
00099         assert(0 == shwild::match(pattern, "abcd", 0));
00100         assert(0 == shwild::match(pattern, "a*c?", 0));
00101         assert(0 == shwild::match(pattern, "abbbbbbbbcd", 0));
00102         assert(0 == shwild::match(pattern, "acd", 0));
00103         assert(0 != shwild::match(pattern, "abdc", 0));
00104         assert(0 == shwild::match(pattern, "abc?", 0));
00105 
00106         ((void)pattern);    /* Needed to silence false Borland warnings. */
00107     }
00108 
00109     /* Using escaped characters. */
00110     {
00111         const char  pattern[]   =   "a\\*c\\?";
00112 
00113         assert(0 != shwild::match(pattern, "abcd", 0));
00114         assert(0 == shwild::match(pattern, "a*c?", 0));
00115         assert(0 != shwild::match(pattern, "a\\*c\\?", 0));
00116         assert(0 != shwild::match(pattern, "abbbbbbbbcd", 0));
00117         assert(0 != shwild::match(pattern, "acd", 0));
00118         assert(0 != shwild::match(pattern, "abdc", 0));
00119         assert(0 != shwild::match(pattern, "abc?", 0));
00120 
00121         /* All of the following search for 'a' followed by '\\' followed by any
00122          * number of any character, following by '\\' followed by one of any
00123          * character.
00124          */
00125         assert(0 != shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE));
00126         assert(0 == shwild::match(pattern, "a\\*c\\?", SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE));
00127 
00128         ((void)pattern);    /* Needed to silence false Borland warnings. */
00129     }
00130 
00131     /* Matching ranges. */
00132     {
00133         const char  pattern[]   =   "a[bc]c[defghijklm]";
00134 
00135         assert(0 == shwild::match(pattern, "abcd", 0));
00136         assert(0 != shwild::match(pattern, "aacd", 0));
00137         assert(0 == shwild::match(pattern, "accm", 0));
00138         assert(0 != shwild::match(pattern, "abcn", 0));
00139         assert(0 != shwild::match(pattern, "a[bc]c[defghijklm]", 0));
00140 
00141         /* All of the following the given pattern as if it is a
00142          * literal string.
00143          */
00144         assert(0 != shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00145         assert(0 != shwild::match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00146         assert(0 != shwild::match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00147         assert(0 != shwild::match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00148         assert(0 == shwild::match(pattern, "a[bc]c[defghijklm]", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00149 
00150         ((void)pattern);    /* Needed to silence false Borland warnings. */
00151     }
00152 
00153     /* Matching ranges with continuum. */
00154     {
00155         const char  pattern[]   =   "a[b-c]c[d-m]";
00156 
00157         assert(0 == shwild::match(pattern, "abcd", 0));
00158         assert(0 != shwild::match(pattern, "aacd", 0));
00159         assert(0 == shwild::match(pattern, "accm", 0));
00160         assert(0 != shwild::match(pattern, "abcn", 0));
00161 
00162         /* All the following search for 'a' followed by 'b' or '-' or 'd',
00163          * followed by 'c' followed by 'd' or '-' or 'm'
00164          */
00165         assert(0 == shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00166         assert(0 == shwild::match(pattern, "a-cd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00167         assert(0 == shwild::match(pattern, "accd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00168         assert(0 != shwild::match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00169         assert(0 == shwild::match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00170         assert(0 != shwild::match(pattern, "accl", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00171         assert(0 != shwild::match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00172 
00173         ((void)pattern);    /* Needed to silence false Borland warnings. */
00174     }
00175 
00176     /* Matching ranges with high-low continuum. */
00177     {
00178         const char  pattern[]   =   "a[c-b]c[m-d]";
00179 
00180         assert(0 == shwild::match(pattern, "abcd", 0));
00181         assert(0 != shwild::match(pattern, "aacd", 0));
00182         assert(0 == shwild::match(pattern, "accm", 0));
00183         assert(0 != shwild::match(pattern, "abcn", 0));
00184 
00185         assert(0 != shwild::match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00186         assert(0 != shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00187         assert(0 != shwild::match(pattern, "accd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00188         assert(0 != shwild::match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00189         assert(0 != shwild::match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00190 
00191         ((void)pattern);    /* Needed to silence false Borland warnings. */
00192     }
00193 
00194     /* Matching ranges with cross-case continuum. */
00195     {
00196         const char  pattern[]   =   "a[b-C]c[d-M]";
00197 
00198         assert(0 == shwild::match(pattern, "abcd", 0));
00199         assert(0 != shwild::match(pattern, "aacd", 0));
00200         assert(0 == shwild::match(pattern, "aCcJ", 0));
00201         assert(0 != shwild::match(pattern, "abcn", 0));
00202 
00203         assert(0 != shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00204         assert(0 != shwild::match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00205         assert(0 != shwild::match(pattern, "aCcJ", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00206         assert(0 != shwild::match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00207 
00208         ((void)pattern);    /* Needed to silence false Borland warnings. */
00209     }
00210 
00211     /* Matching ranges with cross-case continuum. */
00212     {
00213         const char  pattern[]   =   "a[*]c[?]";
00214 
00215         assert(0 != shwild::match(pattern, "abcd", 0));
00216         assert(0 == shwild::match(pattern, "a*c?", 0));
00217         assert(0 != shwild::match(pattern, "abbbbbbbbcd", 0));
00218         assert(0 != shwild::match(pattern, "acd", 0));
00219         assert(0 != shwild::match(pattern, "abdc", 0));
00220         assert(0 != shwild::match(pattern, "abc?", 0));
00221 
00222         assert(0 != shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00223         assert(0 != shwild::match(pattern, "a*c?", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00224         assert(0 != shwild::match(pattern, "abbbbbbbbcd", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00225         assert(0 != shwild::match(pattern, "acd", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00226         assert(0 != shwild::match(pattern, "abdc", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00227         assert(0 != shwild::match(pattern, "abc?", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00228 
00229         ((void)pattern);    /* Needed to silence false Borland warnings. */
00230     }
00231 
00232     /* Matching ranges with continuum and leading/trailing hyphens. */
00233     {
00234         const char  pattern[]   =   "a[-a-c]c[d-]";
00235 
00236         assert(0 == shwild::match(pattern, "abcd", 0));
00237         assert(0 == shwild::match(pattern, "aacd", 0));
00238         assert(0 == shwild::match(pattern, "acc-", 0));
00239         assert(0 == shwild::match(pattern, "a-c-", 0));
00240         assert(0 != shwild::match(pattern, "abce", 0));
00241 
00242         assert(0 != shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00243         assert(0 != shwild::match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00244         assert(0 != shwild::match(pattern, "acc-", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00245         assert(0 != shwild::match(pattern, "a-c-", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00246         assert(0 != shwild::match(pattern, "abce", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00247 
00248         ((void)pattern);    /* Needed to silence false Borland warnings. */
00249     }
00250 
00251     /* Matching ranges with inverse continuum. */
00252     {
00253         const char  pattern[]   =   "a[b-c]c[^d-m]";
00254 
00255         assert(0 != shwild::match(pattern, "abcd", 0));
00256         assert(0 != shwild::match(pattern, "aacd", 0));
00257         assert(0 == shwild::match(pattern, "abcc", 0));
00258         assert(0 != shwild::match(pattern, "accm", 0));
00259         assert(0 == shwild::match(pattern, "abcn", 0));
00260 
00261         assert(0 == shwild::match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00262         assert(0 != shwild::match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00263         assert(0 != shwild::match(pattern, "abcc", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00264         assert(0 == shwild::match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00265         assert(0 != shwild::match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00266 
00267         ((void)pattern);    /* Needed to silence false Borland warnings. */
00268     }
00269 
00270     return EXIT_SUCCESS;
00271 }
00272 
00273 int main(int argc, char *argv[])
00274 {
00275     int             iRet;
00276 
00277 #if defined(_MSC_VER) && \
00278     defined(_DEBUG)
00279     _CrtMemState    memState;
00280 #endif /* _MSC_VER && _MSC_VER */
00281 
00282 #if defined(_MSC_VER) && \
00283     defined(_DEBUG)
00284     _CrtMemCheckpoint(&memState);
00285 #endif /* _MSC_VER && _MSC_VER */
00286 
00287     try
00288     {
00289         iRet = main_(argc, argv);
00290     }
00291     catch(std::exception &x)
00292     {
00293         cerr << "Unhandled error: " << x.what() << endl;
00294 
00295         iRet = EXIT_FAILURE;
00296     }
00297     catch(...)
00298     {
00299         cerr << "Unhandled unknown error" << endl;
00300 
00301         iRet = EXIT_FAILURE;
00302     }
00303 
00304 #if defined(_MSC_VER) && \
00305     defined(_DEBUG)
00306     _CrtMemDumpAllObjectsSince(&memState);
00307 #endif /* _MSC_VER) && _DEBUG */
00308 
00309     return iRet;
00310 }
00311 
00312 /* ////////////////////////////////////////////////////////////////////////// */

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