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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/adrift/scare.h"
#include "glk/adrift/sxprotos.h"
#include "common/debug.h"
#include "common/str.h"
#include "common/file.h"
#include "common/textconsole.h"
namespace Glk {
namespace Adrift {
/*
* sx_trace()
*
* Debugging trace function; printf wrapper that writes to stdout. Note that
* this differs from sc_trace(), which writes to stderr. We use stdout so
* that trace output is synchronized to test expectation failure messages.
*/
void sx_trace(const sc_char *format, ...) {
va_list ap;
assert (format);
va_start(ap, format);
Common::String line = Common::String::vformat(format, ap);
va_end(ap);
debug("%s", line.c_str());
}
/*
* sx_error()
* sx_fatal()
*
* Error reporting functions. sx_error() prints a message and continues.
* sx_fatal() prints a message, then calls abort().
*/
void sx_error(const sc_char *format, ...) {
va_list ap;
assert(format);
va_start(ap, format);
Common::String line = Common::String::vformat(format, ap);
va_end(ap);
warning("%s", line.c_str());
}
void sx_fatal(const sc_char *format, ...) {
va_list ap;
assert(format);
va_start(ap, format);
Common::String line = Common::String::vformat(format, ap);
va_end(ap);
error("%s", line.c_str());
}
/* Unique non-heap address for zero size malloc() and realloc() requests. */
static void *sx_zero_allocation = &sx_zero_allocation;
/*
* sx_malloc()
* sx_realloc()
* sx_free()
*
* Non-failing wrappers around malloc functions. Newly allocated memory is
* cleared to zero. In ANSI/ISO C, zero byte allocations are implementation-
* defined, so we have to take special care to get predictable behavior.
*/
void *
sx_malloc (size_t size)
{
void *allocated;
if (size == 0)
return sx_zero_allocation;
allocated = malloc (size);
if (!allocated)
sx_fatal ("sx_malloc: requested %lu bytes\n", (sc_uint) size);
else if (allocated == sx_zero_allocation)
sx_fatal ("sx_malloc: zero-byte allocation address returned\n");
memset (allocated, 0, size);
return allocated;
}
void *
sx_realloc (void *pointer, size_t size)
{
void *allocated;
if (size == 0)
{
sx_free (pointer);
return sx_zero_allocation;
}
if (pointer == sx_zero_allocation)
pointer = NULL;
allocated = realloc (pointer, size);
if (!allocated)
sx_fatal ("sx_realloc: requested %lu bytes\n", (sc_uint) size);
else if (allocated == sx_zero_allocation)
sx_fatal ("sx_realloc: zero-byte allocation address returned\n");
if (!pointer)
memset (allocated, 0, size);
return allocated;
}
void sx_free (void *pointer) {
if (sx_zero_allocation != &sx_zero_allocation)
sx_fatal ("sx_free: write to zero-byte allocation address detected\n");
if (pointer && pointer != sx_zero_allocation)
free (pointer);
}
/*
* sx_fopen()
*
* Open a file for a given test name with the extension and mode supplied.
* Returns NULL if unsuccessful.
*/
Common::SeekableReadStream *sx_fopen(const sc_char *name, const sc_char *extension, const sc_char *mode) {
assert (name && extension && mode);
Common::String filename = Common::String::format("%s.%s", name, extension);
Common::File *f = new Common::File();
if (f->open(filename))
return f;
delete f;
return nullptr;
}
/* Miscellaneous general ascii constants. */
static const sc_char NUL = '\0';
/*
* sx_isspace()
* sx_isprint()
*
* Built in replacements for locale-sensitive libc ctype.h functions.
*/
static sc_bool
sx_isspace (sc_char character)
{
static const sc_char *const WHITESPACE = "\t\n\v\f\r ";
return character != NUL && strchr (WHITESPACE, character) != NULL;
}
static sc_bool
sx_isprint (sc_char character)
{
static const sc_int MIN_PRINTABLE = ' ', MAX_PRINTABLE = '~';
return character >= MIN_PRINTABLE && character <= MAX_PRINTABLE;
}
/*
* sx_trim_string()
*
* Trim leading and trailing whitespace from a string. Modifies the string
* in place, and returns the string address for convenience.
*/
sc_char *
sx_trim_string (sc_char *string)
{
sc_int index_;
assert (string);
for (index_ = strlen (string) - 1;
index_ >= 0 && sx_isspace (string[index_]); index_--)
string[index_] = NUL;
for (index_ = 0; sx_isspace (string[index_]);)
index_++;
memmove (string, string + index_, strlen (string) - index_ + 1);
return string;
}
/*
* sx_normalize_string()
*
* Trim a string, set all runs of whitespace to a single space character,
* and convert all non-printing characters to '?'. Modifies the string in
* place, and returns the string address for convenience.
*/
sc_char *
sx_normalize_string (sc_char *string)
{
sc_int index_;
assert (string);
string = sx_trim_string (string);
for (index_ = 0; string[index_] != NUL; index_++)
{
if (sx_isspace (string[index_]))
{
sc_int cursor;
string[index_] = ' ';
for (cursor = index_ + 1; sx_isspace (string[cursor]);)
cursor++;
memmove (string + index_ + 1,
string + cursor, strlen (string + cursor) + 1);
}
else if (!sx_isprint (string[index_]))
string[index_] = '?';
}
return string;
}
char *adrift_fgets(char *buf, int max, Common::SeekableReadStream *s) {
char *ptr = buf;
char c;
while (s->pos() < s->size() && --max > 0) {
c = s->readByte();
if (c == '\n' || c == '\0')
break;
*ptr++ = c;
}
*ptr++ = '\0';
return buf;
}
} // End of namespace Adrift
} // End of namespace Glk
|