codepage.c revision 245952
167754Smsmith/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
267754Smsmith   See the file COPYING for copying permission.
367754Smsmith*/
467754Smsmith
585756Smsmith#include "codepage.h"
667754Smsmith
767754Smsmith#if (defined(WIN32) || (defined(__WATCOMC__) && defined(__NT__)))
867754Smsmith#define STRICT 1
967754Smsmith#define WIN32_LEAN_AND_MEAN 1
1067754Smsmith
1167754Smsmith#include <windows.h>
1267754Smsmith
1371867Smsmithint
1470243SmsmithcodepageMap(int cp, int *map)
1567754Smsmith{
1667754Smsmith  int i;
1767754Smsmith  CPINFO info;
1867754Smsmith  if (!GetCPInfo(cp, &info) || info.MaxCharSize > 2)
1967754Smsmith    return 0;
2067754Smsmith  for (i = 0; i < 256; i++)
2167754Smsmith    map[i] = -1;
2267754Smsmith  if (info.MaxCharSize > 1) {
2367754Smsmith    for (i = 0; i < MAX_LEADBYTES; i+=2) {
2467754Smsmith      int j, lim;
2567754Smsmith      if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
2667754Smsmith        break;
2767754Smsmith      lim = info.LeadByte[i + 1];
2867754Smsmith      for (j = info.LeadByte[i]; j <= lim; j++)
2967754Smsmith        map[j] = -2;
3067754Smsmith    }
3167754Smsmith  }
3267754Smsmith  for (i = 0; i < 256; i++) {
3367754Smsmith   if (map[i] == -1) {
3467754Smsmith     char c = (char)i;
3567754Smsmith     unsigned short n;
3667754Smsmith     if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
3767754Smsmith                             &c, 1, &n, 1) == 1)
3867754Smsmith       map[i] = n;
3967754Smsmith   }
4067754Smsmith  }
4167754Smsmith  return 1;
4267754Smsmith}
4367754Smsmith
4467754Smsmithint
4567754SmsmithcodepageConvert(int cp, const char *p)
4667754Smsmith{
4767754Smsmith  unsigned short c;
4867754Smsmith  if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
4967754Smsmith                          p, 2, &c, 1) == 1)
5067754Smsmith    return c;
5167754Smsmith  return -1;
5267754Smsmith}
5367754Smsmith
5467754Smsmith#else /* not WIN32 */
5567754Smsmith
5667754Smsmithint
5767754SmsmithcodepageMap(int cp, int *map)
5867754Smsmith{
5967754Smsmith  return 0;
6067754Smsmith}
6167754Smsmith
6267754Smsmithint
6367754SmsmithcodepageConvert(int cp, const char *p)
6467754Smsmith{
6567754Smsmith  return -1;
6667754Smsmith}
6767754Smsmith
6867754Smsmith#endif /* not WIN32 */
6967754Smsmith