eedff8f5a5a7a04ed0b505616bf2c19b22fac7ca
[platform/upstream/fribidi.git] / charset / fribidi-char-sets.c
1 /* FriBidi
2  * fribidi-char-sets.c - character set conversion routines
3  *
4  * $Id: fribidi-char-sets.c,v 1.7 2006-01-31 03:23:12 behdad Exp $
5  * $Author: behdad $
6  * $Date: 2006-01-31 03:23:12 $
7  * $Revision: 1.7 $
8  * $Source: /home/behdad/src/fdo/fribidi/togit/git/../fribidi/fribidi2/charset/fribidi-char-sets.c,v $
9  *
10  * Authors:
11  *   Behdad Esfahbod, 2001, 2002, 2004
12  *   Dov Grobgeld, 1999, 2000
13  *
14  * Copyright (C) 2004 Sharif FarsiWeb, Inc
15  * Copyright (C) 2001,2002 Behdad Esfahbod
16  * Copyright (C) 1999,2000 Dov Grobgeld
17  * 
18  * This library is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  * 
23  * This library is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * Lesser General Public License for more details.
27  * 
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with this library, in a file named COPYING; if not, write to the
30  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31  * Boston, MA 02110-1301, USA
32  * 
33  * For licensing issues, contact <license@farsiweb.info>.
34  */
35
36 #include <common.h>
37
38 #include <fribidi-char-sets.h>
39
40 #include "fribidi-char-sets-cap-rtl.h"
41 #include "fribidi-char-sets-utf8.h"
42 #include "fribidi-char-sets-iso8859-6.h"
43 #include "fribidi-char-sets-cp1256.h"
44 #include "fribidi-char-sets-iso8859-8.h"
45 #include "fribidi-char-sets-cp1255.h"
46
47 typedef struct
48 {
49   FriBidiChar (
50   *charset_to_unicode_c
51   ) (
52   char ch
53   );
54
55   FriBidiStrIndex (
56   *charset_to_unicode
57   ) (
58   const char *s,
59   FriBidiStrIndex len,
60   FriBidiChar *us
61   );
62
63   char (
64   *unicode_to_charset_c
65   ) (
66   FriBidiChar uch
67   );
68
69   FriBidiStrIndex (
70   *unicode_to_charset
71   ) (
72   const FriBidiChar *us,
73   FriBidiStrIndex len,
74   char *s
75   );
76
77   const char *name;
78
79   const char *title;
80
81   const char *(
82   *desc
83   ) (
84   void
85   );
86 }
87 FriBidiCharSetHandler;
88
89 static FriBidiCharSetHandler char_sets[FRIBIDI_CHAR_SETS_NUM + 1] = {
90   {NULL, NULL, NULL, NULL, "N/A", "Character set not available", NULL},
91 # define _FRIBIDI_ADD_CHAR_SET_ONE2ONE(CHAR_SET, char_set) \
92   { \
93     fribidi_##char_set##_to_unicode_c, \
94     NULL, \
95     fribidi_unicode_to_##char_set##_c, \
96     NULL, \
97     fribidi_char_set_name_##char_set, \
98     fribidi_char_set_title_##char_set, \
99     fribidi_char_set_desc_##char_set \
100   },
101 # define _FRIBIDI_ADD_CHAR_SET_OTHERS(CHAR_SET, char_set) \
102   { \
103     NULL, \
104     fribidi_##char_set##_to_unicode, \
105     NULL, \
106     fribidi_unicode_to_##char_set, \
107     fribidi_char_set_name_##char_set, \
108     fribidi_char_set_title_##char_set, \
109     fribidi_char_set_desc_##char_set \
110   },
111 # include <fribidi-char-sets-list.h>
112 # undef _FRIBIDI_ADD_CHAR_SET_OTHERS
113 # undef _FRIBIDI_ADD_CHAR_SET_ONE2ONE
114 };
115
116 #if FRIBIDI_USE_GLIB+0
117 # include <glib/gstrfuncs.h>
118 # define fribidi_strcasecmp g_ascii_strcasecmp
119 #else /* !FRIBIDI_USE_GLIB */
120 static char
121 toupper (
122   /* input */
123   char c
124 )
125 {
126   return c < 'a' || c > 'z' ? c : c + 'A' - 'a';
127 }
128
129 static int
130 fribidi_strcasecmp (
131   /* input */
132   const char *s1,
133   const char *s2
134 )
135 {
136   while (*s1 && toupper (*s1) == toupper (*s2))
137     {
138       s1++;
139       s2++;
140     }
141   return toupper (*s1) - toupper (*s2);
142 }
143 #endif /* !FRIBIDI_USE_GLIB */
144
145 FRIBIDI_ENTRY FriBidiCharSet
146 fribidi_parse_charset (
147   /* input */
148   const char *s
149 )
150 {
151   int i;
152
153   for (i = FRIBIDI_CHAR_SETS_NUM; i; i--)
154     if (fribidi_strcasecmp (s, char_sets[i].name) == 0)
155       return i;
156
157   return FRIBIDI_CHAR_SET_NOT_FOUND;
158 }
159
160 FRIBIDI_ENTRY FriBidiStrIndex
161 fribidi_charset_to_unicode (
162   /* input */
163   FriBidiCharSet char_set,
164   const char *s,
165   FriBidiStrIndex len,
166   /* output */
167   FriBidiChar *us
168 )
169 {
170   if (char_sets[char_set].charset_to_unicode)
171     return (*char_sets[char_set].charset_to_unicode) (s, len, us);
172   else if (char_sets[char_set].charset_to_unicode_c)
173     {
174       register FriBidiStrIndex l;
175       for (l = len; l; l--)
176         *us++ = (*char_sets[char_set].charset_to_unicode_c) (*s++);
177       return len;
178     }
179   else
180     return 0;
181 }
182
183 FRIBIDI_ENTRY FriBidiStrIndex
184 fribidi_unicode_to_charset (
185   /* input */
186   FriBidiCharSet char_set,
187   const FriBidiChar *us,
188   FriBidiStrIndex len,
189   /* output */
190   char *s
191 )
192 {
193   if (char_sets[char_set].unicode_to_charset)
194     return (*char_sets[char_set].unicode_to_charset) (us, len, s);
195   else if (char_sets[char_set].unicode_to_charset_c)
196     {
197       register FriBidiStrIndex l;
198       for (l = len; l; l--)
199         *s++ = (*char_sets[char_set].unicode_to_charset_c) (*us++);
200       *s = '\0';
201       return len;
202     }
203   else
204     return 0;
205 }
206
207 FRIBIDI_ENTRY const char *
208 fribidi_char_set_name (
209   /* input */
210   FriBidiCharSet char_set
211 )
212 {
213   return char_sets[char_set].name ? char_sets[char_set].name : "";
214 }
215
216 FRIBIDI_ENTRY const char *
217 fribidi_char_set_title (
218   /* input */
219   FriBidiCharSet char_set
220 )
221 {
222   return char_sets[char_set].title ? char_sets[char_set].
223     title : fribidi_char_set_name (char_set);
224 }
225
226 FRIBIDI_ENTRY const char *
227 fribidi_char_set_desc (
228   /* input */
229   FriBidiCharSet char_set
230 )
231 {
232   return char_sets[char_set].desc ? char_sets[char_set].desc () : NULL;
233 }
234
235 /* Editor directions:
236  * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent
237  */