Skip to content

Latest commit

 

History

History
131 lines (121 loc) · 3.4 KB

rwhelper.c

File metadata and controls

131 lines (121 loc) · 3.4 KB
 
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
/* See COPYING.txt for the full license governing this code. */
/**
* \file rwhelper.c
*
* Source file with some helper functions for working with SDL_RWops.
*/
#include <SDL_test.h>
#include "SDL_visualtest_sut_configparser.h"
#include "SDL_visualtest_rwhelper.h"
void
SDLVisualTest_RWHelperResetBuffer(SDLVisualTest_RWHelperBuffer* buffer)
{
if(!buffer)
{
SDLTest_LogError("buffer argument cannot be NULL");
return;
}
buffer->buffer_pos = 0;
buffer->buffer_width = 0;
}
char
SDLVisualTest_RWHelperReadChar(SDL_RWops* rw, SDLVisualTest_RWHelperBuffer* buffer)
{
if(!rw || !buffer)
return 0;
/* if the buffer has been consumed, we fill it up again */
if(buffer->buffer_pos == buffer->buffer_width)
{
buffer->buffer_width = SDL_RWread(rw, buffer->buffer, 1, RWOPS_BUFFER_LEN);
buffer->buffer_pos = 0;
if(buffer->buffer_width == 0)
return 0;
}
buffer->buffer_pos++;
return buffer->buffer[buffer->buffer_pos - 1];
}
/* does not include new lines in the buffer and adds a trailing null character */
char*
SDLVisualTest_RWHelperReadLine(SDL_RWops* rw, char* str, int size,
SDLVisualTest_RWHelperBuffer* buffer,
char comment_char)
{
char ch;
int current_pos, done;
if(!rw)
{
SDLTest_LogError("rw argument cannot be NULL");
return NULL;
}
if(!str)
{
SDLTest_LogError("str argument cannot be NULL");
return NULL;
}
if(!buffer)
{
SDLTest_LogError("buffer argument cannot be NULL");
return NULL;
}
if(size <= 0)
{
SDLTest_LogError("size argument should be positive");
return NULL;
}
done = 0;
while(!done)
{
/* ignore leading whitespace */
for(ch = SDLVisualTest_RWHelperReadChar(rw, buffer); ch && SDL_isspace(ch);
ch = SDLVisualTest_RWHelperReadChar(rw, buffer));
for(current_pos = 0;
ch && ch != '\n' && ch != '\r' && ch != comment_char;
current_pos++)
{
str[current_pos] = ch;
if(current_pos >= size - 2)
{
current_pos++;
break;
}
ch = SDLVisualTest_RWHelperReadChar(rw, buffer);
}
done = 1;
if(ch == comment_char) /* discard all characters until the next line */
{
do
{
ch = SDLVisualTest_RWHelperReadChar(rw, buffer);
}while(ch && ch != '\n' && ch != '\r');
if(current_pos == 0)
done = 0;
}
}
if(current_pos == 0)
return NULL;
str[current_pos] = '\0';
return str;
}
/* Lines with all whitespace are ignored */
int
SDLVisualTest_RWHelperCountNonEmptyLines(SDL_RWops* rw,
SDLVisualTest_RWHelperBuffer* buffer,
char comment_char)
{
int num_lines = 0;
char str[MAX_SUTOPTION_LINE_LENGTH];
if(!rw)
{
SDLTest_LogError("rw argument cannot be NULL");
return -1;
}
if(!buffer)
{
SDLTest_LogError("buffer argument cannot be NULL");
return -1;
}
while(SDLVisualTest_RWHelperReadLine(rw, str, MAX_SUTOPTION_LINE_LENGTH,
buffer, comment_char))
num_lines++;
return num_lines;
}