2
0

codepage.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  3. See the file copying.txt for copying permission.
  4. */
  5. #include "codepage.h"
  6. #ifdef WIN32
  7. #define STRICT 1
  8. #define WIN32_LEAN_AND_MEAN 1
  9. #include <windows.h>
  10. int 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++) {
  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 = 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 codepageConvert(int cp, const char *p)
  40. {
  41. unsigned short c;
  42. if (MultiByteToWideChar(cp, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
  43. p, 2, &c, 1) == 1)
  44. return c;
  45. return -1;
  46. }
  47. #else /* not WIN32 */
  48. int codepageMap(int cp, int *map)
  49. {
  50. return 0;
  51. }
  52. int codepageConvert(int cp, const char *p)
  53. {
  54. return -1;
  55. }
  56. #endif /* not WIN32 */