1104349Sphk/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2104349Sphk   See the file COPYING for copying permission.
3104349Sphk*/
4104349Sphk
5104349Sphk#include "codepage.h"
6302385Sdelphij#include "internal.h"  /* for UNUSED_P only */
7104349Sphk
8178848Scokane#if (defined(WIN32) || (defined(__WATCOMC__) && defined(__NT__)))
9104349Sphk#define STRICT 1
10104349Sphk#define WIN32_LEAN_AND_MEAN 1
11104349Sphk
12104349Sphk#include <windows.h>
13104349Sphk
14104349Sphkint
15104349SphkcodepageMap(int cp, int *map)
16104349Sphk{
17104349Sphk  int i;
18104349Sphk  CPINFO info;
19104349Sphk  if (!GetCPInfo(cp, &info) || info.MaxCharSize > 2)
20104349Sphk    return 0;
21104349Sphk  for (i = 0; i < 256; i++)
22104349Sphk    map[i] = -1;
23104349Sphk  if (info.MaxCharSize > 1) {
24178848Scokane    for (i = 0; i < MAX_LEADBYTES; i+=2) {
25104349Sphk      int j, lim;
26104349Sphk      if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
27104349Sphk        break;
28104349Sphk      lim = info.LeadByte[i + 1];
29178848Scokane      for (j = info.LeadByte[i]; j <= lim; j++)
30104349Sphk        map[j] = -2;
31104349Sphk    }
32104349Sphk  }
33104349Sphk  for (i = 0; i < 256; i++) {
34104349Sphk   if (map[i] == -1) {
35104349Sphk     char c = (char)i;
36104349Sphk     unsigned short n;
37104349Sphk     if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
38104349Sphk                             &c, 1, &n, 1) == 1)
39104349Sphk       map[i] = n;
40104349Sphk   }
41104349Sphk  }
42104349Sphk  return 1;
43104349Sphk}
44104349Sphk
45104349Sphkint
46104349SphkcodepageConvert(int cp, const char *p)
47104349Sphk{
48104349Sphk  unsigned short c;
49104349Sphk  if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
50104349Sphk                          p, 2, &c, 1) == 1)
51104349Sphk    return c;
52104349Sphk  return -1;
53104349Sphk}
54104349Sphk
55104349Sphk#else /* not WIN32 */
56104349Sphk
57104349Sphkint
58302385SdelphijcodepageMap(int UNUSED_P(cp), int *UNUSED_P(map))
59104349Sphk{
60104349Sphk  return 0;
61104349Sphk}
62104349Sphk
63104349Sphkint
64302385SdelphijcodepageConvert(int UNUSED_P(cp), const char *UNUSED_P(p))
65104349Sphk{
66104349Sphk  return -1;
67104349Sphk}
68104349Sphk
69104349Sphk#endif /* not WIN32 */
70