Files
glibc/stdlib/mbtowc.c
T

76 lines
2.3 KiB
C
Raw Normal View History

2002-03-13 08:59:47 +00:00
/* Copyright (C) 1991, 1992, 1995-1999, 2002 Free Software Foundation, Inc.
1997-02-10 03:19:57 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1997-02-10 03:19:57 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1997-02-10 03:19:57 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
1995-02-18 01:27:10 +00:00
#include <stdlib.h>
1999-07-29 23:46:10 +00:00
#include <string.h>
#include <wchar.h>
1998-04-21 19:09:49 +00:00
#include <gconv.h>
#include <wcsmbs/wcsmbsload.h>
1995-02-18 01:27:10 +00:00
/* Common state for all non-restartable conversion functions. */
2002-03-13 08:59:47 +00:00
mbstate_t __no_r_state attribute_hidden;
1995-02-18 01:27:10 +00:00
/* Convert the multibyte character at S, which is no longer
than N characters, to its `wchar_t' representation, placing
this n *PWC and returning its length.
Attention: this function should NEVER be intentionally used.
The interface is completely stupid. The state is shared between
all conversion functions. You should use instead the restartable
version `mbrtowc'. */
1995-02-18 01:27:10 +00:00
int
mbtowc (wchar_t *pwc, const char *s, size_t n)
1995-02-18 01:27:10 +00:00
{
int result;
1995-02-18 01:27:10 +00:00
/* If S is NULL the function has to return null or not null
depending on the encoding having a state depending encoding or
1998-04-21 19:09:49 +00:00
not. */
1995-02-18 01:27:10 +00:00
if (s == NULL)
1998-04-21 19:09:49 +00:00
{
const struct gconv_fcts *fcts;
/* Get the conversion functions. */
fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
1998-04-21 19:09:49 +00:00
1999-07-28 19:37:40 +00:00
/* This is an extension in the Unix standard which does not directly
violate ISO C. */
1999-07-28 22:39:04 +00:00
memset (&__no_r_state, '\0', sizeof __no_r_state);
1999-07-28 19:37:40 +00:00
result = fcts->towc->__stateful;
1998-04-21 19:09:49 +00:00
}
1998-04-20 18:41:05 +00:00
else if (*s == '\0')
{
if (pwc != NULL)
*pwc = L'\0';
result = 0;
}
else
{
result = __mbrtowc (pwc, s, n, &__no_r_state);
1995-02-18 01:27:10 +00:00
1998-04-20 18:41:05 +00:00
/* The `mbrtowc' functions tell us more than we need. Fold the -1
and -2 result into -1. */
if (result < 0)
result = -1;
}
1995-02-18 01:27:10 +00:00
return result;
1995-02-18 01:27:10 +00:00
}