app/core/gimpbrushgenerated-load.c app/core/gimpgradient-load.c

2006-10-04  Michael Natterer  <mitch@gimp.org>

	* app/core/gimpbrushgenerated-load.c
	* app/core/gimpgradient-load.c
	* app/core/gimppalette-load.c
	* app/core/gimppalette-save.c: read and write all files in binary
	mode. Improved parser error messages (added line numbers). Minor
	changes to simply read DOS files instead of barfing on them.
This commit is contained in:
Michael Natterer 2006-10-04 17:50:35 +00:00 committed by Michael Natterer
parent 840053b86d
commit 544d679e55
5 changed files with 133 additions and 64 deletions

View file

@ -1,3 +1,12 @@
2006-10-04 Michael Natterer <mitch@gimp.org>
* app/core/gimpbrushgenerated-load.c
* app/core/gimpgradient-load.c
* app/core/gimppalette-load.c
* app/core/gimppalette-save.c: read and write all files in binary
mode. Improved parser error messages (added line numbers). Minor
changes to simply read DOS files instead of barfing on them.
2006-10-04 Sven Neumann <sven@gimp.org>
* app/tools/gimpforegroundselecttool.c

View file

@ -50,6 +50,7 @@ gimp_brush_generated_load (const gchar *filename,
GimpBrush *brush;
FILE *file;
gchar string[256];
gint linenum;
gchar *name = NULL;
GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE;
gboolean have_shape = FALSE;
@ -76,10 +77,11 @@ gimp_brush_generated_load (const gchar *filename,
/* make sure the file we are reading is the right type */
errno = 0;
linenum = 1;
if (! fgets (string, sizeof (string), file))
goto failed;
if (strncmp (string, "GIMP-VBR", 8) != 0)
if (strncmp (string, "GIMP-VBR", strlen ("GIMP-VBR")))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
@ -90,6 +92,7 @@ gimp_brush_generated_load (const gchar *filename,
/* make sure we are reading a compatible version */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
@ -99,8 +102,8 @@ gimp_brush_generated_load (const gchar *filename,
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unknown GIMP brush version."),
gimp_filename_to_utf8 (filename));
"Unknown GIMP brush version in line %d."),
gimp_filename_to_utf8 (filename), linenum);
goto failed;
}
else
@ -111,6 +114,7 @@ gimp_brush_generated_load (const gchar *filename,
/* read name */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
@ -133,18 +137,19 @@ gimp_brush_generated_load (const gchar *filename,
/* read shape */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
g_strstrip (string);
shape_val = g_enum_get_value_by_nick (enum_class, string);
if (!shape_val)
if (! shape_val)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unknown GIMP brush shape."),
gimp_filename_to_utf8 (filename));
"Unknown GIMP brush shape in line %d."),
gimp_filename_to_utf8 (filename), linenum);
goto failed;
}
@ -153,20 +158,23 @@ gimp_brush_generated_load (const gchar *filename,
/* read brush spacing */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
spacing = g_ascii_strtod (string, NULL);
/* read brush radius */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
radius = g_ascii_strtod (string, NULL);
if (have_shape)
{
/* read brush radius */
/* read number of spikes */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
spikes = CLAMP (atoi (string), 2, 20);
@ -174,18 +182,21 @@ gimp_brush_generated_load (const gchar *filename,
/* read brush hardness */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
hardness = g_ascii_strtod (string, NULL);
/* read brush aspect_ratio */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
aspect_ratio = g_ascii_strtod (string, NULL);
/* read brush angle */
errno = 0;
linenum++;
if (! fgets (string, sizeof (string), file))
goto failed;
angle = g_ascii_strtod (string, NULL);
@ -208,10 +219,20 @@ gimp_brush_generated_load (const gchar *filename,
g_free (name);
if (error && *error == NULL)
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Error while reading brush file '%s': %s"),
gimp_filename_to_utf8 (filename),
errno ? g_strerror (errno) : _("File is truncated"));
{
gchar *msg;
if (errno)
msg = g_strdup_printf (_("Line %d: %s"), linenum, g_strerror (errno));
else
msg = g_strdup_printf (_("File is truncated in line %d"), linenum);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Error while reading brush file '%s': %s"),
gimp_filename_to_utf8 (filename), msg);
g_free (msg);
}
return NULL;
}

View file

@ -48,12 +48,14 @@ gimp_gradient_load (const gchar *filename,
gint i;
FILE *file;
gchar line[1024];
gint linenum;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
file = g_fopen (filename, "rb");
if (!file)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
@ -62,8 +64,18 @@ gimp_gradient_load (const gchar *filename,
return NULL;
}
fgets (line, sizeof (line), file);
if (strcmp (line, "GIMP Gradient\n") != 0)
linenum = 1;
if (! fgets (line, sizeof (line), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in gradient file '%s': "
"Read error in line %d."),
gimp_filename_to_utf8 (filename), linenum);
fclose (file);
return NULL;
}
if (strncmp (line, "GIMP Gradient", strlen ("GIMP Gradient")))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in gradient file '%s': "
@ -77,17 +89,38 @@ gimp_gradient_load (const gchar *filename,
"mime-type", "application/x-gimp-gradient",
NULL);
fgets (line, sizeof (line), file);
linenum++;
if (! fgets (line, sizeof (line), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in gradient file '%s': "
"Read error in line %d."),
gimp_filename_to_utf8 (filename), linenum);
fclose (file);
g_object_unref (gradient);
return NULL;
}
if (! strncmp (line, "Name: ", strlen ("Name: ")))
{
gchar *utf8;
utf8 = gimp_any_to_utf8 (&line[strlen ("Name: ")], -1,
utf8 = gimp_any_to_utf8 (g_strstrip (line + strlen ("Name: ")), -1,
_("Invalid UTF-8 string in gradient file '%s'."),
gimp_filename_to_utf8 (filename));
gimp_object_take_name (GIMP_OBJECT (gradient), g_strstrip (utf8));
gimp_object_take_name (GIMP_OBJECT (gradient), utf8);
fgets (line, sizeof (line), file);
linenum++;
if (! fgets (line, sizeof (line), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in gradient file '%s': "
"Read error in line %d."),
gimp_filename_to_utf8 (filename), linenum);
fclose (file);
g_object_unref (gradient);
return NULL;
}
}
else /* old gradient format */
{
@ -101,8 +134,8 @@ gimp_gradient_load (const gchar *filename,
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in gradient file '%s': "
"File is corrupt."),
gimp_filename_to_utf8 (filename));
"File is corrupt in line %d."),
gimp_filename_to_utf8 (filename), linenum);
g_object_unref (gradient);
fclose (file);
return NULL;
@ -128,7 +161,17 @@ gimp_gradient_load (const gchar *filename,
else
gradient->segments = seg;
fgets (line, sizeof (line), file);
linenum++;
if (! fgets (line, sizeof (line), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in gradient file '%s': "
"Read error in line %d."),
gimp_filename_to_utf8 (filename), linenum);
fclose (file);
g_object_unref (gradient);
return NULL;
}
seg->left = g_ascii_strtod (line, &end);
if (end && errno != ERANGE)
@ -172,8 +215,9 @@ gimp_gradient_load (const gchar *filename,
default:
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Corrupt segment %d in gradient file '%s'."),
i, gimp_filename_to_utf8 (filename));
_("Fatal parse error in gradient file '%s': "
"Corrupt segment %d in line %d."),
gimp_filename_to_utf8 (filename), i, linenum);
g_object_unref (gradient);
fclose (file);
return NULL;
@ -182,8 +226,9 @@ gimp_gradient_load (const gchar *filename,
else
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Corrupt segment %d in gradient file '%s'."),
i, gimp_filename_to_utf8 (filename));
_("Fatal parse error in gradient file '%s': "
"Corrupt segment %d in line %d."),
gimp_filename_to_utf8 (filename), i, linenum);
g_object_unref (gradient);
fclose (file);
return NULL;

View file

@ -18,16 +18,19 @@
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <fcntl.h>
#ifndef _O_BINARY
#define _O_BINARY 0
#endif
#include <glib-object.h>
#include <glib/gstdio.h>
@ -65,7 +68,7 @@ gimp_palette_load (const gchar *filename,
r = g = b = 0;
file = g_fopen (filename, "r");
file = g_fopen (filename, "rb");
if (! file)
{
@ -75,28 +78,24 @@ gimp_palette_load (const gchar *filename,
return NULL;
}
linenum = 0;
fread (str, 13, 1, file);
str[13] = '\0';
linenum++;
if (strcmp (str, "GIMP Palette\n"))
linenum = 1;
if (! fgets (str, sizeof (str), file))
{
/* bad magic, but maybe it has \r\n at the end of lines? */
if (!strcmp (str, "GIMP Palette\r"))
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in palette file '%s': "
"Missing magic header.\n"
"Does this file need converting from DOS?"),
gimp_filename_to_utf8 (filename));
else
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in palette file '%s': "
"Missing magic header."),
gimp_filename_to_utf8 (filename));
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in palette file '%s': "
"Read error in line %d."),
gimp_filename_to_utf8 (filename), linenum);
fclose (file);
return NULL;
}
if (strncmp (str, "GIMP Palette", strlen ("GIMP Palette")))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in palette file '%s': "
"Missing magic header."),
gimp_filename_to_utf8 (filename));
fclose (file);
return NULL;
}
@ -104,6 +103,7 @@ gimp_palette_load (const gchar *filename,
"mime-type", "application/x-gimp-palette",
NULL);
linenum++;
if (! fgets (str, sizeof (str), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
@ -115,18 +115,16 @@ gimp_palette_load (const gchar *filename,
return NULL;
}
linenum++;
if (! strncmp (str, "Name: ", strlen ("Name: ")))
{
gchar *utf8;
utf8 = gimp_any_to_utf8 (&str[strlen ("Name: ")], -1,
utf8 = gimp_any_to_utf8 (g_strstrip (str + strlen ("Name: ")), -1,
_("Invalid UTF-8 string in palette file '%s'"),
gimp_filename_to_utf8 (filename));
gimp_object_take_name (GIMP_OBJECT (palette), utf8);
gimp_object_take_name (GIMP_OBJECT (palette), g_strstrip (utf8));
linenum++;
if (! fgets (str, sizeof (str), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
@ -138,13 +136,11 @@ gimp_palette_load (const gchar *filename,
return NULL;
}
linenum++;
if (! strncmp (str, "Columns: ", strlen ("Columns: ")))
{
gint columns;
columns = atoi (g_strstrip (&str[strlen ("Columns: ")]));
columns = atoi (g_strstrip (str + strlen ("Columns: ")));
if (columns < 0 || columns > 256)
{
@ -157,6 +153,7 @@ gimp_palette_load (const gchar *filename,
palette->n_columns = columns;
linenum++;
if (! fgets (str, sizeof (str), file))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
@ -167,8 +164,6 @@ gimp_palette_load (const gchar *filename,
g_object_unref (palette);
return NULL;
}
linenum++;
}
}
else /* old palette format */
@ -232,6 +227,7 @@ gimp_palette_load (const gchar *filename,
palette->n_colors++;
}
linenum++;
if (! fgets (str, sizeof (str), file))
{
if (feof (file))
@ -245,8 +241,6 @@ gimp_palette_load (const gchar *filename,
g_object_unref (palette);
return NULL;
}
linenum++;
}
fclose (file);
@ -269,7 +263,7 @@ gimp_palette_load_act (const gchar *filename,
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
fd = g_open (filename, O_RDONLY, 0);
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
if (! fd)
{
g_set_error (error,
@ -313,7 +307,7 @@ gimp_palette_load_riff (const gchar *filename,
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
fd = g_open (filename, O_RDONLY, 0);
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
if (! fd)
{
g_set_error (error,
@ -367,7 +361,7 @@ gimp_palette_load_psp (const gchar *filename,
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
fd = g_open (filename, O_RDONLY, 0);
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
if (! fd)
{
g_set_error (error,
@ -444,7 +438,7 @@ gimp_palette_load_detect_format (const gchar *filename)
gchar header[16];
struct stat file_stat;
fd = g_open (filename, O_RDONLY, 0);
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
if (fd)
{
if (read (fd, header, sizeof (header)) == sizeof (header))

View file

@ -48,7 +48,7 @@ gimp_palette_save (GimpData *data,
GList *list;
FILE *file;
file = g_fopen (data->filename, "w");
file = g_fopen (data->filename, "wb");
if (! file)
{