@@ -109,8 +109,8 @@ GScanner *
109
109
gimp_scanner_new_gfile (GFile * file ,
110
110
GError * * error )
111
111
{
112
- GScanner * scanner ;
113
- gchar * path ;
112
+ GScanner * scanner ;
113
+ gchar * path ;
114
114
115
115
g_return_val_if_fail (G_IS_FILE (file ), NULL );
116
116
g_return_val_if_fail (error == NULL || * error == NULL , NULL );
@@ -148,9 +148,6 @@ gimp_scanner_new_gfile (GFile *file,
148
148
else
149
149
{
150
150
GInputStream * input ;
151
- GString * string ;
152
- gchar buffer [4096 ];
153
- gsize bytes_read ;
154
151
155
152
input = G_INPUT_STREAM (g_file_read (file , NULL , error ));
156
153
@@ -167,51 +164,84 @@ gimp_scanner_new_gfile (GFile *file,
167
164
return NULL ;
168
165
}
169
166
170
- string = g_string_new ( NULL );
167
+ g_object_set_data ( G_OBJECT ( input ), "gimp-data" , file );
171
168
172
- do
173
- {
174
- GError * my_error = NULL ;
175
- gboolean success ;
169
+ scanner = gimp_scanner_new_stream (input , error );
170
+
171
+ g_object_unref (input );
172
+ }
173
+
174
+ return scanner ;
175
+ }
176
+
177
+ /**
178
+ * gimp_scanner_new_stream:
179
+ * @input: a #GInputStream
180
+ * @error: return location for #GError, or %NULL
181
+ *
182
+ * Return value: The new #GScanner.
183
+ *
184
+ * Since: GIMP 2.10
185
+ **/
186
+ GScanner *
187
+ gimp_scanner_new_stream (GInputStream * input ,
188
+ GError * * error )
189
+ {
190
+ GScanner * scanner ;
191
+ GFile * file ;
192
+ const gchar * path ;
193
+ GString * string ;
194
+ gchar buffer [4096 ];
195
+ gsize bytes_read ;
196
+
197
+ g_return_val_if_fail (G_IS_INPUT_STREAM (input ), NULL );
198
+ g_return_val_if_fail (error == NULL || * error == NULL , NULL );
199
+
200
+ file = g_object_get_data (G_OBJECT (input ), "gimp-file" );
201
+ if (file )
202
+ path = gimp_file_get_utf8_name (file );
203
+ else
204
+ path = "stream" ;
176
205
177
- success = g_input_stream_read_all (input , buffer , sizeof (buffer ),
178
- & bytes_read , NULL , & my_error );
206
+ string = g_string_new (NULL );
179
207
180
- if (bytes_read > 0 )
181
- g_string_append_len (string , buffer , bytes_read );
208
+ do
209
+ {
210
+ GError * my_error = NULL ;
211
+ gboolean success ;
212
+
213
+ success = g_input_stream_read_all (input , buffer , sizeof (buffer ),
214
+ & bytes_read , NULL , & my_error );
215
+
216
+ if (bytes_read > 0 )
217
+ g_string_append_len (string , buffer , bytes_read );
182
218
183
- if (! success )
219
+ if (! success )
220
+ {
221
+ if (string -> len > 0 )
184
222
{
185
- if (string -> len > 0 )
186
- {
187
- g_printerr ("%s: read error in '%s', trying to scan "
188
- "partial content: %s" ,
189
- G_STRFUNC , gimp_file_get_utf8_name (file ),
190
- my_error -> message );
191
- g_clear_error (& my_error );
192
- break ;
193
- }
194
-
195
- g_string_free (string , TRUE);
196
- g_object_unref (input );
197
-
198
- g_propagate_error (error , my_error );
199
-
200
- return NULL ;
223
+ g_printerr ("%s: read error in '%s', trying to scan "
224
+ "partial content: %s" ,
225
+ G_STRFUNC , path , my_error -> message );
226
+ g_clear_error (& my_error );
227
+ break ;
201
228
}
202
- }
203
- while (bytes_read == sizeof (buffer ));
204
229
205
- g_object_unref ( input );
230
+ g_string_free ( string , TRUE );
206
231
207
- /* gimp_scanner_new() takes a "name" for the scanner, not a filename */
208
- scanner = gimp_scanner_new (gimp_file_get_utf8_name (file ),
209
- NULL , string -> str , error );
232
+ g_propagate_error (error , my_error );
210
233
211
- bytes_read = string -> len ;
212
-
213
- g_scanner_input_text (scanner , g_string_free (string , FALSE), bytes_read );
234
+ return NULL ;
235
+ }
214
236
}
237
+ while (bytes_read == sizeof (buffer ));
238
+
239
+ /* gimp_scanner_new() takes a "name" for the scanner, not a filename */
240
+ scanner = gimp_scanner_new (path , NULL , string -> str , error );
241
+
242
+ bytes_read = string -> len ;
243
+
244
+ g_scanner_input_text (scanner , g_string_free (string , FALSE), bytes_read );
215
245
216
246
return scanner ;
217
247
}
0 commit comments