home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRNSUB.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  153 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /***************************************************************************
  4. * @(#)strnsub
  5. * @(#)-     searches string for a given pattern and replaces it with
  6. * @(#)      a new text string.
  7. *
  8. ***************************************************************************
  9. *@(#)1993 Erik Bachmann
  10. *
  11. * Released to public domain 27-Oct-95
  12. ***************************************************************************/
  13.  
  14. #include "bacstd.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. /*
  19.  /-------------------------------------\
  20. |        STRNSUB                        |------------------------------------|
  21. |\-------------------------------------/
  22. |    Function searches string for a given pattern and replaces it with
  23. | a new text string.
  24. |
  25. | Note that the pattern string is replaced with the
  26. | replacement string and they do not have to be the same length. It is
  27. | assumed, however, that the original string is long enough to hold the
  28. | result. Maximum length + 1 is given in iMaxLength
  29. |
  30. |
  31. |----------------------------------------------------------------------------|
  32. | CALL:
  33. |    strnsub(pszStreng, org, rep, maxlength) ;
  34. |
  35. | HEADER:
  36. |    bacstd.h
  37. |    stdio.h
  38. |    string.h
  39. |    malloc.h
  40. |
  41. | GLOBALE VARIABLES:
  42. |    %
  43. |
  44. | ARGUMENTS:
  45. |    char *pszString    the string to search
  46. |    char *pat          the pattern to locate
  47. |    char *rep          the replacement string
  48. |    int  iMaxLength    the maximum length of string after replacement
  49. |
  50. | PROTOTYPE:
  51. |    char CfnTYPE *strnsub(char *pszString, char *pat, char *rep,
  52. |                          int iMaxLength) ;
  53. |
  54. | RETURN VALUE:
  55. |    char *         pointer to the replaced string
  56. |
  57. | MODULE:
  58. |    strsub.c
  59. |----------------------------------------------------------------------------|
  60. |
  61. |
  62. |
  63. |----------------------------------------------------------------------------|
  64. |1993-03-25/Erik Bachmann
  65. \---------------------------------------------------------------------------|*/
  66.  
  67. char _CfnTYPE *strnsub(char *pszString,
  68.                        char *pszPattern,
  69.                        char *pszReplacement,
  70.                        int iMaxLength)
  71. {
  72.       char  *pszSubstring,
  73.                   *pszTmpSubstring ;
  74.  
  75.       int   iPatternLength,
  76.                   iReplacementLength ;
  77.  
  78.       /*-------------------------------------------------*/
  79.  
  80.       pszTmpSubstring = pszSubstring = pszString ;
  81.       iPatternLength = strlen(pszPattern) ;
  82.       iReplacementLength = strlen(pszReplacement) ;
  83.  
  84.       if ( 0 == strcmp( pszPattern, pszReplacement ) )
  85.             return( 0 ) ;                 /* Pattern == replacement: loop */
  86.  
  87.       if ( NULL == ( pszSubstring = strstr(pszString, pszPattern ) ) )
  88.       {                                   /* No match found               */
  89.             return( NULL );
  90.       }
  91.  
  92.       if ( ( strlen( pszString ) + ( iReplacementLength - iPatternLength ) )
  93.              >= iMaxLength )
  94.  
  95.             /* Enough space for replacement? */
  96.  
  97.             return( NULL ) ;
  98.  
  99.       pszTmpSubstring = (char *) calloc(iMaxLength, sizeof(char)) ;
  100.  
  101.       /* Buy some workspace      */
  102.  
  103.       if ( NULL == pszTmpSubstring )            /* No memory left          */
  104.             return( NULL ) ;
  105.  
  106.       strcpy( pszTmpSubstring, pszSubstring + iPatternLength ) ;
  107.  
  108.       /* If there was space left */
  109.  
  110.       while ( iReplacementLength-- )
  111.       {     /* Copy replacement        */
  112.             *pszSubstring++ = *pszReplacement++ ;
  113.       }
  114.  
  115.       strcpy( pszSubstring, pszTmpSubstring ) ;
  116.  
  117.       /* Add old stuff back      */
  118.  
  119.       free( pszTmpSubstring ) ;
  120.  
  121.       return( pszSubstring - iPatternLength ) ;
  122.  
  123.       /* Return pointer to change*/
  124. }
  125.  
  126. #ifdef      TEST
  127.  
  128. int main()
  129. {
  130.       char  szStreng[20] ;
  131.       char  *org  = "xx" ;
  132.       char  *rep  = "YYYY" ;
  133.  
  134.       /*-------------------------------------------------*/
  135.  
  136.       fprintf(stderr, "strnsub()\n\n") ;
  137.  
  138.       strcpy(szStreng, "testxxtest") ;
  139.  
  140.       fprintf(stderr, "Replacing %s with %s\n\nBefore : %s",
  141.               org, rep, szStreng) ;
  142.  
  143.       if ( NULL == strnsub(szStreng, org, rep, 20) )
  144.             fprintf(stderr, "\nERROR\n\a") ;
  145.       else
  146.             fprintf(stderr, "\nOK\n") ;
  147.  
  148.       fprintf(stderr, "After : %s\n\n", szStreng) ;
  149.  
  150. }
  151.  
  152. #endif
  153.