Changeset 1302 for libcaca


Ignore:
Timestamp:
Nov 8, 2006, 1:11:07 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Deprecate buffer management. We're back to standard void* memory areas.
Location:
libcaca/trunk/cucul
Files:
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/Makefile.am

    r977 r1302  
    1515        cucul.h \
    1616        cucul_internals.h \
    17         buffer.c \
     17        legacy.c \
    1818        canvas.c \
    1919        transform.c \
  • libcaca/trunk/cucul/colour.c

    r1270 r1302  
    137137}
    138138
    139 /* Legacy function for old programs */
    140 int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
    141 {
    142     return cucul_set_color_ansi(cv, fg, bg);
    143 }
    144 
    145139/** \brief Set the default colour pair for text (truecolor version).
    146140 *
     
    186180
    187181    return 0;
    188 }
    189 
    190 /* Legacy function for old programs */
    191 int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
    192 {
    193     return cucul_set_color_argb(cv, fg, bg);
    194182}
    195183
  • libcaca/trunk/cucul/cucul.h

    r1273 r1302  
    3636/** dither structure */
    3737typedef struct cucul_dither cucul_dither_t;
    38 /** data buffer structure */
    39 typedef struct cucul_buffer cucul_buffer_t;
    4038/** font structure */
    4139typedef struct cucul_font cucul_font_t;
     
    8381int cucul_free_canvas(cucul_canvas_t *);
    8482int cucul_rand(int, int);
    85 /*  @} */
    86 
    87 /** \defgroup buffer libcucul buffer handling
    88  *
    89  *  These functions provide methods to handle libcucul buffers.
    90  *
    91  *  @{ */
    92 cucul_buffer_t *cucul_load_memory(void *, unsigned long int);
    93 cucul_buffer_t *cucul_load_file(char const *);
    94 unsigned long int cucul_get_buffer_size(cucul_buffer_t *);
    95 void * cucul_get_buffer_data(cucul_buffer_t *);
    96 int cucul_free_buffer(cucul_buffer_t *);
    9783/*  @} */
    9884
     
    231217 *
    232218 *  @{ */
    233 cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *, char const *);
     219void *cucul_export(cucul_canvas_t *, char const *, unsigned long int *);
     220long int cucul_import(cucul_canvas_t *, unsigned char const *,
     221                      unsigned long int, char const *);
    234222char const * const * cucul_get_export_list(void);
    235 cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *, char const *);
    236223char const * const * cucul_get_import_list(void);
    237224/*  @} */
    238225
    239226#if !defined(_DOXYGEN_SKIP_ME)
    240 /* Legacy stuff from beta versions, will probably disappear in 1.0 */
     227    /* Legacy stuff from beta versions, will probably disappear in 1.0 */
     228    typedef struct cucul_buffer cucul_buffer_t;
    241229#   ifdef __GNUC__
    242230#       define CUCUL_DEPRECATED __attribute__ ((deprecated))
     
    244232#       define CUCUL_DEPRECATED
    245233#   endif
    246 int cucul_set_color(cucul_canvas_t *, unsigned char,
    247                                       unsigned char) CUCUL_DEPRECATED;
    248 int cucul_set_truecolor(cucul_canvas_t *, unsigned int,
    249                                           unsigned int) CUCUL_DEPRECATED;
     234    int cucul_set_color(cucul_canvas_t *, unsigned char,
     235                                          unsigned char) CUCUL_DEPRECATED;
     236    int cucul_set_truecolor(cucul_canvas_t *, unsigned int,
     237                                              unsigned int) CUCUL_DEPRECATED;
     238    cucul_buffer_t *cucul_load_memory(void *,
     239                                      unsigned long int) CUCUL_DEPRECATED;
     240    cucul_buffer_t *cucul_load_file(char const *) CUCUL_DEPRECATED;
     241    unsigned long int cucul_get_buffer_size(cucul_buffer_t *) CUCUL_DEPRECATED;
     242    void * cucul_get_buffer_data(cucul_buffer_t *) CUCUL_DEPRECATED;
     243    int cucul_free_buffer(cucul_buffer_t *) CUCUL_DEPRECATED;
     244    cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *,
     245                                         char const *) CUCUL_DEPRECATED;
     246    cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *,
     247                                         char const *) CUCUL_DEPRECATED;
    250248#   define CUCUL_COLOR_BLACK CUCUL_BLACK
    251249#   define CUCUL_COLOR_BLUE CUCUL_BLUE
  • libcaca/trunk/cucul/export.c

    r1283 r1302  
    4848}
    4949
    50 static int export_caca(cucul_canvas_t *, cucul_buffer_t *);
    51 static int export_ansi(cucul_canvas_t *, cucul_buffer_t *);
    52 static int export_utf8(cucul_canvas_t *, cucul_buffer_t *, int);
    53 static int export_html(cucul_canvas_t *, cucul_buffer_t *);
    54 static int export_html3(cucul_canvas_t *, cucul_buffer_t *);
    55 static int export_irc(cucul_canvas_t *, cucul_buffer_t *);
    56 static int export_ps(cucul_canvas_t *, cucul_buffer_t *);
    57 static int export_svg(cucul_canvas_t *, cucul_buffer_t *);
    58 static int export_tga(cucul_canvas_t *, cucul_buffer_t *);
     50static void *export_caca(cucul_canvas_t *, unsigned long int *);
     51static void *export_ansi(cucul_canvas_t *, unsigned long int *);
     52static void *export_utf8(cucul_canvas_t *, unsigned long int *, int);
     53static void *export_html(cucul_canvas_t *, unsigned long int *);
     54static void *export_html3(cucul_canvas_t *, unsigned long int *);
     55static void *export_irc(cucul_canvas_t *, unsigned long int *);
     56static void *export_ps(cucul_canvas_t *, unsigned long int *);
     57static void *export_svg(cucul_canvas_t *, unsigned long int *);
     58static void *export_tga(cucul_canvas_t *, unsigned long int *);
    5959
    6060/** \brief Export a canvas into a foreign format.
    6161 *
    6262 *  This function exports a libcucul canvas into various foreign formats such
    63  *  as ANSI art, HTML, IRC colours, etc. One should use cucul_get_buffer_data()
    64  *  and cucul_get_buffer_size() to access the buffer contents. The allocated
    65  *  data is valid until cucul_free_buffer() is called.
     63 *  as ANSI art, HTML, IRC colours, etc. The returned pointer should be passed
     64 *  to free() to release the allocated storage when it is no longer needed.
    6665 *
    6766 *  Valid values for \c format are:
     
    8281 *  \param cv A libcucul canvas
    8382 *  \param format A string describing the requested output format.
    84  *  \return A libcucul buffer, or NULL in case of error.
     83 *  \param bytes A pointer to an unsigned long integer where the number of
     84 *         allocated bytes will be written.
     85 *  \return A pointer to the exported memory area, or NULL in case of error.
    8586 */
    86 cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
    87 {
    88     cucul_buffer_t *ex;
    89     int ret = -1;
    90 
    91     ex = malloc(sizeof(cucul_buffer_t));
    92     if(!ex)
    93     {
     87void *cucul_export(cucul_canvas_t *cv, char const *format,
     88                   unsigned long int *bytes)
     89{
     90    if(!strcasecmp("caca", format))
     91        return export_caca(cv, bytes);
     92
     93    if(!strcasecmp("ansi", format))
     94        return export_ansi(cv, bytes);
     95
     96    if(!strcasecmp("utf8", format))
     97        return export_utf8(cv, bytes, 0);
     98
     99    if(!strcasecmp("utf8cr", format))
     100        return export_utf8(cv, bytes, 1);
     101
     102    if(!strcasecmp("html", format))
     103        return export_html(cv, bytes);
     104
     105    if(!strcasecmp("html3", format))
     106        return export_html3(cv, bytes);
     107
     108    if(!strcasecmp("irc", format))
     109        return export_irc(cv, bytes);
     110
     111    if(!strcasecmp("ps", format))
     112        return export_ps(cv, bytes);
     113
     114    if(!strcasecmp("svg", format))
     115        return export_svg(cv, bytes);
     116
     117    if(!strcasecmp("tga", format))
     118        return export_tga(cv, bytes);
     119
    94120#if defined(HAVE_ERRNO_H)
    95         errno = ENOMEM;
     121    errno = EINVAL;
    96122#endif
    97         return NULL;
    98     }
    99 
    100     ex->size = 0;
    101     ex->data = NULL;
    102     ex->user_data = 0;
    103 
    104     if(!strcasecmp("caca", format))
    105         ret = export_caca(cv, ex);
    106     else if(!strcasecmp("ansi", format))
    107         ret = export_ansi(cv, ex);
    108     else if(!strcasecmp("utf8", format))
    109         ret = export_utf8(cv, ex, 0);
    110     else if(!strcasecmp("utf8cr", format))
    111         ret = export_utf8(cv, ex, 1);
    112     else if(!strcasecmp("html", format))
    113         ret = export_html(cv, ex);
    114     else if(!strcasecmp("html3", format))
    115         ret = export_html3(cv, ex);
    116     else if(!strcasecmp("irc", format))
    117         ret = export_irc(cv, ex);
    118     else if(!strcasecmp("ps", format))
    119         ret = export_ps(cv, ex);
    120     else if(!strcasecmp("svg", format))
    121         ret = export_svg(cv, ex);
    122     else if(!strcasecmp("tga", format))
    123         ret = export_tga(cv, ex);
    124 
    125     if(ret < 0)
    126     {
    127         free(ex);
    128 #if defined(HAVE_ERRNO_H)
    129         errno = EINVAL;
    130 #endif
    131         return NULL;
    132     }
    133 
    134     return ex;
     123    return NULL;
    135124}
    136125
     
    171160
    172161/* Generate a native libcaca canvas file. */
    173 static int export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
     162static void *export_caca(cucul_canvas_t *cv, unsigned long int *bytes)
    174163{
    175164    uint32_t *attrs = cv->attrs;
    176165    uint32_t *chars = cv->chars;
    177     char *cur;
     166    char *data, *cur;
    178167    unsigned int n;
    179168
     
    183172     *  - 24 bytes for the frame info
    184173     * 8 bytes for each character cell */
    185     ex->size = 44 + 8 * cv->width * cv->height;
    186     ex->data = malloc(ex->size);
    187 
    188     cur = ex->data;
     174    *bytes = 44 + 8 * cv->width * cv->height;
     175    cur = data = malloc(*bytes);
    189176
    190177    /* magic */
     
    213200    }
    214201
    215     return 0;
     202    return data;
    216203}
    217204
     
    273260
    274261/* Generate UTF-8 representation of current canvas. */
    275 static int export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex, int cr)
     262static void *export_utf8(cucul_canvas_t *cv, unsigned long int *bytes, int cr)
    276263{
    277264    static uint8_t const palette[] =
     
    281268    };
    282269
    283     char *cur;
     270    char *data, *cur;
    284271    unsigned int x, y;
    285272
     
    287274     * 4 max bytes for a UTF-8 character).
    288275     * Add height*9 to that (zeroes color at the end and jump to next line) */
    289     ex->size = (cv->height * 9) + (cv->width * cv->height * 23);
    290     ex->data = malloc(ex->size);
    291 
    292     cur = ex->data;
     276    *bytes = (cv->height * 9) + (cv->width * cv->height * 23);
     277    cur = data = malloc(*bytes);
    293278
    294279    for(y = 0; y < cv->height; y++)
     
    345330
    346331    /* Crop to really used size */
    347     ex->size = (uintptr_t)(cur - ex->data);
    348     ex->data = realloc(ex->data, ex->size);
    349 
    350     return 0;
     332    *bytes = (uintptr_t)(cur - data);
     333    data = realloc(data, *bytes);
     334
     335    return data;
    351336}
    352337
    353338/* Generate ANSI representation of current canvas. */
    354 static int export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
     339static void *export_ansi(cucul_canvas_t *cv, unsigned long int *bytes)
    355340{
    356341    static uint8_t const palette[] =
     
    360345    };
    361346
    362     char *cur;
     347    char *data, *cur;
    363348    unsigned int x, y;
    364349
     
    369354     * 1 byte for a CP437 character).
    370355     * Add height*9 to that (zeroes color at the end and jump to next line) */
    371     ex->size = (cv->height * 9) + (cv->width * cv->height * 16);
    372     ex->data = malloc(ex->size);
    373 
    374     cur = ex->data;
     356    *bytes = (cv->height * 9) + (cv->width * cv->height * 16);
     357    cur = data = malloc(*bytes);
    375358
    376359    for(y = 0; y < cv->height; y++)
     
    423406
    424407    /* Crop to really used size */
    425     ex->size = (uintptr_t)(cur - ex->data);
    426     ex->data = realloc(ex->data, ex->size);
    427 
    428     return 0;
     408    *bytes = (uintptr_t)(cur - data);
     409    data = realloc(data, *bytes);
     410
     411    return data;
    429412}
    430413
    431414/* Generate HTML representation of current canvas. */
    432 static int export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
    433 {
    434     char *cur;
     415static void *export_html(cucul_canvas_t *cv, unsigned long int *bytes)
     416{
     417    char *data, *cur;
    435418    unsigned int x, y, len;
    436419
     
    441424     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
    442425     *          7 chars for "</span>" */
    443     ex->size = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
    444     ex->data = malloc(ex->size);
    445 
    446     cur = ex->data;
     426    *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
     427    cur = data = malloc(*bytes);
    447428
    448429    /* HTML header */
     
    499480
    500481    /* Crop to really used size */
    501     ex->size = strlen(ex->data) + 1;
    502     ex->data = realloc(ex->data, ex->size);
    503 
    504     return 0;
     482    *bytes = (uintptr_t)(cur - data);
     483    data = realloc(data, *bytes);
     484
     485    return data;
    505486}
    506487
     
    509490 * will not work under gecko (mozilla rendering engine) unless you set a
    510491 * correct header. */
    511 static int export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
    512 {
    513     char *cur;
     492static void *export_html3(cucul_canvas_t *cv, unsigned long int *bytes)
     493{
     494    char *data, *cur;
    514495    unsigned int x, y, len;
    515496
     
    520501     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
    521502     *          12 chars for "</font></td>" */
    522     ex->size = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
    523     ex->data = malloc(ex->size);
    524 
    525     cur = ex->data;
     503    *bytes = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
     504    cur = data = malloc(*bytes);
    526505
    527506    /* Table */
     
    594573
    595574    /* Crop to really used size */
    596     ex->size = (uintptr_t)(cur - ex->data);
    597     ex->data = realloc(ex->data, ex->size);
    598 
    599     return 0;
     575    *bytes = (uintptr_t)(cur - data);
     576    data = realloc(data, *bytes);
     577
     578    return data;
    600579}
    601580
    602581/* Export a text file with IRC colours */
    603 static int export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
     582static void *export_irc(cucul_canvas_t *cv, unsigned long int *bytes)
    604583{
    605584    static uint8_t const palette[] =
     
    609588    };
    610589
    611     char *cur;
     590    char *data, *cur;
    612591    unsigned int x, y;
    613592
     
    622601     */
    623602
    624     ex->size = 2 + cv->height * (3 + cv->width * 14);
    625     ex->data = malloc(ex->size);
    626 
    627     cur = ex->data;
     603    *bytes = 2 + cv->height * (3 + cv->width * 14);
     604    cur = data = malloc(*bytes);
    628605
    629606    for(y = 0; y < cv->height; y++)
     
    702679
    703680    /* Crop to really used size */
    704     ex->size = (uintptr_t)(cur - ex->data);
    705     ex->data = realloc(ex->data, ex->size);
    706 
    707     return 0;
     681    *bytes = (uintptr_t)(cur - data);
     682    data = realloc(data, *bytes);
     683
     684    return data;
    708685}
    709686
    710687/* Export a PostScript document. */
    711 static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
     688static void *export_ps(cucul_canvas_t *cv, unsigned long int *bytes)
    712689{
    713690    static char const *ps_header =
     
    736713        "6 10 scale\n";
    737714
    738     char *cur;
     715    char *data, *cur;
    739716    unsigned int x, y;
    740717
    741718    /* 200 is arbitrary but should be ok */
    742     ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
    743     ex->data = malloc(ex->size);
    744 
    745     cur = ex->data;
     719    *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
     720    cur = data = malloc(*bytes);
    746721
    747722    /* Header */
     
    811786
    812787    /* Crop to really used size */
    813     ex->size = (uintptr_t)(cur - ex->data);
    814     ex->data = realloc(ex->data, ex->size);
    815 
    816     return 0;
     788    *bytes = (uintptr_t)(cur - data);
     789    data = realloc(data, *bytes);
     790
     791    return data;
    817792}
    818793
    819794/* Export an SVG vector image */
    820 static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
     795static void *export_svg(cucul_canvas_t *cv, unsigned long int *bytes)
    821796{
    822797    static char const svg_header[] =
     
    827802        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
    828803
    829     char *cur;
     804    char *data, *cur;
    830805    unsigned int x, y;
    831806
    832807    /* 200 is arbitrary but should be ok */
    833     ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
    834     ex->data = malloc(ex->size);
    835 
    836     cur = ex->data;
     808    *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
     809    cur = data = malloc(*bytes);
    837810
    838811    /* Header */
     
    897870
    898871    /* Crop to really used size */
    899     ex->size = (uintptr_t)(cur - ex->data);
    900     ex->data = realloc(ex->data, ex->size);
    901 
    902     return 0;
     872    *bytes = (uintptr_t)(cur - data);
     873    data = realloc(data, *bytes);
     874
     875    return data;
    903876}
    904877
    905878/* Export a TGA image */
    906 static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
     879static void *export_tga(cucul_canvas_t *cv, unsigned long int *bytes)
    907880{
    908881    char const * const *fontlist;
    909     char * cur;
     882    char *data, *cur;
    910883    cucul_font_t *f;
    911884    unsigned int i, w, h;
     
    913886    fontlist = cucul_get_font_list();
    914887    if(!fontlist[0])
    915         return -1;
     888    {
     889#if defined(HAVE_ERRNO_H)
     890        errno = EINVAL;
     891#endif
     892        return NULL;
     893    }
    916894
    917895    f = cucul_load_font(fontlist[0], 0);
     
    920898    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
    921899
    922     ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
    923     ex->data = malloc(ex->size);
    924 
    925     cur = ex->data;
     900    *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
     901    cur = data = malloc(*bytes);
    926902
    927903    /* ID Length */
     
    958934    cucul_free_font(f);
    959935
    960     return 0;
    961 }
    962 
     936    return data;
     937}
     938
  • 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
  • libcaca/trunk/cucul/legacy.c

    r1301 r1302  
    1313
    1414/*
    15  *  This file contains buffer handling functions.
     15 *  This file contains legacy functions that we keep around until all
     16 *  applications are ported.
    1617 */
    1718
     
    2021
    2122#if !defined(__KERNEL__)
     23#   if defined(HAVE_ERRNO_H)
     24#       include <errno.h>
     25#   endif
    2226#   include <stdio.h>
    2327#   include <stdlib.h>
     
    2832#include "cucul_internals.h"
    2933
    30 /** \brief Load a memory area into a buffer.
    31  *
    32  *  Create a \e libcucul buffer that points to the given memory area. The
    33  *  data is not duplicated and any changes made to the original memory area
    34  *  will appear in the buffer.
    35  *
    36  *  Keep in mind that buffers are not strings. When loading a C string, the
    37  *  terminating '\\0' must not be part of the buffer, hence \e size should
    38  *  be computed with strlen(). Conversely, the '\\0' is not appended to
    39  *  exported buffers even when they could be parsed as strings.
    40  *
    41  *  \param data The memory area to load.
    42  *  \param size The size of the memory area.
    43  *  \return A \e libcucul buffer pointing to the memory area, or NULL
    44  *          if an error occurred.
     34/*
     35 * Functions from color.c
    4536 */
     37
     38int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
     39{
     40    return cucul_set_color_ansi(cv, fg, bg);
     41}
     42
     43int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
     44{
     45    return cucul_set_color_argb(cv, fg, bg);
     46}
     47
     48/*
     49 * Functions from import.c
     50 */
     51
     52cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buf, char const *format)
     53{
     54    cucul_canvas_t *cv = cucul_create_canvas(0, 0);
     55    int ret = cucul_import(cv, (unsigned char const *)buf->data,
     56                           buf->size, format);
     57    if(ret < 0)
     58    {
     59        cucul_free_canvas(cv);
     60        return NULL;
     61    }
     62
     63    return cv;
     64}
     65
     66/*
     67 * Functions from export.c
     68 */
     69
     70cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
     71{
     72    cucul_buffer_t *ex;
     73
     74    ex = malloc(sizeof(cucul_buffer_t));
     75    if(!ex)
     76    {
     77#if defined(HAVE_ERRNO_H)
     78        errno = ENOMEM;
     79#endif
     80        return NULL;
     81    }
     82
     83    ex->data = cucul_export(cv, format, &ex->size);
     84    if(!ex->data)
     85    {
     86        free(ex);
     87        return NULL;
     88    }
     89
     90    ex->user_data = 0;
     91
     92    return ex;
     93}
     94
     95/*
     96 * Functions from buffer.c
     97 */
     98
    4699cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
    47100{
     
    59112}
    60113
    61 /** \brief Load a file into a buffer.
    62  *
    63  *  Load a file into memory and returns a \e libcucul buffer for use with
    64  *  other functions.
    65  *
    66  *  \param file The filename
    67  *  \return A \e libcucul buffer containing the file's contents, or NULL
    68  *          if an error occurred.
    69  */
    70114#if !defined(__KERNEL__)
    71115cucul_buffer_t *cucul_load_file(char const *file)
     
    106150}
    107151#endif
    108 /** \brief Get the buffer size.
    109  *
    110  *  Return the length (in bytes) of the memory area stored in the given
    111  *  \e libcucul buffer.
    112  *
    113  *  This function never fails.
    114  *
    115  *  \param buf A \e libcucul buffer
    116  *  \return The buffer data length.
    117  */
     152
    118153unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
    119154{
     
    121156}
    122157
    123 /** \brief Get the buffer data.
    124  *
    125  *  Get a pointer to the memory area stored in the given
    126  *  \e libcucul buffer.
    127  *
    128  *  This function never fails.
    129  *
    130  *  \param buf A \e libcucul buffer
    131  *  \return A pointer to the buffer memory area.
    132  */
    133158void * cucul_get_buffer_data(cucul_buffer_t *buf)
    134159{
     
    136161}
    137162
    138 /** \brief Free a buffer.
    139  *
    140  *  Free the structures associated with the given \e libcucul buffer.
    141  *
    142  *  This function never fails.
    143  *
    144  *  \param buf A \e libcucul buffer
    145  *  \return This function always returns 0.
    146  */
    147163int cucul_free_buffer(cucul_buffer_t *buf)
    148164{
Note: See TracChangeset for help on using the changeset viewer.