diff --git a/app/operations/gimpcurvesconfig.c b/app/operations/gimpcurvesconfig.c index fdb6085158..3f1e670d04 100644 --- a/app/operations/gimpcurvesconfig.c +++ b/app/operations/gimpcurvesconfig.c @@ -20,12 +20,9 @@ #include "config.h" -#include - #include #include #include -#include #include "libgimpbase/gimpbase.h" #include "libgimpcolor/gimpcolor.h" @@ -499,26 +496,34 @@ gimp_curves_config_reset_channel (GimpCurvesConfig *config) gboolean gimp_curves_config_load_cruft (GimpCurvesConfig *config, - gpointer fp, + GInputStream *input, GError **error) { - FILE *file = fp; - gint i, j; - gint fields; - gchar buf[50]; - gint index[5][GIMP_CURVE_N_CRUFT_POINTS]; - gint value[5][GIMP_CURVE_N_CRUFT_POINTS]; + GDataInputStream *data_input; + gint index[5][GIMP_CURVE_N_CRUFT_POINTS]; + gint value[5][GIMP_CURVE_N_CRUFT_POINTS]; + gchar *line; + gsize line_len; + gint i, j; g_return_val_if_fail (GIMP_IS_CURVES_CONFIG (config), FALSE); - g_return_val_if_fail (file != NULL, FALSE); + g_return_val_if_fail (G_IS_INPUT_STREAM (input), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if (! fgets (buf, sizeof (buf), file) || - strcmp (buf, "# GIMP Curves File\n") != 0) + data_input = g_data_input_stream_new (input); + + line_len = 64; + line = g_data_input_stream_read_line (data_input, &line_len, + NULL, error); + if (! line) + return FALSE; + + if (strcmp (line, "# GIMP Curves File") != 0) { - g_set_error_literal (error, - GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, + g_set_error_literal (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, _("not a GIMP Curves file")); + g_object_unref (data_input); + g_free (line); return FALSE; } @@ -526,19 +531,41 @@ gimp_curves_config_load_cruft (GimpCurvesConfig *config, { for (j = 0; j < GIMP_CURVE_N_CRUFT_POINTS; j++) { - fields = fscanf (file, "%d %d ", &index[i][j], &value[i][j]); - if (fields != 2) + gchar *x_str = NULL; + gchar *y_str = NULL; + + if (! (x_str = g_data_input_stream_read_upto (data_input, " ", -1, + NULL, NULL, error)) || + ! g_data_input_stream_read_byte (data_input, NULL, error) || + ! (y_str = g_data_input_stream_read_upto (data_input, " ", -1, + NULL, NULL, error)) || + ! g_data_input_stream_read_byte (data_input, NULL, error)) { - /* FIXME: should have a helpful error message here */ - g_printerr ("fields != 2"); - g_set_error_literal (error, - GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, - _("parse error")); + g_free (x_str); + g_free (y_str); + g_object_unref (data_input); return FALSE; } + + if (sscanf (x_str, "%d", &index[i][j]) != 1 || + sscanf (y_str, "%d", &value[i][j]) != 1) + { + g_set_error_literal (error, + GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, + _("Parse error, didn't find 2 integers")); + g_free (x_str); + g_free (y_str); + g_object_unref (data_input); + return FALSE; + } + + g_free (x_str); + g_free (y_str); } } + g_object_unref (data_input); + g_object_freeze_notify (G_OBJECT (config)); for (i = 0; i < 5; i++) diff --git a/app/operations/gimpcurvesconfig.h b/app/operations/gimpcurvesconfig.h index 0f1064bc77..990a8e930d 100644 --- a/app/operations/gimpcurvesconfig.h +++ b/app/operations/gimpcurvesconfig.h @@ -69,7 +69,7 @@ GObject * gimp_curves_config_new_explicit_cruft (gint32 channel, void gimp_curves_config_reset_channel (GimpCurvesConfig *config); gboolean gimp_curves_config_load_cruft (GimpCurvesConfig *config, - gpointer fp, + GInputStream *input, GError **error); gboolean gimp_curves_config_save_cruft (GimpCurvesConfig *config, GFile *file, diff --git a/app/operations/gimplevelsconfig.c b/app/operations/gimplevelsconfig.c index 0ad84a4d11..ead15ce560 100644 --- a/app/operations/gimplevelsconfig.c +++ b/app/operations/gimplevelsconfig.c @@ -21,12 +21,10 @@ #include "config.h" #include -#include #include #include #include -#include #include "libgimpbase/gimpbase.h" #include "libgimpcolor/gimpcolor.h" @@ -740,52 +738,77 @@ gimp_levels_config_to_curves_config (GimpLevelsConfig *config) gboolean gimp_levels_config_load_cruft (GimpLevelsConfig *config, - gpointer fp, + GInputStream *input, GError **error) { - FILE *file = fp; - gint low_input[5]; - gint high_input[5]; - gint low_output[5]; - gint high_output[5]; - gdouble gamma[5]; - gint i; - gint fields; - gchar buf[50]; - gchar *nptr; + GDataInputStream *data_input; + gint low_input[5]; + gint high_input[5]; + gint low_output[5]; + gint high_output[5]; + gdouble gamma[5]; + gchar *line; + gsize line_len; + gint i; g_return_val_if_fail (GIMP_IS_LEVELS_CONFIG (config), FALSE); - g_return_val_if_fail (file != NULL, FALSE); + g_return_val_if_fail (G_IS_INPUT_STREAM (input), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if (! fgets (buf, sizeof (buf), file) || - strcmp (buf, "# GIMP Levels File\n") != 0) + data_input = g_data_input_stream_new (input); + + line_len = 64; + line = g_data_input_stream_read_line (data_input, &line_len, + NULL, error); + if (! line) + return FALSE; + + if (strcmp (line, "# GIMP Levels File") != 0) { g_set_error_literal (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, _("not a GIMP Levels file")); + g_object_unref (data_input); + g_free (line); return FALSE; } + g_free (line); + for (i = 0; i < 5; i++) { - fields = fscanf (file, "%d %d %d %d ", + gchar float_buf[32]; + gchar *endp; + gint fields; + + line_len = 64; + line = g_data_input_stream_read_line (data_input, &line_len, + NULL, error); + if (! line) + { + g_object_unref (data_input); + return FALSE; + } + + fields = sscanf (line, "%d %d %d %d %31s", &low_input[i], &high_input[i], &low_output[i], - &high_output[i]); + &high_output[i], + float_buf); - if (fields != 4) + g_free (line); + + if (fields != 5) goto error; - if (! fgets (buf, 50, file)) - goto error; + gamma[i] = g_ascii_strtod (float_buf, &endp); - gamma[i] = g_ascii_strtod (buf, &nptr); - - if (buf == nptr || errno == ERANGE) + if (endp == float_buf || errno == ERANGE) goto error; } + g_object_unref (data_input); + g_object_freeze_notify (G_OBJECT (config)); for (i = 0; i < 5; i++) @@ -808,6 +831,8 @@ gimp_levels_config_load_cruft (GimpLevelsConfig *config, return TRUE; error: + g_object_unref (data_input); + g_set_error_literal (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, _("parse error")); return FALSE; diff --git a/app/operations/gimplevelsconfig.h b/app/operations/gimplevelsconfig.h index 84b4b1b910..71ac0370cb 100644 --- a/app/operations/gimplevelsconfig.h +++ b/app/operations/gimplevelsconfig.h @@ -76,7 +76,7 @@ GimpCurvesConfig * gimp_levels_config_to_curves_config (GimpLevelsConfig *config); gboolean gimp_levels_config_load_cruft (GimpLevelsConfig *config, - gpointer fp, + GInputStream *input, GError **error); gboolean gimp_levels_config_save_cruft (GimpLevelsConfig *config, GFile *file, diff --git a/app/tools/gimpcurvestool.c b/app/tools/gimpcurvestool.c index ce0a9dc439..07392fff9a 100644 --- a/app/tools/gimpcurvestool.c +++ b/app/tools/gimpcurvestool.c @@ -598,30 +598,27 @@ gimp_curves_tool_settings_import (GimpImageMapTool *image_map_tool, GError **error) { GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool); - gchar *path; - FILE *f; + GInputStream *input; gchar header[64]; + gsize bytes_read; - path = g_file_get_path (file); - f = g_fopen (path, "rt"); - g_free (path); - - if (! f) + input = G_INPUT_STREAM (g_file_read (file, NULL, error)); + if (! input) { - g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), - _("Could not open '%s' for reading: %s"), - gimp_file_get_utf8_name (file), - g_strerror (errno)); + g_prefix_error (error, + _("Could not open '%s' for reading: "), + gimp_file_get_utf8_name (file)); return FALSE; } - if (! fgets (header, sizeof (header), f)) + if (! g_input_stream_read_all (input, header, sizeof (header), + &bytes_read, NULL, error) || + bytes_read != sizeof (header)) { - g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), - _("Could not read header from '%s': %s"), - gimp_file_get_utf8_name (file), - g_strerror (errno)); - fclose (f); + g_prefix_error (error, + _("Could not read header from '%s': "), + gimp_file_get_utf8_name (file)); + g_object_unref (input); return FALSE; } @@ -629,16 +626,16 @@ gimp_curves_tool_settings_import (GimpImageMapTool *image_map_tool, { gboolean success; - rewind (f); + g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL); - success = gimp_curves_config_load_cruft (tool->config, f, error); + success = gimp_curves_config_load_cruft (tool->config, input, error); - fclose (f); + g_object_unref (input); return success; } - fclose (f); + g_object_unref (input); return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool, file, diff --git a/app/tools/gimplevelstool.c b/app/tools/gimplevelstool.c index 1181b82a37..b4ff5c1844 100644 --- a/app/tools/gimplevelstool.c +++ b/app/tools/gimplevelstool.c @@ -17,9 +17,6 @@ #include "config.h" -#include - -#include #include #include @@ -618,30 +615,27 @@ gimp_levels_tool_settings_import (GimpImageMapTool *image_map_tool, GError **error) { GimpLevelsTool *tool = GIMP_LEVELS_TOOL (image_map_tool); - gchar *path; - FILE *f; + GInputStream *input; gchar header[64]; + gsize bytes_read; - path = g_file_get_path (file); - f = g_fopen (path, "rt"); - g_free (path); - - if (! f) + input = G_INPUT_STREAM (g_file_read (file, NULL, error)); + if (! input) { - g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), - _("Could not open '%s' for reading: %s"), - gimp_file_get_utf8_name (file), - g_strerror (errno)); + g_prefix_error (error, + _("Could not open '%s' for reading: "), + gimp_file_get_utf8_name (file)); return FALSE; } - if (! fgets (header, sizeof (header), f)) + if (! g_input_stream_read_all (input, header, sizeof (header), + &bytes_read, NULL, error) || + bytes_read != sizeof (header)) { - g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), - _("Could not read header from '%s': %s"), - gimp_file_get_utf8_name (file), - g_strerror (errno)); - fclose (f); + g_prefix_error (error, + _("Could not read header from '%s': "), + gimp_file_get_utf8_name (file)); + g_object_unref (input); return FALSE; } @@ -649,16 +643,16 @@ gimp_levels_tool_settings_import (GimpImageMapTool *image_map_tool, { gboolean success; - rewind (f); + g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL); - success = gimp_levels_config_load_cruft (tool->config, f, error); + success = gimp_levels_config_load_cruft (tool->config, input, error); - fclose (f); + g_object_unref (input); return success; } - fclose (f); + g_object_unref (input); return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool, file,