Skip to content

Commit 0acbfc3

Browse files
committed
libgimpconfig: add gimp_config_serialize_to_stream()
and gimp_config_writer_new_stream() which take a GOutputStream. Also fix some new and old corner cases in GimpConfigWriter.
1 parent 5203296 commit 0acbfc3

5 files changed

+125
-33
lines changed

libgimpconfig/gimpconfig-iface.c

+39
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,45 @@ gimp_config_serialize_to_gfile (GimpConfig *config,
338338
return gimp_config_writer_finish (writer, footer, error);
339339
}
340340

341+
/**
342+
* gimp_config_serialize_to_stream:
343+
* @config: a #GObject that implements the #GimpConfigInterface.
344+
* @stream: the #GOutputStream to write the configuration to.
345+
* @header: optional file header (must be ASCII only)
346+
* @footer: optional file footer (must be ASCII only)
347+
* @data: user data passed to the serialize implementation.
348+
* @error: return location for a possible error
349+
*
350+
* Serializes the object properties of @config to the stream specified
351+
* by @stream.
352+
*
353+
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
354+
*
355+
* Since: GIMP 2.10
356+
**/
357+
gboolean
358+
gimp_config_serialize_to_stream (GimpConfig *config,
359+
GOutputStream *output,
360+
const gchar *header,
361+
const gchar *footer,
362+
gpointer data,
363+
GError **error)
364+
{
365+
GimpConfigWriter *writer;
366+
367+
g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
368+
g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), FALSE);
369+
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
370+
371+
writer = gimp_config_writer_new_stream (output, header, error);
372+
if (!writer)
373+
return FALSE;
374+
375+
GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
376+
377+
return gimp_config_writer_finish (writer, footer, error);
378+
}
379+
341380
/**
342381
* gimp_config_serialize_to_fd:
343382
* @config: a #GObject that implements the #GimpConfigInterface.

libgimpconfig/gimpconfig-iface.h

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ gboolean gimp_config_serialize_to_gfile (GimpConfig *config,
8585
const gchar *footer,
8686
gpointer data,
8787
GError **error);
88+
gboolean gimp_config_serialize_to_stream (GimpConfig *config,
89+
GOutputStream *output,
90+
const gchar *header,
91+
const gchar *footer,
92+
gpointer data,
93+
GError **error);
8894
gboolean gimp_config_serialize_to_fd (GimpConfig *config,
8995
gint fd,
9096
gpointer data);

libgimpconfig/gimpconfig.def

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ EXPORTS
3131
gimp_config_serialize_to_fd
3232
gimp_config_serialize_to_file
3333
gimp_config_serialize_to_gfile
34+
gimp_config_serialize_to_stream
3435
gimp_config_serialize_to_string
3536
gimp_config_serialize_value
3637
gimp_config_string_append_escaped
@@ -45,6 +46,7 @@ EXPORTS
4546
gimp_config_writer_new_fd
4647
gimp_config_writer_new_file
4748
gimp_config_writer_new_gfile
49+
gimp_config_writer_new_stream
4850
gimp_config_writer_new_string
4951
gimp_config_writer_open
5052
gimp_config_writer_print

libgimpconfig/gimpconfigwriter.c

+75-33
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,31 @@ struct _GimpConfigWriter
6666
};
6767

6868

69-
static inline void gimp_config_writer_flush (GimpConfigWriter *writer);
70-
static inline void gimp_config_writer_newline (GimpConfigWriter *writer);
71-
static gboolean gimp_config_writer_close_file (GimpConfigWriter *writer,
72-
GError **error);
69+
static inline void gimp_config_writer_flush (GimpConfigWriter *writer);
70+
static inline void gimp_config_writer_newline (GimpConfigWriter *writer);
71+
static gboolean gimp_config_writer_close_output (GimpConfigWriter *writer,
72+
GError **error);
7373

7474
static inline void
7575
gimp_config_writer_flush (GimpConfigWriter *writer)
7676
{
7777
gsize bytes_written;
7878
GError *error = NULL;
7979

80+
if (! writer->output)
81+
return;
82+
8083
if (! g_output_stream_write_all (writer->output,
8184
writer->buffer->str,
8285
writer->buffer->len,
8386
&bytes_written,
8487
NULL, &error))
8588
{
86-
const gchar *path;
87-
88-
if (writer->file)
89-
path = gimp_file_get_utf8_name (writer->file);
90-
else
91-
path = "file descriptor";
92-
9389
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
9490
_("Error writing to '%s': %s"),
95-
path, error->message);
91+
writer->file ?
92+
gimp_file_get_utf8_name (writer->file) : "output stream",
93+
error->message);
9694
g_clear_error (&error);
9795
}
9896

@@ -221,6 +219,43 @@ gimp_config_writer_new_gfile (GFile *file,
221219
return writer;
222220
}
223221

222+
/**
223+
* gimp_config_writer_new_stream:
224+
* @output: a #GOutputStream
225+
* @header: text to include as comment at the top of the file
226+
* @error: return location for errors
227+
*
228+
* Creates a new #GimpConfigWriter and sets it up to write to
229+
* @output.
230+
*
231+
* Return value: a new #GimpConfigWriter or %NULL in case of an error
232+
*
233+
* Since: GIMP 2.10
234+
**/
235+
GimpConfigWriter *
236+
gimp_config_writer_new_stream (GOutputStream *output,
237+
const gchar *header,
238+
GError **error)
239+
{
240+
GimpConfigWriter *writer;
241+
242+
g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), NULL);
243+
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
244+
245+
writer = g_slice_new0 (GimpConfigWriter);
246+
247+
writer->output = g_object_ref (output);
248+
writer->buffer = g_string_new (NULL);
249+
250+
if (header)
251+
{
252+
gimp_config_writer_comment (writer, header);
253+
gimp_config_writer_linefeed (writer);
254+
}
255+
256+
return writer;
257+
}
258+
224259
/**
225260
* gimp_config_writer_new_fd:
226261
* @fd:
@@ -542,8 +577,7 @@ gimp_config_writer_close (GimpConfigWriter *writer)
542577
{
543578
g_string_append_c (writer->buffer, '\n');
544579

545-
if (writer->output)
546-
gimp_config_writer_flush (writer);
580+
gimp_config_writer_flush (writer);
547581
}
548582
}
549583

@@ -591,7 +625,7 @@ gimp_config_writer_finish (GimpConfigWriter *writer,
591625

592626
if (writer->output)
593627
{
594-
success = gimp_config_writer_close_file (writer, error);
628+
success = gimp_config_writer_close_output (writer, error);
595629

596630
if (writer->file)
597631
g_object_unref (writer->file);
@@ -601,7 +635,11 @@ gimp_config_writer_finish (GimpConfigWriter *writer,
601635

602636
if (writer->error)
603637
{
604-
g_propagate_error (error, writer->error);
638+
if (error && *error == NULL)
639+
g_propagate_error (error, writer->error);
640+
else
641+
g_clear_error (&writer->error);
642+
605643
success = FALSE;
606644
}
607645

@@ -618,7 +656,7 @@ gimp_config_writer_linefeed (GimpConfigWriter *writer)
618656
if (writer->error)
619657
return;
620658

621-
if (writer->buffer->len == 0 && !writer->comment)
659+
if (writer->output && writer->buffer->len == 0 && !writer->comment)
622660
{
623661
gsize bytes_written;
624662
GError *error = NULL;
@@ -629,7 +667,9 @@ gimp_config_writer_linefeed (GimpConfigWriter *writer)
629667
{
630668
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
631669
_("Error writing to '%s': %s"),
632-
gimp_file_get_utf8_name (writer->file), error->message);
670+
writer->file ?
671+
gimp_file_get_utf8_name (writer->file) : "output stream",
672+
error->message);
633673
g_clear_error (&error);
634674
}
635675
}
@@ -709,34 +749,36 @@ gimp_config_writer_comment (GimpConfigWriter *writer,
709749
}
710750

711751
static gboolean
712-
gimp_config_writer_close_file (GimpConfigWriter *writer,
713-
GError **error)
752+
gimp_config_writer_close_output (GimpConfigWriter *writer,
753+
GError **error)
714754
{
715-
GError *my_error = NULL;
716-
717755
g_return_val_if_fail (writer->output != NULL, FALSE);
718756

719-
if (! writer->file)
720-
return TRUE;
721-
722757
if (writer->error)
723758
{
724759
g_object_unref (writer->output);
725760
writer->output = NULL;
761+
726762
return FALSE;
727763
}
728764

729-
if (! g_output_stream_close (writer->output, NULL, &my_error))
765+
if (writer->file)
730766
{
731-
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
732-
_("Error writing '%s': %s"),
733-
gimp_file_get_utf8_name (writer->file), my_error->message);
734-
g_clear_error (&my_error);
767+
GError *my_error = NULL;
735768

736-
g_object_unref (writer->output);
737-
writer->output = NULL;
769+
if (! g_output_stream_close (writer->output, NULL, &my_error))
770+
{
771+
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
772+
_("Error writing '%s': %s"),
773+
gimp_file_get_utf8_name (writer->file),
774+
my_error->message);
775+
g_clear_error (&my_error);
738776

739-
return FALSE;
777+
g_object_unref (writer->output);
778+
writer->output = NULL;
779+
780+
return FALSE;
781+
}
740782
}
741783

742784
g_object_unref (writer->output);

libgimpconfig/gimpconfigwriter.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ GimpConfigWriter * gimp_config_writer_new_gfile (GFile *file,
3535
gboolean atomic,
3636
const gchar *header,
3737
GError **error);
38+
GimpConfigWriter * gimp_config_writer_new_stream (GOutputStream *output,
39+
const gchar *header,
40+
GError **error);
3841
GimpConfigWriter * gimp_config_writer_new_fd (gint fd);
3942
GimpConfigWriter * gimp_config_writer_new_string (GString *string);
4043

0 commit comments

Comments
 (0)