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

example_c_1.c

This is an example of how to use the shwild_match() function in a C program.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_1.c
00003  *
00004  * Purpose:     Implementation file for the example_c_1 project.
00005  *
00006  * Created:     27th 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.h>
00047 #include <shwild/implicit_link.h>
00048 
00049 /* cstring Header Files */
00050 #include <cstring/implicit_link.h>
00051 
00052 /* Standard C Header Files */
00053 #include <assert.h>
00054 #include <stdio.h>
00055 #include <stdlib.h>
00056 
00057 /* ////////////////////////////////////////////////////////////////////////// */
00058 
00059 int main(void)
00060 {
00061     /* Matching literal strings. */
00062     {
00063         const char  pattern[]   =   "abcd";
00064 
00065         assert(0 == shwild_match(pattern, "abcd", 0));
00066         assert(0 != shwild_match(pattern, "ABCD", 0));
00067         assert(0 == shwild_match(pattern, "ABCD", SHWILD_F_IGNORE_CASE));
00068 
00069         ((void)pattern);    /* Needed to silence false Borland warnings. */
00070     }
00071 
00072     /* Using wildcards. */
00073     {
00074         const char  pattern[]   =   "a*c?";
00075 
00076         assert(0 == shwild_match(pattern, "abcd", 0));
00077         assert(0 == shwild_match(pattern, "a*c?", 0));
00078         assert(0 == shwild_match(pattern, "abbbbbbbbcd", 0));
00079         assert(0 == shwild_match(pattern, "acd", 0));
00080         assert(0 != shwild_match(pattern, "abdc", 0));
00081         assert(0 == shwild_match(pattern, "abc?", 0));
00082 
00083         ((void)pattern);    /* Needed to silence false Borland warnings. */
00084     }
00085 
00086     /* Using escaped characters. */
00087     {
00088         const char  pattern[]   =   "a\\*c\\?";
00089 
00090         assert(0 != shwild_match(pattern, "abcd", 0));
00091         assert(0 == shwild_match(pattern, "a*c?", 0));
00092         assert(0 != shwild_match(pattern, "a\\*c\\?", 0));
00093         assert(0 != shwild_match(pattern, "abbbbbbbbcd", 0));
00094         assert(0 != shwild_match(pattern, "acd", 0));
00095         assert(0 != shwild_match(pattern, "abdc", 0));
00096         assert(0 != shwild_match(pattern, "abc?", 0));
00097 
00098         /* All of the following search for 'a' followed by '\\' followed by any
00099          * number of any character, following by '\\' followed by one of any
00100          * character.
00101          */
00102         assert(0 != shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE));
00103         assert(0 == shwild_match(pattern, "a\\*c\\?", SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE));
00104 
00105         ((void)pattern);    /* Needed to silence false Borland warnings. */
00106     }
00107 
00108     /* Matching ranges. */
00109     {
00110         const char  pattern[]   =   "a[bc]c[defghijklm]";
00111 
00112         assert(0 == shwild_match(pattern, "abcd", 0));
00113         assert(0 != shwild_match(pattern, "aacd", 0));
00114         assert(0 == shwild_match(pattern, "accm", 0));
00115         assert(0 != shwild_match(pattern, "abcn", 0));
00116         assert(0 != shwild_match(pattern, "a[bc]c[defghijklm]", 0));
00117 
00118         /* All of the following the given pattern as if it is a
00119          * literal string.
00120          */
00121         assert(0 != shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00122         assert(0 != shwild_match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00123         assert(0 != shwild_match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00124         assert(0 != shwild_match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00125         assert(0 == shwild_match(pattern, "a[bc]c[defghijklm]", SHWILD_F_SUPPRESS_RANGE_SUPPORT));
00126 
00127         ((void)pattern);    /* Needed to silence false Borland warnings. */
00128     }
00129 
00130     /* Matching ranges with continuum. */
00131     {
00132         const char  pattern[]   =   "a[b-c]c[d-m]";
00133 
00134         assert(0 == shwild_match(pattern, "abcd", 0));
00135         assert(0 != shwild_match(pattern, "aacd", 0));
00136         assert(0 == shwild_match(pattern, "accm", 0));
00137         assert(0 != shwild_match(pattern, "abcn", 0));
00138 
00139         /* All the following search for 'a' followed by 'b' or '-' or 'd',
00140          * followed by 'c' followed by 'd' or '-' or 'm'
00141          */
00142         assert(0 == shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00143         assert(0 == shwild_match(pattern, "a-cd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00144         assert(0 == shwild_match(pattern, "accd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00145         assert(0 != shwild_match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00146         assert(0 == shwild_match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00147         assert(0 != shwild_match(pattern, "accl", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00148         assert(0 != shwild_match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT));
00149 
00150         ((void)pattern);    /* Needed to silence false Borland warnings. */
00151     }
00152 
00153     /* Matching ranges with high-low continuum. */
00154     {
00155         const char  pattern[]   =   "a[c-b]c[m-d]";
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         assert(0 != shwild_match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00163         assert(0 != shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00164         assert(0 != shwild_match(pattern, "accd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00165         assert(0 != shwild_match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00166         assert(0 != shwild_match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT));
00167 
00168         ((void)pattern);    /* Needed to silence false Borland warnings. */
00169     }
00170 
00171     /* Matching ranges with cross-case continuum. */
00172     {
00173         const char  pattern[]   =   "a[b-C]c[d-M]";
00174 
00175         assert(0 == shwild_match(pattern, "abcd", 0));
00176         assert(0 != shwild_match(pattern, "aacd", 0));
00177         assert(0 == shwild_match(pattern, "aCcJ", 0));
00178         assert(0 != shwild_match(pattern, "abcn", 0));
00179 
00180         assert(0 != shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00181         assert(0 != shwild_match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00182         assert(0 != shwild_match(pattern, "aCcJ", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00183         assert(0 != shwild_match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT));
00184 
00185         ((void)pattern);    /* Needed to silence false Borland warnings. */
00186     }
00187 
00188     /* Matching ranges with cross-case continuum. */
00189     {
00190         const char  pattern[]   =   "a[*]c[?]";
00191 
00192         assert(0 != shwild_match(pattern, "abcd", 0));
00193         assert(0 == shwild_match(pattern, "a*c?", 0));
00194         assert(0 != shwild_match(pattern, "abbbbbbbbcd", 0));
00195         assert(0 != shwild_match(pattern, "acd", 0));
00196         assert(0 != shwild_match(pattern, "abdc", 0));
00197         assert(0 != shwild_match(pattern, "abc?", 0));
00198 
00199         assert(0 != shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00200         assert(0 != shwild_match(pattern, "a*c?", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00201         assert(0 != shwild_match(pattern, "abbbbbbbbcd", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00202         assert(0 != shwild_match(pattern, "acd", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00203         assert(0 != shwild_match(pattern, "abdc", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00204         assert(0 != shwild_match(pattern, "abc?", SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT));
00205 
00206         ((void)pattern);    /* Needed to silence false Borland warnings. */
00207     }
00208 
00209     /* Matching ranges with continuum and leading/trailing hyphens. */
00210     {
00211         const char  pattern[]   =   "a[-a-c]c[d-]";
00212 
00213         assert(0 == shwild_match(pattern, "abcd", 0));
00214         assert(0 == shwild_match(pattern, "aacd", 0));
00215         assert(0 == shwild_match(pattern, "acc-", 0));
00216         assert(0 == shwild_match(pattern, "a-c-", 0));
00217         assert(0 != shwild_match(pattern, "abce", 0));
00218 
00219         assert(0 != shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00220         assert(0 != shwild_match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00221         assert(0 != shwild_match(pattern, "acc-", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00222         assert(0 != shwild_match(pattern, "a-c-", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00223         assert(0 != shwild_match(pattern, "abce", SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT));
00224 
00225         ((void)pattern);    /* Needed to silence false Borland warnings. */
00226     }
00227 
00228     /* Matching ranges with inverse continuum. */
00229     {
00230         const char  pattern[]   =   "a[b-c]c[^d-m]";
00231 
00232         assert(0 != shwild_match(pattern, "abcd", 0));
00233         assert(0 != shwild_match(pattern, "aacd", 0));
00234         assert(0 == shwild_match(pattern, "abcc", 0));
00235         assert(0 != shwild_match(pattern, "accm", 0));
00236         assert(0 == shwild_match(pattern, "abcn", 0));
00237 
00238         assert(0 == shwild_match(pattern, "abcd", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00239         assert(0 != shwild_match(pattern, "aacd", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00240         assert(0 != shwild_match(pattern, "abcc", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00241         assert(0 == shwild_match(pattern, "accm", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00242         assert(0 != shwild_match(pattern, "abcn", SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT));
00243 
00244         ((void)pattern);    /* Needed to silence false Borland warnings. */
00245     }
00246 
00247     return EXIT_SUCCESS;
00248 }
00249 
00250 /* ////////////////////////////////////////////////////////////////////////// */

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