Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
5 /*
6 * The code makes two assumptions: strlen(ld) is 1 or 2; latintab[i].ld can be a
7 * prefix of latintab[j].ld only when j<i.
8 */
9 static struct cvlist
10 {
11 char *ld; /* must be seen before using this conversion */
12 char *si; /* options for last input characters */
13 Rune so[60]; /* the corresponding Rune for each si entry */
14 } latintab[] = {
15 #include "latin1.h"
16 0, 0, { 0 }
17 };
19 /*
20 * Given 5 characters k[0]..k[4], find the rune or return -1 for failure.
21 */
22 static long
23 unicode(Rune *k)
24 {
25 long i, c;
27 k++; /* skip 'X' */
28 c = 0;
29 for(i=0; i<4; i++,k++){
30 c <<= 4;
31 if('0'<=*k && *k<='9')
32 c += *k-'0';
33 else if('a'<=*k && *k<='f')
34 c += 10 + *k-'a';
35 else if('A'<=*k && *k<='F')
36 c += 10 + *k-'A';
37 else
38 return -1;
39 }
40 return c;
41 }
43 /*
44 * Given n characters k[0]..k[n-1], find the corresponding rune or return -1 for
45 * failure, or something < -1 if n is too small. In the latter case, the result
46 * is minus the required n.
47 */
48 int
49 _latin1(Rune *k, int n)
50 {
51 struct cvlist *l;
52 int c;
53 char* p;
55 if(k[0] == 'X'){
56 if(n>=5)
57 return unicode(k);
58 else
59 return -5;
60 }
62 for(l=latintab; l->ld!=0; l++)
63 if(k[0] == l->ld[0]){
64 if(n == 1)
65 return -2;
66 if(l->ld[1] == 0)
67 c = k[1];
68 else if(l->ld[1] != k[1])
69 continue;
70 else if(n == 2)
71 return -3;
72 else
73 c = k[2];
74 for(p=l->si; *p!=0; p++)
75 if(*p == c)
76 return l->so[p - l->si];
77 return -1;
78 }
79 return -1;
80 }