Ignore:
Timestamp:
Nov 8, 2006, 1:11:07 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Deprecate buffer management. We're back to standard void* memory areas.
File:
1 edited

Legend:

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

    r1283 r1302  
    5858};
    5959
    60 static cucul_canvas_t *import_caca(void const *, unsigned int);
    61 static cucul_canvas_t *import_text(void const *, unsigned int);
    62 static cucul_canvas_t *import_ansi(void const *, unsigned int, int);
     60static long int import_caca(cucul_canvas_t *, void const *, unsigned int);
     61static long int import_text(cucul_canvas_t *, void const *, unsigned int);
     62static long int import_ansi(cucul_canvas_t *, void const *, unsigned int, int);
    6363
    6464static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
    6565                            unsigned int, unsigned int const *);
    6666
    67 /** \brief Import a buffer into a canvas
    68  *
    69  *  Import a libcucul buffer as returned by cucul_load_memory()
    70  *  or cucul_load_file() into an internal libcucul canvas.
     67/** \brief Import a memory buffer into a canvas
     68 *
     69 *  Import a memory buffer into the given libcucul canvas's current
     70 *  frame. The current frame is resized accordingly and its contents are
     71 *  replaced with the imported data.
    7172 *
    7273 *  Valid values for \c format are:
     
    7778 *  - \c "caca": import native libcaca files.
    7879 *
    79  *  If an error occurs, NULL is returned and \b errno is set accordingly:
     80 *  If an error occurs, -1 is returned and \b errno is set accordingly:
    8081 *  - \c ENOMEM Not enough memory to allocate canvas.
    8182 *  - \c EINVAL Invalid format requested.
    8283 *
     84 *  \param A libcucul canvas in which to import the file.
    8385 *  \param buffer A \e libcucul buffer containing the data to be loaded
    8486 *         into a canvas.
    8587 *  \param format A string describing the input format.
    86  *  \return A libcucul canvas, or NULL in case of error.
     88 *  \return The number of bytes read, or -1 if an error occurred.
    8789 */
    88 cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
    89 {
    90     char const *buf = (char const*)buffer->data;
    91 
     90long int cucul_import(cucul_canvas_t *cv, unsigned char const *buf,
     91                      unsigned long int len, char const *format)
     92{
    9293    if(!strcasecmp("caca", format))
    93         return import_caca(buffer->data, buffer->size);
     94        return import_caca(cv, buf, len);
    9495    if(!strcasecmp("utf8", format))
    95         return import_ansi(buffer->data, buffer->size, 1);
     96        return import_ansi(cv, buf, len, 1);
    9697    if(!strcasecmp("text", format))
    97         return import_text(buffer->data, buffer->size);
     98        return import_text(cv, buf, len);
    9899    if(!strcasecmp("ansi", format))
    99         return import_ansi(buffer->data, buffer->size, 0);
     100        return import_ansi(cv, buf, len, 0);
    100101
    101102    /* Autodetection */
     
    104105        unsigned int i;
    105106
    106         /* If 4 first letters are CACA */
    107         if(buffer->size >= 4 && (uint8_t)buf[0] == 0xca &&
    108            (uint8_t)buf[1] == 0xca && buf[2] == 'C' && buf[3] == 'V')
    109             return import_caca(buffer->data, buffer->size);
     107        /* If 4 first bytes are 0xcaca + 'CV' */
     108        if(len >= 4 && buf[0] == 0xca &&
     109           buf[1] == 0xca && buf[2] == 'C' && buf[3] == 'V')
     110            return import_caca(cv, buf, len);
    110111
    111112        /* If we find ESC[ argv, we guess it's an ANSI file */
    112         for(i = 0; i + 1 < buffer->size; i++)
     113        for(i = 0; i + 1 < len; i++)
    113114            if((buf[i] == 0x1b) && (buf[i + 1] == '['))
    114                 return import_ansi(buffer->data, buffer->size, 0);
     115                return import_ansi(cv, buf, len, 0);
    115116
    116117        /* Otherwise, import it as text */
    117         return import_text(buffer->data, buffer->size);
     118        return import_text(cv, buf, len);
    118119    }
    119120
     
    121122    errno = EINVAL;
    122123#endif
    123     return NULL;
     124    return -1;
    124125}
    125126
     
    153154 */
    154155
    155 static cucul_canvas_t *import_caca(void const *data, unsigned int size)
    156 {
    157     cucul_canvas_t *cv;
     156static long int import_caca(cucul_canvas_t *cv,
     157                            void const *data, unsigned int size)
     158{
    158159    uint8_t const *buf = (uint8_t const *)data;
    159160    unsigned int control_size, data_size, full_size, frames, f, n;
    160161    uint16_t version, flags;
     162
     163    cucul_set_canvas_size(cv, 0, 0);
    161164
    162165    if(size < 20)
     
    198201
    199202    /* FIXME: read all frames, not only the first one */
    200     cv = cucul_create_canvas(sscanu32(buf + 4 + 16),
    201                              sscanu32(buf + 4 + 16 + 4));
    202 
    203     if(!cv)
    204     {
    205 #if defined(HAVE_ERRNO_H)
    206         errno = ENOMEM;
    207 #endif
    208         return NULL;
    209     }
     203    cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16),
     204                              sscanu32(buf + 4 + 16 + 4));
     205
     206    /* FIXME: check for return value */
    210207
    211208    for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; )
     
    217214    cv->curattr = sscanu32(buf + 4 + 16 + 12);
    218215
    219     return cv;
     216    return size;
    220217
    221218invalid_caca:
     
    223220    errno = EINVAL;
    224221#endif
    225     return NULL;
    226 }
    227 
    228 static cucul_canvas_t *import_text(void const *data, unsigned int size)
    229 {
    230     cucul_canvas_t *cv;
     222    return -1;
     223}
     224
     225static long int import_text(cucul_canvas_t *cv,
     226                            void const *data, unsigned int size)
     227{
    231228    char const *text = (char const *)data;
    232229    unsigned int width = 0, height = 0, x = 0, y = 0, i;
    233230
    234     cv = cucul_create_canvas(width, height);
    235     if(!cv)
    236     {
    237 #if defined(HAVE_ERRNO_H)
    238         errno = ENOMEM;
    239 #endif
    240         return NULL;
    241     }
    242 
     231    cucul_set_canvas_size(cv, width, height);
    243232    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
    244233
     
    275264        cucul_set_canvas_size(cv, width, height = y);
    276265
    277     return cv;
    278 }
    279 
    280 static cucul_canvas_t *import_ansi(void const *data, unsigned int size,
    281                                   int utf8)
     266    return size;
     267}
     268
     269static long int import_ansi(cucul_canvas_t *cv,
     270                            void const *data, unsigned int size, int utf8)
    282271{
    283272    struct ansi_grcm grcm;
    284273    unsigned char const *buffer = (unsigned char const*)data;
    285     cucul_canvas_t *cv;
    286274    unsigned int i, j, skip, dummy = 0;
    287275    unsigned int width = 0, height = 0, wch = 1;
     
    289277    int x = 0, y = 0, save_x = 0, save_y = 0;
    290278
    291     cv = cucul_create_canvas(width, height);
    292     if(!cv)
    293     {
    294 #if defined(HAVE_ERRNO_H)
    295         errno = ENOMEM;
    296 #endif
    297         return NULL;
    298     }
    299 
     279    cucul_set_canvas_size(cv, width, height);
    300280    ansi_parse_grcm(cv, &grcm, 1, &dummy);
    301281
     
    495475    }
    496476
    497     return cv;
     477    return size;
    498478}
    499479
Note: See TracChangeset for help on using the changeset viewer.