Explorar el Código

Make caca_printf(), caca_vprintf() and caca_put_str() return the number of

printed cells instead of always returning 0. This is handy if we want to
advance a cursor after each printf() call.
tags/v0.99.beta17
Sam Hocevar sam hace 16 años
padre
commit
823b59a7fa
Se han modificado 2 ficheros con 55 adiciones y 33 borrados
  1. +10
    -8
      caca/caca_conio.c
  2. +45
    -25
      caca/string.c

+ 10
- 8
caca/caca_conio.c Ver fichero

@@ -73,18 +73,19 @@ void caca_conio_clrscr(void)
int caca_conio_cprintf(const char *format, ...) int caca_conio_cprintf(const char *format, ...)
{ {
va_list args; va_list args;
int ret;


conio_init(); conio_init();


va_start(args, format); va_start(args, format);
caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
va_end(args); va_end(args);


caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv));

conio_refresh(); conio_refresh();


/* FIXME: we should fix caca_vprintf so that it returns the number of
* characters that were printed. */
return 0;
return ret;
} }


/** \brief DOS conio.h cputs() equivalent */ /** \brief DOS conio.h cputs() equivalent */
@@ -112,7 +113,7 @@ void caca_conio_delay(int i)
{ {
conio_init(); conio_init();


/* TODO: implement this function */
_caca_sleep(i * 1000);
} }


/** \brief DOS conio.h delline() equivalent */ /** \brief DOS conio.h delline() equivalent */
@@ -271,17 +272,18 @@ void caca_conio_nosound(void)
int caca_conio_printf(const char *format, ...) int caca_conio_printf(const char *format, ...)
{ {
va_list args; va_list args;
int ret;


conio_init(); conio_init();


va_start(args, format); va_start(args, format);
caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
va_end(args); va_end(args);


caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv));

conio_refresh(); conio_refresh();


/* FIXME: we should fix caca_vprintf so that it returns the number of
* characters that were printed. */
return 0; return 0;
} }




+ 45
- 25
caca/string.c Ver fichero

@@ -101,26 +101,31 @@ int caca_wherey(caca_canvas_t const *cv)
* characters is undefined. To print a sequence of bytes forming an UTF-8 * characters is undefined. To print a sequence of bytes forming an UTF-8
* character instead of an UTF-32 character, use the caca_put_str() function. * character instead of an UTF-32 character, use the caca_put_str() function.
* *
* This function returns the width of the printed character. If it is a
* fullwidth character, 2 is returned. Otherwise, 1 is returned.
*
* This function never fails. * This function never fails.
* *
* \param cv A handle to the libcaca canvas. * \param cv A handle to the libcaca canvas.
* \param x X coordinate. * \param x X coordinate.
* \param y Y coordinate. * \param y Y coordinate.
* \param ch The character to print. * \param ch The character to print.
* \return This function always returns 0.
* \return The width of the printed character: 2 for a fullwidth character,
* 1 otherwise.
*/ */
int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch) int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch)
{ {
uint32_t *curchar, *curattr, attr; uint32_t *curchar, *curattr, attr;
int fullwidth, xmin, xmax;

if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
return 0;
int fullwidth, xmin, xmax, ret;


if(ch == CACA_MAGIC_FULLWIDTH) if(ch == CACA_MAGIC_FULLWIDTH)
return 0;
return 1;


fullwidth = caca_utf32_is_fullwidth(ch); fullwidth = caca_utf32_is_fullwidth(ch);
ret = fullwidth ? 2 : 1;

if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
return ret;


if(x == -1 && fullwidth) if(x == -1 && fullwidth)
{ {
@@ -129,7 +134,7 @@ int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch)
fullwidth = 0; fullwidth = 0;
} }
else if(x < 0) else if(x < 0)
return 0;
return ret;


curchar = cv->chars + x + y * cv->width; curchar = cv->chars + x + y * cv->width;
curattr = cv->attrs + x + y * cv->width; curattr = cv->attrs + x + y * cv->width;
@@ -187,7 +192,7 @@ int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch)
curchar[0] = ch; curchar[0] = ch;
curattr[0] = attr; curattr[0] = attr;


return 0;
return ret;
} }


/** \brief Get the Unicode character at the given coordinates. /** \brief Get the Unicode character at the given coordinates.
@@ -230,36 +235,45 @@ uint32_t caca_get_char(caca_canvas_t const *cv, int x, int y)
* See caca_put_char() for more information on how fullwidth characters * See caca_put_char() for more information on how fullwidth characters
* are handled when overwriting each other or at the canvas' boundaries. * are handled when overwriting each other or at the canvas' boundaries.
* *
* This function returns the number of cells printed by the string. It is
* not the number of characters printed, because fullwidth characters
* account for two cells.
*
* This function never fails. * This function never fails.
* *
* \param cv A handle to the libcaca canvas. * \param cv A handle to the libcaca canvas.
* \param x X coordinate. * \param x X coordinate.
* \param y Y coordinate. * \param y Y coordinate.
* \param s The string to print. * \param s The string to print.
* \return This function always returns 0.
* \return The number of cells printed.
*/ */
int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s) int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
{ {
size_t rd; size_t rd;
int len = 0;


if(y < 0 || y >= (int)cv->height || x >= (int)cv->width) if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
return 0;

while(*s && x < -1)
{ {
x += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1;
s += rd;
while(*s)
{
len += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1;
s += rd;
}
return len;
} }


while(*s && x < (int)cv->width)
while(*s)
{ {
uint32_t ch = caca_utf8_to_utf32(s, &rd); uint32_t ch = caca_utf8_to_utf32(s, &rd);
caca_put_char(cv, x, y, ch);
x += caca_utf32_is_fullwidth(ch) ? 2 : 1;

if(x + len >= -1 && x + len < (int)cv->width)
caca_put_char(cv, x + len, y, ch);

len += caca_utf32_is_fullwidth(ch) ? 2 : 1;
s += rd; s += rd;
} }


return 0;
return len;
} }


/** \brief Print a formated string. /** \brief Print a formated string.
@@ -270,6 +284,10 @@ int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
* accordingly if it is too long. The syntax of the format string is the * accordingly if it is too long. The syntax of the format string is the
* same as for the C printf() function. * same as for the C printf() function.
* *
* This function returns the number of cells printed by the string. It is
* not the number of characters printed, because fullwidth characters
* account for two cells.
*
* This function never fails. * This function never fails.
* *
* \param cv A handle to the libcaca canvas. * \param cv A handle to the libcaca canvas.
@@ -277,7 +295,7 @@ int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
* \param y Y coordinate. * \param y Y coordinate.
* \param format The format string to print. * \param format The format string to print.
* \param ... Arguments to the format string. * \param ... Arguments to the format string.
* \return This function always returns 0.
* \return The number of cells printed.
*/ */
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, ...)
{ {
@@ -297,6 +315,10 @@ int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
* accordingly if it is too long. The syntax of the format string is the * accordingly if it is too long. The syntax of the format string is the
* same as for the C vprintf() function. * same as for the C vprintf() function.
* *
* This function returns the number of cells printed by the string. It is
* not the number of characters printed, because fullwidth characters
* account for two cells.
*
* This function never fails. * This function never fails.
* *
* \param cv A handle to the libcaca canvas. * \param cv A handle to the libcaca canvas.
@@ -304,16 +326,14 @@ int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
* \param y Y coordinate. * \param y Y coordinate.
* \param format The format string to print. * \param format The format string to print.
* \param ap A va_list containting the arguments to the format string. * \param ap A va_list containting the arguments to the format string.
* \return This function always returns 0.
* \return The number of cells printed.
*/ */
int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format, int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
va_list args) va_list args)
{ {
char tmp[BUFSIZ]; char tmp[BUFSIZ];
char *buf = tmp; char *buf = tmp;

if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
return 0;
int ret;


if(cv->width - x + 1 > BUFSIZ) if(cv->width - x + 1 > BUFSIZ)
buf = malloc(cv->width - x + 1); buf = malloc(cv->width - x + 1);
@@ -325,12 +345,12 @@ int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
#endif #endif
buf[cv->width - x] = '\0'; buf[cv->width - x] = '\0';


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


if(buf != tmp) if(buf != tmp)
free(buf); free(buf);


return 0;
return ret;
} }


/** \brief Clear the canvas. /** \brief Clear the canvas.


Cargando…
Cancelar
Guardar