From 7eb36f9021e86a75fd291b6aae424117a230134d Mon Sep 17 00:00:00 2001 From: crunchy Date: Thu, 19 Sep 2002 20:46:02 +0000 Subject: added error handing functions --- libmpio/defs.h | 7 ++++- libmpio/mpio.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++---------- libmpio/mpio.h | 15 ++++++++++- 3 files changed, 91 insertions(+), 16 deletions(-) (limited to 'libmpio') diff --git a/libmpio/defs.h b/libmpio/defs.h index 7e06524..70f789f 100644 --- a/libmpio/defs.h +++ b/libmpio/defs.h @@ -1,7 +1,7 @@ /* -*- linux-c -*- */ /* - * $Id: defs.h,v 1.6 2002/09/18 20:32:21 crunchy Exp $ + * $Id: defs.h,v 1.7 2002/09/19 20:46:02 crunchy Exp $ * * Library for USB MPIO-* * @@ -93,6 +93,11 @@ typedef enum { FTYPE_CONF = 'C', #define INFO_LINE 81 /* error codes */ +typedef struct { + int id; + char *msg; +} mpio_error_t; + #define MPIO_ERR_FILE_NOT_FOUND -1 #define MPIO_ERR_NOT_ENOUGH_SPACE -2 #define MPIO_ERR_FILE_EXISTS -3 diff --git a/libmpio/mpio.c b/libmpio/mpio.c index e9ab24a..ea0f23c 100644 --- a/libmpio/mpio.c +++ b/libmpio/mpio.c @@ -1,6 +1,6 @@ /* * - * $Id: mpio.c,v 1.20 2002/09/18 23:17:03 germeier Exp $ + * $Id: mpio.c,v 1.21 2002/09/19 20:46:02 crunchy Exp $ * * Library for USB MPIO-* * @@ -49,7 +49,31 @@ static BYTE *mpio_model_name[] = { "MPIO-DMB", "MPIO-DMB+", "MPIO-DMK", - "unknown" }; + "unknown" +}; + +static mpio_error_t mpio_errors[] = { + { MPIO_ERR_FILE_NOT_FOUND, + "The selected file can not be found." }, + { MPIO_ERR_NOT_ENOUGH_SPACE, + "There is not enough space left on the selected memory card." }, + { MPIO_ERR_FILE_EXISTS, + "The selected file already exists and can not be overwritten. Remove it first." }, + { MPIO_ERR_FAT_ERROR, + "Internal error while reading the FAT." }, + { MPIO_ERR_READING_FILE, + "The selected file can not be read." }, + { MPIO_ERR_PERMISSION_DENIED, + "There are not enough rights to access the file/directory." }, + { MPIO_ERR_WRITING_FILE, + "There are no permisson to write to the selected file." } +}; + +static const int mpio_error_num = sizeof mpio_errors / sizeof(mpio_error_t); + +static int _mpio_errno = 0; + +#define MPIO_ERR_RETURN(err) _mpio_errno = err; return -1 void mpio_init_internal(mpio_t *); void mpio_init_external(mpio_t *); @@ -318,8 +342,8 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, /* please fix me sometime */ /* the system entries are kind of special ! */ - if (strncmp("sysdum", filename, 6) == 0) - return MPIO_ERR_PERMISSION_DENIED; + if (strncmp("sysdum", filename, 6) == 0) + MPIO_ERR_RETURN(MPIO_ERR_PERMISSION_DENIED); if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; @@ -354,7 +378,7 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, debug("error writing file data\n"); close(fd); free (f); - return MPIO_ERR_WRITING_FILE; + MPIO_ERR_RETURN(MPIO_ERR_WRITING_FILE); } filesize -= towrite; @@ -404,7 +428,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (stat((const char *)filename, &file_stat)!=0) { debug("could not find file: %s\n", filename); - return MPIO_ERR_FILE_NOT_FOUND; + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); } fsize=filesize=file_stat.st_size; debugn(2, "filesize: %d\n", fsize); @@ -413,7 +437,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, mpio_memory_free(m, mem, &free); if (free*1024entry; @@ -459,7 +483,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (fd==-1) { debug("could not open file: %s\n", filename); - return MPIO_ERR_FILE_NOT_FOUND; + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); } while ((filesize>BLOCK_SIZE) && (!abort)) { @@ -473,7 +497,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (read(fd, block, toread)!=toread) { debug("error reading file data\n"); close(fd); - return MPIO_ERR_READING_FILE; + MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); } filesize -= toread; @@ -503,7 +527,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (read(fd, block, toread)!=toread) { debug("error reading file data\n"); close(fd); - return MPIO_ERR_READING_FILE; + MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); } filesize -= toread; @@ -650,7 +674,7 @@ mpio_file_del(mpio_t *m, mpio_mem_t mem, BYTE *filename, /* please fix me sometime */ /* the system entry are kind of special ! */ if (strncmp("sysdum", filename, 6)==0) - return MPIO_ERR_PERMISSION_DENIED; + MPIO_ERR_RETURN(MPIO_ERR_PERMISSION_DENIED); if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; @@ -736,8 +760,41 @@ mpio_memory_debug(mpio_t *m, mpio_mem_t mem) return 0; } +int +mpio_errno(void) +{ + int no = _mpio_errno; + _mpio_errno = 0; + + return no; +} - +char * +mpio_strerror(int errno) +{ + int i; + + printf("mpio_strerror %d\n", errno); + + if (errno >= 0) return NULL; + + for (i = 0; i < mpio_error_num; i++) { + if (mpio_errors[i].id == errno) + return mpio_errors[i].msg; + } + return NULL; +} +void +mpio_perror(char *prefix) +{ + char *msg = mpio_strerror(_mpio_errno); + if (msg == NULL) return; + + if (prefix) + fprintf(stderr, "%s: %s\n", prefix, msg); + else + fprintf(stderr, "%s\n", msg); +} diff --git a/libmpio/mpio.h b/libmpio/mpio.h index b4c381c..7df2349 100644 --- a/libmpio/mpio.h +++ b/libmpio/mpio.h @@ -1,7 +1,7 @@ /* -*- linux-c -*- */ /* - * $Id: mpio.h,v 1.5 2002/09/18 22:18:29 germeier Exp $ + * $Id: mpio.h,v 1.6 2002/09/19 20:46:02 crunchy Exp $ * * Library for USB MPIO-* * @@ -94,6 +94,19 @@ int mpio_sync(mpio_t *, mpio_mem_t); /* context, memory bank */ int mpio_memory_debug(mpio_t *, mpio_mem_t); +/* + * error handling + */ + +/* returns error code of last error */ +int mpio_errno(void); + +/* returns the description of the error */ +char * mpio_strerror(int errno); + +/* prints the error message of the last error*/ +void mpio_perror(char *prefix); + /* * timeline: * --------- -- cgit v1.2.3