Ticket #3723: fix positioning of skin selector dialog.
[midnight-commander.git] / lib / charsets.c
blob9a080542ed4bfe8420674b4deb4f5ae5eb949a2a
1 /*
2 Text conversion from one charset to another.
4 Copyright (C) 2001-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Walery Studennikov <despair@sama.ru>
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file charsets.c
27 * \brief Source: Text conversion from one charset to another
30 #include <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "lib/global.h"
37 #include "lib/strutil.h" /* utf-8 functions */
38 #include "lib/fileloc.h"
39 #include "lib/charsets.h"
41 /*** global variables ****************************************************************************/
43 GPtrArray *codepages = NULL;
45 unsigned char conv_displ[256];
46 unsigned char conv_input[256];
48 const char *cp_display = NULL;
49 const char *cp_source = NULL;
51 /*** file scope macro definitions ****************************************************************/
53 #define UNKNCHAR '\001'
55 #define OTHER_8BIT "Other_8_bit"
57 /*** file scope type declarations ****************************************************************/
59 /*** file scope variables ************************************************************************/
61 /*** file scope functions ************************************************************************/
62 /* --------------------------------------------------------------------------------------------- */
64 static codepage_desc *
65 new_codepage_desc (const char *id, const char *name)
67 codepage_desc *desc;
69 desc = g_new (codepage_desc, 1);
70 desc->id = g_strdup (id);
71 desc->name = g_strdup (name);
73 return desc;
76 /* --------------------------------------------------------------------------------------------- */
78 static void
79 free_codepage_desc (gpointer data, gpointer user_data)
81 codepage_desc *desc = (codepage_desc *) data;
82 (void) user_data;
84 g_free (desc->id);
85 g_free (desc->name);
86 g_free (desc);
89 /* --------------------------------------------------------------------------------------------- */
90 /* returns display codepage */
92 static void
93 load_codepages_list_from_file (GPtrArray ** list, const char *fname)
95 FILE *f;
96 char buf[BUF_MEDIUM];
97 char *default_codepage = NULL;
99 f = fopen (fname, "r");
100 if (f == NULL)
101 return;
103 while (fgets (buf, sizeof buf, f) != NULL)
105 /* split string into id and cpname */
106 char *p = buf;
107 size_t buflen = strlen (buf);
109 if (*p == '\n' || *p == '\0' || *p == '#')
110 continue;
112 if (buflen > 0 && buf[buflen - 1] == '\n')
113 buf[buflen - 1] = '\0';
114 while (*p != '\t' && *p != ' ' && *p != '\0')
115 ++p;
116 if (*p == '\0')
117 goto fail;
119 *p++ = '\0';
120 g_strstrip (p);
121 if (*p == '\0')
122 goto fail;
124 if (strcmp (buf, "default") == 0)
125 default_codepage = g_strdup (p);
126 else
128 const char *id = buf;
130 if (*list == NULL)
132 *list = g_ptr_array_sized_new (16);
133 g_ptr_array_add (*list, new_codepage_desc (id, p));
135 else
137 unsigned int i;
139 /* whether id is already present in list */
140 /* if yes, overwrite description */
141 for (i = 0; i < (*list)->len; i++)
143 codepage_desc *desc;
145 desc = (codepage_desc *) g_ptr_array_index (*list, i);
147 if (strcmp (id, desc->id) == 0)
149 /* found */
150 g_free (desc->name);
151 desc->name = g_strdup (p);
152 break;
156 /* not found */
157 if (i == (*list)->len)
158 g_ptr_array_add (*list, new_codepage_desc (id, p));
163 if (default_codepage != NULL)
165 mc_global.display_codepage = get_codepage_index (default_codepage);
166 g_free (default_codepage);
169 fail:
170 fclose (f);
173 /* --------------------------------------------------------------------------------------------- */
175 static char
176 translate_character (GIConv cd, char c)
178 gchar *tmp_buff = NULL;
179 gsize bytes_read, bytes_written = 0;
180 const char *ibuf = &c;
181 char ch = UNKNCHAR;
183 int ibuflen = 1;
185 tmp_buff = g_convert_with_iconv (ibuf, ibuflen, cd, &bytes_read, &bytes_written, NULL);
186 if (tmp_buff)
187 ch = tmp_buff[0];
188 g_free (tmp_buff);
189 return ch;
192 /* --------------------------------------------------------------------------------------------- */
193 /*** public functions ****************************************************************************/
194 /* --------------------------------------------------------------------------------------------- */
196 void
197 load_codepages_list (void)
199 char *fname;
201 /* 1: try load /usr/share/mc/mc.charsets */
202 fname = g_build_filename (mc_global.share_data_dir, CHARSETS_LIST, (char *) NULL);
203 load_codepages_list_from_file (&codepages, fname);
204 g_free (fname);
206 /* 2: try load /etc/mc/mc.charsets */
207 fname = g_build_filename (mc_global.sysconfig_dir, CHARSETS_LIST, (char *) NULL);
208 load_codepages_list_from_file (&codepages, fname);
209 g_free (fname);
211 if (codepages == NULL)
213 /* files are not found, add defaullt codepage */
214 fprintf (stderr, "%s\n", _("Warning: cannot load codepages list"));
216 codepages = g_ptr_array_new ();
217 g_ptr_array_add (codepages, new_codepage_desc (DEFAULT_CHARSET, _("7-bit ASCII")));
221 /* --------------------------------------------------------------------------------------------- */
223 void
224 free_codepages_list (void)
226 g_ptr_array_foreach (codepages, free_codepage_desc, NULL);
227 g_ptr_array_free (codepages, TRUE);
230 /* --------------------------------------------------------------------------------------------- */
232 const char *
233 get_codepage_id (const int n)
235 return (n < 0) ? OTHER_8BIT : ((codepage_desc *) g_ptr_array_index (codepages, n))->id;
238 /* --------------------------------------------------------------------------------------------- */
241 get_codepage_index (const char *id)
243 size_t i;
244 if (strcmp (id, OTHER_8BIT) == 0)
245 return -1;
246 if (codepages == NULL)
247 return -1;
248 for (i = 0; i < codepages->len; i++)
249 if (strcmp (id, ((codepage_desc *) g_ptr_array_index (codepages, i))->id) == 0)
250 return i;
251 return -1;
254 /* --------------------------------------------------------------------------------------------- */
255 /** Check if specified encoding can be used in mc.
256 * @param encoding name of encoding
257 * @return TRUE if encoding is supported by mc, FALSE otherwise
260 gboolean
261 is_supported_encoding (const char *encoding)
263 gboolean result = FALSE;
264 guint t;
266 for (t = 0; t < codepages->len; t++)
268 const char *id = ((codepage_desc *) g_ptr_array_index (codepages, t))->id;
269 result |= (g_ascii_strncasecmp (encoding, id, strlen (id)) == 0);
272 return result;
275 /* --------------------------------------------------------------------------------------------- */
277 char *
278 init_translation_table (int cpsource, int cpdisplay)
280 int i;
281 GIConv cd;
283 /* Fill inpit <-> display tables */
285 if (cpsource < 0 || cpdisplay < 0 || cpsource == cpdisplay)
287 for (i = 0; i <= 255; ++i)
289 conv_displ[i] = i;
290 conv_input[i] = i;
291 cp_source = cp_display;
293 return NULL;
296 for (i = 0; i <= 127; ++i)
298 conv_displ[i] = i;
299 conv_input[i] = i;
301 cp_source = ((codepage_desc *) g_ptr_array_index (codepages, cpsource))->id;
302 cp_display = ((codepage_desc *) g_ptr_array_index (codepages, cpdisplay))->id;
304 /* display <- inpit table */
306 cd = g_iconv_open (cp_display, cp_source);
307 if (cd == INVALID_CONV)
308 return g_strdup_printf (_("Cannot translate from %s to %s"), cp_source, cp_display);
310 for (i = 128; i <= 255; ++i)
311 conv_displ[i] = translate_character (cd, i);
313 g_iconv_close (cd);
315 /* inpit <- display table */
317 cd = g_iconv_open (cp_source, cp_display);
318 if (cd == INVALID_CONV)
319 return g_strdup_printf (_("Cannot translate from %s to %s"), cp_display, cp_source);
321 for (i = 128; i <= 255; ++i)
323 unsigned char ch;
324 ch = translate_character (cd, i);
325 conv_input[i] = (ch == UNKNCHAR) ? i : ch;
328 g_iconv_close (cd);
330 return NULL;
333 /* --------------------------------------------------------------------------------------------- */
335 void
336 convert_to_display (char *str)
338 if (!str)
339 return;
341 while (*str)
343 *str = conv_displ[(unsigned char) *str];
344 str++;
348 /* --------------------------------------------------------------------------------------------- */
350 GString *
351 str_convert_to_display (const char *str)
353 return str_nconvert_to_display (str, -1);
357 /* --------------------------------------------------------------------------------------------- */
359 GString *
360 str_nconvert_to_display (const char *str, int len)
362 GString *buff;
363 GIConv conv;
365 if (!str)
366 return g_string_new ("");
368 if (cp_display == cp_source)
369 return g_string_new (str);
371 conv = str_crt_conv_from (cp_source);
373 buff = g_string_new ("");
374 str_nconvert (conv, str, len, buff);
375 str_close_conv (conv);
376 return buff;
379 /* --------------------------------------------------------------------------------------------- */
381 void
382 convert_from_input (char *str)
384 if (!str)
385 return;
387 while (*str)
389 *str = conv_input[(unsigned char) *str];
390 str++;
394 /* --------------------------------------------------------------------------------------------- */
396 GString *
397 str_convert_to_input (const char *str)
399 return str_nconvert_to_input (str, -1);
402 /* --------------------------------------------------------------------------------------------- */
404 GString *
405 str_nconvert_to_input (const char *str, int len)
407 GString *buff;
408 GIConv conv;
410 if (!str)
411 return g_string_new ("");
413 if (cp_display == cp_source)
414 return g_string_new (str);
416 conv = str_crt_conv_to (cp_source);
418 buff = g_string_new ("");
419 str_nconvert (conv, str, len, buff);
420 str_close_conv (conv);
421 return buff;
424 /* --------------------------------------------------------------------------------------------- */
426 unsigned char
427 convert_from_utf_to_current (const char *str)
429 unsigned char buf_ch[UTF8_CHAR_LEN + 1];
430 unsigned char ch = '.';
431 GIConv conv;
432 const char *cp_to;
434 if (str == NULL)
435 return '.';
437 cp_to = get_codepage_id (mc_global.source_codepage);
438 conv = str_crt_conv_to (cp_to);
440 if (conv != INVALID_CONV)
442 switch (str_translate_char (conv, str, -1, (char *) buf_ch, sizeof (buf_ch)))
444 case ESTR_SUCCESS:
445 ch = buf_ch[0];
446 break;
447 case ESTR_PROBLEM:
448 case ESTR_FAILURE:
449 ch = '.';
450 break;
451 default:
452 break;
454 str_close_conv (conv);
457 return ch;
460 /* --------------------------------------------------------------------------------------------- */
462 unsigned char
463 convert_from_utf_to_current_c (int input_char, GIConv conv)
465 unsigned char str[UTF8_CHAR_LEN + 1];
466 unsigned char buf_ch[UTF8_CHAR_LEN + 1];
467 unsigned char ch = '.';
468 int res;
470 res = g_unichar_to_utf8 (input_char, (char *) str);
471 if (res == 0)
472 return ch;
474 str[res] = '\0';
476 switch (str_translate_char (conv, (char *) str, -1, (char *) buf_ch, sizeof (buf_ch)))
478 case ESTR_SUCCESS:
479 ch = buf_ch[0];
480 break;
481 case ESTR_PROBLEM:
482 case ESTR_FAILURE:
483 ch = '.';
484 break;
485 default:
486 break;
489 return ch;
492 /* --------------------------------------------------------------------------------------------- */
495 convert_from_8bit_to_utf_c (char input_char, GIConv conv)
497 unsigned char str[2];
498 unsigned char buf_ch[UTF8_CHAR_LEN + 1];
499 int ch;
501 str[0] = (unsigned char) input_char;
502 str[1] = '\0';
504 switch (str_translate_char (conv, (char *) str, -1, (char *) buf_ch, sizeof (buf_ch)))
506 case ESTR_SUCCESS:
508 int res;
510 res = g_utf8_get_char_validated ((char *) buf_ch, -1);
511 ch = res >= 0 ? res : buf_ch[0];
512 break;
514 case ESTR_PROBLEM:
515 case ESTR_FAILURE:
516 default:
517 ch = '.';
518 break;
521 return ch;
524 /* --------------------------------------------------------------------------------------------- */
527 convert_from_8bit_to_utf_c2 (char input_char)
529 unsigned char str[2];
530 int ch = '.';
531 GIConv conv;
532 const char *cp_from;
534 str[0] = (unsigned char) input_char;
535 str[1] = '\0';
537 cp_from = get_codepage_id (mc_global.source_codepage);
538 conv = str_crt_conv_to (cp_from);
540 if (conv != INVALID_CONV)
542 unsigned char buf_ch[UTF8_CHAR_LEN + 1];
544 switch (str_translate_char (conv, (char *) str, -1, (char *) buf_ch, sizeof (buf_ch)))
546 case ESTR_SUCCESS:
548 int res;
550 res = g_utf8_get_char_validated ((char *) buf_ch, -1);
551 ch = res >= 0 ? res : buf_ch[0];
552 break;
554 case ESTR_PROBLEM:
555 case ESTR_FAILURE:
556 default:
557 ch = '.';
558 break;
560 str_close_conv (conv);
563 return ch;
566 /* --------------------------------------------------------------------------------------------- */