Back to index

glibc  2.9
mbtowc.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1995-1999, 2002, 2008
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
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 <stdlib.h>
00021 #include <string.h>
00022 #include <wchar.h>
00023 #include <gconv.h>
00024 #include <wcsmbs/wcsmbsload.h>
00025 
00026 
00027 /* Convert the multibyte character at S, which is no longer
00028    than N characters, to its `wchar_t' representation, placing
00029    this n *PWC and returning its length.
00030 
00031    Attention: this function should NEVER be intentionally used.
00032    The interface is completely stupid.  The state is shared between
00033    all conversion functions.  You should use instead the restartable
00034    version `mbrtowc'.  */
00035 int
00036 mbtowc (wchar_t *pwc, const char *s, size_t n)
00037 {
00038   int result;
00039   static mbstate_t state;
00040 
00041   /* If S is NULL the function has to return null or not null
00042      depending on the encoding having a state depending encoding or
00043      not.  */
00044   if (s == NULL)
00045     {
00046       const struct gconv_fcts *fcts;
00047 
00048       /* Get the conversion functions.  */
00049       fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00050 
00051       /* This is an extension in the Unix standard which does not directly
00052         violate ISO C.  */
00053       memset (&state, '\0', sizeof state);
00054 
00055       result = fcts->towc->__stateful;
00056     }
00057   else if (*s == '\0')
00058     {
00059       if (pwc != NULL)
00060        *pwc = L'\0';
00061       result = 0;
00062     }
00063   else
00064     {
00065       result = __mbrtowc (pwc, s, n, &state);
00066 
00067       /* The `mbrtowc' functions tell us more than we need.  Fold the -1
00068         and -2 result into -1.  */
00069       if (result < 0)
00070        result = -1;
00071     }
00072 
00073   return result;
00074 }