Debian Codegraph @

libhylafax/RE.c++

raw
1 /* $Id$ */
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 /*
28 * Regular expression support.
29 */
30 #include "RE.h"
31
32 RE::RE(const char* pat, int len, int flags)
33 : _pattern(pat, len == 0 ? strlen(pat) : len)
34 {
35 init(flags);
36 }
37 RE::RE(const fxStr& pat, int flags) : _pattern(pat)
38 {
39 init(flags);
40 }
41 RE::RE(const RE& other, int flags)
42 : fxObj(other)
43 , _pattern(other._pattern)
44 {
45 init(flags);
46 }
47 RE::~RE()
48 {
49 regfree(&c_pattern);
50 delete [] matches;
51 }
52
53 void
54 RE::init(int flags)
55 {
56 compResult = regcomp(&c_pattern, _pattern, flags);
57 if (compResult == 0) {
58 matches = new regmatch_t[c_pattern.re_nsub+1];
59 execResult = REG_NOMATCH;
60 } else {
61 matches = NULL;
62 execResult = compResult;
63 }
64 }
65
66 bool
67 RE::Find(const char* text, u_int length, u_int off)
68 {
69 if (compResult == 0) {
70 /*
71 * These two checks are for compatibility with the old
72 * InterViews code; yech (but the DialRules logic needs it).
73 */
74 if (((off || length) && off >= length) || (off && _pattern[0] == '^'))
75 execResult = REG_NOMATCH;
76 else {
77 matches[0].rm_so = off;
78 matches[0].rm_eo = length;
79 execResult = regexec(&c_pattern, text, c_pattern.re_nsub+1,
80 matches, REG_STARTEND);
81 }
82 }
83 return (execResult == 0);
84 }
85
86 void
87 RE::getError(fxStr& emsg) const
88 {
89 char buf[1024];
90 (void) regerror(execResult, &c_pattern, buf, sizeof (buf));
91 emsg = buf;
92 }
93
94 int
95 RE::StartOfMatch(u_int ix) const
96 {
97 if (execResult != 0)
98 return (execResult);
99 return (ix <= c_pattern.re_nsub ? matches[ix].rm_so : -1);
100 }
101
102 int
103 RE::EndOfMatch(u_int ix) const
104 {
105 if (execResult != 0)
106 return (execResult);
107 return (ix <= c_pattern.re_nsub ? matches[ix].rm_eo : -1);
108 }
109
110 REPtr::~REPtr() { destroy(); }
111 void REPtr::destroy() { if (p) p->dec(); }
112
113 REPtr&
114 REPtr::operator=(const REPtr& other)
115 {
116 if (p != other.p) {
117 destroy();
118 p = other.p ? (other.p->inc(),other.p) : 0;
119 }
120 return *this;
121 }
122
123 REPtr&
124 REPtr::operator=(RE* tp)
125 {
126 if (p != tp) {
127 destroy();
128 p = tp ? (tp->inc(),tp) : 0;
129 }
130 return *this;
131 }