Patched to compile on C89 compilers.
1 /* See COPYING.txt for the full license governing this code. */
5 * Source file with some helper functions for parsing strings.
9 #include "SDL_visualtest_harness_argparser.h"
11 /* this function uses a DFA to count the number of tokens in an agruments string.
12 state 0 is taken to be the start and end state. State 1 handles a double quoted
13 argument and state 2 handles unquoted arguments. */
15 CountTokens(char* args)
17 int index, num_tokens;
18 int state; /* current state of the DFA */
28 char ch = args[index];
37 else if(!SDL_isspace(ch))
63 /* - size of tokens is num_tokens + 1
64 - uses the same DFA used in CountTokens() to split args into an array of strings */
66 TokenizeHelper(char* str, char** tokens, int num_tokens, int max_token_len)
68 int index, state, done, st_index, token_index;
72 SDLTest_LogError("str argument cannot be NULL");
77 SDLTest_LogError("tokens argument cannot be NULL");
82 SDLTest_LogError("num_tokens argument must be positive");
85 if(max_token_len <= 0)
87 SDLTest_LogError("max_token_len argument must be positive");
91 /* allocate memory for the tokens */
92 tokens[num_tokens] = NULL;
93 for(index = 0; index < num_tokens; index++)
95 tokens[index] = (char*)SDL_malloc(max_token_len);
99 SDLTest_LogError("malloc() failed.");
100 for(i = 0; i < index; i++)
104 tokens[index][0] = '\0';
107 /* copy the tokens into the array */
115 char ch = str[index];
122 st_index = index + 1;
126 else if(ch && !SDL_isspace(ch))
138 for(i = st_index; i < index; i++)
140 tokens[token_index][i - st_index] = str[i];
142 tokens[token_index][i - st_index] = '\0';
147 SDLTest_LogError("Parsing Error!");
155 if(SDL_isspace(ch) || !ch)
159 for(i = st_index; i < index; i++)
161 tokens[token_index][i - st_index] = str[i];
163 tokens[token_index][i - st_index] = '\0';
174 SDLVisualTest_Tokenize(char* str, int max_token_len)
181 SDLTest_LogError("str argument cannot be NULL");
184 if(max_token_len <= 0)
186 SDLTest_LogError("max_token_len argument must be positive");
190 num_tokens = CountTokens(str);
194 tokens = (char**)SDL_malloc(sizeof(char*) * (num_tokens + 1));
195 if(!TokenizeHelper(str, tokens, num_tokens, max_token_len))
197 SDLTest_LogError("TokenizeHelper() failed");
205 SDLVisualTest_ParseArgsToArgv(char* args)
210 num_tokens = CountTokens(args);
214 /* allocate space for arguments */
215 argv = (char**)SDL_malloc((num_tokens + 2) * sizeof(char*));
218 SDLTest_LogError("malloc() failed.");
223 if(!TokenizeHelper(args, argv + 1, num_tokens, MAX_SUT_ARGS_LEN))
225 SDLTest_LogError("TokenizeHelper() failed");