chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / utf8.c
1 /*
2  * utf8.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.2  2008/02/22 23:39:27  james
14  * *** empty log message ***
15  *
16  * Revision 1.1  2008/02/22 19:12:05  james
17  * *** empty log message ***
18  *
19  */
20
21 #include "project.h"
22
23
24 void utf8_flush(Context *c)
25 {
26 UTF8 *u=c->u;
27 int i;
28
29 for (i=0;i<u->utf_ptr;++i) 
30         vt102_parse(c,u->utf_buf[i]);
31
32 u->utf_ptr=0;
33 u->in_utf8=0;
34 }
35
36 int utf8_parse(Context *c,int ch)
37 {
38 UTF8 *u=&c->u;
39   
40   if (!u->in_utf8) {
41           /*FIXME: for the moment we bodge utf8 support*/
42           if (ch==0xb9) { /*CSI, not a valid utf8 start char*/
43                 vt102_parse(c,ch);
44           } else if ((ch & 0xe0) == 0xc0) { /*Start of two byte unicode sequence*/
45                 u->in_utf8=2;
46                 u->utf_ptr=0;
47                 u->utf_buf[u->utf_ptr++]=ch;
48           } else if ((ch & 0xf0) ==0xe0) { /*Start of three byte unicode sequence*/
49                 u->in_utf8=3;
50                 u->utf_ptr=0;
51                 u->utf_buf[u->utf_ptr++]=ch;
52           } else if ((ch & 0xf8) ==0xf0) {
53                 u->in_utf8=4;
54                 u->utf_ptr=0;
55                 u->utf_buf[u->utf_ptr++]=ch;
56           } else {
57                 vt102_parse(c,ch);
58           } 
59   } else {
60         if ((ch & 0xc0) != 0x80) {
61                 utf8_flush(c);
62                 vt102_parse(c,ch);
63         } else {
64                 u->utf_buf[u->utf_ptr++]=ch;
65                 u->in_utf8--;
66         }
67   }
68 }
69
70
71
72 UTF8 *utf8_new(void)
73 {
74 UTF8 *ret;
75
76 ret=(UTF8 *) malloc(sizeof(UTF8));
77
78 ret->in_utf8=0;
79
80 }
81