Hash :
f848adff
Author :
Date :
2013-11-29T10:06:08
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/* See COPYING.txt for the full license governing this code. */
/**
* \file parsehelper.c
*
* Source file with some helper functions for parsing strings.
*/
#include <SDL_test.h>
#include "SDL_visualtest_harness_argparser.h"
/* this function uses a DFA to count the number of tokens in an agruments string.
state 0 is taken to be the start and end state. State 1 handles a double quoted
argument and state 2 handles unquoted arguments. */
static int
CountTokens(char* args)
{
int index, num_tokens;
int state; /* current state of the DFA */
if(!args)
return -1;
index = 0;
state = 0;
num_tokens = 0;
while(args[index])
{
char ch = args[index];
switch(state)
{
case 0:
if(ch == '\"')
{
state = 1;
num_tokens++;
}
else if(!SDL_isspace(ch))
{
state = 2;
num_tokens++;
}
break;
case 1:
if(ch == '\"')
{
state = 0;
}
break;
case 2:
if(SDL_isspace(ch))
{
state = 0;
}
break;
}
index++;
}
return num_tokens;
}
/* - size of tokens is num_tokens + 1
- uses the same DFA used in CountTokens() to split args into an array of strings */
static int
TokenizeHelper(char* str, char** tokens, int num_tokens, int max_token_len)
{
int index, state, done, st_index, token_index;
if(!str)
{
SDLTest_LogError("str argument cannot be NULL");
return 0;
}
if(!tokens)
{
SDLTest_LogError("tokens argument cannot be NULL");
return 0;
}
if(num_tokens <= 0)
{
SDLTest_LogError("num_tokens argument must be positive");
return 0;
}
if(max_token_len <= 0)
{
SDLTest_LogError("max_token_len argument must be positive");
return 0;
}
/* allocate memory for the tokens */
tokens[num_tokens] = NULL;
for(index = 0; index < num_tokens; index++)
{
tokens[index] = (char*)SDL_malloc(max_token_len);
if(!tokens[index])
{
int i;
SDLTest_LogError("malloc() failed.");
for(i = 0; i < index; i++)
SDL_free(tokens[i]);
return 0;
}
tokens[index][0] = '\0';
}
/* copy the tokens into the array */
st_index = 0;
index = 0;
token_index = 0;
state = 0;
done = 0;
while(!done)
{
char ch = str[index];
switch(state)
{
case 0:
if(ch == '\"')
{
state = 1;
st_index = index + 1;
}
else if(!ch)
done = 1;
else if(ch && !SDL_isspace(ch))
{
state = 2;
st_index = index;
}
break;
case 1:
if(ch == '\"')
{
int i;
state = 0;
for(i = st_index; i < index; i++)
{
tokens[token_index][i - st_index] = str[i];
}
tokens[token_index][i - st_index] = '\0';
token_index++;
}
else if(!ch)
{
SDLTest_LogError("Parsing Error!");
done = 1;
}
break;
case 2:
if(!ch)
done = 1;
if(SDL_isspace(ch) || !ch)
{
int i;
state = 0;
for(i = st_index; i < index; i++)
{
tokens[token_index][i - st_index] = str[i];
}
tokens[token_index][i - st_index] = '\0';
token_index++;
}
break;
}
index++;
}
return 1;
}
char**
SDLVisualTest_Tokenize(char* str, int max_token_len)
{
int num_tokens;
char** tokens;
if(!str)
{
SDLTest_LogError("str argument cannot be NULL");
return NULL;
}
if(max_token_len <= 0)
{
SDLTest_LogError("max_token_len argument must be positive");
return NULL;
}
num_tokens = CountTokens(str);
if(num_tokens == 0)
return NULL;
tokens = (char**)SDL_malloc(sizeof(char*) * (num_tokens + 1));
if(!TokenizeHelper(str, tokens, num_tokens, max_token_len))
{
SDLTest_LogError("TokenizeHelper() failed");
SDL_free(tokens);
return NULL;
}
return tokens;
}
char**
SDLVisualTest_ParseArgsToArgv(char* args)
{
char** argv;
int num_tokens;
num_tokens = CountTokens(args);
if(num_tokens == 0)
return NULL;
/* allocate space for arguments */
argv = (char**)SDL_malloc((num_tokens + 2) * sizeof(char*));
if(!argv)
{
SDLTest_LogError("malloc() failed.");
return NULL;
}
/* tokenize */
if(!TokenizeHelper(args, argv + 1, num_tokens, MAX_SUT_ARGS_LEN))
{
SDLTest_LogError("TokenizeHelper() failed");
SDL_free(argv);
return NULL;
}
argv[0] = NULL;
return argv;
}