1 |
/* $Header: /cvsroot/esrg/sfesrg/esrgpcpj/shared/tcl_base/regc_cvec.c,v 1.1.1.1 2001/06/13 04:31:42 dtashley Exp $ */
|
2 |
|
3 |
/*
|
4 |
* Utility functions for handling cvecs
|
5 |
* This file is #included by regcomp.c.
|
6 |
*
|
7 |
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
|
8 |
*
|
9 |
* Development of this software was funded, in part, by Cray Research Inc.,
|
10 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
|
11 |
* Corporation, none of whom are responsible for the results. The author
|
12 |
* thanks all of them.
|
13 |
*
|
14 |
* Redistribution and use in source and binary forms -- with or without
|
15 |
* modification -- are permitted for any purpose, provided that
|
16 |
* redistributions in source form retain this entire copyright notice and
|
17 |
* indicate the origin and nature of any modifications.
|
18 |
*
|
19 |
* I'd appreciate being given credit for this package in the documentation
|
20 |
* of software which uses it, but that is not a requirement.
|
21 |
*
|
22 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
23 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
24 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
25 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
26 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 |
*
|
33 |
*/
|
34 |
|
35 |
/*
|
36 |
- newcvec - allocate a new cvec
|
37 |
^ static struct cvec *newcvec(int, int, int);
|
38 |
*/
|
39 |
static struct cvec *
|
40 |
newcvec(nchrs, nranges, nmcces)
|
41 |
int nchrs; /* to hold this many chrs... */
|
42 |
int nranges; /* ... and this many ranges... */
|
43 |
int nmcces; /* ... and this many MCCEs */
|
44 |
{
|
45 |
size_t n;
|
46 |
size_t nc;
|
47 |
struct cvec *cv;
|
48 |
|
49 |
nc = (size_t)nchrs + (size_t)nmcces*(MAXMCCE+1) + (size_t)nranges*2;
|
50 |
n = sizeof(struct cvec) + (size_t)(nmcces-1)*sizeof(chr *) +
|
51 |
nc*sizeof(chr);
|
52 |
cv = (struct cvec *)MALLOC(n);
|
53 |
if (cv == NULL)
|
54 |
return NULL;
|
55 |
cv->chrspace = nc;
|
56 |
cv->chrs = (chr *)&cv->mcces[nmcces]; /* chrs just after MCCE ptrs */
|
57 |
cv->mccespace = nmcces;
|
58 |
cv->ranges = cv->chrs + nchrs + nmcces*(MAXMCCE+1);
|
59 |
cv->rangespace = nranges;
|
60 |
return clearcvec(cv);
|
61 |
}
|
62 |
|
63 |
/*
|
64 |
- clearcvec - clear a possibly-new cvec
|
65 |
* Returns pointer as convenience.
|
66 |
^ static struct cvec *clearcvec(struct cvec *);
|
67 |
*/
|
68 |
static struct cvec *
|
69 |
clearcvec(cv)
|
70 |
struct cvec *cv;
|
71 |
{
|
72 |
int i;
|
73 |
|
74 |
assert(cv != NULL);
|
75 |
cv->nchrs = 0;
|
76 |
assert(cv->chrs == (chr *)&cv->mcces[cv->mccespace]);
|
77 |
cv->nmcces = 0;
|
78 |
cv->nmccechrs = 0;
|
79 |
cv->nranges = 0;
|
80 |
for (i = 0; i < cv->mccespace; i++)
|
81 |
cv->mcces[i] = NULL;
|
82 |
|
83 |
return cv;
|
84 |
}
|
85 |
|
86 |
/*
|
87 |
- addchr - add a chr to a cvec
|
88 |
^ static VOID addchr(struct cvec *, pchr);
|
89 |
*/
|
90 |
static VOID
|
91 |
addchr(cv, c)
|
92 |
struct cvec *cv;
|
93 |
pchr c;
|
94 |
{
|
95 |
assert(cv->nchrs < cv->chrspace - cv->nmccechrs);
|
96 |
cv->chrs[cv->nchrs++] = (chr)c;
|
97 |
}
|
98 |
|
99 |
/*
|
100 |
- addrange - add a range to a cvec
|
101 |
^ static VOID addrange(struct cvec *, pchr, pchr);
|
102 |
*/
|
103 |
static VOID
|
104 |
addrange(cv, from, to)
|
105 |
struct cvec *cv;
|
106 |
pchr from;
|
107 |
pchr to;
|
108 |
{
|
109 |
assert(cv->nranges < cv->rangespace);
|
110 |
cv->ranges[cv->nranges*2] = (chr)from;
|
111 |
cv->ranges[cv->nranges*2 + 1] = (chr)to;
|
112 |
cv->nranges++;
|
113 |
}
|
114 |
|
115 |
/*
|
116 |
- addmcce - add an MCCE to a cvec
|
117 |
^ static VOID addmcce(struct cvec *, chr *, chr *);
|
118 |
*/
|
119 |
static VOID
|
120 |
addmcce(cv, startp, endp)
|
121 |
struct cvec *cv;
|
122 |
chr *startp; /* beginning of text */
|
123 |
chr *endp; /* just past end of text */
|
124 |
{
|
125 |
int len;
|
126 |
int i;
|
127 |
chr *s;
|
128 |
chr *d;
|
129 |
|
130 |
if (startp == NULL && endp == NULL)
|
131 |
return;
|
132 |
len = endp - startp;
|
133 |
assert(len > 0);
|
134 |
assert(cv->nchrs + len < cv->chrspace - cv->nmccechrs);
|
135 |
assert(cv->nmcces < cv->mccespace);
|
136 |
d = &cv->chrs[cv->chrspace - cv->nmccechrs - len - 1];
|
137 |
cv->mcces[cv->nmcces++] = d;
|
138 |
for (s = startp, i = len; i > 0; s++, i--)
|
139 |
*d++ = *s;
|
140 |
*d++ = 0; /* endmarker */
|
141 |
assert(d == &cv->chrs[cv->chrspace - cv->nmccechrs]);
|
142 |
cv->nmccechrs += len + 1;
|
143 |
}
|
144 |
|
145 |
/*
|
146 |
- haschr - does a cvec contain this chr?
|
147 |
^ static int haschr(struct cvec *, pchr);
|
148 |
*/
|
149 |
static int /* predicate */
|
150 |
haschr(cv, c)
|
151 |
struct cvec *cv;
|
152 |
pchr c;
|
153 |
{
|
154 |
int i;
|
155 |
chr *p;
|
156 |
|
157 |
for (p = cv->chrs, i = cv->nchrs; i > 0; p++, i--)
|
158 |
if (*p == c)
|
159 |
return 1;
|
160 |
for (p = cv->ranges, i = cv->nranges; i > 0; p += 2, i--)
|
161 |
if (*p <= c && c <= *(p+1))
|
162 |
return 1;
|
163 |
return 0;
|
164 |
}
|
165 |
|
166 |
/*
|
167 |
- getcvec - get a cvec, remembering it as v->cv
|
168 |
^ static struct cvec *getcvec(struct vars *, int, int, int);
|
169 |
*/
|
170 |
static struct cvec *
|
171 |
getcvec(v, nchrs, nranges, nmcces)
|
172 |
struct vars *v;
|
173 |
int nchrs; /* to hold this many chrs... */
|
174 |
int nranges; /* ... and this many ranges... */
|
175 |
int nmcces; /* ... and this many MCCEs */
|
176 |
{
|
177 |
if (v->cv != NULL && nchrs <= v->cv->chrspace &&
|
178 |
nranges <= v->cv->rangespace &&
|
179 |
nmcces <= v->cv->mccespace)
|
180 |
return clearcvec(v->cv);
|
181 |
|
182 |
if (v->cv != NULL)
|
183 |
freecvec(v->cv);
|
184 |
v->cv = newcvec(nchrs, nranges, nmcces);
|
185 |
if (v->cv == NULL)
|
186 |
ERR(REG_ESPACE);
|
187 |
|
188 |
return v->cv;
|
189 |
}
|
190 |
|
191 |
/*
|
192 |
- freecvec - free a cvec
|
193 |
^ static VOID freecvec(struct cvec *);
|
194 |
*/
|
195 |
static VOID
|
196 |
freecvec(cv)
|
197 |
struct cvec *cv;
|
198 |
{
|
199 |
FREE(cv);
|
200 |
}
|
201 |
|
202 |
/* $History: regc_cvec.c $
|
203 |
*
|
204 |
* ***************** Version 1 *****************
|
205 |
* User: Dtashley Date: 1/02/01 Time: 12:03a
|
206 |
* Created in $/IjuScripter, IjuConsole/Source/Tcl Base
|
207 |
* Initial check-in.
|
208 |
*/
|
209 |
|
210 |
/* End of REGC_CVEC.C */ |