visualtest/src/action_configparser.c
changeset 7924 fcb86d323770
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visualtest/src/action_configparser.c	Sun Nov 10 00:32:23 2013 -0500
     1.3 @@ -0,0 +1,396 @@
     1.4 +/* See COPYING.txt for the full license governing this code. */
     1.5 +/**
     1.6 + * \file action_configparser.c
     1.7 + *
     1.8 + * Source file for the parser for action config files.
     1.9 + */
    1.10 +
    1.11 +#include <SDL_stdinc.h>
    1.12 +#include <SDL_test.h>
    1.13 +#include <string.h>
    1.14 +#include "SDL_visualtest_action_configparser.h"
    1.15 +#include "SDL_visualtest_rwhelper.h"
    1.16 +#include "SDL_visualtest_parsehelper.h"
    1.17 +
    1.18 +static void
    1.19 +FreeAction(SDLVisualTest_Action* action)
    1.20 +{
    1.21 +    if(!action)
    1.22 +        return;
    1.23 +    switch(action->type)
    1.24 +    {
    1.25 +        case SDL_ACTION_LAUNCH:
    1.26 +        {
    1.27 +            char* path;
    1.28 +            char* args;
    1.29 +
    1.30 +            path = action->extra.process.path;
    1.31 +            args = action->extra.process.args;
    1.32 +
    1.33 +            if(path)
    1.34 +                SDL_free(path);
    1.35 +            if(args)
    1.36 +                SDL_free(args);
    1.37 +
    1.38 +            action->extra.process.path = NULL;
    1.39 +            action->extra.process.args = NULL;
    1.40 +        }
    1.41 +        break;
    1.42 +    }
    1.43 +}
    1.44 +
    1.45 +int
    1.46 +SDLVisualTest_EnqueueAction(SDLVisualTest_ActionQueue* queue,
    1.47 +                            SDLVisualTest_Action action)
    1.48 +{
    1.49 +    SDLVisualTest_ActionNode* node;
    1.50 +    if(!queue)
    1.51 +    {
    1.52 +        SDLTest_LogError("queue argument cannot be NULL");
    1.53 +        return 0;
    1.54 +    }
    1.55 +
    1.56 +    node = (SDLVisualTest_ActionNode*)SDL_malloc(
    1.57 +                                      sizeof(SDLVisualTest_ActionNode));
    1.58 +    if(!node)
    1.59 +    {
    1.60 +        SDLTest_LogError("malloc() failed");
    1.61 +        return 0;
    1.62 +    }
    1.63 +    node->action = action;
    1.64 +    node->next = NULL;
    1.65 +    queue->size++;
    1.66 +    if(!queue->rear)
    1.67 +        queue->rear = queue->front = node;
    1.68 +    else
    1.69 +    {
    1.70 +        queue->rear->next = node;
    1.71 +        queue->rear = node;
    1.72 +    }
    1.73 +    return 1;
    1.74 +}
    1.75 +
    1.76 +int
    1.77 +SDLVisualTest_DequeueAction(SDLVisualTest_ActionQueue* queue)
    1.78 +{
    1.79 +    SDLVisualTest_ActionNode* node;
    1.80 +    if(!queue)
    1.81 +    {
    1.82 +        SDLTest_LogError("queue argument cannot be NULL");
    1.83 +        return 0;
    1.84 +    }
    1.85 +    if(SDLVisualTest_IsActionQueueEmpty(queue))
    1.86 +    {
    1.87 +        SDLTest_LogError("cannot dequeue from empty queue");
    1.88 +        return 0;
    1.89 +    }
    1.90 +    if(queue->front == queue->rear)
    1.91 +    {
    1.92 +        FreeAction(&queue->front->action);
    1.93 +        SDL_free(queue->front);
    1.94 +        queue->front = queue->rear = NULL;
    1.95 +    }
    1.96 +    else
    1.97 +    {
    1.98 +        node = queue->front;
    1.99 +        queue->front = queue->front->next;
   1.100 +        FreeAction(&node->action);
   1.101 +        SDL_free(node);
   1.102 +    }
   1.103 +    queue->size--;
   1.104 +    return 1;
   1.105 +}
   1.106 +
   1.107 +void
   1.108 +SDLVisualTest_InitActionQueue(SDLVisualTest_ActionQueue* queue)
   1.109 +{
   1.110 +    if(!queue)
   1.111 +    {
   1.112 +        SDLTest_LogError("queue argument cannot be NULL");
   1.113 +        return;
   1.114 +    }
   1.115 +    queue->front = NULL;
   1.116 +    queue->rear = NULL;
   1.117 +    queue->size = 0;
   1.118 +}
   1.119 +
   1.120 +SDLVisualTest_Action*
   1.121 +SDLVisualTest_GetQueueFront(SDLVisualTest_ActionQueue* queue)
   1.122 +{
   1.123 +    if(!queue)
   1.124 +    {
   1.125 +        SDLTest_LogError("queue argument cannot be NULL");
   1.126 +        return NULL;
   1.127 +    }
   1.128 +    if(!queue->front)
   1.129 +    {
   1.130 +        SDLTest_LogError("cannot get front of empty queue");
   1.131 +        return NULL;
   1.132 +    }
   1.133 +
   1.134 +    return &queue->front->action;
   1.135 +}
   1.136 +
   1.137 +int
   1.138 +SDLVisualTest_IsActionQueueEmpty(SDLVisualTest_ActionQueue* queue)
   1.139 +{
   1.140 +    if(!queue)
   1.141 +    {
   1.142 +        SDLTest_LogError("queue argument cannot be NULL");
   1.143 +        return 1;
   1.144 +    }
   1.145 +
   1.146 +    if(queue->size > 0)
   1.147 +        return 0;
   1.148 +    return 1;
   1.149 +}
   1.150 +
   1.151 +void
   1.152 +SDLVisualTest_EmptyActionQueue(SDLVisualTest_ActionQueue* queue)
   1.153 +{
   1.154 +    if(queue)
   1.155 +    {
   1.156 +        while(!SDLVisualTest_IsActionQueueEmpty(queue))
   1.157 +            SDLVisualTest_DequeueAction(queue);
   1.158 +    }
   1.159 +}
   1.160 +
   1.161 +/* Since the size of the queue is not likely to be larger than 100 elements
   1.162 +   we can get away with using insertion sort. */
   1.163 +static void
   1.164 +SortQueue(SDLVisualTest_ActionQueue* queue)
   1.165 +{
   1.166 +    SDLVisualTest_ActionNode* head;
   1.167 +    SDLVisualTest_ActionNode* tail;
   1.168 +
   1.169 +    if(!queue || SDLVisualTest_IsActionQueueEmpty(queue))
   1.170 +        return;
   1.171 +
   1.172 +    head = queue->front;
   1.173 +    for(tail = head; tail && tail->next;)
   1.174 +    {
   1.175 +        SDLVisualTest_ActionNode* pos;
   1.176 +        SDLVisualTest_ActionNode* element = tail->next;
   1.177 +
   1.178 +        if(element->action.time < head->action.time)
   1.179 +        {
   1.180 +            tail->next = tail->next->next;
   1.181 +            element->next = head;
   1.182 +            head = element;
   1.183 +        }
   1.184 +        else if(element->action.time >= tail->action.time)
   1.185 +        {
   1.186 +            tail = tail->next;
   1.187 +        }
   1.188 +        else
   1.189 +        {
   1.190 +            for(pos = head;
   1.191 +                (pos->next->action.time < element->action.time);
   1.192 +                pos = pos->next);
   1.193 +            tail->next = tail->next->next;
   1.194 +            element->next = pos->next;
   1.195 +            pos->next = element;
   1.196 +        }
   1.197 +    }
   1.198 +
   1.199 +    queue->front = head;
   1.200 +    queue->rear = tail;
   1.201 +}
   1.202 +
   1.203 +int
   1.204 +SDLVisualTest_InsertIntoActionQueue(SDLVisualTest_ActionQueue* queue,
   1.205 +                                    SDLVisualTest_Action action)
   1.206 +{
   1.207 +    SDLVisualTest_ActionNode* n;
   1.208 +    SDLVisualTest_ActionNode* prev;
   1.209 +    SDLVisualTest_ActionNode* newnode;
   1.210 +    if(!queue)
   1.211 +    {
   1.212 +        SDLTest_LogError("queue argument cannot be NULL");
   1.213 +        return 0;
   1.214 +    }
   1.215 +
   1.216 +    if(SDLVisualTest_IsActionQueueEmpty(queue))
   1.217 +    {
   1.218 +        if(!SDLVisualTest_EnqueueAction(queue, action))
   1.219 +        {
   1.220 +            SDLTest_LogError("SDLVisualTest_EnqueueAction() failed");
   1.221 +            return 0;
   1.222 +        }
   1.223 +        return 1;
   1.224 +    }
   1.225 +
   1.226 +    newnode = (SDLVisualTest_ActionNode*)malloc(sizeof(SDLVisualTest_ActionNode));
   1.227 +    if(!newnode)
   1.228 +    {
   1.229 +        SDLTest_LogError("malloc() failed");
   1.230 +        return 0;
   1.231 +    }
   1.232 +    newnode->action = action;
   1.233 +
   1.234 +    queue->size++;
   1.235 +    for(n = queue->front, prev = NULL; n; n = n->next)
   1.236 +    {
   1.237 +        if(action.time < n->action.time)
   1.238 +        {
   1.239 +            if(prev)
   1.240 +            {
   1.241 +                prev->next = newnode;
   1.242 +                newnode->next = n;
   1.243 +            }
   1.244 +            else
   1.245 +            {
   1.246 +                newnode->next = queue->front;
   1.247 +                queue->front = newnode;
   1.248 +            }
   1.249 +            return 1;
   1.250 +        }
   1.251 +        prev = n;
   1.252 +    }
   1.253 +
   1.254 +    queue->rear->next = newnode;
   1.255 +    newnode->next = NULL;
   1.256 +    queue->rear = newnode;
   1.257 +
   1.258 +    return 1;
   1.259 +}
   1.260 +
   1.261 +int
   1.262 +SDLVisualTest_ParseActionConfig(char* file, SDLVisualTest_ActionQueue* queue)
   1.263 +{
   1.264 +    char line[MAX_ACTION_LINE_LENGTH];
   1.265 +    SDLVisualTest_RWHelperBuffer buffer;
   1.266 +    char* token_ptr;
   1.267 +    int linenum;
   1.268 +    SDL_RWops* rw;
   1.269 +
   1.270 +    if(!file)
   1.271 +    {
   1.272 +        SDLTest_LogError("file argument cannot be NULL");
   1.273 +        return 0;
   1.274 +    }
   1.275 +    if(!queue)
   1.276 +    {
   1.277 +        SDLTest_LogError("queue argument cannot be NULL");
   1.278 +        return 0;
   1.279 +    }
   1.280 +
   1.281 +    rw = SDL_RWFromFile(file, "r");
   1.282 +    if(!rw)
   1.283 +    {
   1.284 +        SDLTest_LogError("SDL_RWFromFile() failed");
   1.285 +        return 0;
   1.286 +    }
   1.287 +
   1.288 +    SDLVisualTest_RWHelperResetBuffer(&buffer);
   1.289 +    SDLVisualTest_InitActionQueue(queue);
   1.290 +    linenum = 0;
   1.291 +    while(SDLVisualTest_RWHelperReadLine(rw, line, MAX_ACTION_LINE_LENGTH,
   1.292 +                                         &buffer, '#'))
   1.293 +    {
   1.294 +        SDLVisualTest_Action action;
   1.295 +        int hr, min, sec;
   1.296 +
   1.297 +        /* parse time */
   1.298 +        token_ptr = strtok(line, " ");
   1.299 +        if(!token_ptr ||
   1.300 +           (SDL_sscanf(token_ptr, "%d:%d:%d", &hr, &min, &sec) != 3))
   1.301 +        {
   1.302 +            SDLTest_LogError("Could not parse time token at line: %d",
   1.303 +                             linenum);
   1.304 +            SDLVisualTest_EmptyActionQueue(queue);
   1.305 +            SDL_RWclose(rw);
   1.306 +            return 0;
   1.307 +        }
   1.308 +        action.time = (((hr * 60 + min) * 60) + sec) * 1000;
   1.309 +
   1.310 +        /* parse type */
   1.311 +        token_ptr = strtok(NULL, " ");
   1.312 +        if(SDL_strcasecmp(token_ptr, "launch") == 0)
   1.313 +            action.type = SDL_ACTION_LAUNCH;
   1.314 +        else if(SDL_strcasecmp(token_ptr, "kill") == 0)
   1.315 +            action.type = SDL_ACTION_KILL;
   1.316 +        else if(SDL_strcasecmp(token_ptr, "quit") == 0)
   1.317 +            action.type = SDL_ACTION_QUIT;
   1.318 +        else if(SDL_strcasecmp(token_ptr, "screenshot") == 0)
   1.319 +            action.type = SDL_ACTION_SCREENSHOT;
   1.320 +        else if(SDL_strcasecmp(token_ptr, "verify") == 0)
   1.321 +            action.type = SDL_ACTION_VERIFY;
   1.322 +        else
   1.323 +        {
   1.324 +            SDLTest_LogError("Could not parse type token at line: %d",
   1.325 +                             linenum);
   1.326 +            SDLVisualTest_EmptyActionQueue(queue);
   1.327 +            SDL_RWclose(rw);
   1.328 +            return 0;
   1.329 +        }
   1.330 +
   1.331 +        /* parse the extra field */
   1.332 +        if(action.type == SDL_ACTION_LAUNCH)
   1.333 +        {
   1.334 +            int len;
   1.335 +            char* args;
   1.336 +            char* path;
   1.337 +            token_ptr = strtok(NULL, " ");
   1.338 +            len = token_ptr ? SDL_strlen(token_ptr) : 0;
   1.339 +            if(len <= 0)
   1.340 +            {
   1.341 +                SDLTest_LogError("Please specify the process to launch at line: %d",
   1.342 +                                 linenum);
   1.343 +                SDLVisualTest_EmptyActionQueue(queue);
   1.344 +                SDL_RWclose(rw);
   1.345 +                return 0;
   1.346 +            }
   1.347 +            path = (char*)SDL_malloc(sizeof(char) * (len + 1));
   1.348 +            if(!path)
   1.349 +            {
   1.350 +                SDLTest_LogError("malloc() failed");
   1.351 +                SDLVisualTest_EmptyActionQueue(queue);
   1.352 +                SDL_RWclose(rw);
   1.353 +                return 0;
   1.354 +            }
   1.355 +            SDL_strlcpy(path, token_ptr, len + 1);
   1.356 +
   1.357 +            token_ptr = strtok(NULL, "");
   1.358 +            len = token_ptr ? SDL_strlen(token_ptr) : 0;
   1.359 +            if(len > 0)
   1.360 +            {
   1.361 +                args = (char*)SDL_malloc(sizeof(char) * (len + 1));
   1.362 +                if(!args)
   1.363 +                {
   1.364 +                    SDLTest_LogError("malloc() failed");
   1.365 +                    SDL_free(path);
   1.366 +                    SDLVisualTest_EmptyActionQueue(queue);
   1.367 +                    SDL_RWclose(rw);
   1.368 +                    return 0;
   1.369 +                }
   1.370 +                SDL_strlcpy(args, token_ptr, len + 1);
   1.371 +            }
   1.372 +            else
   1.373 +                args = NULL;
   1.374 +
   1.375 +            action.extra.process.path = path;
   1.376 +            action.extra.process.args = args;
   1.377 +        }
   1.378 +
   1.379 +        /* add the action to the queue */
   1.380 +        if(!SDLVisualTest_EnqueueAction(queue, action))
   1.381 +        {
   1.382 +            SDLTest_LogError("SDLVisualTest_EnqueueAction() failed");
   1.383 +            if(action.type == SDL_ACTION_LAUNCH)
   1.384 +            {
   1.385 +                SDL_free(action.extra.process.path);
   1.386 +                if(action.extra.process.args)
   1.387 +                    SDL_free(action.extra.process.args);
   1.388 +            }
   1.389 +            SDLVisualTest_EmptyActionQueue(queue);
   1.390 +            SDL_RWclose(rw);
   1.391 +            return 0;
   1.392 +        }
   1.393 +    }
   1.394 +    /* sort the queue of actions */
   1.395 +    SortQueue(queue);
   1.396 +
   1.397 +    SDL_RWclose(rw);
   1.398 +    return 1;
   1.399 +}
   1.400 \ No newline at end of file