Changeset 896


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.
Location:
libcaca/trunk
Files:
10 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
  • libcaca/trunk/cucul/cucul.h

    r874 r896  
    8585 *
    8686 *  @{ */
     87cucul_buffer_t *cucul_load_memory(void *, unsigned long int);
     88cucul_buffer_t *cucul_load_file(char const *);
    8789unsigned long int cucul_get_buffer_size(cucul_buffer_t *);
    8890void * cucul_get_buffer_data(cucul_buffer_t *);
     
    207209cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *, char const *);
    208210char const * const * cucul_get_export_list(void);
    209 cucul_canvas_t * cucul_import_canvas(void const *, unsigned int, char const *);
     211cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *, char const *);
    210212char const * const * cucul_get_import_list(void);
    211213/*  @} */
  • libcaca/trunk/cucul/cucul_internals.h

    r874 r896  
    4444    unsigned long int size;
    4545    char *data;
     46    int user_data;
    4647};
    4748
  • libcaca/trunk/cucul/export.c

    r859 r896  
    7474    ex->size = 0;
    7575    ex->data = NULL;
     76    ex->user_data = 0;
    7677
    7778    if(!strcasecmp("caca", format))
  • libcaca/trunk/cucul/import.c

    r895 r896  
    4444 *  \li \c "caca": import native libcaca files.
    4545 *
    46  *  \param data The memory area to be loaded into a canvas.
     46 *  \param buffer A \e libcucul buffer containing the data to be loaded
     47 *         into a canvas.
    4748 *  \param size The length of the memory area.
    4849 *  \param format A string describing the input format.
    4950 *  \return A libcucul canvas, or NULL in case of error.
    5051 */
    51 cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
    52                                      char const *format)
    53 {
    54     char const *buf = (char const*) data;
    55 
    56     if(size==0 || data==NULL)
     52cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *b, char const *format)
     53{
     54    char const *buf = (char const*)b->data;
     55
     56    if(b->size == 0 || b->data == NULL)
    5757        return NULL;
    5858
    5959    if(!strcasecmp("caca", format))
    60         return import_caca(data, size);
     60        return import_caca(b->data, b->size);
    6161    if(!strcasecmp("text", format))
    62         return import_text(data, size);
     62        return import_text(b->data, b->size);
    6363    if(!strcasecmp("ansi", format))
    64         return import_ansi(data, size);
     64        return import_ansi(b->data, b->size);
    6565
    6666    /* Autodetection */
     
    6969        unsigned int i=0;
    7070        /* if 4 first letters are CACA */
    71         if(size >= 4 &&
     71        if(b->size >= 4 &&
    7272            buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
    73             return import_caca(data, size);
     73            return import_caca(b->data, b->size);
    7474
    7575        /* If we find ESC[ argv, we guess it's an ANSI file */
    76         while(i<size-1)
     76        while(i < b->size - 1)
    7777        {
    7878            if((buf[i] == 0x1b) && (buf[i+1] == '['))
    79                 return import_ansi(data, size);
     79                return import_ansi(b->data, b->size);
    8080            i++;
    8181        }
    8282
    8383        /* Otherwise, import it as text */
    84         return import_text(data, size);
     84        return import_text(b->data, b->size);
    8585    }
    8686    return NULL;
  • libcaca/trunk/src/cacaplay.c

    r859 r896  
    1616
    1717#include <stdio.h>
    18 #include <sys/types.h>
    19 #include <sys/stat.h>
    20 #include <fcntl.h>
    21 #include <unistd.h>
    2218#include <stdlib.h>
    2319
     
    2723int main(int argc, char **argv)
    2824{
    29     struct stat statbuf;
    3025    caca_event_t ev;
     26    cucul_buffer_t *b;
    3127    cucul_canvas_t *cv;
    3228    caca_display_t *dp;
    33     void *buffer;
    34     int fd;
    3529
    3630    if(argc < 2)
     
    4034    }
    4135
    42     fd = open(argv[1], O_RDONLY);
    43     if(!fd)
     36    b = cucul_load_file(argv[1]);
     37    if(!b)
    4438    {
    4539        fprintf(stderr, "%s: could not open %s.\n", argv[0], argv[1]);
     
    4741    }
    4842
    49     if(fstat(fd, &statbuf))
     43    cv = cucul_import_canvas(b, "caca");
     44    if(!cv)
    5045    {
    51         fprintf(stderr, "%s: could not stat %s.\n", argv[0], argv[1]);
     46        fprintf(stderr, "%s: invalid caca file %s.\n", argv[0], argv[1]);
     47        cucul_free_buffer(b);
    5248        return 1;
    5349    }
    5450
    55     buffer = malloc(statbuf.st_size);
    56     read(fd, buffer, statbuf.st_size);
    57     cv = cucul_import_canvas(buffer, statbuf.st_size, "caca");
    58     free(buffer);
    59 
    60     if(!cv)
    61     {
    62         fprintf(stderr, "%s: invalid caca file %s.\n", argv[0], argv[1]);
    63         return 1;
    64     }
     51    cucul_free_buffer(b);
    6552
    6653    dp = caca_create_display(cv);
  • libcaca/trunk/src/cacaserver.c

    r866 r896  
    205205    for(;;)
    206206    {
     207        cucul_buffer_t *b;
    207208        uint8_t *buf = server->input;
    208209        uint32_t width, height;
     
    235236            cucul_free_canvas(server->canvas);
    236237
    237         server->canvas = cucul_import_canvas(buf, size, "caca");
     238        b = cucul_load_memory(buf, size);
     239        server->canvas = cucul_import_canvas(b, "caca");
     240        cucul_free_buffer(b);
    238241
    239242        if(!server->canvas)
  • libcaca/trunk/test

    • Property svn:ignore
      •  

        old new  
        1212gamma
        1313hsv
         14import
        1415spritedit
        1516text
  • libcaca/trunk/test/import.c

    r873 r896  
    2828{
    2929    cucul_canvas_t *cv;
     30    cucul_buffer_t *b;
    3031    caca_display_t *dp;
    31     FILE *fp;
    32     unsigned char *buffer;
    33     int size=0;
    3432
    3533    if(argc < 2)
     
    3937    }
    4038
    41     fp = fopen(argv[1], "rb");
    42 
    43     if(!fp)
     39    b = cucul_load_file(argv[1]);
     40    if(!b)
    4441    {
    4542        fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
    4643        return 1;
    4744    }
    48     fseek(fp, 0, SEEK_END);
    49     size = ftell(fp);
    50     fseek(fp, 0, SEEK_SET);
    51     buffer = malloc(sizeof(unsigned char) * size);
    52     if(!buffer)
    53     {
    54         fprintf(stderr, "%s: Can't allocate memory (%d bytes)\n", argv[0], size);
    55         return 1;
    56     }
    5745
    58     if(!fread(buffer, size, 1, fp))
    59     {
    60         fprintf(stderr, "%s: Can't read %s\n", argv[0], argv[1]);
    61         return 1;
    62     }
    63 
    64     cv = cucul_import_canvas(buffer, size, "");
    65 
    66     if(cv == NULL)
     46    cv = cucul_import_canvas(b, "");
     47    if(!cv)
    6748    {
    6849        fprintf(stderr, "%s: Can't load %s, unknow reason.\n", argv[0], argv[1]);
    6950        return 1;
    7051    }
     52
     53    cucul_free_buffer(b);
    7154
    7255    dp = caca_create_display(cv);
  • libcaca/trunk/test/text.c

    r858 r896  
    3737    cucul_buffer_t *buffer;
    3838
    39     cv = cucul_import_canvas(STRING, strlen(STRING), "text");
     39    buffer = cucul_load_memory(STRING, strlen(STRING));
     40    cv = cucul_import_canvas(buffer, "text");
     41    cucul_free_buffer(buffer);
    4042
    4143    buffer = cucul_export_canvas(cv, "ansi");
Note: See TracChangeset for help on using the changeset viewer.