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