1 module utile.encoding;
2 
3 import std.conv, std.string, std.encoding, std.exception, std.windows.charset,
4 
5 	core.stdc.errno, utile.except, utile.encoding.iconv;
6 
7 static immutable string[ushort] encodingsTable;
8 
9 shared static this()
10 {
11 	encodingsTable = [
12 		437: `IBM437`,
13 		708: `ASMO-708`,
14 		775: `IBM775`,
15 		850: `IBM850`,
16 		852: `IBM852`,
17 		855: `IBM855`,
18 		857: `IBM857`,
19 		860: `IBM860`,
20 		861: `IBM861`,
21 		863: `IBM863`,
22 		864: `IBM864`,
23 		865: `IBM865`,
24 		866: `CP866`,
25 		869: `IBM869`,
26 		874: `WINDOWS-874`,
27 		932: `SHIFT_JIS`,
28 		936: `GB2312`,
29 		949: `KS_C_5601-1987`,
30 		950: `BIG5`,
31 		1200: `UTF-16`,
32 		1250: `WINDOWS-1250`,
33 		1251: `WINDOWS-1251`,
34 		1252: `WINDOWS-1252`,
35 		1253: `WINDOWS-1253`,
36 		1254: `WINDOWS-1254`,
37 		1255: `WINDOWS-1255`,
38 		1256: `WINDOWS-1256`,
39 		1257: `WINDOWS-1257`,
40 		1258: `WINDOWS-1258`,
41 		1361: `JOHAB`,
42 		10000: `MACINTOSH`,
43 		12000: `UTF-32`,
44 		12001: `UTF-32BE`,
45 		20127: `US-ASCII`,
46 		20866: `KOI8-R`,
47 		20932: `EUC-JP`,
48 		21866: `KOI8-U`,
49 		28591: `ISO-8859-1`,
50 		28592: `ISO-8859-2`,
51 		28593: `ISO-8859-3`,
52 		28594: `ISO-8859-4`,
53 		28595: `ISO-8859-5`,
54 		28596: `ISO-8859-6`,
55 		28597: `ISO-8859-7`,
56 		28598: `ISO-8859-8`,
57 		28599: `ISO-8859-9`,
58 		28603: `ISO-8859-13`,
59 		28605: `ISO-8859-15`,
60 		50221: `CSISO2022JP`,
61 		50222: `ISO-2022-JP`,
62 		50225: `ISO-2022-KR`,
63 		51932: `EUC-JP`,
64 		51936: `EUC-CN`,
65 		51949: `EUC-KR`,
66 		52936: `HZ-GB-2312`,
67 		54936: `GB18030`,
68 		65000: `UTF-7`,
69 	];
70 }
71 
72 auto decode(string s, ushort cp)
73 {
74 	version (linux)
75 	{
76 		s = iconv(s, encodingsTable[cp], `UTF-8`);
77 	}
78 	else
79 	{
80 		s = fromMBSz(s.toStringz, cp);
81 	}
82 
83 	assert(s.isValid);
84 	return s;
85 }
86 
87 auto encode(string s, ushort cp)
88 {
89 	assert(s.isValid);
90 
91 	version (linux)
92 	{
93 		return iconv(s, `UTF-8`, encodingsTable[cp]);
94 	}
95 	else
96 	{
97 		return toMBSz(s, cp).fromStringz.assumeUnique;
98 	}
99 }