Patched to compile on C89 compilers.
1 /* See COPYING.txt for the full license governing this code. */
3 * \file sut_configparser.c
5 * Source file for the parser for SUT config files.
11 #include <SDL_rwops.h>
12 #include "SDL_visualtest_sut_configparser.h"
13 #include "SDL_visualtest_parsehelper.h"
14 #include "SDL_visualtest_rwhelper.h"
17 SDLVisualTest_ParseSUTConfig(char* file, SDLVisualTest_SUTConfig* config)
19 char line[MAX_SUTOPTION_LINE_LENGTH];
20 SDLVisualTest_RWHelperBuffer buffer;
23 int num_lines, i, token_len;
28 SDLTest_LogError("file argument cannot be NULL");
33 SDLTest_LogError("config argument cannot be NULL");
37 /* count the number of lines */
38 rw = SDL_RWFromFile(file, "r");
41 SDLTest_LogError("SDL_RWFromFile() failed");
44 SDLVisualTest_RWHelperResetBuffer(&buffer);
45 num_lines = SDLVisualTest_RWHelperCountNonEmptyLines(rw, &buffer, '#');
48 else if(num_lines == 0)
50 config->options = NULL;
51 config->num_options = 0;
57 SDL_RWseek(rw, 0, RW_SEEK_SET);
58 SDLVisualTest_RWHelperResetBuffer(&buffer);
59 config->num_options = num_lines;
60 config->options = (SDLVisualTest_SUTOption*)SDL_malloc(num_lines *
61 sizeof(SDLVisualTest_SUTOption));
64 SDLTest_LogError("malloc() failed");
69 /* actually parse the options */
70 for(i = 0; i < num_lines; i++)
72 if(!SDLVisualTest_RWHelperReadLine(rw, line, MAX_SUTOPTION_LINE_LENGTH,
75 SDLTest_LogError("SDLVisualTest_RWHelperReadLine() failed");
76 SDL_free(config->options);
82 token_ptr = strtok(line, ", ");
85 SDLTest_LogError("Could not parse line %d", i + 1);
86 SDL_free(config->options);
90 token_len = SDL_strlen(token_ptr) + 1;
91 SDL_strlcpy(config->options[i].name, token_ptr, token_len);
94 token_ptr = strtok(NULL, ", ");
97 SDLTest_LogError("Could not parse line %d", i + 1);
98 SDL_free(config->options);
102 if(SDL_strcmp(token_ptr, "string") == 0)
103 config->options[i].type = SDL_SUT_OPTIONTYPE_STRING;
104 else if(SDL_strcmp(token_ptr, "integer") == 0)
105 config->options[i].type = SDL_SUT_OPTIONTYPE_INT;
106 else if(SDL_strcmp(token_ptr, "enum") == 0)
107 config->options[i].type = SDL_SUT_OPTIONTYPE_ENUM;
108 else if(SDL_strcmp(token_ptr, "boolean") == 0)
109 config->options[i].type = SDL_SUT_OPTIONTYPE_BOOL;
112 SDLTest_LogError("Could not parse type token at line %d", i + 1);
113 SDL_free(config->options);
119 token_ptr = strtok(NULL, "]");
122 SDLTest_LogError("Could not parse line %d", i + 1);
123 SDL_free(config->options);
127 token_ptr = SDL_strchr(token_ptr, '[');
130 SDLTest_LogError("Could not parse enum token at line %d", i + 1);
131 SDL_free(config->options);
136 if(config->options[i].type == SDL_SUT_OPTIONTYPE_INT)
138 if(SDL_sscanf(token_ptr, "%d %d", &config->options[i].data.range.min,
139 &config->options[i].data.range.max) != 2)
141 config->options[i].data.range.min = INT_MIN;
142 config->options[i].data.range.max = INT_MAX;
145 else if(config->options[i].type == SDL_SUT_OPTIONTYPE_ENUM)
147 config->options[i].data.enum_values = SDLVisualTest_Tokenize(token_ptr,
148 MAX_SUTOPTION_ENUMVAL_LEN);
149 if(!config->options[i].data.enum_values)
151 SDLTest_LogError("Could not parse enum token at line %d", i + 1);
152 SDL_free(config->options);
159 token_ptr = strtok(NULL, ", ");
162 SDLTest_LogError("Could not parse line %d", i + 1);
163 SDL_free(config->options);
168 if(SDL_strcmp(token_ptr, "true") == 0)
169 config->options[i].required = SDL_TRUE;
170 else if(SDL_strcmp(token_ptr, "false") == 0)
171 config->options[i].required = SDL_FALSE;
174 SDLTest_LogError("Could not parse required token at line %d", i + 1);
175 SDL_free(config->options);
180 /* parse categories */
181 token_ptr = strtok(NULL, ",");
184 SDLTest_LogError("Could not parse line %d", i + 1);
185 SDL_free(config->options);
189 token_ptr = SDL_strchr(token_ptr, '[');
192 SDLTest_LogError("Could not parse enum token at line %d", i + 1);
193 SDL_free(config->options);
198 token_end = SDL_strchr(token_ptr, ']');
202 SDLTest_LogError("Could not parse enum token at line %d", i + 1);
203 SDL_free(config->options);
207 config->options[i].categories = SDLVisualTest_Tokenize(token_ptr,
208 MAX_SUTOPTION_CATEGORY_LEN);
215 SDLVisualTest_FreeSUTConfig(SDLVisualTest_SUTConfig* config)
217 if(config && config->options)
219 SDLVisualTest_SUTOption* option;
220 for(option = config->options;
221 option != config->options + config->num_options; option++)
223 if(option->categories)
224 SDL_free(option->categories);
225 if(option->type == SDL_SUT_OPTIONTYPE_ENUM && option->data.enum_values)
226 SDL_free(option->data.enum_values);
228 SDL_free(config->options);
229 config->options = NULL;
230 config->num_options = 0;