📄 str.c
字号:
/***********************************************************************
**+
** Module Name: str.c
**
** Description: Generic String Functions
**
** Written by: John Tal
**
**
** Modification history:
**
** Date Engineer Mod # Modification Description
**
** 1987 Tal v 1.0-001 Initial release
**-
***********************************************************************/
#include <memincs.h>
#include <fildir.h>
#include <string.h>
#include <stdlib.h>
/* Strncopy - like strncpy but also places null on end */
#ifdef C_ANSI
INT Strncopy( char *szStr1,
char *szStr2,
INT nLen)
#else
INT Strncopy( *szStr1,*szStr2,nLen)
char * szStr1;
char * szStr2;
INT nLen;
#endif
{
INT i;
/* Strncpy does not put a NULL if the source is > than the Dest */
strncpy( szStr1, szStr2, nLen );
i = strlen( szStr2 );
i = ( nLen > i ) ? i : ( nLen - 1 );
szStr1[ i ] = NULL;
return ( 0 );
}
/* Strnappend - like strncat but also places null on end */
#ifdef C_ANSI
INT Strnappend( char *szStr1,
char *szStr2,
INT nLen)
#else
INT Strnappend( *szStr1,*szStr2,nLen)
char * szStr1;
char * szStr2;
INT nLen;
#endif
{
INT i;
INT iLen;
/* Strnappend does not put a NULL if the source is > than the Dest */
iLen = strlen(szStr1);
strncat( szStr1, szStr2, nLen );
i = iLen + strlen(szStr2);
i = ( nLen > (i + iLen)) ? (i + iLen) : ( (nLen + iLen) );
szStr1[ i ] = NULL;
return ( 0 );
}
/****************************************************************************
FUNCTION: StrAppendPath
PURPOSE: Append sz2 to sz1 returned as sz3
****************************************************************************/
#ifdef C_ANSI
INT StrAppendPath(char * sz1, char * sz2, char * sz3)
#else
INT StrAppendPath(sz1,sz2,sz3)
char * sz1;
char * sz2;
char * sz3;
#endif
{
C_DEF_MODULE("StrAppendPath")
char cChar;
strcpy(sz3,sz1); /* copy sz1 to sz3 */
cChar = sz3[strlen(sz3)-1];
/* if last char not \, then add \ */
if((cChar != DIR_SEPERATOR) &&
(sz2[0] != DIR_SEPERATOR) &&
(sz2[1] != ':')
)
strcat(sz3,DIR_SEPERATOR_STR);
strcat(sz3,sz2); /* append sz2 to sz3 */
C_RETURN
}
/****************************************************************************
FUNCTION: StrBreakFName
PURPOSE: Seperate pathfilename into pathname and filename
****************************************************************************/
#ifdef C_ANSI
INT StrBreakFName(char * pcPathFile, char * pcPath, char * pcFile)
#else
INT StrBreakFName(pcPathFile,pcPath,pcFile)
char * pcPathFile;
char * pcPath;
char * pcFile;
#endif
{
C_DEF_MODULE("StrBreakFName")
INT iPos;
iPos = strlen(pcPathFile) -1;
while(iPos)
{
if(pcPathFile[iPos] == DIR_SEPERATOR)
break;
iPos--;
}
if(iPos)
{
/* filename and path present, break them up into components */
Strncopy(pcPath,pcPathFile,iPos+1);
strcpy(pcFile,&pcPathFile[iPos + 1]);
}
else
{
/* no path, filename only */
strcpy(pcPath,"");
strcpy(pcFile,pcPathFile);
}
C_RETURN
}
/****************************************************************************
FUNCTION: StrBreakFirst
PURPOSE: Seperate pathfilename into first dir and rest
****************************************************************************/
#ifdef C_ANSI
INT StrBreakFirst(char * pcPathFile, char * pcFirst, char * pcRest)
#else
INT StrBreakFirst(pcPathFile,pcFirst,pcRest)
char * pcPathFile;
char * pcFirst;
char * pcRest;
#endif
{
C_DEF_MODULE("StrBreakFName")
INT iPos = 0;
INT ilength;
while(1)
{
if((pcPathFile[iPos] == DIR_SEPERATOR) && (iPos != 0))
break;
iPos++;
ilength = strlen(pcPathFile);
if (iPos >= ilength)
break;
}
if (iPos < ilength)
{
/* filename and path present, break them up into components */
Strncopy(pcFirst,pcPathFile,iPos+1);
if (iPos == ilength)
pcRest[0] = '\0'; /* no seperator found */
else
strcpy(pcRest,&pcPathFile[iPos + 1]);
}
else
{
/* no path, filename only */
strcpy(pcFirst,"");
strcpy(pcRest,pcPathFile);
}
C_RETURN
}
/* Misc String functions Tal - 1987 */
/****************************************************************************
FUNCTION: StrReverse
PURPOSE: Reverse a string in place
1234 becomes 4321
****************************************************************************/
#ifdef C_ANSI
VOID StrReverse(CHAR * s)
#else
VOID StrReverse(s)
CHAR * s;
#endif
{
int c,i,j;
for( i= 0, j=strlen(s)-1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = (char)c;
}
}
/****************************************************************************
FUNCTION: StrItoa
PURPOSE: Turn an integer into a string (like sprintf("%d"))
****************************************************************************/
#ifdef C_ANSI
VOID StrItoa(CHAR * s, INT n)
#else
VOID StrItoa(s,n) /* itoa */
CHAR * s;
INT n;
#endif
{
int i,sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
StrReverse(s);
}
/****************************************************************************
FUNCTION: StrSpaces
PURPOSE: Fill a string with spaces
StrSpaces(s,5) s = " "
****************************************************************************/
#ifdef C_ANSI
VOID StrSpaces(CHAR * s, INT i)
#else
VOID StrSpaces(s,i)
CHAR * s;
int i;
#endif
{
int k;
for ( k = 0; k < i; k++)
*s++ = ' ';
*s = '\0';
}
/****************************************************************************
FUNCTION: StrLeft
PURPOSE: Take specified chars from left of source into dest
StrLeft(dest,"This is a",2) dest = "Th"
****************************************************************************/
#ifdef C_ANSI
VOID StrLeft(CHAR * dest, CHAR * source, INT i)
#else
VOID StrLeft(dest,source,i)
CHAR * dest,* source;
INT i;
#endif
{
int k;
for (k = 0; k < i; k++)
* dest++ = source[k];
* dest++ = '\0';
}
/****************************************************************************
FUNCTION: StrRight
PURPOSE: Take specified chars from right of source into dest
StrRight(dest,"This is a",2) dest = " a"
****************************************************************************/
#ifdef C_ANSI
VOID StrRight(CHAR * dest, CHAR * source, INT i)
#else
VOID StrRight(dest,source,i)
CHAR * dest,* source;
INT i;
#endif
{
int j,k,m,p;
p = strlen(source);
m = MIN(p,i);
j = p - m;
for (k = j; k < p; k++)
* dest++ = source[k];
* dest++ = '\0';
}
/****************************************************************************
FUNCTION: StrRtrim
PURPOSE: Remove spaces from right of string
StrRtrim(dest," 123 ") dest = " 123"
****************************************************************************/
#ifdef C_ANSI
VOID StrRtrim(CHAR * dest, CHAR * source)
#else
VOID StrRtrim(dest,source)
CHAR * dest,* source;
#endif
{
int k,y;
y = strlen(source)-1;
while (source[y] == ' ')
y--;
for (k = 0; k < y+1; k++)
* dest++ = source[k];
* dest++ = '\0';
}
/****************************************************************************
FUNCTION: StrLtrim
PURPOSE: Remove spaces from left of string
StrLtrim(dest," 123 ") dest = "123 "
****************************************************************************/
#ifdef C_ANSI
VOID StrLtrim(CHAR * dest, CHAR * source)
#else
VOID StrLtrim(dest,source)
CHAR * dest,* source;
#endif
{
int k,m,y;
m = strlen(source);
y = 0;
while (source[y] == ' ')
y++;
for (k = y; k < m; k++)
* dest++ = source[k];
* dest++ = '\0';
}
/****************************************************************************
FUNCTION: StrRpad
PURPOSE: Pad with spaces on right
StrRpad(dest,"123", 6) dest = "123 "
****************************************************************************/
#ifdef C_ANSI
VOID StrRpad(CHAR * dest, CHAR * source, INT i)
#else
VOID StrRpad(dest,source,i)
CHAR * dest,* source;
int i;
#endif
{
int c,q;
c = strlen(source);
if (c <= i)
{
strcpy(dest,source);
for (q = c; q < i; q++)
*(dest+q) = ' ';
*(dest+i+1) = '\0';
}
}
#ifdef C_ANSI
VOID StrRpad2(CHAR * dest, CHAR * source, INT i)
#else
VOID StrRpad2(dest,source,i)
CHAR * dest,* source;
INT i;
#endif
{
CHAR temp1[255],temp2[255];
int k;
strcpy(temp1,source);
k = abs(i-strlen(source));
StrSpaces(temp2,k);
strcat(temp1,temp2);
strncpy(dest,temp1,i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -