lc-charset-dispatch.c 2.6 KB

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