Changeset 3595


Ignore:
Timestamp:
07/31/09 02:01:25 (4 years ago)
Author:
sam
Message:

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.

Location:
libcaca/trunk/caca
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/caca_conio.c

    r3594 r3595  
    7474{ 
    7575    va_list args; 
     76    int ret; 
    7677 
    7778    conio_init(); 
    7879 
    7980    va_start(args, format); 
    80     caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args); 
     81    ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args); 
    8182    va_end(args); 
    8283 
     84    caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv)); 
     85 
    8386    conio_refresh(); 
    8487 
    85     /* FIXME: we should fix caca_vprintf so that it returns the number of 
    86      * characters that were printed. */ 
    87     return 0; 
     88    return ret; 
    8889} 
    8990 
     
    113114    conio_init(); 
    114115 
    115     /* TODO: implement this function */ 
     116    _caca_sleep(i * 1000); 
    116117} 
    117118 
     
    272273{ 
    273274    va_list args; 
     275    int ret; 
    274276 
    275277    conio_init(); 
    276278 
    277279    va_start(args, format); 
    278     caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args); 
     280    ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args); 
    279281    va_end(args); 
    280282 
     283    caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv)); 
     284 
    281285    conio_refresh(); 
    282286 
    283     /* FIXME: we should fix caca_vprintf so that it returns the number of 
    284      * characters that were printed. */ 
    285287    return 0; 
    286288} 
  • libcaca/trunk/caca/string.c

    r3587 r3595  
    102102 *  character instead of an UTF-32 character, use the caca_put_str() function. 
    103103 * 
     104 *  This function returns the width of the printed character. If it is a 
     105 *  fullwidth character, 2 is returned. Otherwise, 1 is returned. 
     106 * 
    104107 *  This function never fails. 
    105108 * 
     
    108111 *  \param y Y coordinate. 
    109112 *  \param ch The character to print. 
    110  *  \return This function always returns 0. 
     113 *  \return The width of the printed character: 2 for a fullwidth character, 
     114 *          1 otherwise. 
    111115 */ 
    112116int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch) 
    113117{ 
    114118    uint32_t *curchar, *curattr, attr; 
    115     int fullwidth, xmin, xmax; 
     119    int fullwidth, xmin, xmax, ret; 
     120 
     121    if(ch == CACA_MAGIC_FULLWIDTH) 
     122        return 1; 
     123 
     124    fullwidth = caca_utf32_is_fullwidth(ch); 
     125    ret = fullwidth ? 2 : 1; 
    116126 
    117127    if(x >= (int)cv->width || y < 0 || y >= (int)cv->height) 
    118         return 0; 
    119  
    120     if(ch == CACA_MAGIC_FULLWIDTH) 
    121         return 0; 
    122  
    123     fullwidth = caca_utf32_is_fullwidth(ch); 
     128        return ret; 
    124129 
    125130    if(x == -1 && fullwidth) 
     
    130135    } 
    131136    else if(x < 0) 
    132         return 0; 
     137        return ret; 
    133138 
    134139    curchar = cv->chars + x + y * cv->width; 
     
    188193    curattr[0] = attr; 
    189194 
    190     return 0; 
     195    return ret; 
    191196} 
    192197 
     
    231236 *  are handled when overwriting each other or at the canvas' boundaries. 
    232237 * 
     238 *  This function returns the number of cells printed by the string. It is 
     239 *  not the number of characters printed, because fullwidth characters 
     240 *  account for two cells. 
     241 * 
    233242 *  This function never fails. 
    234243 * 
     
    237246 *  \param y Y coordinate. 
    238247 *  \param s The string to print. 
    239  *  \return This function always returns 0. 
     248 *  \return The number of cells printed. 
    240249 */ 
    241250int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s) 
    242251{ 
    243252    size_t rd; 
     253    int len = 0; 
    244254 
    245255    if(y < 0 || y >= (int)cv->height || x >= (int)cv->width) 
    246         return 0; 
    247  
    248     while(*s && x < -1) 
    249     { 
    250         x += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1; 
     256    { 
     257        while(*s) 
     258        { 
     259            len += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1; 
     260            s += rd; 
     261        } 
     262        return len; 
     263    } 
     264 
     265    while(*s) 
     266    { 
     267        uint32_t ch = caca_utf8_to_utf32(s, &rd); 
     268 
     269        if(x + len >= -1 && x + len < (int)cv->width) 
     270            caca_put_char(cv, x + len, y, ch); 
     271 
     272        len += caca_utf32_is_fullwidth(ch) ? 2 : 1; 
    251273        s += rd; 
    252274    } 
    253275 
    254     while(*s && x < (int)cv->width) 
    255     { 
    256         uint32_t ch = caca_utf8_to_utf32(s, &rd); 
    257         caca_put_char(cv, x, y, ch); 
    258         x += caca_utf32_is_fullwidth(ch) ? 2 : 1; 
    259         s += rd; 
    260     } 
    261  
    262     return 0; 
     276    return len; 
    263277} 
    264278 
     
    271285 *  same as for the C printf() function. 
    272286 * 
     287 *  This function returns the number of cells printed by the string. It is 
     288 *  not the number of characters printed, because fullwidth characters 
     289 *  account for two cells. 
     290 * 
    273291 *  This function never fails. 
    274292 * 
     
    278296 *  \param format The format string to print. 
    279297 *  \param ... Arguments to the format string. 
    280  *  \return This function always returns 0. 
     298 *  \return The number of cells printed. 
    281299 */ 
    282300int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...) 
     
    298316 *  same as for the C vprintf() function. 
    299317 * 
     318 *  This function returns the number of cells printed by the string. It is 
     319 *  not the number of characters printed, because fullwidth characters 
     320 *  account for two cells. 
     321 * 
    300322 *  This function never fails. 
    301323 * 
     
    305327 *  \param format The format string to print. 
    306328 *  \param ap A va_list containting the arguments to the format string. 
    307  *  \return This function always returns 0. 
     329 *  \return The number of cells printed. 
    308330 */ 
    309331int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format, 
     
    312334    char tmp[BUFSIZ]; 
    313335    char *buf = tmp; 
    314  
    315     if(y < 0 || y >= (int)cv->height || x >= (int)cv->width) 
    316         return 0; 
     336    int ret; 
    317337 
    318338    if(cv->width - x + 1 > BUFSIZ) 
     
    326346    buf[cv->width - x] = '\0'; 
    327347 
    328     caca_put_str(cv, x, y, buf); 
     348    ret = caca_put_str(cv, x, y, buf); 
    329349 
    330350    if(buf != tmp) 
    331351        free(buf); 
    332352 
    333     return 0; 
     353    return ret; 
    334354} 
    335355 
Note: See TracChangeset for help on using the changeset viewer.