Back to index

glibc  2.9
btowc.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2000,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <ctype.h>
00021 #include <dlfcn.h>
00022 #include <gconv.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <wchar.h>
00026 #include <wcsmbsload.h>
00027 #include <limits.h>
00028 
00029 #include <sysdep.h>
00030 
00031 
00032 wint_t
00033 __btowc (c)
00034      int c;
00035 {
00036   const struct gconv_fcts *fcts;
00037 
00038   /* If the parameter does not fit into one byte or it is the EOF value
00039      we can give the answer now.  */
00040   if (c < SCHAR_MIN || c > UCHAR_MAX || c == EOF)
00041     return WEOF;
00042 
00043   /* We know that only ASCII compatible encodings are used for the
00044      locale and that the wide character encoding is ISO 10646.  */
00045   if (isascii (c))
00046     return (wint_t) c;
00047 
00048   /* Get the conversion functions.  */
00049   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00050   __gconv_btowc_fct btowc_fct = fcts->towc->__btowc_fct;
00051 
00052   if (__builtin_expect (fcts->towc_nsteps == 1, 1)
00053       && __builtin_expect (btowc_fct != NULL, 1))
00054     {
00055       /* Use the shortcut function.  */
00056 #ifdef PTR_DEMANGLE
00057       if (fcts->towc->__shlib_handle != NULL)
00058        PTR_DEMANGLE (btowc_fct);
00059 #endif
00060       return DL_CALL_FCT (btowc_fct, (fcts->towc, (unsigned char) c));
00061     }
00062   else
00063     {
00064       /* Fall back to the slow but generic method.  */
00065       wchar_t result;
00066       struct __gconv_step_data data;
00067       unsigned char inbuf[1];
00068       const unsigned char *inptr = inbuf;
00069       size_t dummy;
00070       int status;
00071 
00072       /* Tell where we want the result.  */
00073       data.__outbuf = (unsigned char *) &result;
00074       data.__outbufend = data.__outbuf + sizeof (wchar_t);
00075       data.__invocation_counter = 0;
00076       data.__internal_use = 1;
00077       data.__flags = __GCONV_IS_LAST;
00078       data.__statep = &data.__state;
00079       data.__trans = NULL;
00080 
00081       /* Make sure we start in the initial state.  */
00082       memset (&data.__state, '\0', sizeof (mbstate_t));
00083 
00084       /* Create the input string.  */
00085       inbuf[0] = c;
00086 
00087       __gconv_fct fct = fcts->towc->__fct;
00088 #ifdef PTR_DEMANGLE
00089       if (fcts->towc->__shlib_handle != NULL)
00090        PTR_DEMANGLE (fct);
00091 #endif
00092       status = DL_CALL_FCT (fct, (fcts->towc, &data, &inptr, inptr + 1,
00093                               NULL, &dummy, 0, 1));
00094 
00095       if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
00096          && status != __GCONV_EMPTY_INPUT)
00097        /* The conversion failed.  */
00098        result = WEOF;
00099 
00100       return result;
00101     }
00102 }
00103 weak_alias (__btowc, btowc)