parse.c

Go to the documentation of this file.
00001 
00006 /*
00007 All original material Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 Copyright (C) 1997-2001 Id Software, Inc.
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020 See the GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with this program; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00025 
00026 */
00027 
00028 #include "defines.h"
00029 #include "ufotypes.h"
00030 #include "parse.h"
00031 
00032 static char com_token[4096];
00033 static qboolean isUnparsedToken;
00034 static qboolean isQuotedToken;
00035 static qboolean functionScriptTokenEnabled;
00036 
00043 void Com_UnParseLastToken (void)
00044 {
00045     isUnparsedToken = qtrue;
00046 }
00047 
00053 qboolean Com_ParsedTokenIsQuoted (void)
00054 {
00055     return isQuotedToken;
00056 }
00057 
00063 void Com_EnableFunctionScriptToken (qboolean enable)
00064 {
00065     functionScriptTokenEnabled = enable;
00066 }
00067 
00079 const char *Com_Parse (const char *data_p[])
00080 {
00081     char c;
00082     size_t len;
00083     const char *data;
00084 
00085     if (isUnparsedToken) {
00086         isUnparsedToken = qfalse;
00087         return com_token;
00088     }
00089 
00090     data = *data_p;
00091     isQuotedToken = qfalse;
00092     len = 0;
00093     com_token[0] = 0;
00094 
00095     if (!data) {
00096         *data_p = NULL;
00097         return "";
00098     }
00099 
00100     /* skip whitespace */
00101 skipwhite:
00102     while ((c = *data) <= ' ') {
00103         if (c == 0) {
00104             *data_p = NULL;
00105             return "";
00106         }
00107         data++;
00108     }
00109 
00110     if (c == '/' && data[1] == '*') {
00111         int clen = 0;
00112         data += 2;
00113         while (!((data[clen] && data[clen] == '*') && (data[clen + 1] && data[clen + 1] == '/'))) {
00114             clen++;
00115         }
00116         data += clen + 2; /* skip end of multiline comment */
00117         goto skipwhite;
00118     }
00119 
00120     /* skip // comments */
00121     if (c == '/' && data[1] == '/') {
00122         while (*data && *data != '\n')
00123             data++;
00124         goto skipwhite;
00125     }
00126 
00127     /* handle quoted strings specially */
00128     if (c == '\"') {
00129         isQuotedToken = qtrue;
00130         data++;
00131         for (;;) {
00132             c = *data++;
00133             if (c == '\\' && data[0] == 'n') {
00134                 c = '\n';
00135                 data++;
00136             } else if (c == '\\' && data[0] == 't') {
00137                 c = '\t';
00138                 data++;
00139             /* nested quotation */
00140             } else if (c == '\\' && data[0] == '\"') {
00141                 c = '\"';
00142                 data++;
00143             } else if (c == '\"' || !c) {
00144                 com_token[len] = '\0';
00145                 *data_p = data;
00146                 return com_token;
00147             } else if (c == '\0') {
00148                 break;
00149             }
00150 
00151             if (len < sizeof(com_token)) {
00152                 com_token[len] = c;
00153                 len++;
00154             }
00155         }
00156         com_token[len] = '\0';
00157         *data_p = data;
00158         return com_token;
00159     }
00160 
00161     if ((c == '{' || c == '}') || (functionScriptTokenEnabled && (c == '(' || c == ')' || c == ','))) {
00162         data++;
00163         com_token[len] = c;
00164         com_token[len + 1] = '\0';
00165         len++;
00166         *data_p = data;
00167         return com_token;
00168     }
00169 
00170     /* parse a regular word */
00171     do {
00172         if (len < sizeof(com_token)) {
00173             com_token[len] = c;
00174             len++;
00175         }
00176         data++;
00177         c = *data;
00178         if (c == '{' || c == '}')
00179             break;
00180         if (functionScriptTokenEnabled && (c == '(' || c == ')' || c == ','))
00181             break;
00182     } while (c > 32);
00183 
00184     if (len == sizeof(com_token)) {
00185         len = 0;
00186     }
00187     com_token[len] = '\0';
00188 
00189     *data_p = data;
00190     return com_token;
00191 }

Generated by  doxygen 1.6.2