Files
svc.ems/plat/public/src/pub_str.c
2024-09-27 15:39:34 +08:00

978 lines
25 KiB
C

//////////////////////////////////////////////////
//Title : wxc_str.c
//Auhtor : Liu Wei
//Desc : wxc2 string library
//Created : 2007-06-20
//Revision :
//
//Revision :
//
//////////////////////////////////////////////////
#include "./include/pub_str.h"
/*@ignore@*/
///////////////////////////////////////////////////////////////////////////
// Name : StrInc
// Function : test string is included in string "pStr"or not
//////////////////////////////////////////////////////////////////////////
inline char *StrInc ( char *pIncStr, char *pStr )
{
int m;
register char *pCh;
register int n;
m = strlen ( pIncStr );
n = strlen ( pStr );
for ( pCh = pStr; !IsChNull(*pCh) && ( n >= m ); pCh++, n-- )
{
if ( !strncmp ( pIncStr, pCh, m ) )
{
return ( pCh );
}
}
return ( NULL );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrIns
// Function : Insert string "pStrIns" to string "pSrc" , at the position of
// nPos, the nBufLen of string "pSrc" , is not enought , rerurn
// NULL, nor return pSrc
///////////////////////////////////////////////////////////////////////////
inline char *StrIns ( int nPos, const char *pStrIns, char *pSrc, int nBufLen )
{
register int i;
int nLen;
nLen = strlen ( pStrIns );
if ( nBufLen < nLen + strlen ( pSrc ) )
{
return NULL;
}
*(pSrc+nLen+strlen ( pSrc )) = CNULL;
for ( i = strlen ( pSrc ); i >= nPos; i-- )
{
*( pSrc + nLen + i ) = *( pSrc + i );
}
for ( i = 0; i < nLen; i++ )
{
*( pSrc + nPos + i ) = *( pStrIns + i );
}
return ( pSrc );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrDel
// Function : Delete string "pStrDel" in string "pStr" , If pStrDel is not
// exist, rerurn NULL, nor return pSrc
///////////////////////////////////////////////////////////////////////////
inline char *StrDel ( char *pStrDel, char *pStr )
{
char *pCh, *pPos;
pCh = strstr ( pStr, pStrDel );
if ( !pCh )
{
return ( NULL );
}
pPos = pCh + strlen ( pStrDel );
strcpy ( pCh, pPos );
return ( pStr );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrRepCh
// Function : Replace char cSrc in the string pStr with cDst
// Return : return the replace counter
///////////////////////////////////////////////////////////////////////////
inline int StrRepCh ( char *pStr, char cSrc, char cDst )
{
int nReplaced = 0;
register char *pCh;
for ( pCh = pStr; !IsChNull ( *pCh ); pCh++ )
{
if ( cSrc == *pCh )
{
*pCh = cDst;
nReplaced++;
}
}
return ( nReplaced );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrRepStr
// Function : Replace the first string "pSrc" in string "pStr" with "pDst" ,
// If pSrc is not exist or the nBuffLen of pStr is not enought
// to insert pDst , rerurn NULL, nor return new pSrc
///////////////////////////////////////////////////////////////////////////
inline char *StrRepStr ( char *pStr, char *pSrc, char *pDst, int nBuffLen )
{
char *pCh = NULL;
int nPos;
pCh = strstr ( pSrc, pStr);
if ( !IsNull(pCh) )
{
StrDel ( pSrc, pStr );
nPos = pCh - pStr;
if (NULL == StrIns ( nPos, pDst, pStr, nBuffLen ))
pCh = NULL;
else
pCh = pStr;
}
return ( pCh );
}
/*
inline char *StrRepStr ( char *pStr, char *pSrc, char *pDst, int nBuffLen )
{
char *pCh = NULL;
int nPos;
int SrcLen;
int DstLen;
SrcLen = strlen(pSrc);
DstLen = strlen(pDst);
pCh = strstr ( pStr, pSrc);
if ( (!IsNull(pCh)) && (nBuffLen >DstLen + strlen ( pStr ) - SrcLen))
{
StrDel ( pSrc, pStr );
nPos = pCh - pStr;
if (NULL == StrIns ( nPos, pDst, pStr, nBuffLen ))
pCh =NULL;
else
pCh = pStr;
}
else
pCh =NULL;
return ( pCh );
}
*/
///////////////////////////////////////////////////////////////////////////
// Name : StrChg
// Function : Replace all the string "pSrc" in string "pStr" with "pDst" ,
// If pSrc is not exist or the nBuffLen of pStr is not enought
// to insert pDst , rerurn NULL, nor return new pSrc
///////////////////////////////////////////////////////////////////////////
/*
inline char *StrChg ( char *pStr, char *pSrc, char *pDst , int nBuffLen )
{
int n = 0;
char *pCh = pStr;
int i;
i = strlen ( pDst );
for ( ;; )
{
if ( IsNull( StrRepStr ( pCh, pSrc, pDst, nBuffLen ) ) )
{
break;
}
pCh += i;
n++;
}
return ( n ? pStr : NULL );
}
*/
/*
inline char *StrChg ( char *pStr, char *pSrc, char *pDst , int nBuffLen )
{
int n = 0;
char *pCh = pStr;
char *pCounter = pStr;
int DstLen;
int SrcLen;
int counter = 0;
SrcLen = strlen(pSrc);
DstLen = strlen(pDst);
pCounter = strstr(pCounter, pSrc);
while (NULL != pCounter)
{
counter++;
pCounter = strstr(pCounter+SrcLen, pSrc);
}
if ((0 != counter) && (nBuffLen > strlen(pStr)+DstLen*counter-SrcLen*counter))
{
for ( ;; )
{
if ( IsNull( StrRepStr ( pCh, pSrc, pDst, nBuffLen ) ) )
{
break;
}
nBuffLen = nBuffLen - DstLen;
pCh += DstLen;
n++;
}
}
return ( n ? pStr : NULL );
}
*/
///////////////////////////////////////////////////////////////////////////
// Name : StriInc
// Function : test string is included in string "pStr"or not
// Note : No matter Upper or Lower
///////////////////////////////////////////////////////////////////////////
inline char *StriInc ( char *pIncStr, char *pStr )
{
int m;
register char *pCh;
register int n;
m = strlen ( pIncStr );
n = strlen ( pStr );
for ( pCh = pStr; !IsChNull(*pCh) && ( n >= m ); pCh++, n-- )
{
if ( !strncasecmp ( pIncStr, pCh, m ) )
{
return ( pCh );
}
}
return ( NULL );
}
///////////////////////////////////////////////////////////////////////////
// Name : StriDel
// Function : Delete string "pStrDel" in string "pStr" , If pStrDel is not
// exist, rerurn NULL, nor return pSrc
// Note : No matter Upper or Lower
//////////////////////////////////////////////////////////////////////////
inline char *StriDel ( char *pStrDel, char *pStr )
{
char *pCh, *pPos;
pCh = StriInc ( pStrDel, pStr );
if ( !pCh )
{
return ( NULL );
}
pPos = pCh + strlen ( pStrDel );
strcpy ( pCh, pPos );
return ( pStr );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrRepCh
// Function : Replace char cSrc in the string pStr with cDst
// Return : return the replace counter
///////////////////////////////////////////////////////////////////////////
inline int StriRepCh ( char *pStr, char cSrc, char cDst )
{
int nReplaced = 0;
register char *pCh;
cSrc = toupper ( cSrc );
for ( pCh = pStr; !IsChNull( *pCh ); pCh++ )
{
if ( cSrc == toupper ( *pCh ) )
{
*pCh = cDst;
nReplaced++;
}
}
return ( nReplaced );
}
///////////////////////////////////////////////////////////////////////////
// Name : StriRepStr
// Function : Replace the first string "pSrc" in string "pStr" with "pDst" ,
// If pSrc is not exist or the nBuffLen of pStr is not enought
// to insert pDst , rerurn NULL, nor return new pSrc
///////////////////////////////////////////////////////////////////////////
inline char *StriRepStr ( char *pStr, char *pSrc, char *pDst, int nBuffLen )
{
char *pCh = NULL;
int nPos;
pCh = StriInc ( pSrc, pStr);
if ( !IsNull(pCh) )
{
StriDel ( pSrc, pStr );
nPos = pCh - pStr;
if (NULL == StrIns ( nPos, pDst, pStr, nBuffLen ))
pCh = NULL;
else
pCh = pStr;
}
return ( pCh );
}
/*
inline char *StriRepStr ( char *pStr, char *pSrc, char *pDst, int nBuffLen )
{
char *pCh = NULL;
int nPos;
int SrcLen;
int DstLen;
SrcLen = strlen(pSrc);
DstLen = strlen(pDst);
pCh = StriInc ( pSrc, pStr);
if (( !IsNull(pCh) ) && (nBuffLen >DstLen + strlen ( pStr ) - SrcLen))
{
StriDel ( pSrc, pStr );
nPos = pCh - pStr;
if (NULL == StrIns ( nPos, pDst, pStr, nBuffLen ))
pCh = NULL;
else
pCh = pStr;
}
else
pCh = NULL;
return ( pCh );
}
*/
///////////////////////////////////////////////////////////////////////////
// Name : StrChg
// Function : Replace all the string "pSrc" in string "pStr" with "pDst" ,
// If pSrc is not exist or the nBuffLen of pStr is not enought
// to insert pDst , rerurn NULL, nor return new pSrc
// Note : No matter Upper or Lower
///////////////////////////////////////////////////////////////////////////
/*
inline char *StriChg ( char *pStr, char *pSrc, char *pDst , int nBuffLen )
{
int n = 0;
char *pCh = pStr;
int i;
int nSrc , nDst ;
i = strlen ( pDst );
for ( ;; )
{
if ( IsNull( StriRepStr ( pCh, pSrc, pDst, nBuffLen ) ) )
{
break;
}
pCh += i;
n++;
}
return ( n ? pStr : NULL );
}
*/
/*
inline char *StriChg ( char *pStr, char *pSrc, char *pDst , int nBuffLen )
{
int n = 0;
char *pCh = pStr;
char *pCounter = pStr;
int DstLen;
int SrcLen;
int counter = 0;
SrcLen = strlen(pSrc);
DstLen = strlen(pDst);
pCounter = StriInc(pSrc, pCounter);
while (NULL != pCounter)
{
counter++;
pCounter = StriInc(pSrc, pCounter + SrcLen);
}
if ((0 != counter) && (nBuffLen > strlen(pStr)+DstLen*counter-SrcLen*counter))
{
for ( ;; )
{
if ( IsNull( StriRepStr ( pCh, pSrc, pDst, nBuffLen ) ) )
{
break;
}
nBuffLen = nBuffLen - DstLen;
pCh += DstLen;
n++;
}
}
return ( n ? pStr : NULL );
}
*/
///////////////////////////////////////////////////////////////////////////
// Name : StrCode
// Function : Xor the pStr with pSecCode , used in security coding
///////////////////////////////////////////////////////////////////////////
inline char *StrCode ( char *pStr, char *pSecCode )
{
char *q = pSecCode;
register char *pCh;
for ( pCh = pStr; !IsChNull(*pCh); pCh++ )
{
if ( *pCh != *q )
{
*pCh = *pCh ^ *q;
}
q++;
if ( IsChNull(*q) )
{
q = pSecCode;
}
}
return ( pStr );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrFinis
// Function : cut the string with '\0', if nPos > the len of pStr , fill
// the string end with ' ' and set the pStr end positon at nPos
///////////////////////////////////////////////////////////////////////////
inline char *StrFinis ( char *pStr, int nPos )
{
int i;
int nLen;
nLen = strlen ( pStr );
if ( nPos < nLen )
{
*( pStr + nPos ) = CNULL;
}
else
{
for ( i = nLen; i < nPos; i++ )
{
*( pStr + i ) = ' ';
}
*( pStr + i ) = CNULL;
}
return ( pStr );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrEnding
// Function : set the string end with the pEnd string till nPos position
//////////////////////////////////////////////////////////////////////////
inline char *StrEnding( char *pDst, char *pEnd , int nPos )
{
register int nLen = strlen(pDst);
int nEndLen = strlen(pEnd);
if( nPos < nLen + nEndLen )
{
return NULL;
}
for( ; nPos > nLen + nEndLen ; nLen += nEndLen )
{
strcat( pDst , pEnd);
}
}
///////////////////////////////////////////////////////////////////////////
// StrSuff: checks whether suffix is a suffix of src. If it is not,
// the result is NULL. If it is, the result is a pointer
// to the character of src.
///////////////////////////////////////////////////////////////////////////
inline char *StrSuff(const char *pSrc, const char *pSuffix )
{
register int nLen;
for ( nLen = 0; *pSuffix++; nLen++ )
if(!*pSrc++) return NULL;
while (*pSrc++);
for ( --pSrc, --pSuffix; --nLen >= 0; )
if ( *--pSrc != *--pSuffix ) return NULL;
return (char*)pSrc;
}
/* Fill string 'c' into last area of 'str'. */
void StringCat(char *str, char *c, int slen)
{
if (strlen(str) >= slen)
return;
while (strlen(str) < slen)
strcat(str, c);
str[slen] = 0;
}
///////////////////////////////////////////////////////////////////////////
// Name : StrLeftShit
// Function : String left shit , delete first n chars
///////////////////////////////////////////////////////////////////////////
inline char *StrLeftShit ( char *pStr, int n )
{
int i;
int len;
len = strlen(pStr);
if( n > len )
{
return StrFinis(pStr , 0);
}
len -= n;
for ( i = 0; i < len; i++ )
{
*( pStr + i ) = *( pStr + n + i );
}
StrFinis( pStr , len );
return ( pStr );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrRLeftShit
// Function : String round left shit
///////////////////////////////////////////////////////////////////////////
inline char *StrRLeftShit ( char *pStr, int n )
{
int i, j;
char t;
if ( !IsChNull(*pStr) )
{
for ( j = 0; j < n; j++ )
{
t = *( pStr );
for ( i = 0; *( pStr + i ); i++ )
{
*( pStr + i ) = *( pStr + i + 1 );
}
*( pStr + i - 1 ) = t;
}
}
return ( pStr );
}
///////////////////////////////////////////////////////////////////////////
// StrToLower: String all lower case.
///////////////////////////////////////////////////////////////////////////
//inline char *StrToLower ( char *pStr )
char *StrToLower ( char *pStr )
{
register char *pCh;
for ( pCh = pStr; pCh && *pCh; ++pCh )
{
if ( isupper ( *pCh ) )
{
*pCh = tolower ( *pCh );
}
}
return pStr;
}
///////////////////////////////////////////////////////////////////////////
// StrToUpper: String all upper case.
///////////////////////////////////////////////////////////////////////////
inline char *StrToUpper ( char *pStr )
{
register char *pCh;
for ( pCh = pStr; pCh && *pCh; ++pCh )
{
if ( islower ( *pCh ) )
{
*pCh = toupper ( *pCh );
}
}
return pStr;
}
///////////////////////////////////////////////////////////////////////////
// StrDupLower: Return an all lower case version of String
// you can free the return pointer
///////////////////////////////////////////////////////////////////////////
inline char *StrDupLower ( char *pStr )
{
static char *pBuffer = NULL;
register char *pCh;
if ( pBuffer )
{
free ( pBuffer );
}
pBuffer = strdup ( pStr );
for ( pCh = pBuffer; pCh && *pCh; ++pCh )
{
if ( isupper ( *pCh ) )
{
*pCh = tolower ( *pCh );
}
}
return ( pBuffer );
}
///////////////////////////////////////////////////////////////////////////
// StrDupUpper: Return an all upper case version of String // you can free the return pointer
///////////////////////////////////////////////////////////////////////////
inline char *StrDupUpper ( char *pStr )
{
static char *pBuffer = NULL;
register char *pCh;
if ( pBuffer )
{
free ( pBuffer );
}
pBuffer = strdup ( pStr );
for ( pCh = pBuffer; pCh && *pCh; ++pCh )
{
if ( islower ( *pCh ) )
{
*pCh = toupper ( *pCh );
}
}
return ( pBuffer );
}
///////////////////////////////////////////////////////////////////////////
// IsExistCh : Is the char exist in the string
///////////////////////////////////////////////////////////////////////////
inline int IsExistCh ( char ch, char *pStr )
{
register char *pCh;
for ( pCh = pStr; !IsChNull(*pCh); ++pCh )
{
if ( ch == *pCh )
{
return ( 1 );
}
}
return ( 0 );
}
///////////////////////////////////////////////////////////////////////////
// FindSep: Find the first char in char set in String
///////////////////////////////////////////////////////////////////////////
inline char *StrChSet ( char *pChSet, char *pStr )
{
register char *pCh;
for ( pCh = pChSet; !IsChNull(*pCh); ++pCh )
{
if ( IsExistCh ( *pCh, pStr ) )
{
return ( pCh );
}
}
return ( ( char * ) NULL );
}
///////////////////////////////////////////////////////////////////////////
// Name : StrChNum
// Function : return the char number in the string
///////////////////////////////////////////////////////////////////////////
inline int StrChNum ( char *pStr, char ch )
{
int n = 0;
char *pCh = pStr;
for( ; !IsChNull(*pCh); pCh++ )
{
if (ch == *pCh)
n++;
}
return ( n );
}
/*
inline int StrChNum ( char *pStr, char ch )
{
int n = 0;
char *pCh = pStr;
for( ; !IsChNull(*pCh) && (ch == *pCh) ; pCh++ )
n++;
return ( n );
}
*/
///////////////////////////////////////////////////////////////////////////
// StrTrimCh: trim the char in the string
///////////////////////////////////////////////////////////////////////////
char *StrTrimCh ( char *pSrc , char cTrim )
{
register char *pCh;
char *p,*q;
q = pSrc;
p = pCh = strdup(pSrc);
if (NULL == pCh)
{
return NULL;
}
for( ; !IsChNull(*pCh) ; pCh++ )
{
if(*pCh != cTrim)
{
*pSrc++ = *(pCh);
}
}
*pSrc = '\0';
free(p);
return q;
}
///////////////////////////////////////////////////////////////////////////
// StrTrimCh: trim the char of the set in the string
///////////////////////////////////////////////////////////////////////////
inline char *StrTrimChSet ( char *pSrc , char *pTrimSet )
{
register char *pCh;
char *p , *q , *m;
p = pSrc;
m = pCh = strdup(pSrc);
if (NULL == pCh)
{
return NULL;
}
for( ; !IsChNull(*pCh) ; pCh++ )
{
for( q = pTrimSet; *q && *pCh != *q ; q++ )
{
;
}
if( IsChNull(*q) )
{
*pSrc++ = *(pCh);
}
}
*pSrc = CNULL;
pSrc = p;
free(m);
return pSrc;
}
///////////////////////////////////////////////////////////////////////////
// StrTrimLeftSpace: trim left space of string
///////////////////////////////////////////////////////////////////////////
char *StrTrimLeftSpace( const char *pStr )
{
const char *p = pStr;
for ( ; !IsChNull(*p) && isspace(*p) ; )
{
p++;
}
return (char*) p;
}
///////////////////////////////////////////////////////////////////////////
// StrTrimMoreSpace: trim more than one space
///////////////////////////////////////////////////////////////////////////
char *StrTrimMoreSpace ( char *pSrc )
{
char *pCh , *q;
register char *p;
pCh = p = strdup(pSrc);
q = pSrc;
for ( ; *p; p++ )
{
if ( !( isspace ( *p ) && isspace ( *(p+1) ) ) )
{
*pSrc++ = *p;
}
}
*pSrc = CNULL;
free(pCh);
return pSrc;
}
///////////////////////////////////////////////////////////////////////////
// StrPickWord: pick a word from the string
///////////////////////////////////////////////////////////////////////////
inline char *StrPickWord ( const char *pSrc, char *pDst )
{
char *pCh , *q = pDst;
pCh = StrTrimLeftSpace(pSrc);
for( ; !isspace(*pCh) && !IsChNull(*pCh) ; pCh++)
{
*q++ = *pCh;
}
return (char *)pDst;
}
///////////////////////////////////////////////////////////////////////////
// Name : StrEqTok
// Function: Parse the string and return the expression name and value
// Return : NULL, parse over or string expression error , nor while to call
// StrEqTok at odd times , return the name ; and return the value
// while even times
// Note : use it like strtok
// e.g. :
// {
// char *p = NULL;
// u8 uFlag = 0;
//
// p = StrEqTok(tmpStr);
// printf( "Name: %-20s " , p);
// for( ; p = StrEqTok(NULL); )
// {
// uFlag = uFlag ? 0 : 1;
// if( uFlag )
// {
// printf( "Valu: %-20s \n" , p);
// }
// else
// {
// printf( "Name: %-20s " , p);
// }
// }
// }
//////////////////////////////////////////////////////////////////////////
static char *pStrEqTok = NULL;
static u8 uStrEqState = 0;
char *StrEqTok ( char *pStr )
{
char *p, *q, *pCh;
char *pDel = " ";
pCh = pStr;
if ( pCh == NULL )
{ //last position
pCh = pStrEqTok;
}
else
{
StrTrimLeftSpace ( pCh );
StrTrimMoreSpace ( pCh );
}
if ( pCh == NULL )
{
return NULL;
}
uStrEqState = uStrEqState > 2 ? 0 : uStrEqState;
q = strpbrk ( pCh, pDel );
if ( q == NULL )
{ //over , one word
pStrEqTok = NULL;
q = pCh;
}
else
{ //save position
pStrEqTok = q + 1;
*q = CNULL;
}
p = strchr ( pCh, '=' ); //parse word
if ( p == NULL )
{ //simple word exclude '='
if ( pStrEqTok == NULL || uStrEqState == 1 )
{ //miss '='
goto STREQTOK_CLEAN_UP;
}
if ( uStrEqState == 0 || uStrEqState == 2 )
{ //wating expres left or right, recieved
uStrEqState++;
return pCh;
}
}
else
{
if ( uStrEqState == 0 && STR_CHECK_LAST( pCh, '=' ) )
{ //wating expres left , revied left and '='
uStrEqState += 2;
STR_CUT_LAST( pCh );
return pCh;
}
else if ( uStrEqState == 1 && STR_CHECK_FIRST( pCh,'=') )
{ //wating '=' , revied
if ( pCh[1] == CNULL )
{ //just '='
if ( pStrEqTok == NULL )
{
goto STREQTOK_CLEAN_UP;
}
pCh = pStrEqTok;
p = strpbrk ( pCh, pDel );
if ( p == NULL )
{
pStrEqTok = NULL;
p = pCh;
}
else
{
pStrEqTok = p + 1;
*p = CNULL;
}
uStrEqState = 3;
return pCh;
}
else
{ //expres '=' and right
uStrEqState = 3; //all meet
return ++pCh;
}
}
else if ( uStrEqState == 0 )
{ //expres left and '=' and right
if ( pStrEqTok != NULL )
{
*( pStrEqTok - 1 ) = ' ';
}
pStrEqTok = p + 1;
*p = CNULL;
uStrEqState = 2;
return pCh;
}
else
{
goto STREQTOK_CLEAN_UP;
}
}
STREQTOK_CLEAN_UP:
pStrEqTok = NULL;
uStrEqState = 0;
return NULL;
}
/*@end@*/