Debian Codegraph @

terps/scare/sxfile.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o ...
24 */
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "scare.h"
31 #include "sxprotos.h"
32
33
34 /*
35 * Structure for representing a fake game save/restore file. Used to catch
36 * a serialized gamestate, and return it later on restore. For now we allow
37 * only one of these to exist.
38 */
39 typedef struct
40 {
41 sc_byte *data;
42 sc_int length;
43 sc_bool is_open;
44 sc_bool is_writable;
45 } sx_scr_stream_t;
46 static sx_scr_stream_t scr_serialization_stream = {NULL, 0, FALSE, FALSE};
47
48
49 /*
50 * file_open_file_callback()
51 * file_read_file_callback()
52 * file_write_file_callback()
53 * file_close_file_callback()
54 *
55 * Fake a single gamestate save/restore file. Used to satisfy requests from
56 * the script to serialize and restore a gamestate. Only one "file" can
57 * exist, meaning that a script must restore a saved game before trying to
58 * save another.
59 */
60 void *
61 file_open_file_callback (sc_bool is_save)
62 {
63 sx_scr_stream_t *const stream = &scr_serialization_stream;
64
65 /* Detect any problems due to scripting limitations. */
66 if (stream->is_open)
67 {
68 scr_test_failed ("File open error: %s",
69 "stream is in use (script limitation)");
70 return NULL;
71 }
72 else if (is_save && stream->data)
73 {
74 scr_test_failed ("File open error: %s",
75 "stream has not been read (script limitation)");
76 return NULL;
77 }
78
79 /*
80 * Set up the stream for the requested mode. Act as if no such file if
81 * no data available for a read-only open.
82 */
83 if (is_save)
84 {
85 stream->data = NULL;
86 stream->length = 0;
87 }
88 else if (!stream->data)
89 return NULL;
90
91 stream->is_open = TRUE;
92 stream->is_writable = is_save;
93 return stream;
94 }
95
96 sc_int
97 file_read_file_callback (void *opaque, sc_byte *buffer, sc_int length)
98 {
99 sx_scr_stream_t *const stream = opaque;
100 sc_int bytes;
101 assert (opaque && buffer && length > 0);
102
103 /* Detect any problems with the callback parameters. */
104 if (stream != &scr_serialization_stream)
105 {
106 scr_test_failed ("File read error: %s", "stream is invalid");
107 return 0;
108 }
109 else if (!stream->is_open)
110 {
111 scr_test_failed ("File read error: %s", "stream is not open");
112 return 0;
113 }
114 else if (stream->is_writable)
115 {
116 scr_test_failed ("File read error: %s", "stream is not open for read");
117 return 0;
118 }
119
120 /* Read and remove the first block of data (or all if less than length). */
121 bytes = (stream->length < length) ? stream->length : length;
122 memcpy (buffer, stream->data, bytes);
123 memmove (stream->data, stream->data + bytes, stream->length - bytes);
124 stream->length -= bytes;
125 return bytes;
126 }
127
128 void
129 file_write_file_callback (void *opaque, const sc_byte *buffer, sc_int length)
130 {
131 sx_scr_stream_t *const stream = opaque;
132 assert (opaque && buffer && length > 0);
133
134 /* Detect any problems with the callback parameters. */
135 if (stream != &scr_serialization_stream)
136 {
137 scr_test_failed ("File write error: %s", "stream is invalid");
138 return;
139 }
140 else if (!stream->is_open)
141 {
142 scr_test_failed ("File write error: %s", "stream is not open");
143 return;
144 }
145 else if (!stream->is_writable)
146 {
147 scr_test_failed ("File write error: %s", "stream is not open for write");
148 return;
149 }
150
151 /* Reallocate, then add this block of data to the buffer. */
152 stream->data = sx_realloc (stream->data, stream->length + length);
153 memcpy (stream->data + stream->length, buffer, length);
154 stream->length += length;
155 }
156
157 void
158 file_close_file_callback (void *opaque)
159 {
160 sx_scr_stream_t *const stream = opaque;
161 assert (opaque);
162
163 /* Detect any problems with the callback parameters. */
164 if (stream != &scr_serialization_stream)
165 {
166 scr_test_failed ("File close error: %s", "stream is invalid");
167 return;
168 }
169 else if (!stream->is_open)
170 {
171 scr_test_failed ("File close error: %s", "stream is not open");
172 return;
173 }
174
175 /*
176 * If closing after a read, free allocations, and return the stream to
177 * its empty state; if after write, leave the data for the later read.
178 */
179 if (!stream->is_writable)
180 {
181 sx_free (stream->data);
182 stream->data = NULL;
183 stream->length = 0;
184 }
185 stream->is_writable = FALSE;
186 stream->is_open = FALSE;
187 }
188
189
190 /*
191 * file_cleanup()
192 *
193 * Free any pending allocations and clean up on completion of a script.
194 */
195 void
196 file_cleanup (void)
197 {
198 sx_scr_stream_t *const stream = &scr_serialization_stream;
199
200 sx_free (stream->data);
201 stream->data = NULL;
202 stream->length = 0;
203 stream->is_writable = FALSE;
204 stream->is_open = FALSE;
205 }