btowc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ctype.h>
  16. #include <dlfcn.h>
  17. #include <gconv.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <wchar.h>
  21. #include <wcsmbsload.h>
  22. #include <limits.h>
  23. #include <sysdep.h>
  24. wint_t
  25. __btowc (int c)
  26. {
  27. const struct gconv_fcts *fcts;
  28. /* If the parameter does not fit into one byte or it is the EOF value
  29. we can give the answer now. */
  30. if (c < SCHAR_MIN || c > UCHAR_MAX || c == EOF)
  31. return WEOF;
  32. /* We know that only ASCII compatible encodings are used for the
  33. locale and that the wide character encoding is ISO 10646. */
  34. if (isascii (c))
  35. return (wint_t) c;
  36. /* Get the conversion functions. */
  37. fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
  38. __gconv_btowc_fct btowc_fct = fcts->towc->__btowc_fct;
  39. #ifdef PTR_DEMANGLE
  40. if (fcts->towc->__shlib_handle != NULL)
  41. PTR_DEMANGLE (btowc_fct);
  42. #endif
  43. if (__builtin_expect (fcts->towc_nsteps == 1, 1)
  44. && __builtin_expect (btowc_fct != NULL, 1))
  45. {
  46. /* Use the shortcut function. */
  47. return DL_CALL_FCT (btowc_fct, (fcts->towc, (unsigned char) c));
  48. }
  49. else
  50. {
  51. /* Fall back to the slow but generic method. */
  52. wchar_t result;
  53. struct __gconv_step_data data;
  54. unsigned char inbuf[1];
  55. const unsigned char *inptr = inbuf;
  56. size_t dummy;
  57. int status;
  58. /* Tell where we want the result. */
  59. data.__outbuf = (unsigned char *) &result;
  60. data.__outbufend = data.__outbuf + sizeof (wchar_t);
  61. data.__invocation_counter = 0;
  62. data.__internal_use = 1;
  63. data.__flags = __GCONV_IS_LAST;
  64. data.__statep = &data.__state;
  65. /* Make sure we start in the initial state. */
  66. memset (&data.__state, '\0', sizeof (mbstate_t));
  67. /* Create the input string. */
  68. inbuf[0] = c;
  69. __gconv_fct fct = fcts->towc->__fct;
  70. #ifdef PTR_DEMANGLE
  71. if (fcts->towc->__shlib_handle != NULL)
  72. PTR_DEMANGLE (fct);
  73. #endif
  74. status = DL_CALL_FCT (fct, (fcts->towc, &data, &inptr, inptr + 1,
  75. NULL, &dummy, 0, 1));
  76. if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
  77. && status != __GCONV_EMPTY_INPUT)
  78. /* The conversion failed. */
  79. result = WEOF;
  80. return result;
  81. }
  82. }
  83. weak_alias (__btowc, btowc)