RsvgHandle: use a state field instead of finished/is_closed/first_write flags

This makes it easier to see, well, which reading state we are in.
parent fe78b716
......@@ -1347,8 +1347,6 @@ rsvg_handle_close_impl (RsvgHandle * handle, GError ** error)
{
GError *real_error = NULL;
handle->priv->is_closed = TRUE;
handle->priv->error = &real_error;
if (handle->priv->ctxt != NULL) {
......@@ -1366,7 +1364,6 @@ rsvg_handle_close_impl (RsvgHandle * handle, GError ** error)
free_element_name_stack (handle);
handle->priv->finished = TRUE;
handle->priv->error = NULL;
if (real_error != NULL) {
......@@ -1874,10 +1871,13 @@ rsvg_handle_write (RsvgHandle * handle, const guchar * buf, gsize count, GError
rsvg_return_val_if_fail (handle, FALSE, error);
priv = handle->priv;
rsvg_return_val_if_fail (!priv->is_closed, FALSE, error);
rsvg_return_val_if_fail (priv->state == RSVG_HANDLE_STATE_START
|| priv->state == RSVG_HANDLE_STATE_READING,
FALSE,
error);
if (priv->first_write) {
priv->first_write = FALSE;
if (priv->state == RSVG_HANDLE_STATE_START) {
priv->state = RSVG_HANDLE_STATE_READING;
/* test for GZ marker. todo: store the first 2 bytes in the odd circumstance that someone calls
* write() in 1 byte increments */
......@@ -1910,12 +1910,16 @@ gboolean
rsvg_handle_close (RsvgHandle * handle, GError ** error)
{
RsvgHandlePrivate *priv;
gboolean result;
rsvg_return_val_if_fail (handle, FALSE, error);
priv = handle->priv;
if (priv->is_closed)
return TRUE;
if (priv->state == RSVG_HANDLE_STATE_CLOSED_OK
|| priv->state == RSVG_HANDLE_STATE_CLOSED_ERROR) {
/* closing is idempotent */
return TRUE;
}
if (priv->data_input_stream) {
gboolean ret;
......@@ -1927,7 +1931,15 @@ rsvg_handle_close (RsvgHandle * handle, GError ** error)
return ret;
}
return rsvg_handle_close_impl (handle, error);
result = rsvg_handle_close_impl (handle, error);
if (result) {
priv->state = RSVG_HANDLE_STATE_CLOSED_OK;
} else {
priv->state = RSVG_HANDLE_STATE_CLOSED_ERROR;
}
return result;
}
/**
......@@ -1974,6 +1986,7 @@ rsvg_handle_read_stream_sync (RsvgHandle *handle,
stream = g_buffered_input_stream_new (stream);
if (g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream), 2, cancellable, error) != 2) {
g_object_unref (stream);
priv->state = RSVG_HANDLE_STATE_CLOSED_ERROR;
return FALSE;
}
buf = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (stream), NULL);
......@@ -2019,8 +2032,6 @@ rsvg_handle_read_stream_sync (RsvgHandle *handle,
priv->ctxt = rsvg_free_xml_parser_and_doc (priv->ctxt);
priv->finished = TRUE;
res = TRUE;
out:
......@@ -2030,6 +2041,12 @@ rsvg_handle_read_stream_sync (RsvgHandle *handle,
priv->error = NULL;
g_clear_object (&priv->cancellable);
if (res) {
priv->state = RSVG_HANDLE_STATE_CLOSED_OK;
} else {
priv->state = RSVG_HANDLE_STATE_CLOSED_ERROR;
}
return res;
}
......
......@@ -218,7 +218,7 @@ rsvg_handle_render_cairo_sub (RsvgHandle * handle, cairo_t * cr, const char *id)
g_return_val_if_fail (handle != NULL, FALSE);
if (!handle->priv->finished)
if (handle->priv->state != RSVG_HANDLE_STATE_CLOSED_OK)
return FALSE;
if (id && *id)
......
......@@ -68,6 +68,7 @@ rsvg_handle_init (RsvgHandle * self)
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, RSVG_TYPE_HANDLE, RsvgHandlePrivate);
self->priv->flags = RSVG_HANDLE_FLAGS_NONE;
self->priv->state = RSVG_HANDLE_STATE_START;
self->priv->all_nodes = g_ptr_array_new ();
self->priv->defs = rsvg_defs_new (self);
self->priv->handler_nest = 0;
......@@ -88,9 +89,7 @@ rsvg_handle_init (RsvgHandle * self)
self->priv->treebase = NULL;
self->priv->element_name_stack = NULL;
self->priv->finished = FALSE;
self->priv->data_input_stream = NULL;
self->priv->first_write = TRUE;
self->priv->cancellable = NULL;
self->priv->is_disposed = FALSE;
......
......@@ -123,11 +123,20 @@ struct RsvgSaxHandler {
void (*characters) (RsvgSaxHandler * self, const char *ch, int len);
};
/* Reading state for an RsvgHandle */
typedef enum {
RSVG_HANDLE_STATE_START,
RSVG_HANDLE_STATE_READING,
RSVG_HANDLE_STATE_CLOSED_OK,
RSVG_HANDLE_STATE_CLOSED_ERROR
} RsvgHandleState;
struct RsvgHandlePrivate {
RsvgHandleFlags flags;
RsvgHandleState state;
gboolean is_disposed;
gboolean is_closed;
RsvgSizeFunc size_func;
gpointer user_data;
......@@ -170,11 +179,8 @@ struct RsvgHandlePrivate {
gchar *base_uri;
GFile *base_gfile;
gboolean finished;
gboolean in_loop; /* see get_dimension() */
gboolean first_write;
GInputStream *data_input_stream; /* for rsvg_handle_write of svgz data */
gboolean is_testing; /* Are we being run from the test suite? */
......
......@@ -71,7 +71,7 @@ rsvg_handle_get_pixbuf_sub (RsvgHandle * handle, const char *id)
g_return_val_if_fail (handle != NULL, NULL);
if (!handle->priv->finished)
if (handle->priv->state != RSVG_HANDLE_STATE_CLOSED_OK)
return NULL;
rsvg_handle_get_dimensions (handle, &dimensions);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment