libgimpconfig: add gimp_config_deserialize_stream()

and gimp_scanner_new_stream().
This commit is contained in:
Michael Natterer 2014-07-04 03:29:09 +02:00
parent e7d9e01d63
commit d7812173f4
5 changed files with 163 additions and 77 deletions

View file

@ -535,6 +535,54 @@ gimp_config_deserialize_gfile (GimpConfig *config,
return success; return success;
} }
/**
* gimp_config_deserialize_stream:
* @config: a #GObject that implements the #GimpConfigInterface.
* @input: the #GInputStream to read configuration from.
* @data: user data passed to the deserialize implementation.
* @error: return location for a possible error
*
* Reads configuration data from @input and configures @config
* accordingly. Basically this function creates a properly configured
* #GScanner for you and calls the deserialize function of the
* @config's #GimpConfigInterface.
*
* Return value: %TRUE if deserialization succeeded, %FALSE otherwise.
*
* Since: GIMP 2.10
**/
gboolean
gimp_config_deserialize_stream (GimpConfig *config,
GInputStream *input,
gpointer data,
GError **error)
{
GScanner *scanner;
gboolean success;
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
g_return_val_if_fail (G_IS_INPUT_STREAM (input), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
scanner = gimp_scanner_new_stream (input, error);
if (! scanner)
return FALSE;
g_object_freeze_notify (G_OBJECT (config));
success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
scanner, 0, data);
g_object_thaw_notify (G_OBJECT (config));
gimp_scanner_destroy (scanner);
if (! success)
g_assert (error == NULL || *error != NULL);
return success;
}
/** /**
* gimp_config_deserialize_string: * gimp_config_deserialize_string:
* @config: a #GObject that implements the #GimpConfigInterface. * @config: a #GObject that implements the #GimpConfigInterface.

View file

@ -104,6 +104,10 @@ gboolean gimp_config_deserialize_gfile (GimpConfig *config,
GFile *file, GFile *file,
gpointer data, gpointer data,
GError **error); GError **error);
gboolean gimp_config_deserialize_stream (GimpConfig *config,
GInputStream *input,
gpointer data,
GError **error);
gboolean gimp_config_deserialize_string (GimpConfig *config, gboolean gimp_config_deserialize_string (GimpConfig *config,
const gchar *text, const gchar *text,
gint text_len, gint text_len,

View file

@ -12,6 +12,7 @@ EXPORTS
gimp_config_deserialize_properties gimp_config_deserialize_properties
gimp_config_deserialize_property gimp_config_deserialize_property
gimp_config_deserialize_return gimp_config_deserialize_return
gimp_config_deserialize_stream
gimp_config_deserialize_string gimp_config_deserialize_string
gimp_config_diff gimp_config_diff
gimp_config_duplicate gimp_config_duplicate
@ -59,6 +60,7 @@ EXPORTS
gimp_scanner_destroy gimp_scanner_destroy
gimp_scanner_new_file gimp_scanner_new_file
gimp_scanner_new_gfile gimp_scanner_new_gfile
gimp_scanner_new_stream
gimp_scanner_new_string gimp_scanner_new_string
gimp_scanner_parse_boolean gimp_scanner_parse_boolean
gimp_scanner_parse_color gimp_scanner_parse_color

View file

@ -148,9 +148,6 @@ gimp_scanner_new_gfile (GFile *file,
else else
{ {
GInputStream *input; GInputStream *input;
GString *string;
gchar buffer[4096];
gsize bytes_read;
input = G_INPUT_STREAM (g_file_read (file, NULL, error)); input = G_INPUT_STREAM (g_file_read (file, NULL, error));
@ -167,6 +164,45 @@ gimp_scanner_new_gfile (GFile *file,
return NULL; return NULL;
} }
g_object_set_data (G_OBJECT (input), "gimp-data", file);
scanner = gimp_scanner_new_stream (input, error);
g_object_unref (input);
}
return scanner;
}
/**
* gimp_scanner_new_stream:
* @input: a #GInputStream
* @error: return location for #GError, or %NULL
*
* Return value: The new #GScanner.
*
* Since: GIMP 2.10
**/
GScanner *
gimp_scanner_new_stream (GInputStream *input,
GError **error)
{
GScanner *scanner;
GFile *file;
const gchar *path;
GString *string;
gchar buffer[4096];
gsize bytes_read;
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
file = g_object_get_data (G_OBJECT (input), "gimp-file");
if (file)
path = gimp_file_get_utf8_name (file);
else
path = "stream";
string = g_string_new (NULL); string = g_string_new (NULL);
do do
@ -186,14 +222,12 @@ gimp_scanner_new_gfile (GFile *file,
{ {
g_printerr ("%s: read error in '%s', trying to scan " g_printerr ("%s: read error in '%s', trying to scan "
"partial content: %s", "partial content: %s",
G_STRFUNC, gimp_file_get_utf8_name (file), G_STRFUNC, path, my_error->message);
my_error->message);
g_clear_error (&my_error); g_clear_error (&my_error);
break; break;
} }
g_string_free (string, TRUE); g_string_free (string, TRUE);
g_object_unref (input);
g_propagate_error (error, my_error); g_propagate_error (error, my_error);
@ -202,16 +236,12 @@ gimp_scanner_new_gfile (GFile *file,
} }
while (bytes_read == sizeof (buffer)); while (bytes_read == sizeof (buffer));
g_object_unref (input);
/* gimp_scanner_new() takes a "name" for the scanner, not a filename */ /* gimp_scanner_new() takes a "name" for the scanner, not a filename */
scanner = gimp_scanner_new (gimp_file_get_utf8_name (file), scanner = gimp_scanner_new (path, NULL, string->str, error);
NULL, string->str, error);
bytes_read = string->len; bytes_read = string->len;
g_scanner_input_text (scanner, g_string_free (string, FALSE), bytes_read); g_scanner_input_text (scanner, g_string_free (string, FALSE), bytes_read);
}
return scanner; return scanner;
} }

View file

@ -32,6 +32,8 @@ GScanner * gimp_scanner_new_file (const gchar *filename,
GError **error); GError **error);
GScanner * gimp_scanner_new_gfile (GFile *file, GScanner * gimp_scanner_new_gfile (GFile *file,
GError **error); GError **error);
GScanner * gimp_scanner_new_stream (GInputStream *input,
GError **error);
GScanner * gimp_scanner_new_string (const gchar *text, GScanner * gimp_scanner_new_string (const gchar *text,
gint text_len, gint text_len,
GError **error); GError **error);