home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / regsub.cpp < prev    next >
C/C++ Source or Header  |  2000-12-13  |  2KB  |  89 lines

  1. /*
  2.  * regsub
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *** THIS IS AN ALTERED VERSION.  It was altered by Sergey I. Yevtushenko,
  21.  *** es@noos.kiev.ua, on 19 Nov 1997, cleanup for C++ compile.
  22.  */
  23.  
  24. #include <string.h>
  25.  
  26. #include <regexp.h>
  27. #include "regmagic.h"
  28.  
  29. #ifndef CHARBITS
  30. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  31. #else
  32. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  33. #endif
  34.  
  35. /*
  36.  - regsub - perform substitutions after a regexp match
  37.  */
  38. void regsub(regexp *prog, char *source, char *dest)
  39. {
  40.     char *src;
  41.     char *dst;
  42.     char c;
  43.     int no;
  44.     int len;
  45.  
  46.     if (prog == NULL || source == NULL || dest == NULL)
  47.     {
  48.         regerror("NULL parm to regsub");
  49.         return;
  50.     }
  51.     if (UCHARAT(prog->program) != MAGIC)
  52.     {
  53.         regerror("damaged regexp fed to regsub");
  54.         return;
  55.     }
  56.  
  57.     src = (char *)source;
  58.     dst = dest;
  59.     while ((c = *src++) != '\0')
  60.     {
  61.         if (c == '&')
  62.             no = 0;
  63.         else if (c == '\\' && '0' <= *src && *src <= '9')
  64.             no = *src++ - '0';
  65.         else
  66.             no = -1;
  67.         if (no < 0)
  68.         {   /* Ordinary character. */
  69.              if (c == '\\' && (*src == '\\' || *src == '&'))
  70.                  c = *src++;
  71.              *dst++ = c;
  72.         }
  73.         else
  74.             if (prog->startp[no] != NULL && prog->endp[no] != NULL)
  75.             {
  76.                 len = prog->endp[no] - prog->startp[no];
  77.                 strncpy(dst, prog->startp[no], len);
  78.                 dst += len;
  79.                 if (len != 0 && *(dst-1) == '\0')
  80.                 { /* strncpy hit NUL. */
  81.                     regerror("damaged match string");
  82.                     return;
  83.                 }
  84.             }
  85.     }
  86.     *dst++ = '\0';
  87. }
  88.  
  89.