1/* Dispatching based on the current locale's character encoding.
2   Copyright (C) 2018-2022 Free Software Foundation, Inc.
3
4   This file is free software: you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   This file is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2018.  */
18
19#include <config.h>
20
21/* Specification.  */
22#include "lc-charset-dispatch.h"
23
24#if GNULIB_defined_mbstate_t
25
26# include "localcharset.h"
27# include "streq.h"
28
29# if GNULIB_WCHAR_SINGLE_LOCALE
30/* When we know that the locale does not change, provide a speedup by
31   caching the value of locale_encoding_classification.  */
32#  define locale_encoding_classification_cached locale_encoding_classification
33# else
34/* By default, don't make assumptions, hence no caching.  */
35#  define locale_encoding_classification_uncached locale_encoding_classification
36# endif
37
38# if GNULIB_WCHAR_SINGLE_LOCALE
39static inline
40# endif
41enc_t
42locale_encoding_classification_uncached (void)
43{
44  const char *encoding = locale_charset ();
45  if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
46    return enc_utf8;
47  if (STREQ_OPT (encoding, "EUC-JP", 'E', 'U', 'C', '-', 'J', 'P', 0, 0, 0))
48    return enc_eucjp;
49  if (STREQ_OPT (encoding, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
50      || STREQ_OPT (encoding, "GB2312", 'G', 'B', '2', '3', '1', '2', 0, 0, 0)
51      || STREQ_OPT (encoding, "BIG5", 'B', 'I', 'G', '5', 0, 0, 0, 0, 0))
52    return enc_94;
53  if (STREQ_OPT (encoding, "EUC-TW", 'E', 'U', 'C', '-', 'T', 'W', 0, 0, 0))
54    return enc_euctw;
55  if (STREQ_OPT (encoding, "GB18030", 'G', 'B', '1', '8', '0', '3', '0', 0, 0))
56    return enc_gb18030;
57  if (STREQ_OPT (encoding, "SJIS", 'S', 'J', 'I', 'S', 0, 0, 0, 0, 0))
58    return enc_sjis;
59  return enc_other;
60}
61
62# if GNULIB_WCHAR_SINGLE_LOCALE
63
64static int cached_locale_enc = -1;
65
66enc_t
67locale_encoding_classification_cached (void)
68{
69  if (cached_locale_enc < 0)
70    cached_locale_enc = locale_encoding_classification_uncached ();
71  return cached_locale_enc;
72}
73
74# endif
75
76#else
77
78/* This declaration is solely to ensure that after preprocessing
79   this file is never empty.  */
80typedef int dummy;
81
82#endif
83