Changeset 3586 for libcaca/trunk


Ignore:
Timestamp:
Jul 27, 2009, 1:26:08 AM (10 years ago)
Author:
Sam Hocevar
Message:

Implement caca_vprintf() to allow third-party variadic functions to call us.

Location:
libcaca/trunk/caca
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/caca.h

    r3583 r3586  
    2626
    2727#include <caca_types.h>
     28
     29#if !defined(__KERNEL__)
     30#   include <stdarg.h>
     31#endif
    2832
    2933#undef __extern
     
    228232__extern int caca_put_str(caca_canvas_t *, int, int, char const *);
    229233__extern int caca_printf(caca_canvas_t *, int, int, char const *, ...);
     234__extern int caca_vprintf(caca_canvas_t *, int, int, char const *, va_list);
    230235__extern int caca_clear_canvas(caca_canvas_t *);
    231236__extern int caca_set_canvas_handle(caca_canvas_t *, int, int);
  • libcaca/trunk/caca/string.c

    r3583 r3586  
    282282int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
    283283{
     284    va_list args;
     285    int ret;
     286    va_start(args, format);
     287    ret = caca_vprintf(cv, x, y, format, args);
     288    va_end(args);
     289    return ret;
     290}
     291
     292/** \brief Print a formated string (va_list version).
     293 *
     294 *  Format a string at the given coordinates, using the default foreground
     295 *  and background values. The coordinates may be outside the canvas
     296 *  boundaries (eg. a negative Y coordinate) and the string will be cropped
     297 *  accordingly if it is too long. The syntax of the format string is the
     298 *  same as for the C vprintf() function.
     299 *
     300 *  This function never fails.
     301 *
     302 *  \param cv A handle to the libcaca canvas.
     303 *  \param x X coordinate.
     304 *  \param y Y coordinate.
     305 *  \param format The format string to print.
     306 *  \param ap A va_list containting the arguments to the format string.
     307 *  \return This function always returns 0.
     308 */
     309int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
     310                 va_list args)
     311{
    284312    char tmp[BUFSIZ];
    285313    char *buf = tmp;
    286     va_list args;
    287314
    288315    if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
     
    292319        buf = malloc(cv->width - x + 1);
    293320
    294     va_start(args, format);
    295321#if defined(HAVE_VSNPRINTF)
    296322    vsnprintf(buf, cv->width - x + 1, format, args);
     
    299325#endif
    300326    buf[cv->width - x] = '\0';
    301     va_end(args);
    302327
    303328    caca_put_str(cv, x, y, buf);
Note: See TracChangeset for help on using the changeset viewer.