Debian Codegraph @

generic/regc_cvec.c

raw
1 /*
2 * Utility functions for handling cvecs
3 * This file is #included by regcomp.c.
4 *
5 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
6 *
7 * Development of this software was funded, in part, by Cray Research Inc.,
8 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
9 * Corporation, none of whom are responsible for the results. The author
10 * thanks all of them.
11 *
12 * Redistribution and use in source and binary forms -- with or without
13 * modification -- are permitted for any purpose, provided that
14 * redistributions in source form retain this entire copyright notice and
15 * indicate the origin and nature of any modifications.
16 *
17 * I'd appreciate being given credit for this package in the documentation of
18 * software which uses it, but that is not a requirement.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Notes:
34 * Only (selected) functions in _this_ file should treat chr* as non-constant.
35 */
36
37 /*
38 - newcvec - allocate a new cvec
39 ^ static struct cvec *newcvec(int, int);
40 */
41 static struct cvec *
42 newcvec(
43 int nchrs, /* to hold this many chrs... */
44 int nranges) /* ... and this many ranges... */
45 {
46 size_t nc = (size_t)nchrs + (size_t)nranges*2;
47 size_t n = sizeof(struct cvec) + nc*sizeof(chr);
48 struct cvec *cv = (struct cvec *) MALLOC(n);
49
50 if (cv == NULL) {
51 return NULL;
52 }
53 cv->chrspace = nchrs;
54 cv->chrs = (chr *)(((char *)cv)+sizeof(struct cvec));
55 cv->ranges = cv->chrs + nchrs;
56 cv->rangespace = nranges;
57 return clearcvec(cv);
58 }
59
60 /*
61 - clearcvec - clear a possibly-new cvec
62 * Returns pointer as convenience.
63 ^ static struct cvec *clearcvec(struct cvec *);
64 */
65 static struct cvec *
66 clearcvec(
67 struct cvec *cv) /* character vector */
68 {
69 assert(cv != NULL);
70 cv->nchrs = 0;
71 cv->nranges = 0;
72 return cv;
73 }
74
75 /*
76 - addchr - add a chr to a cvec
77 ^ static void addchr(struct cvec *, pchr);
78 */
79 static void
80 addchr(
81 struct cvec *cv, /* character vector */
82 pchr c) /* character to add */
83 {
84 assert(cv->nchrs < cv->chrspace);
85 cv->chrs[cv->nchrs++] = (chr)c;
86 }
87
88 /*
89 - addrange - add a range to a cvec
90 ^ static void addrange(struct cvec *, pchr, pchr);
91 */
92 static void
93 addrange(
94 struct cvec *cv, /* character vector */
95 pchr from, /* first character of range */
96 pchr to) /* last character of range */
97 {
98 assert(cv->nranges < cv->rangespace);
99 cv->ranges[cv->nranges*2] = (chr)from;
100 cv->ranges[cv->nranges*2 + 1] = (chr)to;
101 cv->nranges++;
102 }
103
104 /*
105 - getcvec - get a cvec, remembering it as v->cv
106 ^ static struct cvec *getcvec(struct vars *, int, int);
107 */
108 static struct cvec *
109 getcvec(
110 struct vars *v, /* context */
111 int nchrs, /* to hold this many chrs... */
112 int nranges) /* ... and this many ranges... */
113 {
114 if ((v->cv != NULL) && (nchrs <= v->cv->chrspace) &&
115 (nranges <= v->cv->rangespace)) {
116 return clearcvec(v->cv);
117 }
118
119 if (v->cv != NULL) {
120 freecvec(v->cv);
121 }
122 v->cv = newcvec(nchrs, nranges);
123 if (v->cv == NULL) {
124 ERR(REG_ESPACE);
125 }
126
127 return v->cv;
128 }
129
130 /*
131 - freecvec - free a cvec
132 ^ static void freecvec(struct cvec *);
133 */
134 static void
135 freecvec(
136 struct cvec *cv) /* character vector */
137 {
138 FREE(cv);
139 }
140
141 /*
142 * Local Variables:
143 * mode: c
144 * c-basic-offset: 4
145 * fill-column: 78
146 * End:
147 */