- Timestamp:
- Apr 26, 2006, 1:54:26 PM (15 years ago)
- Location:
- libcaca/trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/cucul/buffer.c
r870 r896 27 27 #include "cucul.h" 28 28 #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 */ 41 cucul_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 */ 65 cucul_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 } 29 101 30 102 /** \brief Get the buffer size. … … 70 142 int cucul_free_buffer(cucul_buffer_t *buf) 71 143 { 72 free(buf->data); 144 if(!buf->user_data) 145 free(buf->data); 146 73 147 free(buf); 74 148 -
libcaca/trunk/cucul/cucul.h
r874 r896 85 85 * 86 86 * @{ */ 87 cucul_buffer_t *cucul_load_memory(void *, unsigned long int); 88 cucul_buffer_t *cucul_load_file(char const *); 87 89 unsigned long int cucul_get_buffer_size(cucul_buffer_t *); 88 90 void * cucul_get_buffer_data(cucul_buffer_t *); … … 207 209 cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *, char const *); 208 210 char const * const * cucul_get_export_list(void); 209 cucul_canvas_t * cucul_import_canvas( void const *, unsigned int, char const *);211 cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *, char const *); 210 212 char const * const * cucul_get_import_list(void); 211 213 /* @} */ -
libcaca/trunk/cucul/cucul_internals.h
r874 r896 44 44 unsigned long int size; 45 45 char *data; 46 int user_data; 46 47 }; 47 48 -
libcaca/trunk/cucul/export.c
r859 r896 74 74 ex->size = 0; 75 75 ex->data = NULL; 76 ex->user_data = 0; 76 77 77 78 if(!strcasecmp("caca", format)) -
libcaca/trunk/cucul/import.c
r895 r896 44 44 * \li \c "caca": import native libcaca files. 45 45 * 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. 47 48 * \param size The length of the memory area. 48 49 * \param format A string describing the input format. 49 50 * \return A libcucul canvas, or NULL in case of error. 50 51 */ 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) 52 cucul_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) 57 57 return NULL; 58 58 59 59 if(!strcasecmp("caca", format)) 60 return import_caca( data,size);60 return import_caca(b->data, b->size); 61 61 if(!strcasecmp("text", format)) 62 return import_text( data,size);62 return import_text(b->data, b->size); 63 63 if(!strcasecmp("ansi", format)) 64 return import_ansi( data,size);64 return import_ansi(b->data, b->size); 65 65 66 66 /* Autodetection */ … … 69 69 unsigned int i=0; 70 70 /* if 4 first letters are CACA */ 71 if( size >= 4 &&71 if(b->size >= 4 && 72 72 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); 74 74 75 75 /* If we find ESC[ argv, we guess it's an ANSI file */ 76 while(i <size-1)76 while(i < b->size - 1) 77 77 { 78 78 if((buf[i] == 0x1b) && (buf[i+1] == '[')) 79 return import_ansi( data,size);79 return import_ansi(b->data, b->size); 80 80 i++; 81 81 } 82 82 83 83 /* Otherwise, import it as text */ 84 return import_text( data,size);84 return import_text(b->data, b->size); 85 85 } 86 86 return NULL; -
libcaca/trunk/src/cacaplay.c
r859 r896 16 16 17 17 #include <stdio.h> 18 #include <sys/types.h>19 #include <sys/stat.h>20 #include <fcntl.h>21 #include <unistd.h>22 18 #include <stdlib.h> 23 19 … … 27 23 int main(int argc, char **argv) 28 24 { 29 struct stat statbuf;30 25 caca_event_t ev; 26 cucul_buffer_t *b; 31 27 cucul_canvas_t *cv; 32 28 caca_display_t *dp; 33 void *buffer;34 int fd;35 29 36 30 if(argc < 2) … … 40 34 } 41 35 42 fd = open(argv[1], O_RDONLY);43 if(! fd)36 b = cucul_load_file(argv[1]); 37 if(!b) 44 38 { 45 39 fprintf(stderr, "%s: could not open %s.\n", argv[0], argv[1]); … … 47 41 } 48 42 49 if(fstat(fd, &statbuf)) 43 cv = cucul_import_canvas(b, "caca"); 44 if(!cv) 50 45 { 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); 52 48 return 1; 53 49 } 54 50 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); 65 52 66 53 dp = caca_create_display(cv); -
libcaca/trunk/src/cacaserver.c
r866 r896 205 205 for(;;) 206 206 { 207 cucul_buffer_t *b; 207 208 uint8_t *buf = server->input; 208 209 uint32_t width, height; … … 235 236 cucul_free_canvas(server->canvas); 236 237 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); 238 241 239 242 if(!server->canvas) -
libcaca/trunk/test
- Property svn:ignore
-
old new 12 12 gamma 13 13 hsv 14 import 14 15 spritedit 15 16 text
-
- Property svn:ignore
-
libcaca/trunk/test/import.c
r873 r896 28 28 { 29 29 cucul_canvas_t *cv; 30 cucul_buffer_t *b; 30 31 caca_display_t *dp; 31 FILE *fp;32 unsigned char *buffer;33 int size=0;34 32 35 33 if(argc < 2) … … 39 37 } 40 38 41 fp = fopen(argv[1], "rb"); 42 43 if(!fp) 39 b = cucul_load_file(argv[1]); 40 if(!b) 44 41 { 45 42 fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); 46 43 return 1; 47 44 } 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 }57 45 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) 67 48 { 68 49 fprintf(stderr, "%s: Can't load %s, unknow reason.\n", argv[0], argv[1]); 69 50 return 1; 70 51 } 52 53 cucul_free_buffer(b); 71 54 72 55 dp = caca_create_display(cv); -
libcaca/trunk/test/text.c
r858 r896 37 37 cucul_buffer_t *buffer; 38 38 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); 40 42 41 43 buffer = cucul_export_canvas(cv, "ansi");
Note: See TracChangeset
for help on using the changeset viewer.