1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
(c) 1995-2001 by Wilhelm Noeker (wnoeker@t-online.de)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
/*========================================================================*\
| File: strstuff.c Date: 20 Sep 1997 |
*------------------------------------------------------------------------*
| Some string functions, similar to those from the standard library |
| in <string.h>, but more flexible. |
| You must call str_setup() before using any of these functions !!! |
| |
\*========================================================================*/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "strstuff.h"
#define is_space(c) is_space[(unsigned char)(c)]
#define to_upper(c) to_upper[(unsigned char)(c)]
UBYTE to_upper[ 256 ]; /* conversion table */
int is_space[ 256 ]; /* lookup table */
int bordermode, case_sense;
/*
* Like strncpy(), but guarantees a null delimiter.
*/
UBYTE *str_ncpy( UBYTE *t, UBYTE *s, size_t n )
{
strncpy( t, s, n );
t[ n-1 ] = '\0';
return t;
}
/*
* Set up a to_upper[] table that can convert all national characters from
* the ISO Latin-1 charset to uppercase, not only 'a'-'z'.
* Depending on the global variable <case_sense> this table may just as
* well do nothing at all.
* Similarly, the is_space[] table which is built here may call anything
* but alphanumerics a 'space', depending on the setting of <bordermode>.
*/
void str_setup( int bm, int cs )
{
int c, d;
bordermode = bm;
case_sense = cs; /* set global variables */
for( c = 0; c < 256; c++ )
{
to_upper[ c ] = c;
is_space[ c ] = ( bordermode < 2 || (c & 0x7f) <= ' ' );
}
for( c = 'a'; c <= 'z'; c++ )
{
d = c + 'A' - 'a';
to_upper[ c ] = d;
is_space[ c ] = 0;
is_space[ d ] = 0;
}
for( c = '0'; c <= '9'; c++ )
{
is_space[ c ] = 0;
}
for( c = 224; c < 256; c++ )
if( c != 247 )
{ /* 247 is the division sign -:- */
d = c - 32;
is_space[ c ] = 0;
is_space[ d ] = 0;
if( c != 255 ) /* and don't convert '"y' to 'sz' : ) */
to_upper[ c ] = d;
}
if( case_sense ) /* destroy the to_upper[] table again */
for( c = 0; c < 256; c++ )
to_upper[ c ] = c;
is_space[ 0 ] = 0; /* important, we'll take advantage of this! */
}
/*
* str_cmp() will work like strcmp() if( case_sense && bordermode == 3 ),
* but can change its behaviour depending on those variables.
* Will also indicate by special (but legal) return values if <s> was an
* abbreviation of <t> or vice versa.
*/
int str_cmp( UBYTE *s, UBYTE *t )
{
if( bordermode < 3 )
{ /* modes where number of 'spaces' doesn't count */
while( is_space( *s ) )
s++; /* advance to first word */
while( is_space( *t ) )
t++;
while( to_upper( *s ) == to_upper( *t ) )
{
if( *s == '\0' )
return 0;
s++;
t++;
if( !bordermode || (is_space( *s ) && is_space( *t )) )
{
/* both at the end of a word OR in sloppy bordermode */
while( is_space( *s ) )
s++; /* skip the spaces */
while( is_space( *t ) )
t++;
}
}
}
else
{
while( to_upper( *s ) == to_upper( *t ) )
{
if( *s == '\0' )
return 0;
s++;
t++;
}
}
if( *s == '\0' )
return STR_SHORTER;
else if( *t == '\0' )
return STR_LONGER;
else
return( to_upper( *s ) - to_upper( *t ) );
}
/*
* The same as str_cmp(), but will only compare up to <n> characters.
* (Only two lines of code are different.)
*/
int str_ncmp( UBYTE *s, UBYTE *t, size_t n )
{
if( bordermode < 3 )
{
while( is_space( *s ) )
s++;
while( is_space( *t ) )
t++;
while( to_upper( *s ) == to_upper( *t ) )
{
if( --n == 0 || *s == '\0' )
return 0; /* that's the difference */
s++;
t++;
if( !bordermode || (is_space( *s ) && is_space( *t )) )
{
while( is_space( *s ) )
s++;
while( is_space( *t ) )
t++;
}
}
}
else
{
while( to_upper( *s ) == to_upper( *t ) )
{
if( --n == 0 || *s == '\0' )
return 0; /* and here once more */
s++;
t++;
}
}
if( *s == '\0' )
return STR_SHORTER;
else if( *t == '\0' )
return STR_LONGER;
else
return( to_upper( *s ) - to_upper( *t ) );
}
/*
* Find a copy of t in s, like strstr( )does.
* Note that bordermode has NO effect here!
*/
UBYTE *str_str( UBYTE *s, UBYTE *t )
{
int c;
while( *s )
{
c = str_cmp( s, t );
if( c == 0 || c == STR_LONGER )
return s;
s++;
}
return NULL;
}
/*
* Some user info printed to <stdout>.
*/
void print_strstat( void )
{
printf( "string processing:\n" );
printf( " upper-/lowercase: " );
if( case_sense )
printf( "strict\n" );
else
printf( "'ABC'='abc'\n" );
printf( " word delimiters: " );
switch( bordermode )
{
case 0:
printf( "'a,b c'='abc'\n" );
break;
case 1:
printf( "'a, b, c'='a,b c'\n" );
break;
case 2:
printf( "'a b c'='a b c'\n" );
break;
case 3:
printf( "strict\n" );
break;
}
}
|