Ignore:
Timestamp:
Apr 26, 2006, 1:54:26 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Implemented cucul_load_file() and cucul_load_memory() to load respectively a file and a memory area into a libcucul buffer.
  • Changed the cucul_import_canvas() prototype so that it uses libcucul buffers instead of simple memory areas.
File:
1 edited

Legend:

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

    r870 r896  
    2727#include "cucul.h"
    2828#include "cucul_internals.h"
     29
     30/** \brief Load a memory area into a buffer.
     31 *
     32 *  This function creates a \e libcucul buffer that points to the given
     33 *  memory area. The data is not duplicated and any changes made to the
     34 *  original memory area appear in the buffer.
     35 *
     36 *  \param data The memory area to load.
     37 *  \param size The size of the memory area.
     38 *  \return A \e libcucul buffer pointing to the memory area, or NULL
     39 *          if an error occurred.
     40 */
     41cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
     42{
     43    cucul_buffer_t *buf;
     44
     45    buf = malloc(sizeof(cucul_buffer_t));
     46    if(!buf)
     47        return NULL;
     48
     49    buf->data = data;
     50    buf->size = size;
     51    buf->user_data = 1;
     52
     53    return buf;
     54}
     55
     56/** \brief Load a file into a buffer.
     57 *
     58 *  This function loads a file into memory and returns a \e libcucul buffer
     59 *  for use with other functions.
     60 *
     61 *  \param file The filename
     62 *  \return A \e libcucul buffer containing the file's contents, or NULL
     63 *          if an error occurred.
     64 */
     65cucul_buffer_t *cucul_load_file(char const *file)
     66{
     67    cucul_buffer_t *buf;
     68    FILE *fp;
     69    long int size;
     70
     71    fp = fopen(file, "rb");
     72    if(!fp)
     73        return NULL;
     74
     75    buf = malloc(sizeof(cucul_buffer_t));
     76    if(!buf)
     77    {
     78        fclose(fp);
     79        return NULL;
     80    }
     81
     82    fseek(fp, 0, SEEK_END);
     83    size = ftell(fp);
     84
     85    buf->data = malloc(size);
     86    if(!buf->data)
     87    {
     88        free(buf);
     89        fclose(fp);
     90        return NULL;
     91    }
     92    buf->size = size;
     93    buf->user_data = 0;
     94
     95    fseek(fp, 0, SEEK_SET);
     96    fread(buf->data, buf->size, 1, fp);
     97    fclose(fp);
     98
     99    return buf;
     100}
    29101
    30102/** \brief Get the buffer size.
     
    70142int cucul_free_buffer(cucul_buffer_t *buf)
    71143{
    72     free(buf->data);
     144    if(!buf->user_data)
     145        free(buf->data);
     146
    73147    free(buf);
    74148
Note: See TracChangeset for help on using the changeset viewer.