Browse Source

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

tags/v0.99.beta17
Sam Hocevar sam 15 years ago
parent
commit
4648ab11aa
2 changed files with 33 additions and 3 deletions
  1. +5
    -0
      caca/caca.h
  2. +28
    -3
      caca/string.c

+ 5
- 0
caca/caca.h View File

@@ -26,6 +26,10 @@


#include <caca_types.h> #include <caca_types.h>


#if !defined(__KERNEL__)
# include <stdarg.h>
#endif

#undef __extern #undef __extern
#if defined(_DOXYGEN_SKIP_ME) #if defined(_DOXYGEN_SKIP_ME)
#elif defined(_WIN32) && defined(__LIBCACA__) #elif defined(_WIN32) && defined(__LIBCACA__)
@@ -227,6 +231,7 @@ __extern int caca_put_char(caca_canvas_t *, int, int, uint32_t);
__extern uint32_t caca_get_char(caca_canvas_t const *, int, int); __extern uint32_t caca_get_char(caca_canvas_t const *, int, int);
__extern int caca_put_str(caca_canvas_t *, int, int, char const *); __extern int caca_put_str(caca_canvas_t *, int, int, char const *);
__extern int caca_printf(caca_canvas_t *, int, int, char const *, ...); __extern int caca_printf(caca_canvas_t *, int, int, char const *, ...);
__extern int caca_vprintf(caca_canvas_t *, int, int, char const *, va_list);
__extern int caca_clear_canvas(caca_canvas_t *); __extern int caca_clear_canvas(caca_canvas_t *);
__extern int caca_set_canvas_handle(caca_canvas_t *, int, int); __extern int caca_set_canvas_handle(caca_canvas_t *, int, int);
__extern int caca_get_canvas_handle_x(caca_canvas_t const *); __extern int caca_get_canvas_handle_x(caca_canvas_t const *);


+ 28
- 3
caca/string.c View File

@@ -280,10 +280,37 @@ int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
* \return This function always returns 0. * \return This function always returns 0.
*/ */
int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...) int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
{
va_list args;
int ret;
va_start(args, format);
ret = caca_vprintf(cv, x, y, format, args);
va_end(args);
return ret;
}

/** \brief Print a formated string (va_list version).
*
* Format a string at the given coordinates, using the default foreground
* and background values. The coordinates may be outside the canvas
* boundaries (eg. a negative Y coordinate) and the string will be cropped
* accordingly if it is too long. The syntax of the format string is the
* same as for the C vprintf() function.
*
* This function never fails.
*
* \param cv A handle to the libcaca canvas.
* \param x X coordinate.
* \param y Y coordinate.
* \param format The format string to print.
* \param ap A va_list containting the arguments to the format string.
* \return This function always returns 0.
*/
int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
va_list args)
{ {
char tmp[BUFSIZ]; char tmp[BUFSIZ];
char *buf = tmp; char *buf = tmp;
va_list args;


if(y < 0 || y >= (int)cv->height || x >= (int)cv->width) if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
return 0; return 0;
@@ -291,14 +318,12 @@ int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
if(cv->width - x + 1 > BUFSIZ) if(cv->width - x + 1 > BUFSIZ)
buf = malloc(cv->width - x + 1); buf = malloc(cv->width - x + 1);


va_start(args, format);
#if defined(HAVE_VSNPRINTF) #if defined(HAVE_VSNPRINTF)
vsnprintf(buf, cv->width - x + 1, format, args); vsnprintf(buf, cv->width - x + 1, format, args);
#else #else
vsprintf(buf, format, args); vsprintf(buf, format, args);
#endif #endif
buf[cv->width - x] = '\0'; buf[cv->width - x] = '\0';
va_end(args);


caca_put_str(cv, x, y, buf); caca_put_str(cv, x, y, buf);




Loading…
Cancel
Save