source: libcaca/trunk/cucul/import.c @ 841

Last change on this file since 841 was 841, checked in by Sam Hocevar, 15 years ago
  • Added a simple "text" importer, until the cool ANSI importer arrives.
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: import.c 841 2006-04-22 19:10:41Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains various import functions.
16 */
17
18#include "config.h"
19
20#if !defined(__KERNEL__)
21#   include <stdio.h>
22#   include <stdlib.h>
23#   include <string.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29static cucul_canvas_t *import_caca(void const *, unsigned int);
30static cucul_canvas_t *import_text(void const *, unsigned int);
31
32/** \brief Import a buffer into a canvas
33 *
34 *  This function imports a memory area into an internal libcucul canvas.
35 *
36 *  Valid values for \c format are:
37 *
38 *  \li \c "": attempt to autodetect the file format.
39 *
40 *  \li \c "caca": import native libcaca files.
41 *
42 *  \param data The memory area to be loaded into a canvas.
43 *  \param size The length of the memory area.
44 *  \param format A string describing the input format.
45 *  \return A libcucul canvas, or NULL in case of error.
46 */
47cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
48                                     char const *format)
49{
50    if(!strcasecmp("caca", format))
51        return import_caca(data, size);
52    if(!strcasecmp("text", format))
53        return import_text(data, size);
54
55    /* FIXME: Try to autodetect */
56    if(!strcasecmp("", format))
57        return import_caca(data, size);
58
59    return NULL;
60}
61
62/** \brief Get available import formats
63 *
64 *  Return a list of available import formats. The list is a NULL-terminated
65 *  array of strings, interleaving a string containing the internal value for
66 *  the import format, to be used with cucul_import_canvas(), and a string
67 *  containing the natural language description for that import format.
68 *
69 *  \return An array of strings.
70 */
71char const * const * cucul_get_import_list(void)
72{
73    static char const * const list[] =
74    {
75        "", "autodetect",
76        "text", "plain text",
77        "caca", "native libcaca format",
78        NULL, NULL
79    };
80
81    return list;
82}
83
84/*
85 * XXX: the following functions are local.
86 */
87
88static cucul_canvas_t *import_caca(void const *data, unsigned int size)
89{
90    cucul_canvas_t *cv;
91    uint8_t const *buf = (uint8_t const *)data;
92    unsigned int width, height, n;
93
94    if(size < 16)
95        return NULL;
96
97    if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
98        return NULL;
99
100    if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
101        return NULL;
102
103    width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
104           | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
105    height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
106            | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
107
108    if(!width || !height)
109        return NULL;
110
111    if(size != 16 + width * height * 8)
112        return NULL;
113
114    cv = cucul_create_canvas(width, height);
115
116    if(!cv)
117        return NULL;
118
119    for(n = height * width; n--; )
120    {
121        cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
122                     | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
123                     | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
124                     | (uint32_t)buf[16 + 3 + 8 * n];
125        cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
126                    | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
127                    | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
128                    | (uint32_t)buf[16 + 7 + 8 * n];
129    }
130
131    return cv;
132}
133
134static cucul_canvas_t *import_text(void const *data, unsigned int size)
135{
136    cucul_canvas_t *cv;
137    char const *text = (char const *)data;
138    unsigned int width = 1, height = 1, x = 0, y = 0, i;
139
140    cv = cucul_create_canvas(width, height);
141    cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
142
143    for(i = 0; i < size; i++)
144    {
145        unsigned char ch = *text++;
146
147        if(ch == '\r')
148            continue;
149
150        if(ch == '\n')
151        {
152            x = 0;
153            y++;
154            continue;
155        }
156
157        while(x >= width)
158        {
159            width++;
160            cucul_set_canvas_size(cv, width, height);
161        }
162
163        while(y >= height)
164        {
165            height++;
166            cucul_set_canvas_size(cv, width, height);
167        }
168
169        cucul_putchar(cv, x, y, ch);
170        x++;
171    }
172
173    return cv;
174}
175
Note: See TracBrowser for help on using the repository browser.