From 3022049352e8a585380ed27fe1f8d6c8cd32028e Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 13 Nov 2003 16:45:25 +0000 Subject: [PATCH] * libee/ee.c: + Error checking in ee_init(). + Pre-generate the empty line for ee_clear(). * libee/sprite.c: + Better error checking in ee_sprite_load(). git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@168 92316355-f0b4-4df1-b90c-862c8a59935f --- libee/ee.c | 11 ++++++-- libee/ee_internals.h | 2 ++ libee/graphics.c | 11 +------- libee/sprite.c | 63 ++++++++++++++++++++++++++++++++------------ 4 files changed, 58 insertions(+), 29 deletions(-) diff --git a/libee/ee.c b/libee/ee.c index d59f042..4840d68 100644 --- a/libee/ee.c +++ b/libee/ee.c @@ -67,6 +67,7 @@ char *ee_color_names[16] = }; static int _ee_delay; +char *_ee_empty_line; #if defined(USE_NCURSES) int _ee_attr[16]; @@ -166,11 +167,13 @@ int ee_init(void) } #elif defined(USE_CONIO) + gettextinfo(&ti); + _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight); + if(_ee_screen == NULL) + return -1; _wscroll = 0; _setcursortype(_NOCURSOR); clrscr(); - gettextinfo(&ti); - _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight); # if defined(SCREENUPDATE_IN_PC_H) ScreenRetrieve(_ee_screen); # else @@ -178,6 +181,10 @@ int ee_init(void) # endif #endif + _ee_empty_line = malloc(ee_get_width() + 1); + memset(_ee_empty_line, ' ', ee_get_width()); + _ee_empty_line[ee_get_width()] = '\0'; + _ee_delay = 0; return 0; diff --git a/libee/ee_internals.h b/libee/ee_internals.h index 36f10f1..7bb8b7e 100644 --- a/libee/ee_internals.h +++ b/libee/ee_internals.h @@ -31,4 +31,6 @@ extern int _ee_attr[]; extern char *_ee_screen; #endif +extern char *_ee_empty_line; + #endif /* __EE_INTERNALS_H__ */ diff --git a/libee/graphics.c b/libee/graphics.c index 2b9a12f..b930bc5 100644 --- a/libee/graphics.c +++ b/libee/graphics.c @@ -118,18 +118,9 @@ void ee_putstr(int x, int y, char *s) void ee_clear(void) { /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */ - int x = ee_get_width(); int y = ee_get_height(); - char *empty_line = malloc((x + 1) * sizeof(char)); - - memset(empty_line, ' ', x); - empty_line[x] = '\0'; while(y--) - { - ee_putstr(0, y, empty_line); - } - - free(empty_line); + ee_putstr(0, y, _ee_empty_line); } diff --git a/libee/sprite.c b/libee/sprite.c index 756ed5d..8e2ad33 100644 --- a/libee/sprite.c +++ b/libee/sprite.c @@ -54,6 +54,9 @@ struct ee_sprite *ee_load_sprite(const char *file) return NULL; sprite = malloc(sizeof(struct ee_sprite)); + if(sprite == NULL) + goto sprite_alloc_failed; + sprite->nf = 0; sprite->frames = NULL; @@ -71,11 +74,23 @@ struct ee_sprite *ee_load_sprite(const char *file) if(w <= 0 || h <= 0 || w > BUFSIZ / 2) break; - if(sprite->nf++) - sprite->frames = realloc(sprite->frames, - sprite->nf * sizeof(struct ee_frame)); + if(sprite->nf) + { + void *tmp = realloc(sprite->frames, + (sprite->nf + 1) * sizeof(struct ee_frame)); + if(tmp == NULL) + goto frame_failed; + sprite->frames = tmp; + sprite->nf++; + } else - sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame)); + { + sprite->frames = malloc((sprite->nf + 1) * sizeof(struct ee_frame)); + if(sprite->frames == NULL) + goto sprite_failed; + sprite->nf++; + } + frame = &sprite->frames[sprite->nf - 1]; frame->w = w; @@ -83,12 +98,23 @@ struct ee_sprite *ee_load_sprite(const char *file) frame->dx = dx; frame->dy = dy; frame->chars = malloc(w * h * sizeof(char)); + if(frame->chars == NULL) + { + sprite->nf--; + goto frame_failed; + } frame->color = malloc(w * h * sizeof(int)); + if(frame->color == NULL) + { + free(frame->chars); + sprite->nf--; + goto frame_failed; + } for(y = 0; y < h; y++) { if(!fgets(buf, BUFSIZ, fd)) - goto failed; + goto frame_failed; for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++) frame->chars[w * y + x] = buf[x]; @@ -100,7 +126,7 @@ struct ee_sprite *ee_load_sprite(const char *file) for(y = 0; y < h; y++) { if(!fgets(buf, BUFSIZ, fd)) - goto failed; + goto frame_failed; for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++) frame->color[w * y + x] = buf[x] - 'a'; @@ -110,23 +136,26 @@ struct ee_sprite *ee_load_sprite(const char *file) } continue; - - failed: - free(sprite->frames[sprite->nf - 1].chars); - free(sprite->frames[sprite->nf - 1].color); - sprite->nf--; - break; } + if(sprite->nf == 0) + goto sprite_failed; + fclose(fd); + return sprite; - if(sprite->nf == 0) +frame_failed: + while(sprite->nf) { - free(sprite); - return NULL; + free(sprite->frames[sprite->nf - 1].color); + free(sprite->frames[sprite->nf - 1].chars); + sprite->nf--; } - - return sprite; +sprite_failed: + free(sprite); +sprite_alloc_failed: + fclose(fd); + return NULL; } int ee_get_sprite_frames(struct ee_sprite *sprite)