瀏覽代碼

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

tags/v0.99.beta17
Sam Hocevar sam 15 年之前
父節點
當前提交
4648ab11aa
共有 2 個文件被更改,包括 33 次插入3 次删除
  1. +5
    -0
      caca/caca.h
  2. +28
    -3
      caca/string.c

+ 5
- 0
caca/caca.h 查看文件

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

#include <caca_types.h>

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

#undef __extern
#if defined(_DOXYGEN_SKIP_ME)
#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 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_vprintf(caca_canvas_t *, int, int, char const *, va_list);
__extern int caca_clear_canvas(caca_canvas_t *);
__extern int caca_set_canvas_handle(caca_canvas_t *, int, int);
__extern int caca_get_canvas_handle_x(caca_canvas_t const *);


+ 28
- 3
caca/string.c 查看文件

@@ -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.
*/
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 *buf = tmp;
va_list args;

if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
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)
buf = malloc(cv->width - x + 1);

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

caca_put_str(cv, x, y, buf);



Loading…
取消
儲存