1
0

Encodings.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "expat.h"
  2. #include <wchar.h>
  3. #include <windows.h>
  4. #include "../WAT/WAT.h"
  5. struct WindowsEncodings
  6. {
  7. const wchar_t *name;
  8. UINT codePage;
  9. };
  10. static const WindowsEncodings encodings[] =
  11. {
  12. {L"iso-8859-2", 28592},
  13. {L"iso-8859-3", 28593},
  14. {L"iso-8859-4", 28594},
  15. {L"iso-8859-5", 28595},
  16. {L"iso-8859-6", 28596},
  17. {L"iso-8859-7", 28597},
  18. {L"iso-8859-8", 28598},
  19. {L"iso-8859-9", 28599},
  20. {L"iso-8859-15", 28605},
  21. {L"windows-1251", 1251},
  22. {L"windows-1252", 1252},
  23. {L"windows-1253", 1253},
  24. {L"windows-1254", 1254},
  25. {L"windows-1255", 1255},
  26. {L"windows-1256", 1256},
  27. {L"windows-1257", 1257},
  28. {L"windows-1258", 1258},
  29. };
  30. void MakeMap(int *map, UINT codepage)
  31. {
  32. unsigned char i=0;
  33. unsigned char mb[2] = {0,0};
  34. wchar_t temp[2] = {0};
  35. do
  36. {
  37. mb[0]=i;
  38. // if (IsDBLeadByteEx(codepage, i)) map[i]=-2; else {
  39. int len = MultiByteToWideChar(codepage, 0, (char *)mb, 2, 0, 0);
  40. switch(len)
  41. {
  42. case 0:
  43. map[i]=-1;
  44. break;
  45. case 2:
  46. {
  47. MultiByteToWideChar(codepage, 0, (char *)mb, 2, temp, 2);
  48. map[i]=temp[0];
  49. }
  50. break;
  51. }
  52. // }
  53. } while (i++ != 255);
  54. }
  55. #define NUM_ENCODINGS (sizeof(encodings)/sizeof(WindowsEncodings))
  56. int XMLCALL UnknownEncoding(void *data, const wchar_t *name, XML_Encoding *info)
  57. {
  58. for (int i=0;i<NUM_ENCODINGS;i++)
  59. {
  60. if (!_wcsicmp(name, encodings[i].name))
  61. {
  62. MakeMap(info->map, encodings[i].codePage);
  63. info->data = 0;
  64. info->convert = 0;
  65. info->release = 0;
  66. return XML_STATUS_OK;
  67. }
  68. }
  69. return XML_STATUS_ERROR;
  70. }
  71. int XMLCALL UnknownEncoding(void* data, const char* name, XML_Encoding* info)
  72. {
  73. auto wszName = wa::strings::wa_string(name).GetW().c_str();
  74. return UnknownEncoding(data, wszName, info);
  75. }