sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

rwhelper.c (3482B)


      1 /* See COPYING.txt for the full license governing this code. */
      2 /**
      3  * \file rwhelper.c
      4  *
      5  * Source file with some helper functions for working with SDL_RWops.
      6  */
      7 
      8 #include <SDL_test.h>
      9 #include "SDL_visualtest_sut_configparser.h"
     10 #include "SDL_visualtest_rwhelper.h"
     11 
     12 void
     13 SDLVisualTest_RWHelperResetBuffer(SDLVisualTest_RWHelperBuffer* buffer)
     14 {
     15     if(!buffer)
     16     {
     17         SDLTest_LogError("buffer argument cannot be NULL");
     18         return;
     19     }
     20     buffer->buffer_pos = 0;
     21     buffer->buffer_width = 0;
     22 }
     23 
     24 char
     25 SDLVisualTest_RWHelperReadChar(SDL_RWops* rw, SDLVisualTest_RWHelperBuffer* buffer)
     26 {
     27     if(!rw || !buffer)
     28         return 0;
     29     /* if the buffer has been consumed, we fill it up again */
     30     if(buffer->buffer_pos == buffer->buffer_width)
     31     {
     32         buffer->buffer_width = SDL_RWread(rw, buffer->buffer, 1, RWOPS_BUFFER_LEN);
     33         buffer->buffer_pos = 0;
     34         if(buffer->buffer_width == 0)
     35             return 0;
     36     }
     37     buffer->buffer_pos++;
     38     return buffer->buffer[buffer->buffer_pos - 1];
     39 }
     40 
     41 /* does not include new lines in the buffer and adds a trailing null character */
     42 char*
     43 SDLVisualTest_RWHelperReadLine(SDL_RWops* rw, char* str, int size,
     44                                SDLVisualTest_RWHelperBuffer* buffer,
     45                                char comment_char)
     46 {
     47     char ch;
     48     int current_pos, done;
     49     if(!rw)
     50     {
     51         SDLTest_LogError("rw argument cannot be NULL");
     52         return NULL;
     53     }
     54     if(!str)
     55     {
     56         SDLTest_LogError("str argument cannot be NULL");
     57         return NULL;
     58     }
     59     if(!buffer)
     60     {
     61         SDLTest_LogError("buffer argument cannot be NULL");
     62         return NULL;
     63     }
     64     if(size <= 0)
     65     {
     66         SDLTest_LogError("size argument should be positive");
     67         return NULL;
     68     }
     69 
     70     done = 0;
     71     while(!done)
     72     {
     73         /* ignore leading whitespace */
     74         for(ch = SDLVisualTest_RWHelperReadChar(rw, buffer); ch && SDL_isspace(ch);
     75             ch = SDLVisualTest_RWHelperReadChar(rw, buffer));
     76 
     77         for(current_pos = 0;
     78             ch && ch != '\n' && ch != '\r' && ch != comment_char;
     79             current_pos++)
     80         {
     81             str[current_pos] = ch;
     82             if(current_pos >= size - 2)
     83             {
     84                 current_pos++;
     85                 break;
     86             }
     87             ch = SDLVisualTest_RWHelperReadChar(rw, buffer);
     88         }
     89 
     90         done = 1;
     91         if(ch == comment_char) /* discard all characters until the next line */
     92         {
     93             do
     94             {
     95                 ch = SDLVisualTest_RWHelperReadChar(rw, buffer);
     96             }while(ch && ch != '\n' && ch != '\r');
     97 
     98             if(current_pos == 0)
     99                 done = 0;
    100         }
    101     }
    102     if(current_pos == 0)
    103         return NULL;
    104 
    105     str[current_pos] = '\0';
    106     return str;
    107 }
    108 
    109 /* Lines with all whitespace are ignored */
    110 int
    111 SDLVisualTest_RWHelperCountNonEmptyLines(SDL_RWops* rw,
    112                                          SDLVisualTest_RWHelperBuffer* buffer,
    113                                          char comment_char)
    114 {
    115     int num_lines = 0;
    116     char str[MAX_SUTOPTION_LINE_LENGTH];
    117     if(!rw)
    118     {
    119         SDLTest_LogError("rw argument cannot be NULL");
    120         return -1;
    121     }
    122     if(!buffer)
    123     {
    124         SDLTest_LogError("buffer argument cannot be NULL");
    125         return -1;
    126     }
    127     while(SDLVisualTest_RWHelperReadLine(rw, str, MAX_SUTOPTION_LINE_LENGTH,
    128                                          buffer, comment_char))
    129         num_lines++;
    130     return num_lines;
    131 }