summaryrefslogtreecommitdiffstats
path: root/gl/lib/lc-charset-dispatch.c
blob: cd74466b4cd6410b3497ac0e5ce5e23f1c3b393b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Dispatching based on the current locale's character encoding.
   Copyright (C) 2018-2023 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2018.  */

#include <config.h>

/* Specification.  */
#include "lc-charset-dispatch.h"

#if GNULIB_defined_mbstate_t

# include "localcharset.h"
# include "streq.h"

# if GNULIB_WCHAR_SINGLE_LOCALE
/* When we know that the locale does not change, provide a speedup by
   caching the value of locale_encoding_classification.  */
#  define locale_encoding_classification_cached locale_encoding_classification
# else
/* By default, don't make assumptions, hence no caching.  */
#  define locale_encoding_classification_uncached locale_encoding_classification
# endif

# if GNULIB_WCHAR_SINGLE_LOCALE
static inline
# endif
enc_t
locale_encoding_classification_uncached (void)
{
  const char *encoding = locale_charset ();
  if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
    return enc_utf8;
  if (STREQ_OPT (encoding, "EUC-JP", 'E', 'U', 'C', '-', 'J', 'P', 0, 0, 0))
    return enc_eucjp;
  if (STREQ_OPT (encoding, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
      || STREQ_OPT (encoding, "GB2312", 'G', 'B', '2', '3', '1', '2', 0, 0, 0)
      || STREQ_OPT (encoding, "BIG5", 'B', 'I', 'G', '5', 0, 0, 0, 0, 0))
    return enc_94;
  if (STREQ_OPT (encoding, "EUC-TW", 'E', 'U', 'C', '-', 'T', 'W', 0, 0, 0))
    return enc_euctw;
  if (STREQ_OPT (encoding, "GB18030", 'G', 'B', '1', '8', '0', '3', '0', 0, 0))
    return enc_gb18030;
  if (STREQ_OPT (encoding, "SJIS", 'S', 'J', 'I', 'S', 0, 0, 0, 0, 0))
    return enc_sjis;
  return enc_other;
}

# if GNULIB_WCHAR_SINGLE_LOCALE

static int cached_locale_enc = -1;

enc_t
locale_encoding_classification_cached (void)
{
  if (cached_locale_enc < 0)
    cached_locale_enc = locale_encoding_classification_uncached ();
  return cached_locale_enc;
}

# endif

#else

/* This declaration is solely to ensure that after preprocessing
   this file is never empty.  */
typedef int dummy;

#endif