Changeset 168


Ignore:
Timestamp:
Nov 13, 2003, 5:45:25 PM (20 years ago)
Author:
Sam Hocevar
Message:
  • 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().
Location:
ttyvaders/trunk/libee
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ttyvaders/trunk/libee/ee.c

    r160 r168  
    6868
    6969static int _ee_delay;
     70char *_ee_empty_line;
    7071
    7172#if defined(USE_NCURSES)
     
    167168
    168169#elif defined(USE_CONIO)
     170    gettextinfo(&ti);
     171    _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
     172    if(_ee_screen == NULL)
     173        return -1;
    169174    _wscroll = 0;
    170175    _setcursortype(_NOCURSOR);
    171176    clrscr();
    172     gettextinfo(&ti);
    173     _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
    174177#   if defined(SCREENUPDATE_IN_PC_H)
    175178    ScreenRetrieve(_ee_screen);
     
    179182
    180183#endif
     184    _ee_empty_line = malloc(ee_get_width() + 1);
     185    memset(_ee_empty_line, ' ', ee_get_width());
     186    _ee_empty_line[ee_get_width()] = '\0';
     187
    181188    _ee_delay = 0;
    182189
  • ttyvaders/trunk/libee/ee_internals.h

    r160 r168  
    3232#endif
    3333
     34extern char *_ee_empty_line;
     35
    3436#endif /* __EE_INTERNALS_H__ */
  • ttyvaders/trunk/libee/graphics.c

    r160 r168  
    119119{
    120120    /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
    121     int x = ee_get_width();
    122121    int y = ee_get_height();
    123     char *empty_line = malloc((x + 1) * sizeof(char));
    124 
    125     memset(empty_line, ' ', x);
    126     empty_line[x] = '\0';
    127122
    128123    while(y--)
    129     {
    130         ee_putstr(0, y, empty_line);
    131     }
    132 
    133     free(empty_line);
     124        ee_putstr(0, y, _ee_empty_line);
    134125}
    135126
  • ttyvaders/trunk/libee/sprite.c

    r160 r168  
    5555
    5656    sprite = malloc(sizeof(struct ee_sprite));
     57    if(sprite == NULL)
     58        goto sprite_alloc_failed;
     59
    5760    sprite->nf = 0;
    5861    sprite->frames = NULL;
     
    7275            break;
    7376
    74         if(sprite->nf++)
    75             sprite->frames = realloc(sprite->frames,
    76                                      sprite->nf * sizeof(struct ee_frame));
     77        if(sprite->nf)
     78        {
     79            void *tmp = realloc(sprite->frames,
     80                                (sprite->nf + 1) * sizeof(struct ee_frame));
     81            if(tmp == NULL)
     82                goto frame_failed;
     83            sprite->frames = tmp;
     84            sprite->nf++;
     85        }
    7786        else
    78             sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
     87        {
     88            sprite->frames = malloc((sprite->nf + 1) * sizeof(struct ee_frame));
     89            if(sprite->frames == NULL)
     90                goto sprite_failed;
     91            sprite->nf++;
     92        }
     93
    7994        frame = &sprite->frames[sprite->nf - 1];
    8095
     
    8499        frame->dy = dy;
    85100        frame->chars = malloc(w * h * sizeof(char));
     101        if(frame->chars == NULL)
     102        {
     103            sprite->nf--;
     104            goto frame_failed;
     105        }
    86106        frame->color = malloc(w * h * sizeof(int));
     107        if(frame->color == NULL)
     108        {
     109            free(frame->chars);
     110            sprite->nf--;
     111            goto frame_failed;
     112        }
    87113
    88114        for(y = 0; y < h; y++)
    89115        {
    90116            if(!fgets(buf, BUFSIZ, fd))
    91                 goto failed;
     117                goto frame_failed;
    92118
    93119            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
     
    101127        {
    102128            if(!fgets(buf, BUFSIZ, fd))
    103                 goto failed;
     129                goto frame_failed;
    104130
    105131            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
     
    111137
    112138        continue;
    113 
    114     failed:
     139    }
     140
     141    if(sprite->nf == 0)
     142        goto sprite_failed;
     143
     144    fclose(fd);
     145    return sprite;
     146
     147frame_failed:
     148    while(sprite->nf)
     149    {
     150        free(sprite->frames[sprite->nf - 1].color);
    115151        free(sprite->frames[sprite->nf - 1].chars);
    116         free(sprite->frames[sprite->nf - 1].color);
    117152        sprite->nf--;
    118         break;
    119     }
    120 
     153    }
     154sprite_failed:
     155    free(sprite);
     156sprite_alloc_failed:
    121157    fclose(fd);
    122 
    123     if(sprite->nf == 0)
    124     {
    125         free(sprite);
    126         return NULL;
    127     }
    128 
    129     return sprite;
     158    return NULL;
    130159}
    131160
Note: See TracChangeset for help on using the changeset viewer.