codepage.c 1.3 KB

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