xref: /freebsd/contrib/expat/xmlwf/codepage.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
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 #include "internal.h"  /* for UNUSED_P only */
7 
8 #if (defined(WIN32) || (defined(__WATCOMC__) && defined(__NT__)))
9 #define STRICT 1
10 #define WIN32_LEAN_AND_MEAN 1
11 
12 #include <windows.h>
13 
14 int
15 codepageMap(int cp, int *map)
16 {
17   int i;
18   CPINFO info;
19   if (!GetCPInfo(cp, &info) || info.MaxCharSize > 2)
20     return 0;
21   for (i = 0; i < 256; i++)
22     map[i] = -1;
23   if (info.MaxCharSize > 1) {
24     for (i = 0; i < MAX_LEADBYTES; i+=2) {
25       int j, lim;
26       if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
27         break;
28       lim = info.LeadByte[i + 1];
29       for (j = info.LeadByte[i]; j <= lim; j++)
30         map[j] = -2;
31     }
32   }
33   for (i = 0; i < 256; i++) {
34    if (map[i] == -1) {
35      char c = (char)i;
36      unsigned short n;
37      if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
38                              &c, 1, &n, 1) == 1)
39        map[i] = n;
40    }
41   }
42   return 1;
43 }
44 
45 int
46 codepageConvert(int cp, const char *p)
47 {
48   unsigned short c;
49   if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
50                           p, 2, &c, 1) == 1)
51     return c;
52   return -1;
53 }
54 
55 #else /* not WIN32 */
56 
57 int
58 codepageMap(int UNUSED_P(cp), int *UNUSED_P(map))
59 {
60   return 0;
61 }
62 
63 int
64 codepageConvert(int UNUSED_P(cp), const char *UNUSED_P(p))
65 {
66   return -1;
67 }
68 
69 #endif /* not WIN32 */
70