xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/lc-charset-dispatch.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* Dispatching based on the current locale's character encoding.
2*4b169a6bSchristos    Copyright (C) 2018-2022 Free Software Foundation, Inc.
38dffb485Schristos 
4*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
5*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
6*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
7*4b169a6bSchristos    License, or (at your option) any later version.
88dffb485Schristos 
9*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
108dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
118dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*4b169a6bSchristos    GNU Lesser General Public License for more details.
138dffb485Schristos 
14*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
158dffb485Schristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
168dffb485Schristos 
178dffb485Schristos /* Written by Bruno Haible <bruno@clisp.org>, 2018.  */
188dffb485Schristos 
198dffb485Schristos #include <config.h>
208dffb485Schristos 
218dffb485Schristos /* Specification.  */
228dffb485Schristos #include "lc-charset-dispatch.h"
238dffb485Schristos 
248dffb485Schristos #if GNULIB_defined_mbstate_t
258dffb485Schristos 
268dffb485Schristos # include "localcharset.h"
278dffb485Schristos # include "streq.h"
288dffb485Schristos 
29*4b169a6bSchristos # if GNULIB_WCHAR_SINGLE_LOCALE
308dffb485Schristos /* When we know that the locale does not change, provide a speedup by
318dffb485Schristos    caching the value of locale_encoding_classification.  */
328dffb485Schristos #  define locale_encoding_classification_cached locale_encoding_classification
338dffb485Schristos # else
348dffb485Schristos /* By default, don't make assumptions, hence no caching.  */
358dffb485Schristos #  define locale_encoding_classification_uncached locale_encoding_classification
368dffb485Schristos # endif
378dffb485Schristos 
38*4b169a6bSchristos # if GNULIB_WCHAR_SINGLE_LOCALE
398dffb485Schristos static inline
408dffb485Schristos # endif
418dffb485Schristos enc_t
locale_encoding_classification_uncached(void)428dffb485Schristos locale_encoding_classification_uncached (void)
438dffb485Schristos {
448dffb485Schristos   const char *encoding = locale_charset ();
458dffb485Schristos   if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
468dffb485Schristos     return enc_utf8;
478dffb485Schristos   if (STREQ_OPT (encoding, "EUC-JP", 'E', 'U', 'C', '-', 'J', 'P', 0, 0, 0))
488dffb485Schristos     return enc_eucjp;
498dffb485Schristos   if (STREQ_OPT (encoding, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
508dffb485Schristos       || STREQ_OPT (encoding, "GB2312", 'G', 'B', '2', '3', '1', '2', 0, 0, 0)
518dffb485Schristos       || STREQ_OPT (encoding, "BIG5", 'B', 'I', 'G', '5', 0, 0, 0, 0, 0))
528dffb485Schristos     return enc_94;
538dffb485Schristos   if (STREQ_OPT (encoding, "EUC-TW", 'E', 'U', 'C', '-', 'T', 'W', 0, 0, 0))
548dffb485Schristos     return enc_euctw;
558dffb485Schristos   if (STREQ_OPT (encoding, "GB18030", 'G', 'B', '1', '8', '0', '3', '0', 0, 0))
568dffb485Schristos     return enc_gb18030;
578dffb485Schristos   if (STREQ_OPT (encoding, "SJIS", 'S', 'J', 'I', 'S', 0, 0, 0, 0, 0))
588dffb485Schristos     return enc_sjis;
598dffb485Schristos   return enc_other;
608dffb485Schristos }
618dffb485Schristos 
62*4b169a6bSchristos # if GNULIB_WCHAR_SINGLE_LOCALE
638dffb485Schristos 
648dffb485Schristos static int cached_locale_enc = -1;
658dffb485Schristos 
668dffb485Schristos enc_t
locale_encoding_classification_cached(void)678dffb485Schristos locale_encoding_classification_cached (void)
688dffb485Schristos {
698dffb485Schristos   if (cached_locale_enc < 0)
708dffb485Schristos     cached_locale_enc = locale_encoding_classification_uncached ();
718dffb485Schristos   return cached_locale_enc;
728dffb485Schristos }
738dffb485Schristos 
748dffb485Schristos # endif
758dffb485Schristos 
768dffb485Schristos #else
778dffb485Schristos 
788dffb485Schristos /* This declaration is solely to ensure that after preprocessing
798dffb485Schristos    this file is never empty.  */
808dffb485Schristos typedef int dummy;
818dffb485Schristos 
828dffb485Schristos #endif
83