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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* 
  4. **  sstrdel() - Delete multiple substrings
  5. **
  6. **  public domain by Shamim Islam
  7. **
  8. **  Usage: sstrdel(char * s,char * del0,del1...deln)
  9. **
  10. **  Remarks: sstrdel takes a string s, and removes all occurrences of
  11. **           the substrings del0, del1, ... deln
  12. **  
  13. **  Return:  sstrdel returns a pointer to the string s, unless s is NULL.
  14. **           sstrdel will return a NULL for this exception.
  15. **
  16. **  Comment: Use sstrdel to remove a list of substrings from a string.
  17. **
  18. **           sstrdel matches the largest substring for deletion, if more than
  19. **           one substring matches a particular portion of the string s.
  20. **
  21. **  NOTE:    The last element in the variable substring list MUST be NULL
  22. **           or your program will have a high likelihood of hanging.
  23. */
  24.  
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include "snip_str.h"
  28.  
  29. #if defined(__cplusplus) && __cplusplus
  30.  extern "C" {
  31. #endif
  32.  
  33. char *sstrdel(char *s, ...)
  34. {
  35.       /* Find out how many arguments are present */
  36.  
  37.       int c = 0;
  38.       va_list ap, hold;
  39.  
  40.       if (s == NULL)
  41.             return NULL;
  42.       va_start(ap, s);
  43.       memcpy(&hold, &ap, sizeof(va_list));
  44.       while (va_arg(ap, char*) != NULL)
  45.             c++;
  46.       va_end(ap);
  47.       if (c)
  48.       {
  49.             /* Assign pointers  */
  50.  
  51.             char *r = s,*n = s; 
  52.             char *p;
  53.             int len, i;
  54.  
  55.             /* Copy next character to result */
  56.             /* And then check for matches if */
  57.             /* not at end of string          */
  58.  
  59.             while ((*r = *n) != 0)
  60.             {
  61.                   int l = 0;
  62.  
  63.                   /* Substitute for va_start(ap,s) */
  64.  
  65.                   memcpy(&ap, &hold, sizeof(va_list));
  66.                   for (i = 0; i < c; i++)
  67.                   {
  68.                         /* Initialise the pointer and the length    */
  69.  
  70.                         len = strlen(p = va_arg(ap, char*));
  71.  
  72.                         /* Compare ONLY if we haven't found one yet */
  73.                         /* or if this one is bigger than the one we */
  74.                         /* found AND this arg has a length > 0      */
  75.  
  76.                         if(len > 0 && (l == 0 || len> l) &&
  77.                               strncmp(n, p, len) == 0)
  78.                         {
  79.                               l = len;
  80.                         }
  81.                   }
  82.                   va_end(ap);
  83.                   if (l)
  84.                         n += l;
  85.                   else  n++, r++;
  86.             }
  87.       }
  88.       return s;
  89. }
  90.  
  91. #if defined(__cplusplus) && __cplusplus
  92.  }
  93. #endif
  94.  
  95. #ifdef TEST
  96.  
  97. #include <stdio.h>
  98.  
  99. main()
  100. {
  101.       char *testr = "The quick brown fox jumps over the lazy dog.";
  102.       char *s1 = "quick", *s2 = "over", *s3 = "lazy";
  103.       char *s4 = "he", *s5 = "xyz";
  104.  
  105.       printf("sstrdel(\"%s\",\"%s\",\"%s\",\"%s\")\n returned ",
  106.             testr, s1, s2, s3);
  107.       printf("\"%s\"\n", sstrdel(testr, s1, s2, s3, NULL));
  108.  
  109.       printf("sstrdel(\"%s\",\"%s\",\"%s\")\n returned ",
  110.             testr, s4, s5);
  111.       printf("\"%s\"\n", sstrdel(testr, s4, s5, NULL));
  112.       return 0;
  113. }
  114.  
  115. #endif /* TEST */
  116.