codepage.c 1.4 KB

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