xref: /freebsd/contrib/expat/xmlwf/codepage.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2    See the file COPYING for copying permission.
3 */
4 
5 #include "codepage.h"
6 
7 #ifdef WIN32
8 #define STRICT 1
9 #define WIN32_LEAN_AND_MEAN 1
10 
11 #include <windows.h>
12 
13 int
14 codepageMap(int cp, int *map)
15 {
16   int i;
17   CPINFO info;
18   if (!GetCPInfo(cp, &info) || info.MaxCharSize > 2)
19     return 0;
20   for (i = 0; i < 256; i++)
21     map[i] = -1;
22   if (info.MaxCharSize > 1) {
23     for (i = 0; i < MAX_LEADBYTES; i++) {
24       int j, lim;
25       if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
26         break;
27       lim = info.LeadByte[i + 1];
28       for (j = info.LeadByte[i]; j < lim; j++)
29         map[j] = -2;
30     }
31   }
32   for (i = 0; i < 256; i++) {
33    if (map[i] == -1) {
34      char c = (char)i;
35      unsigned short n;
36      if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
37                              &c, 1, &n, 1) == 1)
38        map[i] = n;
39    }
40   }
41   return 1;
42 }
43 
44 int
45 codepageConvert(int cp, const char *p)
46 {
47   unsigned short c;
48   if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
49                           p, 2, &c, 1) == 1)
50     return c;
51   return -1;
52 }
53 
54 #else /* not WIN32 */
55 
56 int
57 codepageMap(int cp, int *map)
58 {
59   return 0;
60 }
61 
62 int
63 codepageConvert(int cp, const char *p)
64 {
65   return -1;
66 }
67 
68 #endif /* not WIN32 */
69