1
0

encodings_c.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "expat/expat.h"
  2. #include <CoreFoundation/CoreFoundation.h>
  3. #include <wchar.h>
  4. // TODO: make this work
  5. struct AppleEncodings
  6. {
  7. const wchar_t *name;
  8. CFStringEncoding codePage;
  9. };
  10. static const AppleEncodings encodings[] =
  11. {
  12. {L"iso-8859-2", kCFStringEncodingISOLatin2},
  13. {L"iso-8859-3", kCFStringEncodingISOLatin3},
  14. {L"iso-8859-4", kCFStringEncodingISOLatin4},
  15. {L"iso-8859-5", kCFStringEncodingISOLatinCyrillic},
  16. {L"iso-8859-6", kCFStringEncodingISOLatinArabic},
  17. {L"iso-8859-7", kCFStringEncodingISOLatinGreek},
  18. {L"iso-8859-8", kCFStringEncodingISOLatinHebrew},
  19. {L"iso-8859-9", kCFStringEncodingISOLatin5},
  20. {L"iso-8859-10", kCFStringEncodingISOLatin6},
  21. {L"iso-8859-11", kCFStringEncodingISOLatinThai},
  22. {L"iso-8859-13", kCFStringEncodingISOLatin7},
  23. {L"iso-8859-14", kCFStringEncodingISOLatin8},
  24. {L"iso-8859-15", kCFStringEncodingISOLatin9},
  25. {L"windows-1251", kCFStringEncodingWindowsCyrillic},
  26. {L"windows-1252", kCFStringEncodingWindowsLatin1},
  27. {L"windows-1253", kCFStringEncodingWindowsGreek},
  28. {L"windows-1254", kCFStringEncodingWindowsLatin5},
  29. {L"windows-1255", kCFStringEncodingWindowsHebrew},
  30. {L"windows-1256", kCFStringEncodingWindowsArabic},
  31. {L"windows-1257", kCFStringEncodingWindowsBalticRim},
  32. {L"windows-1258", kCFStringEncodingWindowsVietnamese},
  33. };
  34. static void MakeMap(int *map, CFStringEncoding encoding)
  35. {
  36. unsigned char i=0;
  37. unsigned char mb[2] = {0,0};
  38. do
  39. {
  40. mb[0]=i;
  41. // if (IsDBLeadByteEx(codepage, i)) map[i]=-2; else {
  42. CFStringRef cfstr = CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)mb, 1, encoding, false);
  43. if (!cfstr || CFStringGetBytes(cfstr, CFRangeMake(0,1), kCFStringEncodingUTF32, 0, false, (UInt8 *)(map+i), sizeof(*map), 0))
  44. {
  45. map[i]=-1;
  46. }
  47. // }
  48. } while (i++ != 255);
  49. }
  50. static bool wcsmatch(const wchar_t *a, const wchar_t *b)
  51. {
  52. if (!a || !b)
  53. return false; // wtf
  54. while (*a || *b)
  55. {
  56. if (!*a)
  57. return false;
  58. if (!*b)
  59. return false;
  60. if (towlower(*a) != towlower(*b))
  61. return false;
  62. a++;
  63. b++;
  64. }
  65. return true;
  66. }
  67. #define NUM_ENCODINGS (sizeof(encodings)/sizeof(AppleEncodings))
  68. int XMLCALL UnknownEncoding(void *data, const XML_Char *name, XML_Encoding *info)
  69. {
  70. for (int i=0;i<NUM_ENCODINGS;i++)
  71. {
  72. if (!wcsmatch(name, encodings[i].name))
  73. {
  74. MakeMap(info->map, encodings[i].codePage);
  75. info->data = 0;
  76. info->convert = 0;
  77. info->release = 0;
  78. return XML_STATUS_OK;
  79. }
  80. }
  81. return XML_STATUS_ERROR;
  82. }