Extra arguments for the length of non-UTF-8 strings.
[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 <string.h>
16 #include "libcharset.h"
17
18 static convert_t locale_conv = NULL;
19
20 void close_conversion_contexts()
21 {
22   cleanup_utf8_conversion(locale_conv);
23 }
24
25 int open_conversion_contexts()
26 {
27   assert (locale_conv == NULL);
28   locale_conv = initialize_utf8_conversion(locale_charset(), 0);
29
30   if (locale_conv) {
31     atexit(close_conversion_contexts);
32     return 0;
33   }
34   else {
35     return -1;
36   }
37 }
38
39 void convert_set_unknown(const char* unknown)
40 {
41   if (!locale_conv)
42     open_conversion_contexts();
43   conversion_set_unknown(locale_conv, unknown);
44 }
45
46 char* convert_utf8_to_locale(const char* input, int *conv_fails)
47 {
48   if (!locale_conv)
49     open_conversion_contexts();
50
51   return convert_from_utf8(locale_conv, input, conv_fails, NULL);
52 }
53
54 char* convert_locale_to_utf8(const char* input)
55 {
56   if (!locale_conv)
57     open_conversion_contexts();
58
59   return convert_to_utf8(locale_conv, input, strlen(input));
60 }