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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.    --------------------------------------------------------------------
  5.    Module:     REPLACE.C
  6.    Author:     Gilles Kohl
  7.    Started:    09.06.1992   12:16:47
  8.    Modified:   09.06.1992   12:41:41
  9.                22-Sep-95    Bob Stout
  10.    Subject:    Replace one string by another in a given buffer.
  11.                This code is public domain. Use freely.
  12.    --------------------------------------------------------------------
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "snip_str.h"
  19.  
  20. /*
  21. ** strrepl: Replace OldStr by NewStr in string Str contained in buffer
  22. **          of size BufSiz.
  23. **
  24. ** Str should have enough allocated space for the replacement - if not,
  25. ** NULL is returned. Str and OldStr/NewStr should not overlap.
  26. **
  27. ** The empty string ("") is found at the beginning of every string.
  28. **
  29. ** Returns: pointer to first location behind where NewStr was inserted.
  30. **          Str if OldStr was not found.
  31. **          NULL if replacement would overflow Str's buffer
  32. **
  33. ** This is useful for multiple replacements, see example in main() below
  34. ** (be careful not to replace the empty string this way !)
  35. **
  36. **  NOTE: The name of this funtion violates ANSI/ISO 9899:1990 sec. 7.1.3,
  37. **        but this violation seems preferable to either violating sec. 7.13.8
  38. **        or coming up with some hideous mixed-case or underscore infested
  39. **        naming. Also, many SNIPPETS str---() functions duplicate existing
  40. **        functions which are supported by various vendors, so the naming
  41. **        violation may be required for portability.
  42. */
  43.  
  44. #if defined(__cplusplus) && __cplusplus
  45.  extern "C" {
  46. #endif
  47.  
  48. char *strrepl(char *Str, size_t BufSiz, char *OldStr, char *NewStr)
  49. {
  50.       int OldLen, NewLen;
  51.       char *p, *q;
  52.  
  53.       if(NULL == (p = strstr(Str, OldStr)))
  54.             return Str;
  55.       OldLen = strlen(OldStr);
  56.       NewLen = strlen(NewStr);
  57.       if ((strlen(Str) + NewLen - OldLen + 1) > BufSiz)
  58.             return NULL;
  59.       memmove(q = p+NewLen, p+OldLen, strlen(p+OldLen)+1);
  60.       memcpy(p, NewStr, NewLen);
  61.       return q;
  62. }
  63.  
  64. #if defined(__cplusplus) && __cplusplus
  65.  }
  66. #endif
  67.  
  68. #ifdef TEST
  69.  
  70. /*
  71. ** Test main().
  72. ** Given two arguments, replaces the first arg. in the lines read from
  73. ** stdin by the second one.
  74. ** Example invocation:
  75. ** replace printf puts <replace.c
  76. ** will replace all printf's by puts in replace's source.
  77. **
  78. */
  79.  
  80. int main(int argc, char *argv[])
  81. {
  82.       char buf[200];
  83.       char *Start, *Str;
  84.       size_t BufSiz, BufLeft;
  85.  
  86.       if(argc < 3)
  87.       {
  88.             puts("Usage: STRREPL old_string new_string [buffer_size]");
  89.             return EXIT_FAILURE;
  90.       }
  91.       if (argc > 3)
  92.       {
  93.             BufSiz = atoi(argv[3]);
  94.             if (200 < BufSiz)
  95.                   BufSiz = 200;
  96.       }
  97.       else  BufSiz = 20;            /* Pretend we have a short buffer   */
  98.  
  99.       /* Repeat until all occurrences replaced */
  100.  
  101.       while(NULL != (Start = fgets(buf, 200, stdin)))
  102.       {
  103.             if ('\n' == LAST_CHAR(Start))
  104.                   LAST_CHAR(Start) = NUL;       /* Strip trailing \n    */
  105.             for (Str = Start, BufLeft = BufSiz; Start != NULL; )
  106.             {
  107.                   Start = strrepl(Start, BufLeft, argv[1], argv[2]);
  108.                   if (Start == Str)
  109.                         break;
  110.                   BufLeft -= Start - Str;
  111.                   Str = Start;
  112.             }
  113.                   
  114.             if (NULL == Start)
  115.                   puts("\a*** Modified string will not fit in buffer ***");
  116.             else  printf("%s\n", buf);
  117.       }
  118.       return EXIT_SUCCESS;
  119. }
  120.  
  121. #endif /* TEST */
  122.  
  123.