summaryrefslogtreecommitdiff
path: root/src/charconv.c
blob: e869e41dbe54dd2ab4635631c78e3ebb9a7c6596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "charconv.h"
#include <iconv.h>
#include <langinfo.h>
#include <locale.h>
#include <stdio.h>

static iconv_t iconv_init_codepage(int codepage)
{
    iconv_t result;
    char codepage_name[16];
    snprintf(codepage_name, sizeof(codepage_name), "CP%d", codepage);
    result = iconv_open(nl_langinfo(CODESET), codepage_name);
    if (result == (iconv_t) - 1)
	perror(codepage_name);
    return result;
}

static iconv_t dos_to_local;

/*
 * Initialize conversion from codepage.
 * codepage = -1 means default codepage.
 * Returns 0 on success, non-zero on failure
 */
static int init_conversion(int codepage)
{
    static int initialized = -1;
    if (initialized < 0) {
	initialized = 1;
	if (codepage < 0)
	    codepage = DEFAULT_DOS_CODEPAGE;
	setlocale(LC_ALL, "");	/* initialize locale */
	dos_to_local = iconv_init_codepage(codepage);
	if (dos_to_local == (iconv_t) - 1 && codepage != DEFAULT_DOS_CODEPAGE) {
	    printf("Trying to set fallback DOS codepage %d\n",
		   DEFAULT_DOS_CODEPAGE);
	    dos_to_local = iconv_init_codepage(DEFAULT_DOS_CODEPAGE);
	    if (dos_to_local == (iconv_t) - 1)
		initialized = 0;	/* no conversion available */
	}
    }
    return initialized;
}

int set_dos_codepage(int codepage)
{
    return init_conversion(codepage);
}

int dos_char_to_printable(char **p, unsigned char c)
{
    char in[1] = { c };
    char *pin = in;
    size_t bytes_in = 1;
    size_t bytes_out = 4;
    if (!init_conversion(-1))
	return 0;
    return iconv(dos_to_local, &pin, &bytes_in, p, &bytes_out) != -1;
}