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