home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / adt / regexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-11  |  3.5 KB  |  181 lines

  1. /*
  2.  *  regexp.c -- regular expression handling code.
  3.  *
  4.  *    $Header: /private/postgres/src/utils/adt/RCS/regexp.c,v 1.8 1991/11/11 22:39:44 mer Exp $
  5.  */
  6. #include "tmp/postgres.h"    /* postgres system include file */
  7. #include "utils/log.h"        /* for logging postgres errors */
  8.  
  9. /*
  10.  *  macros to support the regexp(3) library calls
  11.  */
  12. #define INIT        register char *p = instring;
  13. #define GETC()        (*p++)
  14. #define PEEKC()        *(p+1)
  15. #define    UNGETC(c)    (*--p = (c))
  16. #define    RETURN(v)    return(v)
  17. #define    ERROR(val)    elog(WARN, "regexp library reports error %d", (val));
  18.  
  19. #define    EXPBUFSZ    256
  20. #define    PCHARLEN    16
  21.  
  22. #ifdef linux
  23. #include <regex.h>
  24. #else
  25. #ifdef sprite
  26. #include "regexp.h"
  27. #else
  28. #include <regexp.h>
  29. #endif /* sprite */
  30. #endif /* linux */
  31.  
  32. /*
  33.  *  interface routines called by the function manager
  34.  */
  35.  
  36. /*
  37.  *  routines that use the regexp stuff
  38.  */
  39. bool
  40. char16regexeq(s, p)
  41.     char *s;
  42.     char *p;
  43. {
  44. #ifdef linux
  45.     struct re_pattern_buffer rpb;
  46. #else
  47.     char *expbuf, *endbuf;
  48. #endif
  49.     char *sterm, *pterm;
  50.     int result;
  51.  
  52.     if (!s || !p)
  53.         return FALSE;
  54.  
  55. #ifdef linux
  56.     memset((void *) &rpb, 0, sizeof(rpb));
  57.     rpb.buffer = (unsigned char *) palloc(EXPBUFSZ);
  58.     rpb.allocated = EXPBUFSZ;
  59. #else
  60.     expbuf = (char *) palloc(EXPBUFSZ);
  61.     endbuf = expbuf + (EXPBUFSZ - 1);
  62. #endif
  63.  
  64.     /* be sure the strings are null-terminated */
  65.     sterm = (char *) palloc(PCHARLEN + 1);
  66.     bzero(sterm, PCHARLEN + 1);
  67.     strncpy(sterm, s, PCHARLEN);
  68.     pterm = (char *) palloc(PCHARLEN + 1);
  69.     bzero(pterm, PCHARLEN + 1);
  70.     strncpy(pterm, p, PCHARLEN);
  71.  
  72.     /* compile the re */
  73. #ifdef linux
  74.     (void) re_compile_pattern(pterm, strlen(pterm), &rpb);
  75. #else
  76.     (void) compile(pterm, expbuf, endbuf, NULL);
  77. #endif
  78.  
  79.     /* do the regexp matching */
  80. #ifdef linux
  81.     result = re_search(&rpb, sterm, strlen(sterm), 0, strlen(sterm), 0);
  82.     pfree(rpb.buffer);
  83. #else
  84.     result = step(sterm, expbuf);
  85.     pfree(expbuf);
  86. #endif
  87.  
  88.     pfree(sterm);
  89.     pfree(pterm);
  90.  
  91. #ifdef linux
  92.     return (bool)(result >= 0);
  93. #else
  94.     return ((bool) result);
  95. #endif
  96. }
  97.  
  98. bool
  99. char16regexne(s, p)
  100.     char *s;
  101.     char *p;
  102. {
  103.     return (!char16regexeq(s, p));
  104. }
  105.  
  106. bool
  107. textregexeq(s, p)
  108.     struct varlena *s;
  109.     struct varlena *p;
  110. {
  111. #ifdef linux
  112.     struct re_pattern_buffer rpb;
  113. #else
  114.     char *expbuf, *endbuf;
  115. #endif
  116.     char *sbuf, *pbuf;
  117.     int result;
  118.  
  119.     if (!s || !p)
  120.         return FALSE;
  121.  
  122.     /* ---------------
  123.      * text is a varlena, not a string so we have to make 
  124.      * a string from the vl_data field of the struct. 
  125.      * jeff 13 July 1991
  126.      * ---------------
  127.      */
  128.     
  129.     /* palloc the length of the text + the null character */
  130.     sbuf = (char *) palloc(s->vl_len - sizeof(int32) + 1);
  131.     pbuf = (char *) palloc(p->vl_len - sizeof(int32) + 1);
  132.  
  133. #ifdef linux
  134.     memset((void *) &rpb, 0, sizeof(rpb));
  135.     rpb.buffer = (unsigned char *) palloc(EXPBUFSZ);
  136.     rpb.allocated = EXPBUFSZ;
  137. #else
  138.     expbuf = (char *) palloc(EXPBUFSZ);
  139.     endbuf = expbuf + (EXPBUFSZ - 1);
  140. #endif
  141.  
  142.     bcopy(s->vl_dat, sbuf, s->vl_len - sizeof(int32));
  143.     bcopy(p->vl_dat, pbuf, p->vl_len - sizeof(int32));
  144.     *(sbuf + s->vl_len - sizeof(int32)) = (char)NULL;
  145.     *(pbuf + p->vl_len - sizeof(int32)) = (char)NULL;
  146.  
  147.  
  148.     /* compile the re */
  149. #ifdef linux
  150.     (void) re_compile_pattern(pbuf, strlen(pbuf), &rpb);
  151. #else
  152.     (void) compile(pbuf, expbuf, endbuf, NULL);
  153. #endif
  154.  
  155.     /* do the regexp matching */
  156. #ifdef linux
  157.     result = re_search(&rpb, sbuf, strlen(sbuf), 0, strlen(sbuf), 0);
  158.     pfree(rpb.buffer);
  159. #else
  160.     result = step(sbuf, expbuf);
  161.     pfree(expbuf);
  162. #endif
  163.  
  164.     pfree(sbuf);
  165.     pfree(pbuf);
  166.  
  167. #ifdef linux
  168.     return (bool)(result >= 0);
  169. #else
  170.     return ((bool) result);
  171. #endif
  172. }
  173.  
  174. bool
  175. textregexne(s, p)
  176.     char *s;
  177.     char *p;
  178. {
  179.     return (!textregexeq(s, p));
  180. }
  181.