| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // TxLib generic wildcard handling |
|---|
| 34 | // |
|---|
| 35 | |
|---|
| 36 | #include <txlib.h> // TxLib interface |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /*****************************************************************************/ |
|---|
| 40 | // Function : count number of non-wildcard chars in string |
|---|
| 41 | /*****************************************************************************/ |
|---|
| 42 | int TxStrWcnt |
|---|
| 43 | ( |
|---|
| 44 | char *s |
|---|
| 45 | ) |
|---|
| 46 | { |
|---|
| 47 | int result = 0; |
|---|
| 48 | |
|---|
| 49 | if (!s) |
|---|
| 50 | return 0; |
|---|
| 51 | |
|---|
| 52 | do // start of DO loop |
|---|
| 53 | { |
|---|
| 54 | if ((*s != '*') && (*s != '?')) |
|---|
| 55 | { |
|---|
| 56 | result ++; |
|---|
| 57 | } |
|---|
| 58 | } while ( *( ++s) ); /* until '\0' */ |
|---|
| 59 | |
|---|
| 60 | return result; |
|---|
| 61 | } /* end TxStrWcnt */ |
|---|
| 62 | /*---------------------------------------------------------------------------*/ |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /*****************************************************************************/ |
|---|
| 66 | /* Function : wildcard compare of candidate to template string */ |
|---|
| 67 | /* check if template is a valid description of the candidate */ |
|---|
| 68 | /* wildcard characters are : '*' : matches zero or more of any */ |
|---|
| 69 | /* character in the candidate */ |
|---|
| 70 | /* '?' : matches exactly one of any */ |
|---|
| 71 | /* character in the candidate */ |
|---|
| 72 | /* except the '*' character */ |
|---|
| 73 | /* return value : negative if template is no valid description */ |
|---|
| 74 | /* */ |
|---|
| 75 | /* Note that the routine is case sensitive on candidate and template */ |
|---|
| 76 | /* Note that trailing spaces are considered as significant */ |
|---|
| 77 | /*****************************************************************************/ |
|---|
| 78 | int TxStrWcmp |
|---|
| 79 | ( |
|---|
| 80 | char *cs, // IN candidate string |
|---|
| 81 | char *ts // IN template string |
|---|
| 82 | ) |
|---|
| 83 | { |
|---|
| 84 | int dc = 0; // declare result variable |
|---|
| 85 | TXLN sub; // substring buffer |
|---|
| 86 | int l; // length of sub string |
|---|
| 87 | |
|---|
| 88 | while ((*ts != '\0') && (dc == 0)) |
|---|
| 89 | { |
|---|
| 90 | switch (*ts) |
|---|
| 91 | { |
|---|
| 92 | case '*': |
|---|
| 93 | do // skip adjacent wildcard |
|---|
| 94 | { // characters in template |
|---|
| 95 | if (*ts++ == '?') // need one candidate char |
|---|
| 96 | { |
|---|
| 97 | if (*cs++ == '\0') // end of candidate |
|---|
| 98 | { |
|---|
| 99 | dc--; // difference found ! |
|---|
| 100 | } |
|---|
| 101 | } // end if single wildcard |
|---|
| 102 | } while ((*ts == '*') || |
|---|
| 103 | (*ts == '?') ); // until non-wildcard |
|---|
| 104 | |
|---|
| 105 | if (dc == 0) |
|---|
| 106 | { |
|---|
| 107 | if (*ts == '\0') |
|---|
| 108 | { // signal '*' at end of |
|---|
| 109 | dc++; // template |
|---|
| 110 | } |
|---|
| 111 | else |
|---|
| 112 | { |
|---|
| 113 | l = strcspn(ts, "*?"); // next wildcard position |
|---|
| 114 | strncpy( sub, ts, l); |
|---|
| 115 | sub[ l] = '\0'; // terminate it |
|---|
| 116 | |
|---|
| 117 | if ((cs = strstr(cs, sub)) != NULL) |
|---|
| 118 | { |
|---|
| 119 | ts += l; // skip common substring |
|---|
| 120 | cs += l; |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | dc--; // difference found ! |
|---|
| 125 | } // end if |
|---|
| 126 | } // end if |
|---|
| 127 | } // end if no difference yet |
|---|
| 128 | break; |
|---|
| 129 | |
|---|
| 130 | case '?': |
|---|
| 131 | if (*cs++ != '*') |
|---|
| 132 | { |
|---|
| 133 | ts++; |
|---|
| 134 | } |
|---|
| 135 | else |
|---|
| 136 | { |
|---|
| 137 | dc--; // difference found ! |
|---|
| 138 | } |
|---|
| 139 | break; |
|---|
| 140 | |
|---|
| 141 | default: |
|---|
| 142 | if (*cs++ != *ts++) |
|---|
| 143 | { |
|---|
| 144 | dc--; // difference found ! |
|---|
| 145 | } |
|---|
| 146 | break; |
|---|
| 147 | } // end switch *template |
|---|
| 148 | } // end while in template |
|---|
| 149 | switch (dc) |
|---|
| 150 | { |
|---|
| 151 | case 0: |
|---|
| 152 | if ((*ts == '\0') && (cs != NULL) && (*cs != '\0')) |
|---|
| 153 | { |
|---|
| 154 | dc--; // candidate too long |
|---|
| 155 | } |
|---|
| 156 | break; |
|---|
| 157 | |
|---|
| 158 | case 1: |
|---|
| 159 | dc--; // template ends in '*' |
|---|
| 160 | break; // result is no-difference |
|---|
| 161 | |
|---|
| 162 | default: |
|---|
| 163 | break; |
|---|
| 164 | } // end switch |
|---|
| 165 | return dc; |
|---|
| 166 | } /* end TxStrWcmp */ |
|---|
| 167 | /*---------------------------------------------------------------------------*/ |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /*****************************************************************************/ |
|---|
| 171 | // Function : See FlStrwcmp; case-insensitive |
|---|
| 172 | /*****************************************************************************/ |
|---|
| 173 | int TxStrWicmp |
|---|
| 174 | ( |
|---|
| 175 | char *cs, // IN candidate string |
|---|
| 176 | char *ts // IN template string |
|---|
| 177 | ) |
|---|
| 178 | { |
|---|
| 179 | TX1K candi; // candidate string |
|---|
| 180 | TXLN templ; // template string |
|---|
| 181 | |
|---|
| 182 | return( TxStrWcmp( strupr(strcpy( candi, cs)), |
|---|
| 183 | strupr(strcpy( templ, ts)))); |
|---|
| 184 | } /* end TxStrWicmp */ |
|---|
| 185 | /*---------------------------------------------------------------------------*/ |
|---|