Skip to content

Commit

Permalink
WRaster: Improve error messages to provide useful information to user
Browse files Browse the repository at this point in the history
The original error messages tended to be a bit sparse, now they try to be
a little bit more helpful, and translatable in user's language.

In xutil.c:122, took opportunity fix a problem because calling 'perror'
after other function which are likely to have changed the errno is likely
to provide a wrong error string.

Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
  • Loading branch information
Christophe CURIS authored and crmafra committed May 18, 2021
1 parent e5f7ef2 commit bcb5370
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 34 deletions.
3 changes: 2 additions & 1 deletion wrlib/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ static Bool setupPseudoColorColormap(RContext * context)
}

if (theMap < 0) {
puts("wrlib: no std cmap found");
fprintf(stderr, _("wrlib: no standard colormap found for visual 0x%lX\n"),
context->visual->visualid);
}

if (theMap >= 0 && allocateStandardPseudoColor(context, &maps[theMap])) {
Expand Down
42 changes: 21 additions & 21 deletions wrlib/load_ppm.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,48 +47,48 @@
* implied warranty.
*/

char pm_getc(FILE * const fileP)
char pm_getc(FILE *const fileP, const char *filename)
{
int ich;
char ch;

ich = getc(fileP);
if (ich == EOF)
fprintf(stderr, "EOF / read error reading a byte\n");
fprintf(stderr, _("wrlib: EOF / read error reading a byte from PPM file \"%s\"\n"), filename);
ch = (char)ich;

if (ch == '#') {
do {
ich = getc(fileP);
if (ich == EOF)
fprintf(stderr, "EOF / read error reading a byte\n");
fprintf(stderr, _("wrlib: EOF / read error reading a byte from PPM file \"%s\"\n"), filename);
ch = (char)ich;
} while (ch != '\n' && ch != '\r');
}
return ch;
}

unsigned char pm_getrawbyte(FILE * const file)
unsigned char pm_getrawbyte(FILE *const file, const char *filename)
{
int iby;

iby = getc(file);
if (iby == EOF)
fprintf(stderr, "EOF / read error reading a one-byte sample\n");
fprintf(stderr, _("wrlib: EOF / read error reading a byte from PPM file \"%s\"\n"), filename);
return (unsigned char)iby;
}

int pm_getuint(FILE * const ifP)
int pm_getuint(FILE *const ifP, const char *filename)
{
char ch;
unsigned int i;

do {
ch = pm_getc(ifP);
ch = pm_getc(ifP, filename);
} while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');

if (ch < '0' || ch > '9') {
fprintf(stderr, "junk in file where an unsigned integer should be\n");
fprintf(stderr, _("wrlib: junk in PPM file \"%s\", expected an unsigned integer but got 0x%02X\n"), filename, (int)ch);
return -1;
}

Expand All @@ -97,20 +97,20 @@ int pm_getuint(FILE * const ifP)
unsigned int const digitVal = ch - '0';

if (i > INT_MAX / 10) {
fprintf(stderr, "ASCII decimal integer in file is too large to be processed\n");
fprintf(stderr, _("wrlib: ASCII decimal integer in PPM file \"%s\" is too large to be processed\n"), filename);
return -1;
}

i *= 10;

if (i > INT_MAX - digitVal) {
fprintf(stderr, "ASCII decimal integer in file is too large to be processed\n");
fprintf(stderr, _("wrlib: ASCII decimal integer in PPM file \"%s\" is too large to be processed\n"), filename);
return -1;
}

i += digitVal;

ch = pm_getc(ifP);
ch = pm_getc(ifP, filename);
} while (ch >= '0' && ch <= '9');

return i;
Expand All @@ -119,7 +119,7 @@ int pm_getuint(FILE * const ifP)
/******************************************************************************************/

/* PGM: support for portable graymap ascii and binary encoding */
static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
static RImage *load_graymap(FILE *file, int w, int h, int max, int raw, const char *filename)
{
RImage *image;
unsigned char *ptr;
Expand All @@ -143,7 +143,7 @@ static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)

for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
val = pm_getuint(file);
val = pm_getuint(file, filename);

if (val > max || val < 0) {
RErrorCode = RERR_BADIMAGEFILE;
Expand Down Expand Up @@ -189,7 +189,7 @@ static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
}

/* PPM: support for portable pixmap ascii and binary encoding */
static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
static RImage *load_pixmap(FILE *file, int w, int h, int max, int raw, const char *filename)
{
RImage *image;
int i;
Expand All @@ -214,7 +214,7 @@ static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
for (i = 0; i < 3; i++) {
val = pm_getuint(file);
val = pm_getuint(file, filename);

if (val > max || val < 0) {
RErrorCode = RERR_BADIMAGEFILE;
Expand Down Expand Up @@ -249,7 +249,7 @@ static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
}

/* PBM: support for portable bitmap ascii and binary encoding */
static RImage *load_bitmap(FILE * file, int w, int h, int max, int raw)
static RImage *load_bitmap(FILE *file, int w, int h, int max, int raw, const char *filename)
{
RImage *image;
int val;
Expand All @@ -271,7 +271,7 @@ static RImage *load_bitmap(FILE * file, int w, int h, int max, int raw)
int i = 0;

while (i < w * h) {
val = pm_getuint(file);
val = pm_getuint(file, filename);

if (val > max || val < 0) {
RErrorCode = RERR_BADIMAGEFILE;
Expand All @@ -295,7 +295,7 @@ static RImage *load_bitmap(FILE * file, int w, int h, int max, int raw)
bitshift = -1;
for (x = 0; x < w; x++) {
if (bitshift == -1) {
buf = pm_getrawbyte(file);
buf = pm_getrawbyte(file, filename);
bitshift = 7;
}
val = (buf >> bitshift) & 1;
Expand Down Expand Up @@ -380,13 +380,13 @@ RImage *RLoadPPM(const char *file_name)

if (type == '1' || type == '4') {
/* Portable Bit Map: P1 is for 'plain' (ascii, rare), P4 for 'regular' (binary) */
image = load_bitmap(file, w, h, m, type);
image = load_bitmap(file, w, h, m, type, file_name);
} else if (type == '2' || type == '5') {
/* Portable Gray Map: P2 is for 'plain' (ascii, rare), P5 for 'regular' (binary) */
image = load_graymap(file, w, h, m, type);
image = load_graymap(file, w, h, m, type, file_name);
} else if (type == '3' || type == '6') {
/* Portable Pix Map: P3 is for 'plain' (ascii, rare), P6 for 'regular' (binary) */
image = load_pixmap(file, w, h, m, type);
image = load_pixmap(file, w, h, m, type, file_name);
}

fclose(file);
Expand Down
39 changes: 36 additions & 3 deletions wrlib/load_webp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <webp/decode.h>

Expand All @@ -33,6 +34,34 @@
#include "wr_i18n.h"


/*
* webp_message_from_status
*
* return the text message from a VP8 status code
*/
static const char *webp_message_from_status(VP8StatusCode status)
{
static const char *const known_message[] = {
/* Known codes as per libWebP 0.4.1 */
[VP8_STATUS_OUT_OF_MEMORY] = N_("out of memory"),
[VP8_STATUS_INVALID_PARAM] = N_("invalid parameter"),
[VP8_STATUS_BITSTREAM_ERROR] = N_("error in the bitstream"),
[VP8_STATUS_UNSUPPORTED_FEATURE] = N_("feature is not supported"),
[VP8_STATUS_SUSPENDED] = N_("operation suspended"),
[VP8_STATUS_USER_ABORT] = N_("aborted by user"),
[VP8_STATUS_NOT_ENOUGH_DATA] = N_("not enough data")
};
static char custom_message[128];

if (status >= 0 && status < sizeof(known_message) / sizeof(known_message[0]))
if (known_message[status] != NULL)
return known_message[status];

snprintf(custom_message, sizeof(custom_message),
_("unknow status code %d"), status);
return custom_message;
}

RImage *RLoadWEBP(const char *file_name)
{
FILE *file;
Expand All @@ -41,6 +70,7 @@ RImage *RLoadWEBP(const char *file_name)
int raw_data_size;
int r;
uint8_t *raw_data;
VP8StatusCode status;
WebPBitstreamFeatures features;
uint8_t *ret = NULL;

Expand Down Expand Up @@ -73,7 +103,8 @@ RImage *RLoadWEBP(const char *file_name)
raw_data_size = ftell(file);

if (raw_data_size <= 0) {
fprintf(stderr, "wrlib: Failed to find the WEBP file size for \"%s\"\n", file_name);
fprintf(stderr, _("wrlib: could not get size of WebP file \"%s\", %s\n"),
file_name, strerror(errno));
RErrorCode = RERR_BADIMAGEFILE;
fclose(file);
return NULL;
Expand All @@ -97,8 +128,10 @@ RImage *RLoadWEBP(const char *file_name)
return NULL;
}

if (WebPGetFeatures(raw_data, raw_data_size, &features) != VP8_STATUS_OK) {
fprintf(stderr, "wrlib: WebPGetFeatures has failed on \"%s\"\n", file_name);
status = WebPGetFeatures(raw_data, raw_data_size, &features);
if (status != VP8_STATUS_OK) {
fprintf(stderr, _("wrlib: could not get features from WebP file \"%s\", %s\n"),
file_name, webp_message_from_status(status));
RErrorCode = RERR_BADIMAGEFILE;
free(raw_data);
return NULL;
Expand Down
18 changes: 9 additions & 9 deletions wrlib/xutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <assert.h>

Expand Down Expand Up @@ -111,17 +112,17 @@ RXImage *RCreateXImage(RContext * context, int depth, unsigned width, unsigned h
rximg->info.shmid = shmget(IPC_PRIVATE, rximg->image->bytes_per_line * height, IPC_CREAT | 0777);
if (rximg->info.shmid < 0) {
context->attribs->use_shared_memory = 0;
perror("wrlib: could not allocate shared memory segment");
fprintf(stderr, _("wrlib: could not allocate shared memory segment, %s: %s\n"), "shmget", strerror(errno));
XDestroyImage(rximg->image);
goto retry_without_shm;
}

rximg->info.shmaddr = shmat(rximg->info.shmid, 0, 0);
if (rximg->info.shmaddr == (void *)-1) {
fprintf(stderr, _("wrlib: could not allocate shared memory segment, %s: %s\n"), "shmat", strerror(errno));
context->attribs->use_shared_memory = 0;
if (shmctl(rximg->info.shmid, IPC_RMID, 0) < 0)
perror("wrlib: shmctl");
perror("wrlib: could not allocate shared memory");
fprintf(stderr, _("wrlib: error occured while aborting %s, %s\n"), "shmctl", strerror(errno));
XDestroyImage(rximg->image);
goto retry_without_shm;
}
Expand All @@ -137,14 +138,13 @@ RXImage *RCreateXImage(RContext * context, int depth, unsigned width, unsigned h
/* rximg->image->obdata = &(rximg->info); */

if (shmError) {
fprintf(stderr, _("wrlib: could not attach shared memory segment to XImage\n"));
context->attribs->use_shared_memory = 0;
XDestroyImage(rximg->image);
if (shmdt(rximg->info.shmaddr) < 0)
perror("wrlib: shmdt");
fprintf(stderr, _("wrlib: error occured while aborting %s, %s\n"), "shmdt", strerror(errno));
if (shmctl(rximg->info.shmid, IPC_RMID, 0) < 0)
perror("wrlib: shmctl");
/* printf("wrlib:error attaching shared memory segment to XImage\n");
*/
fprintf(stderr, _("wrlib: error occured while aborting %s, %s\n"), "shmctl", strerror(errno));
goto retry_without_shm;
}
}
Expand All @@ -166,9 +166,9 @@ void RDestroyXImage(RContext * context, RXImage * rximage)
XShmDetach(context->dpy, &rximage->info);
XDestroyImage(rximage->image);
if (shmdt(rximage->info.shmaddr) < 0)
perror("wrlib: shmdt");
fprintf(stderr, _("wrlib: error occured while releasing XImage, %s: %s\n"), "shmdt", strerror(errno));
if (shmctl(rximage->info.shmid, IPC_RMID, 0) < 0)
perror("wrlib: shmctl");
fprintf(stderr, _("wrlib: error occured while releasing XImage, %s: %s\n"), "shmctl", strerror(errno));
} else {
XDestroyImage(rximage->image);
}
Expand Down

0 comments on commit bcb5370

Please sign in to comment.