4 #include "CTextParsers.h"
6 CTextParsers g_TextParse;
8 static int g_ini_chartable1[255] = {0};
10 CTextParsers::CTextParsers()
12 g_ini_chartable1['_'] = 1;
13 g_ini_chartable1['-'] = 1;
14 g_ini_chartable1[','] = 1;
15 g_ini_chartable1['+'] = 1;
16 g_ini_chartable1['.'] = 1;
17 g_ini_chartable1['$'] = 1;
18 g_ini_chartable1['?'] = 1;
19 g_ini_chartable1['/'] = 1;
22 bool CTextParsers::ParseFile_SMC(const char *file, ITextListener_SMC *smc_listener, unsigned int *line, unsigned int *col)
24 /* :TODO: Implement this */
33 bool CTextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listener, unsigned int *line, unsigned int *col)
35 FILE *fp = fopen(file, "rt");
36 unsigned int curline = 0;
58 if (fgets(buffer, sizeof(buffer), fp) == NULL)
63 /* Preprocess the string before anything */
66 /* First strip beginning whitespace */
67 while ((*ptr != '\0') && isspace(*ptr))
79 /* Now search for comment characters */
82 for (size_t i=0; i<len; i++,ptr++)
97 /* Terminate the string here */
117 /* Lastly, strip ending whitespace off */
118 for (size_t i=len-1; i>=0 && i<len; i--)
134 if (!ini_listener->ReadINI_RawLine(ptr, &curtok))
141 bool invalid_tokens = false;
142 bool got_bracket = false;
143 bool extra_tokens = false;
146 for (size_t i=1; i<len; i++)
149 if (!isalnum(c) && !g_ini_chartable1[c])
151 /* First check - is this a bracket? */
156 /* If this isn't the last character... */
165 /* n...No! Continue copying. */
166 invalid_tokens = true;
171 /* Tell the handler */
172 if (!ini_listener->ReadINI_NewSection(&ptr[1], invalid_tokens, got_bracket, extra_tokens, &curtok))
178 char *val_ptr = NULL;
180 size_t first_space = 0;
181 bool invalid_tokens = false;
182 bool equal_token = false;
185 for (size_t i=0; i<len; i++)
188 /* is this an invalid char? */
189 if (!isalnum(c) && !g_ini_chartable1[c])
193 /* if it's a space, keep track of the last space */
201 /* if it's an equal sign, we're done with the key */
204 /* remove excess whitespace */
205 key_ptr[first_space] = '\0';
207 /* remove the equal sign */
210 if (ptr[++i] != '\0')
212 /* If this isn't the end, set next pointer */
218 /* Mark that we got something invalid! */
219 invalid_tokens = true;
226 /* Now we need to parse the value, if any */
229 /* eat up spaces! there shouldn't be any h*/
230 while ((*val_ptr != '\0') && isspace(*val_ptr))
234 if (*val_ptr == '\0')
239 /* Do we have an initial quote? If so, the parsing rules change! */
240 if (*val_ptr == '"' && *val_ptr != '\0')
242 len = strlen(val_ptr);
243 if (val_ptr[len-1] == '"')
246 val_ptr[--len] = '\0';
254 curtok = val_ptr - buffer;
255 if (!ini_listener->ReadINI_KeyValue(key_ptr, val_ptr, invalid_tokens, equal_token, quotes, &curtok))