Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

parse.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright (c) 1999 Michael Graff
00016  * All rights reserved.
00017  * 
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions
00020  * are met:
00021  * 1. Redistributions of source code must retain the above copyright
00022  *    notice, this list of conditions and the following disclaimer.
00023  * 2. Redistributions in binary form must reproduce the above copyright
00024  *    notice, this list of conditions and the following disclaimer in the
00025  *    documentation and/or other materials provided with the distribution.
00026  * 3. Neither the name of the authors nor the names of its contributors
00027  *    may be used to endorse or promote products derived from this software
00028  *    without specific prior written permission.
00029  * 
00030  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
00031  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
00034  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00036  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00037  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00038  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00039  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00040  * SUCH DAMAGE.
00041  */
00042 
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <stdio.h>
00046 
00047 #include "parse.h"
00048 
00050 #if 0
00051 #define dprintf(x) do { printf x; fflush(stdout); } while (0);
00052 #else
00053 #define dprintf(x)
00054 #endif
00055 
00056 /*
00057  * parse_getarg() - get the next argument in the **args list
00058  */
00059 char *
00060 parse_getarg(parse_t *state)
00061 {
00062     char *c;
00063     char *old_current;
00064 
00069     /*
00070      * state->current is set to NULL when the end of string is reached.
00071      */
00072     if (state->current == NULL) {
00073         dprintf(("parse_getarg() returning NULL (1)\n"));
00074         return (NULL);
00075     }
00076 
00077     c = state->current;
00078 
00079     /*
00080      * Skip over any leading whitespace
00081      */
00082     while (*c == state->delim)
00083         c++;
00084 
00085     /*
00086      * If *c == \0, no more arguments were found.
00087      */
00088     if (*c == '\0') {
00089         state->current = NULL;
00090         dprintf(("parse_getarg() returning NULL (2)\n"));
00091         return (NULL);
00092     }
00093 
00094     /*
00095      * We found a non-space non-NUL character.  Scan forward in the
00096      * string, looking for another space or the end of the string.
00097      */
00098     old_current = c;
00099     state->current = c;
00100     while (*c != state->delim && *c != '\0')
00101         c++;
00102 
00103     /*
00104      * If a space was found, change it to NUL, update current to point to
00105      * the character after the space and and return the old current
00106      * value.
00107      */
00108     if (*c == state->delim) {
00109         *c++ = '\0';
00110         state->current = c;
00111         dprintf(("parse_getarg() returning %s\n", old_current));
00112         return (old_current);
00113     }
00114 
00115     /*
00116      * To get here, *c must be NUL, so we have hit the end of the
00117      * string.  This means our current argument is valid, but any more
00118      * calls will return NULL, so change state->current as needed here.
00119      */
00120     state->current = NULL;
00121     dprintf(("parse_getarg() returning %s\n", old_current));
00122     return (old_current);
00123 }
00124 
00125 char *
00126 parse_getallargs(parse_t *state)
00127 {
00128     char *c;
00129     char *e;
00130 
00131     /*
00132      * Already called once, or at end of string?
00133      */
00134     if (state->current == NULL) {
00135         dprintf(("parse_getallargs() returning NULL (1)\n"));
00136         return (NULL);
00137     }
00138 
00139     c = state->current;
00140     state->current = NULL;
00141 
00142     /*
00143      * Skip over any leading whitespace
00144      */
00145     while (*c == state->delim)
00146         c++;
00147 
00148     if (1)
00149     {
00150         /*
00151          * Skip over leading ':'.
00152          *
00153          * XXX should be higher level...
00154          */
00155         if (*c == ':')
00156             c++;
00157 
00158         /*
00159          * Skip over whitespace again, in case we skipped over a colon above.
00160          */
00161         while (*c == state->delim)
00162             c++;
00163     }
00164 
00165     /*
00166      * If the string had nothing BUT spaces, return NULL instead
00167      * of an empty string.
00168      */
00169     if (*c == '\0') {
00170         dprintf(("parse_getallargs() returning NULL (2)\n"));
00171         return (NULL);
00172     }
00173 
00174     /*
00175      * strip off any trailing whitespace
00176      */
00177     e = c + strlen(c) - 1; /* ABC0 would make e point to C, not 0 */
00178     while ((e != c) && (*e == state->delim))
00179         e--;
00180 
00181     /*
00182      * Nothing left in the string?  This SHOULD have been checked for
00183      * above, but check here too.
00184      */
00185     if ((e == c) && (*e == state->delim)) {
00186         dprintf(("parse_getallargs() returning NULL (3)\n"));
00187         return (NULL);
00188     }
00189 
00190     /*
00191      * set the character after 'e' to be NUL.
00192      */
00193     e++;
00194     *e = '\0';
00195 
00196     dprintf(("parse_getallargs() returning %s\n", c));
00197     return (c);
00198 }
00199 
00200 int
00201 parse_init(parse_t *state, char *pointer)
00202 {
00203     if (pointer && !*pointer)
00204         pointer = NULL;
00205 
00206     state->current = pointer;
00207     state->base = pointer;
00208     state->delim = ' ';
00209 
00210     return (0);
00211 }
00212 
00213 void
00214 parse_cleanup(parse_t *state)
00215 {
00216     state->base = NULL;
00217     state->current = NULL;
00218     state->delim = ' ';
00219 }

Generated at Sat Oct 25 20:56:09 2003 for Services using Doxygen.
Services Copyr. 1996-2001 Chip Norkus, Max Byrd, Greg Poma, Michael Graff, James Hess, Dafydd James. All rights reserved See LICENSE for licensing information.