home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TIMEXSRC.ZIP
/
STRING.C
< prev
next >
Wrap
Text File
|
1990-03-29
|
7KB
|
334 lines
/* string.c -- String operations
December 1989 Mark E. Mallett
Copied from the FFS program.
This file contains routines dealing with character strings.
These include:
gettoken Extract a token from a string
newstr Make a new copy of a string
stricmp strcmp without case sensitivity
strnicmp strncmp without case sensitivity
strscmp strcmp that returns sign only.
tkline Tokenize a line
*/
#include <stdio.h>
#include <string.h>
#include <os2.h>
#include "timex.h"
/* Local Definitions */
#ifndef NUL
#define NUL '\0'
#endif /* NUL */
/* External data referenced */
/* External routines used */
extern char *malloc();
/* Local data publicly available */
/* Local routines and forward references */
/* Private data */
/*
*//* tkline( bufP, tbufP, tokcP, tokv, tokmax, vsepP, isepP )
Tokenize a line of text
Accepts :
bufP The buffer containing data to be tokenized
tbufP A buffer in which tokens will be placed
tokv Ptr to tokv array -- ptrs to tokens
tokmax Maximum number of tokens to return
vsepP, isepP Separators, as in call to gettoken() (refer there)
Returns :
<value> Number of tokens extracted
*tokv Ptrs to tokens
Notes :
Caller must insure that the token buffer is large enough to receive
all of the tokens.
The source and destination buffers may overlap, as long as the
source buffer's address is greater than the token buffer's. Thus,
&bufP[0] can be &tbufP[1] (this is a convenient arrangement).
*/
int
tkline( bufP, tbufP, tokv, tokmax, vsepP, isepP )
char *bufP; /* Buffer ptr */
char *tbufP; /* Token buffer pointer */
char **tokv; /* Arg vectors */
int tokmax; /* Max # tokens */
char *vsepP; /* Visible separators */
char *isepP; /* Invisible separators */
{
int tokC; /* # of tokens */
/* Extract the tokens */
for( tokC = 0;
( tokC < tokmax ) &&
( bufP = gettoken( bufP, tbufP, 9999, vsepP, isepP ) ) != NULL;
++tokC ) {
*tokv++ = tbufP;
tbufP += strlen( tbufP ) +1;
}
return( tokC );
}
/*
*//* gettoken( strP, tokP, tokmax, vsepP, isepP )
Extract a token from a string
Accepts :
strP Source string
tokP Buffer to contain the token
tokmax Maximum lenght of token (including NUL)
vsepP "visible" separators -- chars that will terminate
and will also be returned as single-character tokens;
isepP "invisible" separators -- chars that will terminate
a token and will be deleted from the source string.
Returns :
<value> Ptr to the rest of the string, or NULL if no
more tokens;
*tokP Token extracted
*/
char *
gettoken( strP, tokP, tokmax, vsepP, isepP )
char *strP; /* Source string */
char *tokP; /* Where to put token */
int tokmax; /* Max length of token */
char *vsepP; /* Visible separators */
char *isepP; /* Invisible separators */
{
char ch; /* Character */
char quote; /* Quoting character */
int tokX; /* Index into token string */
/* Trap NULL source string pointer */
if ( strP == NULL )
return( NULL );
/* Strip leading "invisible" separators */
for( ; ( ( ch = *strP ) != NUL ) && ( strchr( isepP, ch ) != NULL ); )
++strP;
/* Check end of character string */
if ( ch == NUL )
return( NULL );
/* Loop to get token characters */
for( quote = NUL, tokX = 0; ( ch = *strP ) != NUL; ++strP ) {
/* Check for escaped character -- only a quote or an escape
character may be quoted.
*/
if ( ( ch == '\\' ) &&
( ( strP[1] == '\\' ) ||
( strP[1] == '\"' ) ||
( strP[1] == '\'' )
)
)
ch = *++strP; /* Use the escaped char */
else {
/* Check for terminating quote */
if ( quote != NUL ) {
if ( ch == quote ) {
quote = NUL; /* No more quoted string */
continue; /* Try next character */
}
}
else {
/* Check for quoted character string */
if ( ( ch == '"' ) || ( ch == '\'' ) ) {
quote = ch; /* Remember quote char */
continue; /* Go for next char */
}
/* Check "visible" delimiters */
if ( strchr( vsepP, ch ) != NULL ) {
if ( tokX == 0 ) {
/* Only character of token */
tokP[tokX++] = ch;
++strP;
}
break; /* End of token */
}
/* Check "invisible" delimiters */
if ( strchr( isepP, ch ) != NULL ) {
++strP; /* Skip the invisible char */
break;
}
}
}
/* Add the character to the token */
if ( tokX < tokmax -1 )
tokP[tokX++] = ch;
}
tokP[tokX] = NUL;
return( strP );
}
/*
*//* newstr( facP, nameP, oldP )
Allocate a new copy of a string
Accepts :
facP Name of facility (e.g., subroutine name)
nameP Name of item being allocated
oldP Ptr to old string to duplicate
Returns :
< value > Ptr to new string
Notes :
error return is taken if allocation fails.
*/
char *
newstr( facP, nameP, oldP )
char *facP; /* Ptr to facility name */
char *nameP; /* Ptr to name of thing */
char *oldP; /* Ptr to existing string */
{
char *newP; /* Ptr to memory */
newP = (char *)emalloc( facP, nameP, strlen( oldP ) +1 );
strcpy( newP, oldP );
return( newP );
}
/*
*//* strscmp( str1P, str2P )
Like strcmp() but return just the sign.
Accepts :
str1P First string
str2P Second string
Returns :
<value> -1 if str1P < str2P (stringwise, of course)
0 if str1P == str2P
1 if str1P > str2P
Notes :
This mimics the "old" behaviour of strcmp().
*/
int
strscmp( str1P, str2P )
char *str1P; /* First string */
char *str2P; /* Second string */
{
int cmpval; /* Comparison value */
if ( ( cmpval = strcmp( str1P, str2P ) ) < 0 )
return( -1 );
else if ( cmpval == 0 )
return( 0 );
else
return( 1 );
}
/*
*//* stricmp() and strnicmp()
Like strcmp() and strncmp() but ignoring case. These may
not be present in the C library in use, in which case these
routines may be conditionally compiled.
*/
#ifndef HAVE_STRICMP
#include <ctype.h>
int
stricmp( s1, s2 )
register char *s1, *s2;
{
register unsigned char c1, c2;
for( ; ; ) {
c1 = *s1++;
c2 = *s2++;
if ( isupper( c1 ) )
c1 = tolower( c1 );
if ( isupper( c2 ) )
c2 = tolower( c2 );
if ( c1 < c2 )
return( -1 );
if ( c1 > c2 )
return( 1 );
if ( c1 == NUL )
return( 0 );
}
}
int
strnicmp( s1, s2, max )
register char *s1, *s2;
int max;
{
register unsigned char c1, c2;
for( ; max > 0; --max ) {
c1 = *s1++;
c2 = *s2++;
if ( isupper( c1 ) )
c1 = tolower( c1 );
if ( isupper( c2 ) )
c2 = tolower( c2 );
if ( c1 < c2 )
return( -1 );
if ( c1 > c2 )
return( 1 );
if ( c1 == NUL )
return( 0 );
}
return( 0 );
}
#endif /* HAVE_STRICMP */