00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef HAVE_CONFIG_H
00019 #include <config.h>
00020 #endif
00021
00022 using namespace std;
00023
00024 #include "projectFile.h"
00025
00026 enum myparserstates
00027 {
00028 SEARCH,
00029 OPENQUOTE,
00030 BACKSLASH,
00031 INWORD,
00032 BACKQUOTE,
00033 PLINVALID
00034 };
00035
00036 int ProjectFile::ParseLine(char *linebuf, char ***pwords)
00037 {
00038 char *ptr, *lastptr, **words;
00039 int state, c;
00040 int Nwords;
00041
00042 Nwords = 0;
00043 state = SEARCH;
00044 ptr = linebuf;
00045
00046 lastptr = ptr + strlen(ptr);
00047 for( ; ptr<lastptr; ++ptr)
00048 {
00049 c = *ptr;
00050 switch(state)
00051 {
00052 case SEARCH:
00053 if( c=='"' )
00054 {
00055 ++Nwords;
00056 state=OPENQUOTE;
00057 }
00058 else if( c=='\\' )
00059 {
00060 ++Nwords;
00061 state=BACKSLASH;
00062 }
00063 else if( c==' ' || c=='\n' || c=='\t' || c=='\r' )
00064 ;
00065 else
00066 {
00067 ++Nwords;
00068 state = INWORD;
00069 }
00070 break;
00071
00072 case OPENQUOTE:
00073 if( c=='\\' )
00074 state = BACKQUOTE;
00075 else if( c=='"' )
00076 state = SEARCH;
00077 break;
00078
00079 case BACKQUOTE:
00080 state = OPENQUOTE;
00081 break;
00082
00083 case BACKSLASH:
00084 state = INWORD;
00085 break;
00086
00087 case INWORD:
00088 if( c==' ' || c=='\n' || c=='\t' || c=='\r' )
00089 state = SEARCH;
00090 break;
00091 }
00092 }
00093
00094 *pwords = words = (char **)malloc(Nwords * sizeof(char*));
00095
00096 Nwords = 0;
00097 state = SEARCH;
00098 ptr = linebuf;
00099
00100 for( ; ptr<lastptr; ++ptr)
00101 {
00102 c = *ptr;
00103 switch(state)
00104 {
00105 case SEARCH:
00106 if( c=='"' )
00107 {
00108 words[Nwords++] = ptr+1;
00109 state=OPENQUOTE;
00110 }
00111 else if( c=='\\' )
00112 {
00113 words[Nwords++] = ptr+1;
00114 state=BACKSLASH;
00115 }
00116 else if( c==' ' || c=='\n' || c=='\t' || c=='\r' )
00117 ;
00118 else
00119 {
00120 words[Nwords++] = ptr;
00121 state = INWORD;
00122 }
00123 break;
00124
00125 case OPENQUOTE:
00126 if( c=='\\' )
00127 state = BACKQUOTE;
00128 else if( c=='"' )
00129 {
00130 *ptr = 0;
00131 state = SEARCH;
00132 }
00133 break;
00134
00135 case BACKQUOTE:
00136 state = OPENQUOTE;
00137 break;
00138
00139 case BACKSLASH:
00140 state = INWORD;
00141 break;
00142
00143 case INWORD:
00144 if( c==' ' || c=='\n' || c=='\t' || c=='\r' )
00145 {
00146 *ptr = 0;
00147 state = SEARCH;
00148 }
00149 break;
00150 }
00151 }
00152
00153 return Nwords;
00154 }
00155
00156
00157 #ifdef TEST
00158 void main()
00159 {
00160 char *line;
00161 char **words;
00162 int Nwords;
00163 int i;
00164
00165 line = strdup("one two three");
00166 printf("Parsing: [%s]\n",line);
00167 Nwords = ParseLine(line,&words);
00168 for( i=0; i<Nwords; ++i )
00169 printf("%3d: [%s]\n",i,words[i]);
00170
00171 line = strdup("one two \"hi there!!\" three");
00172 printf("Parsing: [%s]\n",line);
00173 Nwords = ParseLine(line,&words);
00174 for( i=0; i<Nwords; ++i )
00175 printf("%3d: [%s]\n",i,words[i]);
00176
00177 line = strdup("resource maggie \"Maggie Wong\"");
00178 printf("Parsing: [%s]\n",line);
00179 Nwords = ParseLine(line,&words);
00180 for( i=0; i<Nwords; ++i )
00181 printf("%3d: [%s]\n",i,words[i]);
00182
00183
00184 line = strdup("candidate oracle_install idan");
00185 printf("Parsing: [%s]\n",line);
00186 Nwords = ParseLine(line,&words);
00187 for( i=0; i<Nwords; ++i )
00188 printf("%3d: [%s]\n",i,words[i]);
00189
00190 }
00191
00192
00193 #endif
00194