RE.c++
上传用户:weiyuanprp
上传日期:2020-05-20
资源大小:1169k
文件大小:3k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. /* $Id: RE.c++,v 1.3 2008/12/08 21:14:33 faxguy Exp $ */
  2. /*
  3.  * Copyright (c) 1994-1996 Sam Leffler
  4.  * Copyright (c) 1994-1996 Silicon Graphics, Inc.
  5.  * HylaFAX is a trademark of Silicon Graphics
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. /*
  27.  * Regular expression support.
  28.  */
  29. #include "RE.h"
  30. RE::RE(const char* pat, int len, int flags)
  31.     : _pattern(pat, len == 0 ? strlen(pat) : len)
  32. {
  33.     init(flags);
  34. }
  35. RE::RE(const fxStr& pat, int flags) : _pattern(pat)
  36. {
  37.     init(flags);
  38. }
  39. RE::RE(const RE& other, int flags)
  40.     : fxObj(other)
  41.     , _pattern(other._pattern)
  42. {
  43.     init(flags);
  44. }
  45. RE::~RE()
  46. {
  47.     regfree(&c_pattern);
  48.     delete [] matches;
  49. }
  50. void
  51. RE::init(int flags)
  52. {
  53.     memset(&c_pattern, 0, sizeof(c_pattern));
  54.     compResult = regcomp(&c_pattern, _pattern, flags);
  55.     if (compResult == 0) {
  56. matches = new regmatch_t[c_pattern.re_nsub+1];
  57. execResult = REG_NOMATCH;
  58.     } else {
  59. matches = NULL;
  60. execResult = compResult;
  61.     }
  62. }
  63. bool
  64. RE::Find(const char* text, u_int length, u_int off)
  65. {
  66.     if (compResult == 0) {
  67. /*
  68.  * These two checks are for compatibility with the old
  69.  * InterViews code; yech (but the DialRules logic needs it).
  70.  */
  71. if (((off || length) && off >= length) || (off && _pattern[0] == '^'))
  72.     execResult = REG_NOMATCH;
  73. else {
  74.     matches[0].rm_so = off;
  75.     matches[0].rm_eo = length;
  76.     execResult = regexec(&c_pattern, text, c_pattern.re_nsub+1,
  77.     matches, REG_STARTEND);
  78. }
  79.     }
  80.     return (execResult == 0);
  81. }
  82. void
  83. RE::getError(fxStr& emsg) const
  84. {
  85.     char buf[1024];
  86.     (void) regerror(execResult, &c_pattern, buf, sizeof (buf));
  87.     emsg = buf;
  88. }
  89. int
  90. RE::StartOfMatch(u_int ix) const
  91. {
  92.     if (execResult != 0)
  93. return (execResult);
  94.     return (ix <= c_pattern.re_nsub ? matches[ix].rm_so : -1);
  95. }
  96. int
  97. RE::EndOfMatch(u_int ix) const
  98. {
  99.     if (execResult != 0)
  100. return (execResult);
  101.     return (ix <= c_pattern.re_nsub ? matches[ix].rm_eo : -1);
  102. }
  103. REPtr::~REPtr() { destroy(); }
  104. void REPtr::destroy() { if (p) p->dec(); }
  105. REPtr&
  106. REPtr::operator=(const REPtr& other)
  107. {
  108.     if (p != other.p) {
  109. destroy();
  110. p = other.p ? (other.p->inc(),other.p) : 0;
  111.     }
  112.     return *this;
  113. }
  114. REPtr&
  115. REPtr::operator=(RE* tp)
  116. {
  117.     if (p != tp) {
  118. destroy();
  119. p = tp ? (tp->inc(),tp) : 0;
  120.     }
  121.     return *this;
  122. }