home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- **+
- ** 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);
- }
-
- /****************************************************************************
-
- FUNCTION: StrLpad
-
- PURPOSE: Pad with spaces on left
-
- StrRpad(dest,"123", 6) dest = " 123"
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- VOID StrLpad(CHAR * dest, CHAR * source, INT i)
- #else
- VOID StrLpad(dest,source,i)
- CHAR * dest, * source;
- int i;
- #endif
- {
- int c,q,m;
-
- c = strlen(source);
- if (c < i)
- {
- q = i-c;
- StrSpaces(dest,q);
- for (m = 0; m < q; m++)
- *(dest+m+q) = source[m];
-
- *(dest+i+1) = '\0';
- }
- }
-
- /****************************************************************************
-
- FUNCTION: StrMid
-
- PURPOSE: Like basic mid$() extract from middle of string
-
- StrMid(dest,"123",2,2) dest = "23"
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- VOID StrMid(CHAR * dest, CHAR * source, INT beg, INT len)
- #else
- VOID StrMid(dest,source,beg,len) /* like Mid$() */
- CHAR * dest,* source;
- INT beg,len;
- #endif
- {
- int i,k;
-
- k = MIN(len,strlen(source));
- for (i = 0; i < k; i++)
- * dest++ = source[beg+i];
- * dest = '\0';
- }
-
- #if 0
- /****************************************************************************
-
- FUNCTION: StrCopy
-
- PURPOSE: string copy
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- VOID StrCopy(CHAR * dest, CHAR * source)
- #else
- VOID StrCopy(dest,source)
- CHAR * dest,* source;
- #endif
- {
- int i,k;
-
- k = strlen(source);
- for (i = 0; i < k; i++)
- * dest++ = source[i];
- * dest = '\0';
- }
-
- /****************************************************************************
-
- FUNCTION: StrCat
-
- PURPOSE: string cat
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- VOID StrCat(CHAR * dest, CHAR * source)
- #else
- VOID StrCat(dest,source)
- CHAR * dest,* source;
- #endif
- {
- int i,k,m;
-
- k = strlen(dest);
- m = strlen(source);
- for (i = 0; i < m; i++)
- dest[k+i] = source[i];
-
- dest[k+m] = '\0';
- }
-
- #endif
-
- /****************************************************************************
-
- FUNCTION: StrInChar
-
- PURPOSE: Find a character within a string
-
- StrInChar("123", '3') return = [2]
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- INT StrInChar(CHAR * source, CHAR find_char)
- #else
- INT StrInChar(source,find_char) /* Instr$() */
- CHAR * source;
- CHAR find_char;
- #endif
- {
- int i = 0;
-
- while ((source[i] != find_char) && (source[i] != '\0'))
- i++;
-
- if (source[i] == find_char)
- return(i);
- else
- return(-1);
- }
-
- /****************************************************************************
-
- FUNCTION: StrPos
-
- PURPOSE: Find one string within another, returns -1 if not in
-
- StrPos("1234567", "456" ) return = [3]
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- INT StrPos(CHAR * s, CHAR *t)
- #else
- INT StrPos(s,t) /* look for t in s */
- CHAR *s, *t;
- #endif
- {
- int i,j,k;
-
- for (i = 0; s[i] != '\0'; i++)
- {
- for (j = i, k = 0; t[k] != '\0' && s[j]==t[k]; j++, k++);
- if (t[k] == '\0')
- return(i);
- }
- return(-1);
-
- }
-
-
- /****************************************************************************
-
- FUNCTION: StrRep
-
- PURPOSE: Replace characters within a string
-
- StrRep(dest,"_1_2_3", '_', ' ') dest = " 1 2 3"
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- VOID StrRep(CHAR * dest, CHAR * source, CHAR find_char, CHAR rep_char)
- #else
- VOID StrRep(dest,source,find_char,rep_char) /* replace chars */
- CHAR * dest,* source;
- CHAR find_char,rep_char;
- #endif
- {
- int k,m,n;
-
- m = strlen(source);
- for (n = 0; n < m; n++)
- *(dest+n) = source[n];
- *(dest+m) = '\0';
- while ((k = StrInChar(dest,find_char)) != -1)
- *(dest+k) = rep_char;
-
- }
-
-
- /****************************************************************************
-
- FUNCTION: StrRpt
-
- PURPOSE: Repeat characters into a string
-
- StrRpt(dest,"0", 9) dest = "000000000"
-
- ****************************************************************************/
-
- #ifdef C_ANSI
- VOID StrRpt(CHAR * dest, CHAR rpt_char, INT i)
- #else
- VOID StrRpt(dest,rpt_char,i) /* repeating digits */
- CHAR * dest;
- CHAR rpt_char;
- INT i;
- #endif
- {
- int q;
-
- for (q = 0; q < i; q++)
- * dest++ = rpt_char;
- * dest++ = '\0';
- }
-
- #if 0
-
- double fnround(x,m)
- double x;
- int m;
- {
- int k;
- double f,g;
- double fabs();
-
- f = (fabs(x) + 0.00501);
- k = (f * 100.0);
- g = k / 100.0;
- g = g * SGN(x);
-
- return(g);
- }
-
- wait_key(command)
- CHAR *command;
- {
- CHAR inky;
-
- inky = getcnb();
- printf("%c",inky);
-
- if ( inky == '\00')
- {
- *command = getcnb();
- return(1);
- }
- else
- {
- *command = inky;
- return(0);
- }
- }
-
-
- void print_tab_mid(fp,tab,str,beg,len)
- FILE *fp;
- int tab;
- CHAR *str;
- int beg,len;
- {
- static int print_head;
- static CHAR temp_s[255];
- static CHAR s_s;
-
- int i,k,r,crlf = 0;
-
- if (tab < 0)
- {
- crlf = 1;
- tab = abs(tab);
- }
-
- k = MIN(len,strlen(str));
- for (i = 0; i < k; i++)
- temp_s[i] = str[beg+i];
- temp_s[k] = '\0';
-
- r = tab-print_head;
- if (r > 1)
- {
- StrSpaces(s_s,r);
- fprintf(fp,s_s); /* advance spaces from last position to new tab */
- }
- if (r > -1)
- {
- fprintf(fp,temp_s); /* print data */
- print_head = (print_head + r + strlen(str));
- /* increment print_head for next read */
- }
- if (r < 0)
- {
- fprintf("\n"); /* do newline if tab too close */
- StrSpaces(s_s,tab);
- fprintf(fp,temp_s);
- print_head = tab + strlen(temp_s);
- }
- if (crlf)
- {
- fprintf(fp,"\n");
- print_head = 1;
- }
-
- }
-
-
-
- void print_tab(fp,tab,str)
- FILE *fp;
- int tab;
- CHAR *str;
- {
- static int print_head;
- static CHAR s_s;
-
- int k,r,crlf = 0;
-
- if (tab < 0)
- {
- crlf = 1;
- tab = abs(tab);
- }
- k = strlen(str);
- r = tab-print_head;
- if (r > 1)
- {
- StrSpaces(s_s,r);
- fprintf(fp,s_s); /* advance spaces from last position to new tab */
- }
- if (r > -1)
- {
- fprintf(fp,str); /* print data */
- print_head = (print_head + r + strlen(str));
- /* increment print_head for next read */
- }
- if (r < 0)
- {
- fprintf("\n"); /* do newline if tab too close */
- StrSpaces(s_s,tab);
- fprintf(fp,str);
- print_head = tab + strlen(str);
- }
- if (crlf)
- {
- fprintf(fp,"\n");
- print_head = 1;
- }
-
- }
-
- void print_mid(source,beg,len)
- CHAR * source;
- int beg,len;
- {
- int i,j;
- j = MIN(strlen(source),(beg+len));
- for (i = beg; i < j; i++)
- putchar(source[i]);
- }
-
- void print_midrtrm(source,beg,len)
- CHAR * source;
- int beg,len;
- {
- int i,j;
- static CHAR temp_1[255],temp_2[255];
-
- copy_s(temp_1,source);
- rtrm(temp_2,temp_1);
- j = MIN(strlen(temp_2),(beg+len));
- for (i = beg; i < j; i++)
- putchar(temp_2[i]);
- }
-
- CHAR mid_asn(dest,beg,len,source)
- CHAR * dest;
- int beg,len;
- CHAR * source;
- {
- int i,k,p,x;
- static CHAR temp_s[255];
-
- k = MIN(len,strlen(source));
- p = strlen(dest);
- if (p == 0)
- {
- StrSpaces(dest,beg);
- p = beg;
- }
- x = beg - p;
- if ( x > 0 )
- {
- rpad(temp_s,dest,beg);
- copy_s(dest,temp_s);
- }
- for (i = beg; i < beg+len; i++)
- dest[i] = source[i-beg];
- if (strlen(dest) <= (beg+len) )
- dest[beg+len] = '\0';
- }
-
- CHAR mid_asn_mid(dest,beg1,len1,source,beg2,len2)
- CHAR * dest;
- int beg1,len1;
- CHAR * source;
- int beg2,len2;
- {
- int i,j,k,p,x;
- static CHAR temp_s[255];
-
- k = MIN(len1,strlen(source));
- p = strlen(dest);
-
- if (p == 0)
- {
- StrSpaces(dest,beg1);
- p = beg1;
- }
-
- x = beg1 - p;
-
- if ( x > 0 )
- {
- rpad(temp_s,dest,beg1);
- copy_s(dest,temp_s);
- }
-
- for (i = beg1, j = beg2; i < beg1+len1; i++, j++)
- dest[i] = source[j];
- if (strlen(dest) <= (beg1+len1) )
- dest[beg1+len1] = '\0';
- }
-
-
-
-
- #ifdef UNIX
- int strstr(pcStr1,pcStr2)
- char * pcStr1;
- char * pcStr2;
- {
- int iLen1;
- int iLen2;
- int iNdx1;
- int iNdx2;
- int iRet = C_FALSE;
- int iMatches;
-
- iLen1 = strlen(pcStr1); /* main string */
- iLen2 = strlen(pcStr2); /* string being looked for in main string */
-
- for(iNdx1 = 0; iNdx1 < iLen1; iNdx1++)
- {
- iMatches = 0;
-
- for(iNdx2 = 0; (iNdx2 < iLen2) && (iNdx1+iNdx2 < iLen1); iNdx2++)
- if(pcStr1[iNdx1 + iNdx2] == pcStr2[iNdx2])
- iMatches++;
-
- if(iMatches == iLen2)
- {
- iRet = C_TRUE;
- break;
- }
- }
-
- return(iRet);
-
- }
- #endif
-
-
-
- #endif
-
-
-
-
-
-
-
-
-
-