Changeset 874 for libcaca


Ignore:
Timestamp:
Apr 25, 2006, 1:05:25 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • More error checking in libcucul.
Location:
libcaca/trunk/cucul
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/canvas.c

    r870 r874  
    5959 *  \return This function always returns 0.
    6060 */
    61 void cucul_putchar(cucul_canvas_t *cv, int x, int y, char ch)
    62 {
    63     if(x < 0 || x >= (int)cv->width ||
    64        y < 0 || y >= (int)cv->height)
    65         return;
     61int cucul_putchar(cucul_canvas_t *cv, int x, int y, char ch)
     62{
     63    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
     64        return 0;
    6665
    6766    if((unsigned char)ch < 0x20 || (unsigned char)ch > 0x7f)
     
    7069    cv->chars[x + y * cv->width] = ch;
    7170    cv->attr[x + y * cv->width] = (cv->bgcolor << 16) | cv->fgcolor;
     71
     72    return 0;
    7273}
    7374
     
    8788 *  \return This function always returns 0.
    8889 */
    89 void cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
     90int cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
    9091{
    9192    uint32_t *chars, *attr;
     
    9394
    9495    if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
    95         return;
     96        return 0;
    9697
    9798    len = _cucul_strlen_utf8(s);
     
    100101    {
    101102        if(len < (unsigned int)-x)
    102             return;
     103            return 0;
    103104        len -= -x;
    104105        s = _cucul_skip_utf8(s, -x);
     
    120121        len--;
    121122    }
     123
     124    return 0;
    122125}
    123126
     
    139142 *  \return This function always returns 0.
    140143 */
    141 void cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
     144int cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
    142145{
    143146    char tmp[BUFSIZ];
     
    146149
    147150    if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
    148         return;
     151        return 0;
    149152
    150153    if(cv->width - x + 1 > BUFSIZ)
     
    164167    if(buf != tmp)
    165168        free(buf);
     169
     170    return 0;
    166171}
    167172
     
    175180 *  \return This function always returns 0.
    176181 */
    177 void cucul_clear_canvas(cucul_canvas_t *cv)
     182int cucul_clear_canvas(cucul_canvas_t *cv)
    178183{
    179184    uint32_t color = (cv->bgcolor << 16) | cv->fgcolor;
     
    186191        cv->attr[n] = color;
    187192    }
     193
     194    return 0;
    188195}
    189196
     
    193200 *  An optional mask canvas can be used.
    194201 *
     202 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     203 *  - \c EINVAL A mask was specified but the mask size and source canvas
     204 *    size do not match.
     205 *
    195206 *  \param dst The destination canvas.
    196207 *  \param x X coordinate.
     
    198209 *  \param src The source canvas.
    199210 *  \param mask The mask canvas.
    200  *  \return 0 in case of success, -1 otherwise.
     211 *  \return 0 in case of success, -1 if an error occurred.
    201212 */
    202213int cucul_blit(cucul_canvas_t *dst, int x, int y,
  • libcaca/trunk/cucul/colour.c

    r859 r874  
    2020#include "common.h"
    2121
     22#if defined(HAVE_ERRNO_H)
     23#   include <errno.h>
     24#endif
     25
    2226#include "cucul.h"
    2327#include "cucul_internals.h"
     
    3842 *  or CUCUL_COLOR_TRANSPARENT.
    3943 *
     44 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     45 *  - \c EINVAL At least one of the colour values is invalid.
     46 *
    4047 *  \param cv A handle to the libcucul canvas.
    4148 *  \param fg The requested foreground colour.
    4249 *  \param bg The requested background colour.
    43  */
    44 void cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
     50 *  \return 0 in case of success, -1 if an error occurred.
     51 */
     52int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
    4553{
    4654    if(fg > 0x20 || bg > 0x20)
    47         return;
     55    {
     56#if defined(HAVE_ERRNO_H)
     57        errno = EINVAL;
     58#endif
     59        return -1;
     60    }
    4861
    4962    cv->fgcolor = fg;
    5063    cv->bgcolor = bg;
     64
     65    return 0;
    5166}
    5267
     
    6176 *  white with 50% alpha (A=8 R=15 G=15 B=15).
    6277 *
     78 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     79 *  - \c EINVAL At least one of the colour values is invalid.
     80 *
    6381 *  \param cv A handle to the libcucul canvas.
    6482 *  \param fg The requested foreground colour.
    6583 *  \param bg The requested background colour.
    66  */
    67 void cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
     84 *  \return 0 in case of success, -1 if an error occurred.
     85 */
     86int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
    6887{
    6988    if(fg > 0xffff || bg > 0xffff)
    70         return;
     89    {
     90#if defined(HAVE_ERRNO_H)
     91        errno = EINVAL;
     92#endif
     93        return -1;
     94    }
    7195
    7296    if(fg < 0x100)
     
    78102    cv->fgcolor = fg;
    79103    cv->bgcolor = bg;
     104
     105    return 0;
    80106}
    81107
  • libcaca/trunk/cucul/cucul.c

    r859 r874  
    2424#   include <stdlib.h>
    2525#   include <string.h>
     26#   if defined(HAVE_ERRNO_H)
     27#       include <errno.h>
     28#   endif
    2629#endif
    2730
     
    3942 *  of 80x32 is used instead.
    4043 *
     44 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     45 *  - \c ENOMEM Not enough memory for the requested canvas size.
     46 *
    4147 *  \param width The desired canvas width
    4248 *  \param height The desired canvas height
     
    4652{
    4753    cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
     54    int ret;
     55
     56    if(!cv)
     57        goto nomem;
    4858
    4959    cv->refcount = 0;
     
    5969    cv->framecount = 1;
    6070    cv->allchars = malloc(sizeof(uint32_t *));
     71    if(!cv->allchars)
     72    {
     73        free(cv);
     74        goto nomem;
     75    }
    6176    cv->allattr = malloc(sizeof(uint32_t *));
     77    if(!cv->allattr)
     78    {
     79        free(cv->allchars);
     80        free(cv);
     81        goto nomem;
     82    }
    6283    cv->allchars[0] = NULL;
    6384    cv->allattr[0] = NULL;
     
    6788     * a different size. */
    6889    if(width && height)
    69         _cucul_set_canvas_size(cv, width, height);
     90        ret = _cucul_set_canvas_size(cv, width, height);
    7091    else
    71         _cucul_set_canvas_size(cv, 80, 32);
    72 
    73     if(_cucul_init_dither())
    74     {
     92        ret = _cucul_set_canvas_size(cv, 80, 32);
     93
     94    if(ret < 0)
     95    {
     96#if defined(HAVE_ERRNO_H)
     97        int saved_errno = errno;
     98#endif
     99        free(cv->allattr);
     100        free(cv->allchars);
    75101        free(cv);
     102#if defined(HAVE_ERRNO_H)
     103        errno = saved_errno;
     104#endif
    76105        return NULL;
    77106    }
    78107
     108    /* FIXME: this shouldn't happen here */
     109    _cucul_init_dither();
     110
    79111    return cv;
     112
     113nomem:
     114#if defined(HAVE_ERRNO_H)
     115    errno = ENOMEM;
     116#endif
     117    return NULL;
    80118}
    81119
     
    95133 *  for more about this.
    96134 *
     135 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     136 *  - \c EBUSY The canvas is in use by a display driver and cannot be resized.
     137 *  - \c ENOMEM Not enough memory for the requested canvas size. If this
     138 *    happens, the canvas handle becomes invalid and should not be used.
     139 *
    97140 *  \param cv A libcucul canvas
    98141 *  \param width The desired canvas width
    99142 *  \param height The desired canvas height
    100  */
    101 void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
    102                                                unsigned int height)
     143 *  \return 0 in case of success, -1 if an error occurred.
     144 */
     145int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
     146                                              unsigned int height)
    103147{
    104148    if(cv->refcount)
    105         return;
    106 
    107     _cucul_set_canvas_size(cv, width, height);
     149    {
     150#if defined(HAVE_ERRNO_H)
     151        errno = EBUSY;
     152#endif
     153        return -1;
     154    }
     155
     156    return _cucul_set_canvas_size(cv, width, height);
    108157}
    109158
     
    111160 *
    112161 *  This function returns the current canvas width, in character cells.
     162 *
     163 *  This function never fails.
    113164 *
    114165 *  \param cv A libcucul canvas
     
    124175 *  This function returns the current canvas height, in character cells.
    125176 *
     177 *  This function never fails.
     178 *
    126179 *  \param cv A libcucul canvas
    127180 *  \return The canvas height.
     
    137190 *  description string of the associated colour.
    138191 *
     192 *  This function never fails.
     193 *
    139194 *  \param color The colour value.
    140  *  \return A static string containing the colour's name.
     195 *  \return A static string containing the colour's name, or \c "unknown" if
     196 *  the colour is unknown.
    141197 */
    142198char const *cucul_get_color_name(unsigned int color)
     
    174230 *  used unless a new call to cucul_create_canvas() is done.
    175231 *
     232 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     233 *  - \c EBUSY The canvas is in use by a display driver and cannot be freed.
     234 *
    176235 *  \param cv A libcucul canvas
    177  */
    178 void cucul_free_canvas(cucul_canvas_t *cv)
     236 *  \return 0 in case of success, -1 if an error occurred.
     237 */
     238int cucul_free_canvas(cucul_canvas_t *cv)
    179239{
    180240    unsigned int f;
    181241
     242    if(cv->refcount)
     243    {
     244#if defined(HAVE_ERRNO_H)
     245        errno = EBUSY;
     246#endif
     247        return -1;
     248    }
     249
     250    /* FIXME: this shouldn't be here either (see above) */
    182251    _cucul_end_dither();
    183252
     
    189258
    190259    free(cv);
     260
     261    return 0;
    191262}
    192263
    193264/** \brief Generate a random integer within a range.
     265 *
     266 *  This function never fails.
    194267 *
    195268 *  \param min The lower bound of the integer range.
     
    207280 */
    208281
    209 void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
    210                                                 unsigned int height)
     282int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
     283                                               unsigned int height)
    211284{
    212285    unsigned int x, y, f, old_width, old_height, new_size, old_size;
     
    229302            cv->allattr[f] = realloc(cv->allattr[f],
    230303                                     new_size * sizeof(uint32_t));
     304            if(!cv->allchars[f] || !cv->allattr[f])
     305            {
     306#if defined(HAVE_ERRNO_H)
     307                errno = ENOMEM;
     308#endif
     309                return -1;
     310            }
    231311        }
    232312    }
     
    311391            cv->allattr[f] = realloc(cv->allattr[f],
    312392                                     new_size * sizeof(uint32_t));
     393            if(!cv->allchars[f] || !cv->allattr[f])
     394            {
     395#if defined(HAVE_ERRNO_H)
     396                errno = ENOMEM;
     397#endif
     398                return -1;
     399            }
    313400        }
    314401    }
     
    317404    cv->chars = cv->allchars[cv->frame];
    318405    cv->attr = cv->allattr[cv->frame];
    319 }
    320 
     406
     407    return 0;
     408}
     409
  • libcaca/trunk/cucul/cucul.h

    r870 r874  
    7373 *  @{ */
    7474cucul_canvas_t * cucul_create_canvas(unsigned int, unsigned int);
    75 void cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int);
     75int cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int);
    7676unsigned int cucul_get_canvas_width(cucul_canvas_t *);
    7777unsigned int cucul_get_canvas_height(cucul_canvas_t *);
    78 void cucul_free_canvas(cucul_canvas_t *);
     78int cucul_free_canvas(cucul_canvas_t *);
    7979int cucul_rand(int, int);
    8080/*  @} */
     
    9696 *
    9797 *  @{ */
    98 void cucul_set_color(cucul_canvas_t *, unsigned char, unsigned char);
    99 void cucul_set_truecolor(cucul_canvas_t *, unsigned int, unsigned int);
     98int cucul_set_color(cucul_canvas_t *, unsigned char, unsigned char);
     99int cucul_set_truecolor(cucul_canvas_t *, unsigned int, unsigned int);
    100100char const *cucul_get_color_name(unsigned int);
    101 void cucul_putchar(cucul_canvas_t *, int, int, char);
    102 void cucul_putstr(cucul_canvas_t *, int, int, char const *);
    103 void cucul_printf(cucul_canvas_t *, int, int, char const *, ...);
    104 void cucul_clear_canvas(cucul_canvas_t *);
     101int cucul_putchar(cucul_canvas_t *, int, int, char);
     102int cucul_putstr(cucul_canvas_t *, int, int, char const *);
     103int cucul_printf(cucul_canvas_t *, int, int, char const *, ...);
     104int cucul_clear_canvas(cucul_canvas_t *);
    105105int cucul_blit(cucul_canvas_t *, int, int, cucul_canvas_t const *, cucul_canvas_t const *);
    106106/*  @} */
  • libcaca/trunk/cucul/cucul_internals.h

    r859 r874  
    5151
    5252/* Canvas functions */
    53 extern void _cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int);
     53extern int _cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int);
    5454extern void _cucul_putchar32(cucul_canvas_t *, int, int, uint32_t);
    5555
Note: See TracChangeset for help on using the changeset viewer.