localenc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * localenc.c - translate our internal character set codes to and from
  3. * our own set of plausibly legible character-set names. Also
  4. * provides a canonical name for each encoding (useful for software
  5. * announcing what character set it will be using), and a set of
  6. * enumeration functions which return a list of supported
  7. * encodings one by one.
  8. *
  9. * charset_from_localenc will attempt all other text translations
  10. * as well as this table, to maximise the number of different ways
  11. * you can select a supported charset.
  12. */
  13. #include <ctype.h>
  14. #include "charset.h"
  15. #include "internal.h"
  16. static const struct {
  17. const char *name;
  18. int charset;
  19. int return_in_enum; /* enumeration misses some charsets */
  20. } localencs[] = {
  21. { "<UNKNOWN>", CS_NONE, 0 },
  22. { "UTF-8", CS_UTF8, 1 },
  23. { "ISO-8859-1", CS_ISO8859_1, 1 },
  24. { "ISO-8859-1 with X11 line drawing", CS_ISO8859_1_X11, 0 },
  25. { "ISO-8859-2", CS_ISO8859_2, 1 },
  26. { "ISO-8859-3", CS_ISO8859_3, 1 },
  27. { "ISO-8859-4", CS_ISO8859_4, 1 },
  28. { "ISO-8859-5", CS_ISO8859_5, 1 },
  29. { "ISO-8859-6", CS_ISO8859_6, 1 },
  30. { "ISO-8859-7", CS_ISO8859_7, 1 },
  31. { "ISO-8859-8", CS_ISO8859_8, 1 },
  32. { "ISO-8859-9", CS_ISO8859_9, 1 },
  33. { "ISO-8859-10", CS_ISO8859_10, 1 },
  34. { "ISO-8859-11", CS_ISO8859_11, 1 },
  35. { "ISO-8859-13", CS_ISO8859_13, 1 },
  36. { "ISO-8859-14", CS_ISO8859_14, 1 },
  37. { "ISO-8859-15", CS_ISO8859_15, 1 },
  38. { "ISO-8859-16", CS_ISO8859_16, 1 },
  39. { "CP437", CS_CP437, 1 },
  40. { "CP850", CS_CP850, 1 },
  41. { "CP852", CS_CP852, 1 },
  42. { "CP866", CS_CP866, 1 },
  43. { "CP1250", CS_CP1250, 1 },
  44. { "CP1251", CS_CP1251, 1 },
  45. { "CP1252", CS_CP1252, 1 },
  46. { "CP1253", CS_CP1253, 1 },
  47. { "CP1254", CS_CP1254, 1 },
  48. { "CP1255", CS_CP1255, 1 },
  49. { "CP1256", CS_CP1256, 1 },
  50. { "CP1257", CS_CP1257, 1 },
  51. { "CP1258", CS_CP1258, 1 },
  52. { "KOI8-R", CS_KOI8_R, 1 },
  53. { "KOI8-U", CS_KOI8_U, 1 },
  54. { "Mac Roman", CS_MAC_ROMAN, 1 },
  55. { "Mac Turkish", CS_MAC_TURKISH, 1 },
  56. { "Mac Croatian", CS_MAC_CROATIAN, 1 },
  57. { "Mac Iceland", CS_MAC_ICELAND, 1 },
  58. { "Mac Romanian", CS_MAC_ROMANIAN, 1 },
  59. { "Mac Greek", CS_MAC_GREEK, 1 },
  60. { "Mac Cyrillic", CS_MAC_CYRILLIC, 1 },
  61. { "Mac Thai", CS_MAC_THAI, 1 },
  62. { "Mac Centeuro", CS_MAC_CENTEURO, 1 },
  63. { "Mac Symbol", CS_MAC_SYMBOL, 1 },
  64. { "Mac Dingbats", CS_MAC_DINGBATS, 1 },
  65. { "Mac Roman (old)", CS_MAC_ROMAN_OLD, 0 },
  66. { "Mac Croatian (old)", CS_MAC_CROATIAN_OLD, 0 },
  67. { "Mac Iceland (old)", CS_MAC_ICELAND_OLD, 0 },
  68. { "Mac Romanian (old)", CS_MAC_ROMANIAN_OLD, 0 },
  69. { "Mac Greek (old)", CS_MAC_GREEK_OLD, 0 },
  70. { "Mac Cyrillic (old)", CS_MAC_CYRILLIC_OLD, 0 },
  71. { "Mac Ukraine", CS_MAC_UKRAINE, 1 },
  72. { "Mac VT100", CS_MAC_VT100, 1 },
  73. { "Mac VT100 (old)", CS_MAC_VT100_OLD, 0 },
  74. { "VISCII", CS_VISCII, 1 },
  75. { "HP ROMAN8", CS_HP_ROMAN8, 1 },
  76. { "DEC MCS", CS_DEC_MCS, 1 },
  77. };
  78. const char *charset_to_localenc(int charset)
  79. {
  80. int i;
  81. for (i = 0; i < (int)lenof(localencs); i++)
  82. if (charset == localencs[i].charset)
  83. return localencs[i].name;
  84. return NULL; /* not found */
  85. }
  86. int charset_from_localenc(const char *name)
  87. {
  88. int i;
  89. if ( (i = charset_from_mimeenc(name)) != CS_NONE)
  90. return i;
  91. if ( (i = charset_from_xenc(name)) != CS_NONE)
  92. return i;
  93. for (i = 0; i < (int)lenof(localencs); i++) {
  94. const char *p, *q;
  95. p = name;
  96. q = localencs[i].name;
  97. while (*p || *q) {
  98. if (tolower((unsigned char)*p) != tolower((unsigned char)*q))
  99. break;
  100. p++; q++;
  101. }
  102. if (!*p && !*q)
  103. return localencs[i].charset;
  104. }
  105. return CS_NONE; /* not found */
  106. }
  107. int charset_localenc_nth(int n)
  108. {
  109. int i;
  110. for (i = 0; i < (int)lenof(localencs); i++)
  111. if (localencs[i].return_in_enum && !n--)
  112. return localencs[i].charset;
  113. return CS_NONE; /* end of list */
  114. }