Ignore:
Timestamp:
Nov 9, 2006, 6:20:47 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • If not enough data is available for import, return 0 instead of an error.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/import.c

    r1313 r1314  
    6868 *  Valid values for \c format are:
    6969 *  - \c "": attempt to autodetect the file format.
     70 *  - \c "caca": import native libcaca files.
    7071 *  - \c "text": import ASCII text files.
    7172 *  - \c "ansi": import ANSI files.
    7273 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
    73  *  - \c "caca": import native libcaca files.
     74 *
     75 *  The number of bytes read is returned. If the file format is valid, but
     76 *  not enough data was available, 0 is returned.
    7477 *
    7578 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     
    8184 *  \param len The size in bytes of the memory area.
    8285 *  \param format A string describing the input format.
    83  *  \return The number of bytes read, or -1 if an error occurred.
     86 *  \return The number of bytes read, or 0 if there was not enough data,
     87 *  or -1 if an error occurred.
    8488 */
    8589long int cucul_import_memory(cucul_canvas_t *cv, void const *data,
     
    129133 *  Valid values for \c format are:
    130134 *  - \c "": attempt to autodetect the file format.
     135 *  - \c "caca": import native libcaca files.
    131136 *  - \c "text": import ASCII text files.
    132137 *  - \c "ansi": import ANSI files.
    133138 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
    134  *  - \c "caca": import native libcaca files.
     139 *
     140 *  The number of bytes read is returned. If the file format is valid, but
     141 *  not enough data was available, 0 is returned.
    135142 *
    136143 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     
    144151 *  \param filename The name of the file to load.
    145152 *  \param format A string describing the input format.
    146  *  \return The number of bytes read, or -1 if an error occurred.
     153 *  \return The number of bytes read, or 0 if there was not enough data,
     154 *  or -1 if an error occurred.
    147155 */
    148156long int cucul_import_file(cucul_canvas_t *cv, char const *filename,
     
    202210{
    203211    static char const * const list[] =
    204         {
    205             "", "autodetect",
    206             "text", "plain text",
    207             "caca", "native libcaca format",
    208             "ansi", "ANSI coloured text",
    209             NULL, NULL
    210         };
     212    {
     213        "", "autodetect",
     214        "caca", "native libcaca format",
     215        "text", "plain text",
     216        "ansi", "ANSI coloured text",
     217        "utf8", "UTF-8 files with ANSI colour codes",
     218        NULL, NULL
     219    };
    211220
    212221    return list;
     
    221230{
    222231    uint8_t const *buf = (uint8_t const *)data;
    223     unsigned int control_size, data_size, full_size, frames, f, n;
     232    unsigned int control_size, data_size, expected_size, frames, f, n;
    224233    uint16_t version, flags;
    225234
    226     cucul_set_canvas_size(cv, 0, 0);
    227 
    228235    if(size < 20)
    229         goto invalid_caca;
     236        return 0;
    230237
    231238    if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
     
    238245    flags = sscanu16(buf + 18);
    239246
    240     if(size != 4 + control_size + data_size)
    241         goto invalid_caca;
     247    if(size < 4 + control_size + data_size)
     248        return 0;
    242249
    243250    if(control_size < 16 + frames * 24)
    244251        goto invalid_caca;
    245252
    246     for(full_size = 0, f = 0; f < frames; f++)
     253    for(expected_size = 0, f = 0; f < frames; f++)
    247254    {
    248255        unsigned int width, height, duration;
     
    257264        y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20);
    258265
    259         full_size += width * height * 8;
    260     }
    261 
    262     if(full_size != data_size)
     266        expected_size += width * height * 8;
     267    }
     268
     269    if(expected_size != data_size)
    263270        goto invalid_caca;
    264271
    265272    /* FIXME: read all frames, not only the first one */
     273    cucul_set_canvas_size(cv, 0, 0);
    266274    cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16),
    267275                              sscanu32(buf + 4 + 16 + 4));
     
    277285    cv->curattr = sscanu32(buf + 4 + 16 + 12);
    278286
    279     return size;
     287    return 4 + control_size + data_size;
    280288
    281289invalid_caca:
Note: See TracChangeset for help on using the changeset viewer.