Some changes in interface.
[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    Permission granted to do anything with this file that you want, as long
5    as the above copyright is retained in all copies.
6    THERE IS NO WARRANTY - USE AT YOUR OWN RISK
7 */
8
9 /* $Id$ */
10 /* $Name$ */
11
12 #include "utf8.h"
13 #include <stdlib.h>
14 #include <assert.h>
15 #include "libcharset.h"
16
17 static convert_t locale_conv = NULL;
18
19 void close_conversion_contexts()
20 {
21   cleanup_utf8_conversion(locale_conv);
22 }
23
24 int open_conversion_contexts()
25 {
26   assert (locale_conv == NULL);
27   locale_conv = initialize_utf8_conversion(locale_charset(), 0);
28
29   if (locale_conv) {
30     atexit(close_conversion_contexts);
31     return 0;
32   }
33   else {
34     return -1;
35   }
36 }
37
38 void convert_set_unknown(const char* unknown)
39 {
40   if (!locale_conv)
41     open_conversion_contexts();
42   conversion_set_unknown(locale_conv, unknown);
43 }
44
45 char* convert_utf8_to_locale(const char* input, int *conv_fails)
46 {
47   if (!locale_conv)
48     open_conversion_contexts();
49
50   return convert_from_utf8(locale_conv, input, conv_fails);
51 }
52
53 char* convert_locale_to_utf8(const char* input)
54 {
55   if (!locale_conv)
56     open_conversion_contexts();
57
58   return convert_to_utf8(locale_conv, input);
59 }