Main Page | Data Structures | File List | Data Fields | Globals

parse.c

Go to the documentation of this file.
00001 /*
00002  * luau (Lib Update/Auto-Update): Simple Update Library
00003  * Copyright (C) 2003  David Eklund
00004  *
00005  * - This library is free software; you can redistribute it and/or             -
00006  * - modify it under the terms of the GNU Lesser General Public                -
00007  * - License as published by the Free Software Foundation; either              -
00008  * - version 2.1 of the License, or (at your option) any later version.        -
00009  * -                                                                           -
00010  * - This library is distributed in the hope that it will be useful,           -
00011  * - but WITHOUT ANY WARRANTY; without even the implied warranty of            -
00012  * - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         -
00013  * - Lesser General Public License for more details.                           -
00014  * -                                                                           -
00015  * - You should have received a copy of the GNU Lesser General Public          -
00016  * - License along with this library; if not, write to the Free Software       -
00017  * - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA -
00018  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #  include <config.h>
00022 #endif
00023 
00024 #include <string.h>
00025 #include <glib.h>
00026 #include <ctype.h>
00027 
00028 #include "parse.h"
00029 #include "util.h"
00030 #include "error.h"
00031 
00032 #ifdef WITH_DMALLOC
00033 #  include <dmalloc.h>
00034 #endif
00035 
00045 LUAU_parsedLine *
00046 lutil_parse_parseLine(char *input) {
00047         LUAU_parsedLine *parsed = (LUAU_parsedLine*) malloc(sizeof(LUAU_parsedLine));
00048         char *arg, *ptr;
00049         
00050         parsed->args = g_ptr_array_new();
00051         
00052         parsed->keyword = lutil_parse_nextToken_r(input, &ptr);
00053         while ((arg = lutil_parse_nextToken_r(NULL, &ptr)) != NULL)
00054                 g_ptr_array_add(parsed->args, arg);
00055         
00056         return parsed;
00057 }
00058 
00075 char
00076 lutil_parse_parseSymbol(const char *input, const GPtrArray *symbols) {
00077         char *curr;
00078         char result = -1;
00079         int i;
00080         
00081         for (i = 0; i < symbols->len; i += 2) {
00082                 curr = g_ptr_array_index(symbols, i);
00083                 if (lutil_streq(input, curr)) {
00084                         result = GPOINTER_TO_INT (g_ptr_array_index(symbols, i+1));
00085                         break;
00086                 }
00087         }
00088         
00089         return result;
00090 }
00091 
00102 char
00103 lutil_parse_parseSymbolArray(const char *input, const char *symbols[]) {
00104         const char *curr;
00105         char result = -1;
00106         int i;
00107         
00108         for (i = 0; symbols[i] != NULL; i += 2) {
00109                 curr = symbols[i];
00110                 if (lutil_streq(input, curr)) {
00111                         result = (symbols[i+1])[0];
00112                         break;
00113                 }
00114         }
00115         
00116         return result;
00117 }
00118 
00125 void
00126 lutil_parse_freeParsedLine(LUAU_parsedLine *line) {
00127         int i;
00128         
00129         if (line != NULL) {
00130                 g_free(line->keyword);
00131                 
00132                 for (i = 0; i < line->args->len; ++i)
00133                         g_free(g_ptr_array_index(line->args, i));
00134                 
00135                 g_ptr_array_free(line->args, TRUE);
00136                 
00137                 g_free(line);
00138         }
00139 }
00140 
00153 char *
00154 lutil_parse_nextToken(char *input) {
00155         static char *currentString = NULL;
00156         
00157         return lutil_parse_nextToken_r(input, &currentString);
00158 }
00159 
00160 char *
00161 lutil_parse_nextToken_r(char *input, char **ptrptr) {
00162         char quoted = 0, *temp;
00163         GString *result = g_string_new("");
00164         gboolean escaped = FALSE, append;
00165         
00166         if (input != NULL)
00167                 *ptrptr = input;
00168         if (*ptrptr == NULL) {
00169                 ERROR("No input specified!");
00170                 return NULL;
00171         }
00172         
00173         /* skip leading whitespace */
00174         while (**ptrptr == ' ') 
00175                 ++*ptrptr;
00176         
00177         while (**ptrptr != '\0') {
00178                 if (escaped) {
00179                         /* if given an escaped sequence of anything other than '\\' or '\"' inside a doubly quoted string or
00180                          * a '\'' inside a singly quoted string, we pass the back-slash through. */
00181                         if (**ptrptr != '\\' && **ptrptr != quoted)
00182                                 g_string_append_c(result, '\\');
00183                         
00184                         append = TRUE;
00185                         escaped = FALSE;
00186                 } else if (quoted) {
00187                         if (**ptrptr == quoted) {
00188                                 append = FALSE;
00189                                 quoted = 0;
00190                         } else {
00191                                 append = TRUE;
00192                         }
00193                 } else {
00194                         if (**ptrptr == ' ') {
00195                                 ++*ptrptr;
00196                                 break;
00197                         } else if (**ptrptr == '\\') {
00198                                 escaped = TRUE;
00199                                 append = FALSE;
00200                         } else if (**ptrptr == '"' || **ptrptr == '\'') {
00201                                 quoted = **ptrptr;
00202                                 append = FALSE;
00203                         } else {
00204                                 append = TRUE;
00205                         }
00206                 }
00207                 if (append)
00208                         g_string_append_c(result, **ptrptr);
00209                 
00210                 ++*ptrptr;
00211         }
00212         
00213         if (quoted) 
00214                 ERROR("Quote not closed - multi-line quotes not supported");
00215         if (escaped)
00216                 ERROR("Back-slash at end of line - not supported");
00217         
00218         temp = result->str;
00219         g_string_free(result, FALSE);
00220         
00221         if (*temp == '\0') { /* empty string */
00222                 g_free(temp);
00223                 return NULL;
00224         } else {
00225                 return temp;
00226         }
00227 }
00228 
00237 char *
00238 lutil_parse_deleteWhitespace(char* string) {
00239         int i;
00240         
00241         if (string == NULL)
00242                 return NULL;
00243         
00244         while (isspace(*string)) ++string;
00245         for (i = strlen(string)-1; i >= 0 && isspace(string[i]); --i) 
00246                 string[i] = '\0';
00247         
00248         return string;
00249 }
00250 
00258 char *
00259 lutil_parse_skipString(char* input, char* string) {
00260         return (input + strlen(string));
00261 }
00262 

Generated on Mon Apr 12 22:17:11 2004 for luau by doxygen 1.3.2