charsets.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. Text conversion from one charset to another.
  3. Copyright (C) 2001-2018
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Walery Studennikov <despair@sama.ru>
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file charsets.c
  20. * \brief Source: Text conversion from one charset to another
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "lib/global.h"
  27. #include "lib/strutil.h" /* utf-8 functions */
  28. #include "lib/fileloc.h"
  29. #include "lib/util.h" /* whitespace() */
  30. #include "lib/charsets.h"
  31. /*** global variables ****************************************************************************/
  32. GPtrArray *codepages = NULL;
  33. unsigned char conv_displ[256];
  34. unsigned char conv_input[256];
  35. const char *cp_display = NULL;
  36. const char *cp_source = NULL;
  37. /*** file scope macro definitions ****************************************************************/
  38. #define UNKNCHAR '\001'
  39. #define OTHER_8BIT "Other_8_bit"
  40. /*** file scope type declarations ****************************************************************/
  41. /*** file scope variables ************************************************************************/
  42. /*** file scope functions ************************************************************************/
  43. /* --------------------------------------------------------------------------------------------- */
  44. static codepage_desc *
  45. new_codepage_desc (const char *id, const char *name)
  46. {
  47. codepage_desc *desc;
  48. desc = g_new (codepage_desc, 1);
  49. desc->id = g_strdup (id);
  50. desc->name = g_strdup (name);
  51. return desc;
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. static void
  55. free_codepage_desc (gpointer data, gpointer user_data)
  56. {
  57. codepage_desc *desc = (codepage_desc *) data;
  58. (void) user_data;
  59. g_free (desc->id);
  60. g_free (desc->name);
  61. g_free (desc);
  62. }
  63. /* --------------------------------------------------------------------------------------------- */
  64. /* returns display codepage */
  65. static void
  66. load_codepages_list_from_file (GPtrArray ** list, const char *fname)
  67. {
  68. FILE *f;
  69. char buf[BUF_MEDIUM];
  70. char *default_codepage = NULL;
  71. f = fopen (fname, "r");
  72. if (f == NULL)
  73. return;
  74. while (fgets (buf, sizeof buf, f) != NULL)
  75. {
  76. /* split string into id and cpname */
  77. char *p = buf;
  78. size_t buflen;
  79. if (*p == '\n' || *p == '\0' || *p == '#')
  80. continue;
  81. buflen = strlen (buf);
  82. if (buflen != 0 && buf[buflen - 1] == '\n')
  83. buf[buflen - 1] = '\0';
  84. while (*p != '\0' && !whitespace (*p))
  85. ++p;
  86. if (*p == '\0')
  87. goto fail;
  88. *p++ = '\0';
  89. g_strstrip (p);
  90. if (*p == '\0')
  91. goto fail;
  92. if (strcmp (buf, "default") == 0)
  93. default_codepage = g_strdup (p);
  94. else
  95. {
  96. const char *id = buf;
  97. if (*list == NULL)
  98. {
  99. *list = g_ptr_array_sized_new (16);
  100. g_ptr_array_add (*list, new_codepage_desc (id, p));
  101. }
  102. else
  103. {
  104. unsigned int i;
  105. /* whether id is already present in list */
  106. /* if yes, overwrite description */
  107. for (i = 0; i < (*list)->len; i++)
  108. {
  109. codepage_desc *desc;
  110. desc = (codepage_desc *) g_ptr_array_index (*list, i);
  111. if (strcmp (id, desc->id) == 0)
  112. {
  113. /* found */
  114. g_free (desc->name);
  115. desc->name = g_strdup (p);
  116. break;
  117. }
  118. }
  119. /* not found */
  120. if (i == (*list)->len)
  121. g_ptr_array_add (*list, new_codepage_desc (id, p));
  122. }
  123. }
  124. }
  125. if (default_codepage != NULL)
  126. {
  127. mc_global.display_codepage = get_codepage_index (default_codepage);
  128. g_free (default_codepage);
  129. }
  130. fail:
  131. fclose (f);
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. static char
  135. translate_character (GIConv cd, char c)
  136. {
  137. gchar *tmp_buff = NULL;
  138. gsize bytes_read, bytes_written = 0;
  139. const char *ibuf = &c;
  140. char ch = UNKNCHAR;
  141. int ibuflen = 1;
  142. tmp_buff = g_convert_with_iconv (ibuf, ibuflen, cd, &bytes_read, &bytes_written, NULL);
  143. if (tmp_buff != NULL)
  144. ch = tmp_buff[0];
  145. g_free (tmp_buff);
  146. return ch;
  147. }
  148. /* --------------------------------------------------------------------------------------------- */
  149. /*** public functions ****************************************************************************/
  150. /* --------------------------------------------------------------------------------------------- */
  151. void
  152. load_codepages_list (void)
  153. {
  154. char *fname;
  155. /* 1: try load /usr/share/mc/mc.charsets */
  156. fname = g_build_filename (mc_global.share_data_dir, CHARSETS_LIST, (char *) NULL);
  157. load_codepages_list_from_file (&codepages, fname);
  158. g_free (fname);
  159. /* 2: try load /etc/mc/mc.charsets */
  160. fname = g_build_filename (mc_global.sysconfig_dir, CHARSETS_LIST, (char *) NULL);
  161. load_codepages_list_from_file (&codepages, fname);
  162. g_free (fname);
  163. if (codepages == NULL)
  164. {
  165. /* files are not found, add defaullt codepage */
  166. fprintf (stderr, "%s\n", _("Warning: cannot load codepages list"));
  167. codepages = g_ptr_array_new ();
  168. g_ptr_array_add (codepages, new_codepage_desc (DEFAULT_CHARSET, _("7-bit ASCII")));
  169. }
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. void
  173. free_codepages_list (void)
  174. {
  175. g_ptr_array_foreach (codepages, free_codepage_desc, NULL);
  176. g_ptr_array_free (codepages, TRUE);
  177. /* NULL-ize pointer to make unit tests happy */
  178. codepages = NULL;
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. const char *
  182. get_codepage_id (const int n)
  183. {
  184. return (n < 0) ? OTHER_8BIT : ((codepage_desc *) g_ptr_array_index (codepages, n))->id;
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. int
  188. get_codepage_index (const char *id)
  189. {
  190. size_t i;
  191. if (codepages == NULL)
  192. return -1;
  193. if (strcmp (id, OTHER_8BIT) == 0)
  194. return -1;
  195. for (i = 0; i < codepages->len; i++)
  196. if (strcmp (id, ((codepage_desc *) g_ptr_array_index (codepages, i))->id) == 0)
  197. return i;
  198. return -1;
  199. }
  200. /* --------------------------------------------------------------------------------------------- */
  201. /** Check if specified encoding can be used in mc.
  202. * @param encoding name of encoding
  203. * @return TRUE if encoding is supported by mc, FALSE otherwise
  204. */
  205. gboolean
  206. is_supported_encoding (const char *encoding)
  207. {
  208. gboolean result = FALSE;
  209. guint t;
  210. for (t = 0; t < codepages->len; t++)
  211. {
  212. const char *id;
  213. id = ((codepage_desc *) g_ptr_array_index (codepages, t))->id;
  214. result |= (g_ascii_strncasecmp (encoding, id, strlen (id)) == 0);
  215. }
  216. return result;
  217. }
  218. /* --------------------------------------------------------------------------------------------- */
  219. char *
  220. init_translation_table (int cpsource, int cpdisplay)
  221. {
  222. int i;
  223. GIConv cd;
  224. /* Fill inpit <-> display tables */
  225. if (cpsource < 0 || cpdisplay < 0 || cpsource == cpdisplay)
  226. {
  227. for (i = 0; i <= 255; ++i)
  228. {
  229. conv_displ[i] = i;
  230. conv_input[i] = i;
  231. }
  232. cp_source = cp_display;
  233. return NULL;
  234. }
  235. for (i = 0; i <= 127; ++i)
  236. {
  237. conv_displ[i] = i;
  238. conv_input[i] = i;
  239. }
  240. cp_source = ((codepage_desc *) g_ptr_array_index (codepages, cpsource))->id;
  241. cp_display = ((codepage_desc *) g_ptr_array_index (codepages, cpdisplay))->id;
  242. /* display <- inpit table */
  243. cd = g_iconv_open (cp_display, cp_source);
  244. if (cd == INVALID_CONV)
  245. return g_strdup_printf (_("Cannot translate from %s to %s"), cp_source, cp_display);
  246. for (i = 128; i <= 255; ++i)
  247. conv_displ[i] = translate_character (cd, i);
  248. g_iconv_close (cd);
  249. /* inpit <- display table */
  250. cd = g_iconv_open (cp_source, cp_display);
  251. if (cd == INVALID_CONV)
  252. return g_strdup_printf (_("Cannot translate from %s to %s"), cp_display, cp_source);
  253. for (i = 128; i <= 255; ++i)
  254. {
  255. unsigned char ch;
  256. ch = translate_character (cd, i);
  257. conv_input[i] = (ch == UNKNCHAR) ? i : ch;
  258. }
  259. g_iconv_close (cd);
  260. return NULL;
  261. }
  262. /* --------------------------------------------------------------------------------------------- */
  263. void
  264. convert_to_display (char *str)
  265. {
  266. if (str != NULL)
  267. for (; *str != '\0'; str++)
  268. *str = conv_displ[(unsigned char) *str];
  269. }
  270. /* --------------------------------------------------------------------------------------------- */
  271. GString *
  272. str_convert_to_display (const char *str)
  273. {
  274. return str_nconvert_to_display (str, -1);
  275. }
  276. /* --------------------------------------------------------------------------------------------- */
  277. GString *
  278. str_nconvert_to_display (const char *str, int len)
  279. {
  280. GString *buff;
  281. GIConv conv;
  282. if (str == NULL)
  283. return g_string_new ("");
  284. if (cp_display == cp_source)
  285. return g_string_new (str);
  286. conv = str_crt_conv_from (cp_source);
  287. buff = g_string_new ("");
  288. str_nconvert (conv, str, len, buff);
  289. str_close_conv (conv);
  290. return buff;
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. void
  294. convert_from_input (char *str)
  295. {
  296. if (str != NULL)
  297. for (; *str != '\0'; str++)
  298. *str = conv_input[(unsigned char) *str];
  299. }
  300. /* --------------------------------------------------------------------------------------------- */
  301. GString *
  302. str_convert_to_input (const char *str)
  303. {
  304. return str_nconvert_to_input (str, -1);
  305. }
  306. /* --------------------------------------------------------------------------------------------- */
  307. GString *
  308. str_nconvert_to_input (const char *str, int len)
  309. {
  310. GString *buff;
  311. GIConv conv;
  312. if (str == NULL)
  313. return g_string_new ("");
  314. if (cp_display == cp_source)
  315. return g_string_new (str);
  316. conv = str_crt_conv_to (cp_source);
  317. buff = g_string_new ("");
  318. str_nconvert (conv, str, len, buff);
  319. str_close_conv (conv);
  320. return buff;
  321. }
  322. /* --------------------------------------------------------------------------------------------- */
  323. unsigned char
  324. convert_from_utf_to_current (const char *str)
  325. {
  326. unsigned char buf_ch[UTF8_CHAR_LEN + 1];
  327. unsigned char ch = '.';
  328. GIConv conv;
  329. const char *cp_to;
  330. if (str == NULL)
  331. return '.';
  332. cp_to = get_codepage_id (mc_global.source_codepage);
  333. conv = str_crt_conv_to (cp_to);
  334. if (conv != INVALID_CONV)
  335. {
  336. switch (str_translate_char (conv, str, -1, (char *) buf_ch, sizeof (buf_ch)))
  337. {
  338. case ESTR_SUCCESS:
  339. ch = buf_ch[0];
  340. break;
  341. case ESTR_PROBLEM:
  342. case ESTR_FAILURE:
  343. ch = '.';
  344. break;
  345. default:
  346. break;
  347. }
  348. str_close_conv (conv);
  349. }
  350. return ch;
  351. }
  352. /* --------------------------------------------------------------------------------------------- */
  353. unsigned char
  354. convert_from_utf_to_current_c (int input_char, GIConv conv)
  355. {
  356. unsigned char str[UTF8_CHAR_LEN + 1];
  357. unsigned char buf_ch[UTF8_CHAR_LEN + 1];
  358. unsigned char ch = '.';
  359. int res;
  360. res = g_unichar_to_utf8 (input_char, (char *) str);
  361. if (res == 0)
  362. return ch;
  363. str[res] = '\0';
  364. switch (str_translate_char (conv, (char *) str, -1, (char *) buf_ch, sizeof (buf_ch)))
  365. {
  366. case ESTR_SUCCESS:
  367. ch = buf_ch[0];
  368. break;
  369. case ESTR_PROBLEM:
  370. case ESTR_FAILURE:
  371. ch = '.';
  372. break;
  373. default:
  374. break;
  375. }
  376. return ch;
  377. }
  378. /* --------------------------------------------------------------------------------------------- */
  379. int
  380. convert_from_8bit_to_utf_c (char input_char, GIConv conv)
  381. {
  382. unsigned char str[2];
  383. unsigned char buf_ch[UTF8_CHAR_LEN + 1];
  384. int ch;
  385. str[0] = (unsigned char) input_char;
  386. str[1] = '\0';
  387. switch (str_translate_char (conv, (char *) str, -1, (char *) buf_ch, sizeof (buf_ch)))
  388. {
  389. case ESTR_SUCCESS:
  390. {
  391. int res;
  392. res = g_utf8_get_char_validated ((char *) buf_ch, -1);
  393. ch = res >= 0 ? res : buf_ch[0];
  394. break;
  395. }
  396. case ESTR_PROBLEM:
  397. case ESTR_FAILURE:
  398. default:
  399. ch = '.';
  400. break;
  401. }
  402. return ch;
  403. }
  404. /* --------------------------------------------------------------------------------------------- */
  405. int
  406. convert_from_8bit_to_utf_c2 (char input_char)
  407. {
  408. int ch = '.';
  409. GIConv conv;
  410. const char *cp_from;
  411. cp_from = get_codepage_id (mc_global.source_codepage);
  412. conv = str_crt_conv_to (cp_from);
  413. if (conv != INVALID_CONV)
  414. {
  415. ch = convert_from_8bit_to_utf_c (input_char, conv);
  416. str_close_conv (conv);
  417. }
  418. return ch;
  419. }
  420. /* --------------------------------------------------------------------------------------------- */