Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / libunls / nls_iconv.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)nls_iconv.c      1.0 02/04/20 2002 J. Schilling  */
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2, or (at your option)
18  * any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; see the file COPYING.  If not, write to
27  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29 /*
30  *      Modifications to make the code portable Copyright (c) 2000 J. Schilling
31  *
32  * nls_iconv: create a pseudo-charset table to use iconv() provided by C
33  * library or libiconv by Bruno Haible
34  * The Unicode to charset table has only exact mappings.
35  *
36  *
37  * Jungshik Shin (jshin@mailaps.org) 04-Feb-2002
38  */
39
40 #ifdef USE_ICONV
41 #include <mconfig.h>
42 #include <stdio.h>
43 #include <stdxlib.h>
44 #include <strdefs.h>
45 #include "nls.h"
46 #include <iconv.h>
47
48 static void     inc_use_count(void);
49 static void     dec_use_count(void);
50
51
52 static void
53 inc_use_count()
54 {
55         MOD_INC_USE_COUNT;
56 }
57
58 static void
59 dec_use_count()
60 {
61         MOD_DEC_USE_COUNT;
62 }
63
64 int
65 init_nls_iconv(char *charset)
66 {
67         iconv_t iconv_d;  /* iconv conversion descriptor */
68         struct unls_table *table;
69
70         /* give up if no charset is given */
71         if (charset == NULL)
72                 return -1;
73
74         /* see if we already have a table with this name - built in tables
75            have precedence over iconv() - i.e. can't have the name of an
76            existing table. Also, we may have already registered this file
77            table */
78         if (find_unls(charset) != NULL)
79                 return -1;
80
81         if ((iconv_d = iconv_open("UCS-2BE", charset)) == (iconv_t) -1)
82                 return -1;
83
84
85         /* set up the table */
86         if ((table = (struct unls_table *)malloc(sizeof (struct unls_table)))
87                                                         == NULL) {
88                 return -1;
89         }
90
91         /* give the table the file name, so we can find it again if needed */
92         table->unls_name = strdup(charset);
93         table->unls_uni2cs = NULL;
94         table->unls_cs2uni = NULL;
95         table->unls_next = NULL;
96         table->iconv_d = iconv_d;
97
98         /* register the table */
99         return register_unls(table);
100 }
101 #endif