Finishing up for release.
[gedcom-parse.git] / utf8 / utf8-locale.c
1 /* Encoding utility from UTF-8 to locale and vice versa
2    Copyright (C) 2001, 2002 Peter Verthez
3
4    The UTF8 tools library is free software; you can redistribute it
5    and/or modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The Gedcom parser library is distributed in the hope that it will be
10    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the Gedcom parser library; if not, write to the
16    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.
18 */
19
20 /* $Id$ */
21 /* $Name$ */
22
23 #include "utf8tools.h"
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <string.h>
27 #include "libcharset.h"
28
29 static convert_t locale_conv = NULL;
30
31 static void close_conversion_contexts()
32 {
33   cleanup_utf8_conversion(locale_conv);
34 }
35
36 static int open_conversion_contexts()
37 {
38   assert (locale_conv == NULL);
39   locale_conv = initialize_utf8_conversion(locale_charset(), 0);
40
41   if (locale_conv) {
42     atexit(close_conversion_contexts);
43     return 0;
44   }
45   else {
46     return -1;
47   }
48 }
49
50 void convert_set_unknown(const char* unknown)
51 {
52   if (!locale_conv)
53     open_conversion_contexts();
54   conversion_set_unknown(locale_conv, unknown);
55 }
56
57 char* convert_utf8_to_locale(const char* input, int *conv_fails)
58 {
59   if (!locale_conv)
60     open_conversion_contexts();
61
62   return convert_from_utf8(locale_conv, input, conv_fails, NULL);
63 }
64
65 char* convert_locale_to_utf8(const char* input)
66 {
67   if (!locale_conv)
68     open_conversion_contexts();
69
70   return convert_to_utf8(locale_conv, input, strlen(input));
71 }