File: libTutf.c

package info (click to toggle)
twin 0.4.0-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,804 kB
  • ctags: 23,904
  • sloc: ansic: 61,860; cpp: 1,023; makefile: 777; sh: 552; lex: 302; yacc: 231
file content (198 lines) | stat: -rw-r--r-- 5,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

/*
 *   libTwUTF.c -- ASCII/VGA/UTF/UNICODE pseudographics characters database
 * 
 *  Copyright (C) 2001 by Massimiliano Ghilardi
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 *
 */

#include "Tw/Tw.h"

#include "Tutf/Tutf.h"
#include "Tutf/Tutf_defs.h"

typedef struct {
    hwfont utf, ch;
} utf_to_ch;

static int Cmp(TW_CONST utf_to_ch *u1, TW_CONST utf_to_ch *u2) {
    return u1->utf - u2->utf;
}

static TW_CONST utf_to_ch *my_bsearch(TW_CONST utf_to_ch *key, TW_CONST utf_to_ch *base, size_t nmemb) {
    TW_CONST utf_to_ch *low = base, *high = base + nmemb;

    while (low + 1 < high) {
	base = low + (high - low) / 2;
	
	if (base->utf > key->utf)
	    high = base;
	else if (base->utf < key->utf)
	    low = base;
	else
	    return base;
    }
    return NULL;
}


#define QSORT(array) qsort((void *)(array), sizeof(array)/sizeof((array)[0]), \
			       sizeof((array)[0]), (void *)Cmp)

#define BSEARCH(key, array) \
	(utf_to_ch *)my_bsearch((key), (array), sizeof(array)/sizeof((array)[0]))

#include "ascii.c"

#define ISO_8859_X ISO_8859_1
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_2
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_3
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_4
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_5
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_6
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_7
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_8
#include "iso_8859_x.c"
#undef ISO_8859_X

#define ISO_8859_X ISO_8859_9
#include "iso_8859_x.c"
#undef ISO_8859_X

#include "ibm437.c"
#include "ibm850.c"
#include "ibm865.c"

#define TEMPLATE CP866
#include "template.c"
#undef TEMPLATE

#define _NLIST(EL) \
	EL(T_MAP(ASCII)) \
	EL(T_MAP(ISO_8859_1)) EL(T_MAP(ISO_8859_2)) EL(T_MAP(ISO_8859_3)) \
	EL(T_MAP(ISO_8859_4)) EL(T_MAP(ISO_8859_5)) EL(T_MAP(ISO_8859_6)) \
	EL(T_MAP(ISO_8859_7)) EL(T_MAP(ISO_8859_8)) EL(T_MAP(ISO_8859_9)) \
	EL(T_MAP(IBM437))     EL(T_MAP(IBM850))     EL(T_MAP(IBM865))     \
	EL(T_MAP(CP866))

#define _LIST(EL) \
	EL(T_MAP(UTF_16)) _NLIST(EL)

typedef enum {
#define DECL_TYPE(x) T_CAT(T_ID_,x) ,
    _LIST(DECL_TYPE)
#undef DECL_TYPE
} id_e;

#define DECL_NAME(ch) static byte *T_CAT(names_,ch) [] = { T_CAT(T_NAME_,ch), T_CAT(T_ALIASES_,ch), NULL };
_LIST(DECL_NAME)
#undef DECL_NAME

typedef struct {
    byte * TW_CONST * names;
    Tutf_array array;
    Tutf_function function;
} Tutf_struct;

#define DECL_CH(ch) { T_CAT(names_,ch), T_CAT(T_CAT(Tutf_,ch),_to_UTF_16), T_CAT(Tutf_UTF_16_to_,ch) },

static Tutf_struct Tutf_structs[] = {
    { T_CAT(names_,UTF_16), NULL, NULL },
    _NLIST(DECL_CH)
    { NULL }
};

#undef DECL_CH

static int strloosecmp(TW_CONST byte *s1, TW_CONST byte *s2) {
    byte c1, c2;

    for (;;) {
	c1 = *s1++;
	c2 = *s2++;
	if (!c1 || !c2 ||
	    !(c1 == c2 ||
	      (
	       (c1 == '-' || c1 == '_' || c1 == '.' || c1 == ':') &&
	       (c2 == '-' || c2 == '_' || c2 == '.' || c2 == ':')
	      ) ||
	      (c1 >= 'A' && c1 <= 'Z' && c1 + ('a'-'A') == c2) ||
	      (c2 >= 'A' && c2 <= 'Z' && c2 + ('a'-'A') == c1)
	     )
	   )
	    
	    return (int)c1-(int)c2;
    }
}

uldat Tutf_charset_id(TW_CONST byte * alias) {
    Tutf_struct *CH;
    byte * TW_CONST * names;
    if (alias) for (CH = Tutf_structs; (names = CH->names); CH++) {
	for (; *names; names++) {
	    if (!strloosecmp(alias, *names))
		return (uldat)(CH - Tutf_structs);
	}
    }
    return (uldat)-1;
}

TW_CONST byte *Tutf_charset_name(uldat id) {
    return id < sizeof(Tutf_structs)/sizeof(Tutf_structs[0]) ? Tutf_structs[id].names[0] : NULL;
}

TW_CONST byte *Tutf_charset_alias(TW_CONST byte * alias) {
    uldat id = Tutf_charset_id(alias);
    return id != (uldat)-1 ? Tutf_structs[id].names[0] : NULL;
}


/* return a function capable to translate from UTF_16 to given charset */
Tutf_function Tutf_UTF_16_to_charset_function(uldat id) {
    return id < sizeof(Tutf_structs)/sizeof(Tutf_structs[0]) ? Tutf_structs[id].function : NULL;
}

/* return the array to translate from given charset to UTF_16 */
Tutf_array Tutf_charset_to_UTF_16_array(uldat id) {
    return id < sizeof(Tutf_structs)/sizeof(Tutf_structs[0]) ? Tutf_structs[id].array : NULL;
}