Browse Source

* Add "utf8" import format.

* Documented "text" import format.
tags/v0.99.beta14
Sam Hocevar sam 18 years ago
parent
commit
682d2d297b
1 changed files with 21 additions and 5 deletions
  1. +21
    -5
      cucul/import.c

+ 21
- 5
cucul/import.c View File

@@ -40,7 +40,7 @@ struct ansi_grcm


static cucul_canvas_t *import_caca(void const *, unsigned int); static cucul_canvas_t *import_caca(void const *, unsigned int);
static cucul_canvas_t *import_text(void const *, unsigned int); static cucul_canvas_t *import_text(void const *, unsigned int);
static cucul_canvas_t *import_ansi(void const *, unsigned int);
static cucul_canvas_t *import_ansi(void const *, unsigned int, int);


static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *, static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
unsigned int, unsigned int const *); unsigned int, unsigned int const *);
@@ -52,7 +52,9 @@ static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
* *
* Valid values for \c format are: * Valid values for \c format are:
* - \c "": attempt to autodetect the file format. * - \c "": attempt to autodetect the file format.
* - \c "text": import ASCII text files.
* - \c "ansi": import ANSI files. * - \c "ansi": import ANSI files.
* - \c "utf8": import UTF-8 files with ANSI colour files.
* - \c "caca": import native libcaca files. * - \c "caca": import native libcaca files.
* *
* If an error occurs, NULL is returned and \b errno is set accordingly: * If an error occurs, NULL is returned and \b errno is set accordingly:
@@ -70,10 +72,12 @@ cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)


if(!strcasecmp("caca", format)) if(!strcasecmp("caca", format))
return import_caca(buffer->data, buffer->size); return import_caca(buffer->data, buffer->size);
if(!strcasecmp("utf8", format))
return import_ansi(buffer->data, buffer->size, 1);
if(!strcasecmp("text", format)) if(!strcasecmp("text", format))
return import_text(buffer->data, buffer->size); return import_text(buffer->data, buffer->size);
if(!strcasecmp("ansi", format)) if(!strcasecmp("ansi", format))
return import_ansi(buffer->data, buffer->size);
return import_ansi(buffer->data, buffer->size, 0);


/* Autodetection */ /* Autodetection */
if(!strcasecmp("", format)) if(!strcasecmp("", format))
@@ -88,7 +92,7 @@ cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
/* If we find ESC[ argv, we guess it's an ANSI file */ /* If we find ESC[ argv, we guess it's an ANSI file */
for(i = 0; i < buffer->size - 1; i++) for(i = 0; i < buffer->size - 1; i++)
if((buf[i] == 0x1b) && (buf[i + 1] == '[')) if((buf[i] == 0x1b) && (buf[i + 1] == '['))
return import_ansi(buffer->data, buffer->size);
return import_ansi(buffer->data, buffer->size, 0);


/* Otherwise, import it as text */ /* Otherwise, import it as text */
return import_text(buffer->data, buffer->size); return import_text(buffer->data, buffer->size);
@@ -235,13 +239,15 @@ static cucul_canvas_t *import_text(void const *data, unsigned int size)
return cv; return cv;
} }


static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
static cucul_canvas_t *import_ansi(void const *data, unsigned int size,
int utf8)
{ {
struct ansi_grcm grcm; struct ansi_grcm grcm;
unsigned char const *buffer = (unsigned char const*)data; unsigned char const *buffer = (unsigned char const*)data;
cucul_canvas_t *cv; cucul_canvas_t *cv;
unsigned int i, j, skip, dummy = 0; unsigned int i, j, skip, dummy = 0;
unsigned int width = 1, height = 1; unsigned int width = 1, height = 1;
unsigned long int ch;
int x = 0, y = 0, save_x = 0, save_y = 0; int x = 0, y = 0, save_x = 0, save_y = 0;


cv = cucul_create_canvas(width, height); cv = cucul_create_canvas(width, height);
@@ -411,8 +417,18 @@ static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
} }


/* Now paste our character */ /* Now paste our character */
if(utf8)
{
unsigned int bytes;
ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
skip += bytes - 1;
}
else
{
ch = cucul_cp437_to_utf32(buffer[i]);
}
cucul_set_color(cv, grcm.efg, grcm.ebg); cucul_set_color(cv, grcm.efg, grcm.ebg);
cucul_putchar(cv, x, y, cucul_cp437_to_utf32(buffer[i]));
cucul_putchar(cv, x, y, ch);
x++; x++;
} }




Loading…
Cancel
Save