regc_cvec.c

Go to the documentation of this file.
00001 /*
00002  * Utility functions for handling cvecs
00003  * This file is #included by regcomp.c.
00004  *
00005  * Copyright (c) 1998, 1999 Henry Spencer.  All rights reserved.
00006  *
00007  * Development of this software was funded, in part, by Cray Research Inc.,
00008  * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
00009  * Corporation, none of whom are responsible for the results. The author
00010  * thanks all of them.
00011  *
00012  * Redistribution and use in source and binary forms -- with or without
00013  * modification -- are permitted for any purpose, provided that
00014  * redistributions in source form retain this entire copyright notice and
00015  * indicate the origin and nature of any modifications.
00016  *
00017  * I'd appreciate being given credit for this package in the documentation of
00018  * software which uses it, but that is not a requirement.
00019  *
00020  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00021  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00022  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00023  * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00026  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00027  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00028  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00029  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 /*
00033  * Notes:
00034  * Only (selected) functions in _this_ file should treat chr* as non-constant.
00035  */
00036 
00037 /*
00038  - newcvec - allocate a new cvec
00039  ^ static struct cvec *newcvec(int, int);
00040  */
00041 static struct cvec *
00042 newcvec(
00043     int nchrs,                  /* to hold this many chrs... */
00044     int nranges)                /* ... and this many ranges... */
00045 {
00046     size_t nc = (size_t)nchrs + (size_t)nranges*2;
00047     size_t n = sizeof(struct cvec) + nc*sizeof(chr);
00048     struct cvec *cv = (struct cvec *) MALLOC(n);
00049 
00050     if (cv == NULL) {
00051         return NULL;
00052     }
00053     cv->chrspace = nchrs;
00054     cv->chrs = (chr *)(((char *)cv)+sizeof(struct cvec));
00055     cv->ranges = cv->chrs + nchrs;
00056     cv->rangespace = nranges;
00057     return clearcvec(cv);
00058 }
00059 
00060 /*
00061  - clearcvec - clear a possibly-new cvec
00062  * Returns pointer as convenience.
00063  ^ static struct cvec *clearcvec(struct cvec *);
00064  */
00065 static struct cvec *
00066 clearcvec(
00067     struct cvec *cv)            /* character vector */
00068 {
00069     assert(cv != NULL);
00070     cv->nchrs = 0;
00071     cv->nranges = 0;
00072     return cv;
00073 }
00074 
00075 /*
00076  - addchr - add a chr to a cvec
00077  ^ static VOID addchr(struct cvec *, pchr);
00078  */
00079 static void
00080 addchr(
00081     struct cvec *cv,            /* character vector */
00082     pchr c)                     /* character to add */
00083 {
00084     cv->chrs[cv->nchrs++] = (chr)c;
00085 }
00086 
00087 /*
00088  - addrange - add a range to a cvec
00089  ^ static VOID addrange(struct cvec *, pchr, pchr);
00090  */
00091 static void
00092 addrange(
00093     struct cvec *cv,            /* character vector */
00094     pchr from,                  /* first character of range */
00095     pchr to)                    /* last character of range */
00096 {
00097     assert(cv->nranges < cv->rangespace);
00098     cv->ranges[cv->nranges*2] = (chr)from;
00099     cv->ranges[cv->nranges*2 + 1] = (chr)to;
00100     cv->nranges++;
00101 }
00102 
00103 /*
00104  - getcvec - get a cvec, remembering it as v->cv
00105  ^ static struct cvec *getcvec(struct vars *, int, int);
00106  */
00107 static struct cvec *
00108 getcvec(
00109     struct vars *v,             /* context */
00110     int nchrs,                  /* to hold this many chrs... */
00111     int nranges)                /* ... and this many ranges... */
00112 {
00113     if ((v->cv != NULL) && (nchrs <= v->cv->chrspace) &&
00114             (nranges <= v->cv->rangespace)) {
00115         return clearcvec(v->cv);
00116     }
00117 
00118     if (v->cv != NULL) {
00119         freecvec(v->cv);
00120     }
00121     v->cv = newcvec(nchrs, nranges);
00122     if (v->cv == NULL) {
00123         ERR(REG_ESPACE);
00124     }
00125 
00126     return v->cv;
00127 }
00128 
00129 /*
00130  - freecvec - free a cvec
00131  ^ static VOID freecvec(struct cvec *);
00132  */
00133 static void
00134 freecvec(
00135     struct cvec *cv)            /* character vector */
00136 {
00137     FREE(cv);
00138 }
00139 
00140 /*
00141  * Local Variables:
00142  * mode: c
00143  * c-basic-offset: 4
00144  * fill-column: 78
00145  * End:
00146  */



Generated on Wed Mar 12 12:18:10 2008 by  doxygen 1.5.1