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.tags/v0.99.beta14
@@ -27,6 +27,78 @@ | |||
#include "cucul.h" | |||
#include "cucul_internals.h" | |||
/** \brief Load a memory area into a buffer. | |||
* | |||
* This function creates a \e libcucul buffer that points to the given | |||
* memory area. The data is not duplicated and any changes made to the | |||
* original memory area appear in the buffer. | |||
* | |||
* \param data The memory area to load. | |||
* \param size The size of the memory area. | |||
* \return A \e libcucul buffer pointing to the memory area, or NULL | |||
* if an error occurred. | |||
*/ | |||
cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size) | |||
{ | |||
cucul_buffer_t *buf; | |||
buf = malloc(sizeof(cucul_buffer_t)); | |||
if(!buf) | |||
return NULL; | |||
buf->data = data; | |||
buf->size = size; | |||
buf->user_data = 1; | |||
return buf; | |||
} | |||
/** \brief Load a file into a buffer. | |||
* | |||
* This function loads a file into memory and returns a \e libcucul buffer | |||
* for use with other functions. | |||
* | |||
* \param file The filename | |||
* \return A \e libcucul buffer containing the file's contents, or NULL | |||
* if an error occurred. | |||
*/ | |||
cucul_buffer_t *cucul_load_file(char const *file) | |||
{ | |||
cucul_buffer_t *buf; | |||
FILE *fp; | |||
long int size; | |||
fp = fopen(file, "rb"); | |||
if(!fp) | |||
return NULL; | |||
buf = malloc(sizeof(cucul_buffer_t)); | |||
if(!buf) | |||
{ | |||
fclose(fp); | |||
return NULL; | |||
} | |||
fseek(fp, 0, SEEK_END); | |||
size = ftell(fp); | |||
buf->data = malloc(size); | |||
if(!buf->data) | |||
{ | |||
free(buf); | |||
fclose(fp); | |||
return NULL; | |||
} | |||
buf->size = size; | |||
buf->user_data = 0; | |||
fseek(fp, 0, SEEK_SET); | |||
fread(buf->data, buf->size, 1, fp); | |||
fclose(fp); | |||
return buf; | |||
} | |||
/** \brief Get the buffer size. | |||
* | |||
* This function returns the length (in bytes) of the memory area stored | |||
@@ -69,7 +141,9 @@ void * cucul_get_buffer_data(cucul_buffer_t *buf) | |||
*/ | |||
int cucul_free_buffer(cucul_buffer_t *buf) | |||
{ | |||
free(buf->data); | |||
if(!buf->user_data) | |||
free(buf->data); | |||
free(buf); | |||
return 0; | |||
@@ -84,6 +84,8 @@ int cucul_rand(int, int); | |||
* These functions provide methods to handle libcucul buffers. | |||
* | |||
* @{ */ | |||
cucul_buffer_t *cucul_load_memory(void *, unsigned long int); | |||
cucul_buffer_t *cucul_load_file(char const *); | |||
unsigned long int cucul_get_buffer_size(cucul_buffer_t *); | |||
void * cucul_get_buffer_data(cucul_buffer_t *); | |||
int cucul_free_buffer(cucul_buffer_t *); | |||
@@ -206,7 +208,7 @@ void cucul_free_font(cucul_font_t *); | |||
* @{ */ | |||
cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *, char const *); | |||
char const * const * cucul_get_export_list(void); | |||
cucul_canvas_t * cucul_import_canvas(void const *, unsigned int, char const *); | |||
cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *, char const *); | |||
char const * const * cucul_get_import_list(void); | |||
/* @} */ | |||
@@ -43,6 +43,7 @@ struct cucul_buffer | |||
{ | |||
unsigned long int size; | |||
char *data; | |||
int user_data; | |||
}; | |||
/* Bitmap functions */ | |||
@@ -73,6 +73,7 @@ cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format) | |||
ex = malloc(sizeof(cucul_buffer_t)); | |||
ex->size = 0; | |||
ex->data = NULL; | |||
ex->user_data = 0; | |||
if(!strcasecmp("caca", format)) | |||
export_caca(cv, ex); | |||
@@ -43,45 +43,45 @@ static cucul_canvas_t *import_ansi(void const *, unsigned int); | |||
* | |||
* \li \c "caca": import native libcaca files. | |||
* | |||
* \param data The memory area to be loaded into a canvas. | |||
* \param buffer A \e libcucul buffer containing the data to be loaded | |||
* into a canvas. | |||
* \param size The length of the memory area. | |||
* \param format A string describing the input format. | |||
* \return A libcucul canvas, or NULL in case of error. | |||
*/ | |||
cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size, | |||
char const *format) | |||
cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *b, char const *format) | |||
{ | |||
char const *buf = (char const*) data; | |||
char const *buf = (char const*)b->data; | |||
if(size==0 || data==NULL) | |||
if(b->size == 0 || b->data == NULL) | |||
return NULL; | |||
if(!strcasecmp("caca", format)) | |||
return import_caca(data, size); | |||
return import_caca(b->data, b->size); | |||
if(!strcasecmp("text", format)) | |||
return import_text(data, size); | |||
return import_text(b->data, b->size); | |||
if(!strcasecmp("ansi", format)) | |||
return import_ansi(data, size); | |||
return import_ansi(b->data, b->size); | |||
/* Autodetection */ | |||
if(!strcasecmp("", format)) | |||
{ | |||
unsigned int i=0; | |||
/* if 4 first letters are CACA */ | |||
if(size >= 4 && | |||
if(b->size >= 4 && | |||
buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') | |||
return import_caca(data, size); | |||
return import_caca(b->data, b->size); | |||
/* If we find ESC[ argv, we guess it's an ANSI file */ | |||
while(i<size-1) | |||
while(i < b->size - 1) | |||
{ | |||
if((buf[i] == 0x1b) && (buf[i+1] == '[')) | |||
return import_ansi(data, size); | |||
return import_ansi(b->data, b->size); | |||
i++; | |||
} | |||
/* Otherwise, import it as text */ | |||
return import_text(data, size); | |||
return import_text(b->data, b->size); | |||
} | |||
return NULL; | |||
} | |||
@@ -15,10 +15,6 @@ | |||
#include "common.h" | |||
#include <stdio.h> | |||
#include <sys/types.h> | |||
#include <sys/stat.h> | |||
#include <fcntl.h> | |||
#include <unistd.h> | |||
#include <stdlib.h> | |||
#include "cucul.h" | |||
@@ -26,12 +22,10 @@ | |||
int main(int argc, char **argv) | |||
{ | |||
struct stat statbuf; | |||
caca_event_t ev; | |||
cucul_buffer_t *b; | |||
cucul_canvas_t *cv; | |||
caca_display_t *dp; | |||
void *buffer; | |||
int fd; | |||
if(argc < 2) | |||
{ | |||
@@ -39,30 +33,23 @@ int main(int argc, char **argv) | |||
return 1; | |||
} | |||
fd = open(argv[1], O_RDONLY); | |||
if(!fd) | |||
b = cucul_load_file(argv[1]); | |||
if(!b) | |||
{ | |||
fprintf(stderr, "%s: could not open %s.\n", argv[0], argv[1]); | |||
return 1; | |||
} | |||
if(fstat(fd, &statbuf)) | |||
{ | |||
fprintf(stderr, "%s: could not stat %s.\n", argv[0], argv[1]); | |||
return 1; | |||
} | |||
buffer = malloc(statbuf.st_size); | |||
read(fd, buffer, statbuf.st_size); | |||
cv = cucul_import_canvas(buffer, statbuf.st_size, "caca"); | |||
free(buffer); | |||
cv = cucul_import_canvas(b, "caca"); | |||
if(!cv) | |||
{ | |||
fprintf(stderr, "%s: invalid caca file %s.\n", argv[0], argv[1]); | |||
cucul_free_buffer(b); | |||
return 1; | |||
} | |||
cucul_free_buffer(b); | |||
dp = caca_create_display(cv); | |||
caca_refresh_display(dp); | |||
@@ -204,6 +204,7 @@ int main(void) | |||
/* Main loop */ | |||
for(;;) | |||
{ | |||
cucul_buffer_t *b; | |||
uint8_t *buf = server->input; | |||
uint32_t width, height; | |||
unsigned int size; | |||
@@ -234,7 +235,9 @@ int main(void) | |||
if(server->canvas) | |||
cucul_free_canvas(server->canvas); | |||
server->canvas = cucul_import_canvas(buf, size, "caca"); | |||
b = cucul_load_memory(buf, size); | |||
server->canvas = cucul_import_canvas(b, "caca"); | |||
cucul_free_buffer(b); | |||
if(!server->canvas) | |||
continue; /* Load error */ | |||
@@ -27,10 +27,8 @@ | |||
int main(int argc, char *argv[]) | |||
{ | |||
cucul_canvas_t *cv; | |||
cucul_buffer_t *b; | |||
caca_display_t *dp; | |||
FILE *fp; | |||
unsigned char *buffer; | |||
int size=0; | |||
if(argc < 2) | |||
{ | |||
@@ -38,37 +36,22 @@ int main(int argc, char *argv[]) | |||
return 1; | |||
} | |||
fp = fopen(argv[1], "rb"); | |||
if(!fp) | |||
b = cucul_load_file(argv[1]); | |||
if(!b) | |||
{ | |||
fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); | |||
return 1; | |||
} | |||
fseek(fp, 0, SEEK_END); | |||
size = ftell(fp); | |||
fseek(fp, 0, SEEK_SET); | |||
buffer = malloc(sizeof(unsigned char) * size); | |||
if(!buffer) | |||
{ | |||
fprintf(stderr, "%s: Can't allocate memory (%d bytes)\n", argv[0], size); | |||
return 1; | |||
} | |||
if(!fread(buffer, size, 1, fp)) | |||
{ | |||
fprintf(stderr, "%s: Can't read %s\n", argv[0], argv[1]); | |||
return 1; | |||
} | |||
cv = cucul_import_canvas(buffer, size, ""); | |||
if(cv == NULL) | |||
cv = cucul_import_canvas(b, ""); | |||
if(!cv) | |||
{ | |||
fprintf(stderr, "%s: Can't load %s, unknow reason.\n", argv[0], argv[1]); | |||
return 1; | |||
} | |||
cucul_free_buffer(b); | |||
dp = caca_create_display(cv); | |||
caca_refresh_display(dp); | |||
@@ -36,7 +36,9 @@ int main(void) | |||
cucul_canvas_t *cv; | |||
cucul_buffer_t *buffer; | |||
cv = cucul_import_canvas(STRING, strlen(STRING), "text"); | |||
buffer = cucul_load_memory(STRING, strlen(STRING)); | |||
cv = cucul_import_canvas(buffer, "text"); | |||
cucul_free_buffer(buffer); | |||
buffer = cucul_export_canvas(cv, "ansi"); | |||
fwrite(cucul_get_buffer_data(buffer), | |||