Ignore:
Timestamp:
Nov 8, 2006, 5:34:43 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Renamed cucul_import() to cucul_import_memory() and implemented cucul_import_file().
File:
1 edited

Legend:

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

    r1303 r1305  
    1919#include "common.h"
    2020
    21 #if !defined(__KERNEL__)
    22 #   if defined(HAVE_ERRNO_H)
     21#if !defined __KERNEL__
     22#   if defined HAVE_ERRNO_H
    2323#       include <errno.h>
    2424#   endif
     
    8383 *  \return The number of bytes read, or -1 if an error occurred.
    8484 */
    85 long int cucul_import(cucul_canvas_t *cv, unsigned char const *buf,
    86                       unsigned long int len, char const *format)
     85long int cucul_import_memory(cucul_canvas_t *cv, unsigned char const *buf,
     86                             unsigned long int len, char const *format)
    8787{
    8888    if(!strcasecmp("caca", format))
     
    114114    }
    115115
    116 #if defined(HAVE_ERRNO_H)
     116#if defined HAVE_ERRNO_H
    117117    errno = EINVAL;
    118118#endif
     
    120120}
    121121
     122/** \brief Import a file into a canvas
     123 *
     124 *  Import a file into the given libcucul canvas's current frame. The
     125 *  current frame is resized accordingly and its contents are replaced
     126 *  with the imported data.
     127 *
     128 *  Valid values for \c format are:
     129 *  - \c "": attempt to autodetect the file format.
     130 *  - \c "text": import ASCII text files.
     131 *  - \c "ansi": import ANSI files.
     132 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
     133 *  - \c "caca": import native libcaca files.
     134 *
     135 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     136 *  - \c ENOSYS File access is not implemented on this system.
     137 *  - \c ENOMEM Not enough memory to allocate canvas.
     138 *  - \c EINVAL Invalid format requested.
     139 *  cucul_import_file() may also fail and set \b errno for any of the
     140 *  errors specified for the routine fopen().
     141 *
     142 *  \param A libcucul canvas in which to import the file.
     143 *  \param filename The name of the file to load.
     144 *  \param format A string describing the input format.
     145 *  \return The number of bytes read, or -1 if an error occurred.
     146 */
     147long int cucul_import_file(cucul_canvas_t *cv, char const *filename,
     148                           char const *format)
     149{
     150#if defined __KERNEL__
     151#   if defined HAVE_ERRNO_H
     152    errno = ENOSYS;
     153#   endif
     154    return -1;
     155#else
     156    FILE *fp;
     157    void *data;
     158    long int size;
     159    int ret;
     160
     161    fp = fopen(filename, "rb");
     162    if(!fp)
     163        return -1; /* fopen already set errno */
     164
     165    fseek(fp, 0, SEEK_END);
     166    size = ftell(fp);
     167
     168    data = malloc(size);
     169    if(!data)
     170    {
     171        fclose(fp);
     172#   if defined HAVE_ERRNO_H
     173        errno = ENOMEM;
     174#   endif
     175        return -1;
     176    }
     177
     178    fseek(fp, 0, SEEK_SET);
     179    fread(data, size, 1, fp);
     180    fclose(fp);
     181
     182    ret = cucul_import_memory(cv, data, size, format);
     183    free(data);
     184
     185    return ret;
     186#endif
     187}
     188   
    122189/** \brief Get available import formats
    123190 *
     
    212279
    213280invalid_caca:
    214 #if defined(HAVE_ERRNO_H)
     281#if defined HAVE_ERRNO_H
    215282    errno = EINVAL;
    216283#endif
Note: See TracChangeset for help on using the changeset viewer.