diff options
Diffstat (limited to 'engines/glk/adrift/sxfile.cpp')
-rw-r--r-- | engines/glk/adrift/sxfile.cpp | 236 |
1 files changed, 106 insertions, 130 deletions
diff --git a/engines/glk/adrift/sxfile.cpp b/engines/glk/adrift/sxfile.cpp index 41cfcc485e..4b91db9687 100644 --- a/engines/glk/adrift/sxfile.cpp +++ b/engines/glk/adrift/sxfile.cpp @@ -32,10 +32,10 @@ namespace Adrift { * only one of these to exist. */ struct sx_scr_stream_t { - sc_byte *data; - sc_int length; - sc_bool is_open; - sc_bool is_writable; + sc_byte *data; + sc_int length; + sc_bool is_open; + sc_bool is_writable; }; static sx_scr_stream_t scr_serialization_stream = {NULL, 0, FALSE, FALSE}; @@ -51,132 +51,109 @@ static sx_scr_stream_t scr_serialization_stream = {NULL, 0, FALSE, FALSE}; * exist, meaning that a script must restore a saved game before trying to * save another. */ -void *file_open_file_callback (sc_bool is_save) -{ - sx_scr_stream_t *const stream = &scr_serialization_stream; - - /* Detect any problems due to scripting limitations. */ - if (stream->is_open) - { - scr_test_failed ("File open error: %s", - "stream is in use (script limitation)"); - return NULL; - } - else if (is_save && stream->data) - { - scr_test_failed ("File open error: %s", - "stream has not been read (script limitation)"); - return NULL; - } - - /* - * Set up the stream for the requested mode. Act as if no such file if - * no data available for a read-only open. - */ - if (is_save) - { - stream->data = NULL; - stream->length = 0; - } - else if (!stream->data) - return NULL; - - stream->is_open = TRUE; - stream->is_writable = is_save; - return stream; +void *file_open_file_callback(sc_bool is_save) { + sx_scr_stream_t *const stream = &scr_serialization_stream; + + /* Detect any problems due to scripting limitations. */ + if (stream->is_open) { + scr_test_failed("File open error: %s", + "stream is in use (script limitation)"); + return NULL; + } else if (is_save && stream->data) { + scr_test_failed("File open error: %s", + "stream has not been read (script limitation)"); + return NULL; + } + + /* + * Set up the stream for the requested mode. Act as if no such file if + * no data available for a read-only open. + */ + if (is_save) { + stream->data = NULL; + stream->length = 0; + } else if (!stream->data) + return NULL; + + stream->is_open = TRUE; + stream->is_writable = is_save; + return stream; } sc_int -file_read_file_callback (void *opaque, sc_byte *buffer, sc_int length) -{ - sx_scr_stream_t *const stream = (sx_scr_stream_t *)opaque; - sc_int bytes; - assert (opaque && buffer && length > 0); - - /* Detect any problems with the callback parameters. */ - if (stream != &scr_serialization_stream) - { - scr_test_failed ("File read error: %s", "stream is invalid"); - return 0; - } - else if (!stream->is_open) - { - scr_test_failed ("File read error: %s", "stream is not open"); - return 0; - } - else if (stream->is_writable) - { - scr_test_failed ("File read error: %s", "stream is not open for read"); - return 0; - } - - /* Read and remove the first block of data (or all if less than length). */ - bytes = (stream->length < length) ? stream->length : length; - memcpy (buffer, stream->data, bytes); - memmove (stream->data, stream->data + bytes, stream->length - bytes); - stream->length -= bytes; - return bytes; +file_read_file_callback(void *opaque, sc_byte *buffer, sc_int length) { + sx_scr_stream_t *const stream = (sx_scr_stream_t *)opaque; + sc_int bytes; + assert(opaque && buffer && length > 0); + + /* Detect any problems with the callback parameters. */ + if (stream != &scr_serialization_stream) { + scr_test_failed("File read error: %s", "stream is invalid"); + return 0; + } else if (!stream->is_open) { + scr_test_failed("File read error: %s", "stream is not open"); + return 0; + } else if (stream->is_writable) { + scr_test_failed("File read error: %s", "stream is not open for read"); + return 0; + } + + /* Read and remove the first block of data (or all if less than length). */ + bytes = (stream->length < length) ? stream->length : length; + memcpy(buffer, stream->data, bytes); + memmove(stream->data, stream->data + bytes, stream->length - bytes); + stream->length -= bytes; + return bytes; } void -file_write_file_callback (void *opaque, const sc_byte *buffer, sc_int length) -{ - sx_scr_stream_t *const stream = (sx_scr_stream_t *)opaque; - assert (opaque && buffer && length > 0); - - /* Detect any problems with the callback parameters. */ - if (stream != &scr_serialization_stream) - { - scr_test_failed ("File write error: %s", "stream is invalid"); - return; - } - else if (!stream->is_open) - { - scr_test_failed ("File write error: %s", "stream is not open"); - return; - } - else if (!stream->is_writable) - { - scr_test_failed ("File write error: %s", "stream is not open for write"); - return; - } - - /* Reallocate, then add this block of data to the buffer. */ - stream->data = (sc_byte *)sx_realloc(stream->data, stream->length + length); - memcpy (stream->data + stream->length, buffer, length); - stream->length += length; +file_write_file_callback(void *opaque, const sc_byte *buffer, sc_int length) { + sx_scr_stream_t *const stream = (sx_scr_stream_t *)opaque; + assert(opaque && buffer && length > 0); + + /* Detect any problems with the callback parameters. */ + if (stream != &scr_serialization_stream) { + scr_test_failed("File write error: %s", "stream is invalid"); + return; + } else if (!stream->is_open) { + scr_test_failed("File write error: %s", "stream is not open"); + return; + } else if (!stream->is_writable) { + scr_test_failed("File write error: %s", "stream is not open for write"); + return; + } + + /* Reallocate, then add this block of data to the buffer. */ + stream->data = (sc_byte *)sx_realloc(stream->data, stream->length + length); + memcpy(stream->data + stream->length, buffer, length); + stream->length += length; } void -file_close_file_callback (void *opaque) -{ - sx_scr_stream_t *const stream = (sx_scr_stream_t *)opaque; - assert (opaque); - - /* Detect any problems with the callback parameters. */ - if (stream != &scr_serialization_stream) - { - scr_test_failed ("File close error: %s", "stream is invalid"); - return; - } - else if (!stream->is_open) - { - scr_test_failed ("File close error: %s", "stream is not open"); - return; - } - - /* - * If closing after a read, free allocations, and return the stream to - * its empty state; if after write, leave the data for the later read. - */ - if (!stream->is_writable) - { - sx_free (stream->data); - stream->data = NULL; - stream->length = 0; - } - stream->is_writable = FALSE; - stream->is_open = FALSE; +file_close_file_callback(void *opaque) { + sx_scr_stream_t *const stream = (sx_scr_stream_t *)opaque; + assert(opaque); + + /* Detect any problems with the callback parameters. */ + if (stream != &scr_serialization_stream) { + scr_test_failed("File close error: %s", "stream is invalid"); + return; + } else if (!stream->is_open) { + scr_test_failed("File close error: %s", "stream is not open"); + return; + } + + /* + * If closing after a read, free allocations, and return the stream to + * its empty state; if after write, leave the data for the later read. + */ + if (!stream->is_writable) { + sx_free(stream->data); + stream->data = NULL; + stream->length = 0; + } + stream->is_writable = FALSE; + stream->is_open = FALSE; } @@ -186,15 +163,14 @@ file_close_file_callback (void *opaque) * Free any pending allocations and clean up on completion of a script. */ void -file_cleanup (void) -{ - sx_scr_stream_t *const stream = &scr_serialization_stream; - - sx_free (stream->data); - stream->data = NULL; - stream->length = 0; - stream->is_writable = FALSE; - stream->is_open = FALSE; +file_cleanup(void) { + sx_scr_stream_t *const stream = &scr_serialization_stream; + + sx_free(stream->data); + stream->data = NULL; + stream->length = 0; + stream->is_writable = FALSE; + stream->is_open = FALSE; } } // End of namespace Adrift |