Fix so that one of three different error status values will be returned:

CAIRO_STATUS_NO_MEMORY CAIRO_STATUS_FILE_NOT_FOUND CAIRO_STATUS_READ_ERROR
Add new CAIRO_STATUS_FILE_NOT_FOUND.
Add new _cairo_surface_nil_read_error and _cairo_surface_nil_file_not_found.
Test the new FILE_NOT_FOUND error.
This commit is contained in:
Carl Worth 2005-07-28 10:41:08 +00:00
parent fe44b2406d
commit a83124a3ee
7 changed files with 93 additions and 15 deletions

View file

@ -1,3 +1,24 @@
2005-07-28 Carl Worth <cworth@cworth.org>
* src/cairo-png.c: (read_png),
(cairo_image_surface_create_from_png): Fix so that one of three
different error status values will be returned:
CAIRO_STATUS_NO_MEMORY
CAIRO_STATUS_FILE_NOT_FOUND
CAIRO_STATUS_READ_ERROR
* src/cairo.h:
* src/cairo.c: (cairo_status_to_string): Add new
CAIRO_STATUS_FILE_NOT_FOUND.
* src/cairoint.h:
* src/cairo-surface.c: Add new _cairo_surface_nil_read_error and
_cairo_surface_nil_file_not_found.
* test/create-from-png.c: (draw): Test the new FILE_NOT_FOUND
error.
2005-07-28 Stuart Parmenter <pavlov@pavlov.net>
* src/cairo-win32-font.c

View file

@ -340,8 +340,10 @@ read_png (png_rw_ptr read_func,
png_set_read_fn (png, closure, read_func);
if (setjmp (png_jmpbuf (png)))
if (setjmp (png_jmpbuf (png))) {
surface = &_cairo_surface_nil_read_error;
goto BAIL;
}
png_read_info (png, info);
@ -413,8 +415,8 @@ read_png (png_rw_ptr read_func,
if (png)
png_destroy_read_struct (&png, &info, NULL);
if (surface == &_cairo_surface_nil)
_cairo_error (CAIRO_STATUS_NO_MEMORY);
if (surface->status)
_cairo_error (surface->status);
return surface;
}
@ -437,16 +439,13 @@ stdio_read_func (png_structp png, png_bytep data, png_size_t size)
* given PNG file.
*
* Return value: a new #cairo_surface_t initialized with the contents
* of the PNG file, or a "nil" surface if any error occured. A nil
* of the PNG file, or a "nil" surface if any error occurred. A nil
* surface can be checked for with cairo_surface_status(surface) which
* may return one of the following values:
*
* CAIRO_STATUS_NO_MEMORY
*
* XXX: We may want to add a few more error values here, (file not
* found? permission denied? file not png?). One way to do this would
* be to create several variations on cairo_surface_nil to house the
* status values we care about.
* CAIRO_STATUS_FILE_NOT_FOUND
* CAIRO_STATUS_READ_ERROR
**/
cairo_surface_t *
cairo_image_surface_create_from_png (const char *filename)
@ -455,8 +454,19 @@ cairo_image_surface_create_from_png (const char *filename)
cairo_surface_t *surface;
fp = fopen (filename, "rb");
if (fp == NULL)
return (cairo_surface_t*) &_cairo_surface_nil;
if (fp == NULL) {
switch (errno) {
case ENOMEM:
_cairo_error (CAIRO_STATUS_NO_MEMORY);
return (cairo_surface_t*) &_cairo_surface_nil;
case ENOENT:
_cairo_error (CAIRO_STATUS_FILE_NOT_FOUND);
return (cairo_surface_t*) &_cairo_surface_nil_file_not_found;
default:
_cairo_error (CAIRO_STATUS_READ_ERROR);
return (cairo_surface_t*) &_cairo_surface_nil_read_error;
}
}
surface = read_png (stdio_read_func, fp);

View file

@ -56,6 +56,38 @@ const cairo_surface_t _cairo_surface_nil = {
0 /* current_clip_serial */
};
const cairo_surface_t _cairo_surface_nil_file_not_found = {
&cairo_image_surface_backend, /* backend */
-1, /* ref_count */
CAIRO_STATUS_FILE_NOT_FOUND, /* status */
FALSE, /* finished */
{ 0, /* size */
0, /* num_elements */
0, /* element_size */
NULL, /* elements */
}, /* user_data */
0.0, /* device_x_offset */
0.0, /* device_y_offset */
0, /* next_clip_serial */
0 /* current_clip_serial */
};
const cairo_surface_t _cairo_surface_nil_read_error = {
&cairo_image_surface_backend, /* backend */
-1, /* ref_count */
CAIRO_STATUS_READ_ERROR, /* status */
FALSE, /* finished */
{ 0, /* size */
0, /* num_elements */
0, /* element_size */
NULL, /* elements */
}, /* user_data */
0.0, /* device_x_offset */
0.0, /* device_y_offset */
0, /* next_clip_serial */
0 /* current_clip_serial */
};
/**
* _cairo_surface_set_error:
* @surface: a surface

View file

@ -62,7 +62,7 @@ static const cairo_t cairo_nil = {
* a bit of a pain, but it should be easy to always catch as long as
* one adds a new test case to test a trigger of the new status value.
*/
#define CAIRO_STATUS_LAST_STATUS CAIRO_STATUS_INVALID_VISUAL
#define CAIRO_STATUS_LAST_STATUS CAIRO_STATUS_FILE_NOT_FOUND
/**
* _cairo_error:
@ -2391,6 +2391,8 @@ cairo_status_to_string (cairo_status_t status)
return "invalid value for an input cairo_format_t";
case CAIRO_STATUS_INVALID_VISUAL:
return "invalid value for an input Visual*";
case CAIRO_STATUS_FILE_NOT_FOUND:
return "file not found";
}
return "<unknown error status>";

View file

@ -157,6 +157,7 @@ typedef struct _cairo_user_data_key {
* @CAIRO_STATUS_INVALID_CONTENT: invalid value for an input cairo_content_t
* @CAIRO_STATUS_INVALID_FORMAT: invalid value for an input cairo_format_t
* @CAIRO_STATUS_INVALID_VISUAL: invalid value for an input Visual*
* @CAIRO_STATUS_FILE_NOT_FOUND: file not found
*
* #cairo_status_t is used to indicate errors that can occur when
* using Cairo. In some cases it is returned directly by functions.
@ -181,7 +182,8 @@ typedef enum _cairo_status {
CAIRO_STATUS_PATTERN_TYPE_MISMATCH,
CAIRO_STATUS_INVALID_CONTENT,
CAIRO_STATUS_INVALID_FORMAT,
CAIRO_STATUS_INVALID_VISUAL
CAIRO_STATUS_INVALID_VISUAL,
CAIRO_STATUS_FILE_NOT_FOUND
} cairo_status_t;
/**

View file

@ -1484,6 +1484,8 @@ _cairo_path_fixed_stroke_to_traps (cairo_path_fixed_t *path,
/* cairo-surface.c */
extern const cairo_surface_t _cairo_surface_nil;
extern const cairo_surface_t _cairo_surface_nil_read_error;
extern const cairo_surface_t _cairo_surface_nil_file_not_found;
cairo_private cairo_surface_t *
_cairo_surface_create_similar_scratch (cairo_surface_t *other,

View file

@ -43,13 +43,22 @@ draw (cairo_t *cr, int width, int height)
char *filename;
cairo_surface_t *surface;
surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
if (cairo_surface_status (surface) != CAIRO_STATUS_FILE_NOT_FOUND) {
cairo_test_log ("Error: expected \"file not found\", but got: %s\n",
cairo_status_to_string (cairo_surface_status (surface)));
return CAIRO_TEST_FAILURE;
}
xasprintf (&filename, "%s/%s", srcdir ? srcdir : ".",
"create-from-png-ref.png");
surface = cairo_image_surface_create_from_png (filename);
if (surface == NULL) {
cairo_test_log ("Error: failed to open file %s\n", filename);
if (cairo_surface_status (surface)) {
cairo_test_log ("Error reading PNG image %s: %s\n",
filename,
cairo_status_to_string (cairo_surface_status (surface)));
free (filename);
return CAIRO_TEST_FAILURE;
}