xenc.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * xenc.c - translate our internal character set codes to and from
  3. * X11 character encoding names.
  4. *
  5. */
  6. #include <ctype.h>
  7. #include "charset.h"
  8. #include "internal.h"
  9. static const struct {
  10. const char *name;
  11. int charset;
  12. } xencs[] = {
  13. /*
  14. * Officially registered encoding names. This list is derived
  15. * from the font encodings section of
  16. *
  17. * http://ftp.x.org/pub/DOCS/registry
  18. *
  19. * Where multiple encoding names map to the same encoding id
  20. * (such as iso8859-15 and fcd8859-15), the first is considered
  21. * canonical and will be returned when translating the id to a
  22. * string.
  23. */
  24. { "iso8859-1", CS_ISO8859_1 },
  25. { "iso8859-2", CS_ISO8859_2 },
  26. { "iso8859-3", CS_ISO8859_3 },
  27. { "iso8859-4", CS_ISO8859_4 },
  28. { "iso8859-5", CS_ISO8859_5 },
  29. { "iso8859-6", CS_ISO8859_6 },
  30. { "iso8859-7", CS_ISO8859_7 },
  31. { "iso8859-8", CS_ISO8859_8 },
  32. { "iso8859-9", CS_ISO8859_9 },
  33. { "iso8859-10", CS_ISO8859_10 },
  34. { "iso8859-13", CS_ISO8859_13 },
  35. { "iso8859-14", CS_ISO8859_14 },
  36. { "iso8859-15", CS_ISO8859_15 },
  37. { "fcd8859-15", CS_ISO8859_15 },
  38. { "hp-roman8", CS_HP_ROMAN8 },
  39. { "koi8-r", CS_KOI8_R },
  40. /*
  41. * Unofficial encoding names found in the wild.
  42. */
  43. { "iso8859-16", CS_ISO8859_16 },
  44. { "koi8-u", CS_KOI8_U },
  45. { "ibm-cp437", CS_CP437 },
  46. { "ibm-cp850", CS_CP850 },
  47. { "ibm-cp852", CS_CP852 },
  48. { "ibm-cp866", CS_CP866 },
  49. { "microsoft-cp1250", CS_CP1250 },
  50. { "microsoft-cp1251", CS_CP1251 },
  51. { "microsoft-cp1252", CS_CP1252 },
  52. { "microsoft-cp1253", CS_CP1253 },
  53. { "microsoft-cp1254", CS_CP1254 },
  54. { "microsoft-cp1255", CS_CP1255 },
  55. { "microsoft-cp1256", CS_CP1256 },
  56. { "microsoft-cp1257", CS_CP1257 },
  57. { "microsoft-cp1258", CS_CP1258 },
  58. { "mac-roman", CS_MAC_ROMAN },
  59. { "viscii1.1-1", CS_VISCII },
  60. { "viscii1-1", CS_VISCII },
  61. };
  62. const char *charset_to_xenc(int charset)
  63. {
  64. int i;
  65. for (i = 0; i < (int)lenof(xencs); i++)
  66. if (charset == xencs[i].charset)
  67. return xencs[i].name;
  68. return NULL; /* not found */
  69. }
  70. int charset_from_xenc(const char *name)
  71. {
  72. int i;
  73. for (i = 0; i < (int)lenof(xencs); i++) {
  74. const char *p, *q;
  75. p = name;
  76. q = xencs[i].name;
  77. while (*p || *q) {
  78. if (tolower((unsigned char)*p) != tolower((unsigned char)*q))
  79. break;
  80. p++; q++;
  81. }
  82. if (!*p && !*q)
  83. return xencs[i].charset;
  84. }
  85. return CS_NONE; /* not found */
  86. }