@@ -66,33 +66,31 @@ struct _GimpConfigWriter
66
66
};
67
67
68
68
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 );
73
73
74
74
static inline void
75
75
gimp_config_writer_flush (GimpConfigWriter * writer )
76
76
{
77
77
gsize bytes_written ;
78
78
GError * error = NULL ;
79
79
80
+ if (! writer -> output )
81
+ return ;
82
+
80
83
if (! g_output_stream_write_all (writer -> output ,
81
84
writer -> buffer -> str ,
82
85
writer -> buffer -> len ,
83
86
& bytes_written ,
84
87
NULL , & error ))
85
88
{
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
-
93
89
g_set_error (& writer -> error , GIMP_CONFIG_ERROR , GIMP_CONFIG_ERROR_WRITE ,
94
90
_ ("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 );
96
94
g_clear_error (& error );
97
95
}
98
96
@@ -221,6 +219,43 @@ gimp_config_writer_new_gfile (GFile *file,
221
219
return writer ;
222
220
}
223
221
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
+
224
259
/**
225
260
* gimp_config_writer_new_fd:
226
261
* @fd:
@@ -542,8 +577,7 @@ gimp_config_writer_close (GimpConfigWriter *writer)
542
577
{
543
578
g_string_append_c (writer -> buffer , '\n' );
544
579
545
- if (writer -> output )
546
- gimp_config_writer_flush (writer );
580
+ gimp_config_writer_flush (writer );
547
581
}
548
582
}
549
583
@@ -591,7 +625,7 @@ gimp_config_writer_finish (GimpConfigWriter *writer,
591
625
592
626
if (writer -> output )
593
627
{
594
- success = gimp_config_writer_close_file (writer , error );
628
+ success = gimp_config_writer_close_output (writer , error );
595
629
596
630
if (writer -> file )
597
631
g_object_unref (writer -> file );
@@ -601,7 +635,11 @@ gimp_config_writer_finish (GimpConfigWriter *writer,
601
635
602
636
if (writer -> error )
603
637
{
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
+
605
643
success = FALSE;
606
644
}
607
645
@@ -618,7 +656,7 @@ gimp_config_writer_linefeed (GimpConfigWriter *writer)
618
656
if (writer -> error )
619
657
return ;
620
658
621
- if (writer -> buffer -> len == 0 && !writer -> comment )
659
+ if (writer -> output && writer -> buffer -> len == 0 && !writer -> comment )
622
660
{
623
661
gsize bytes_written ;
624
662
GError * error = NULL ;
@@ -629,7 +667,9 @@ gimp_config_writer_linefeed (GimpConfigWriter *writer)
629
667
{
630
668
g_set_error (& writer -> error , GIMP_CONFIG_ERROR , GIMP_CONFIG_ERROR_WRITE ,
631
669
_ ("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 );
633
673
g_clear_error (& error );
634
674
}
635
675
}
@@ -709,34 +749,36 @@ gimp_config_writer_comment (GimpConfigWriter *writer,
709
749
}
710
750
711
751
static gboolean
712
- gimp_config_writer_close_file (GimpConfigWriter * writer ,
713
- GError * * error )
752
+ gimp_config_writer_close_output (GimpConfigWriter * writer ,
753
+ GError * * error )
714
754
{
715
- GError * my_error = NULL ;
716
-
717
755
g_return_val_if_fail (writer -> output != NULL , FALSE);
718
756
719
- if (! writer -> file )
720
- return TRUE;
721
-
722
757
if (writer -> error )
723
758
{
724
759
g_object_unref (writer -> output );
725
760
writer -> output = NULL ;
761
+
726
762
return FALSE;
727
763
}
728
764
729
- if (! g_output_stream_close ( writer -> output , NULL , & my_error ) )
765
+ if (writer -> file )
730
766
{
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 ;
735
768
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 );
738
776
739
- return FALSE;
777
+ g_object_unref (writer -> output );
778
+ writer -> output = NULL ;
779
+
780
+ return FALSE;
781
+ }
740
782
}
741
783
742
784
g_object_unref (writer -> output );
0 commit comments