Files
glibc/stdlib/mblen.c
T

70 lines
2.1 KiB
C
Raw Normal View History

/* Copyright (C) 1991,1997,1998,1999,2000,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-28 19:37:40 +00:00
#include <string.h>
1997-10-26 20:13:00 +00:00
#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
1999-07-28 19:37:40 +00:00
/* Internal state. */
static mbstate_t state;
1995-02-18 01:27:10 +00:00
/* Return the length of the multibyte character (if there is one)
1997-10-26 20:13:00 +00:00
at S which is no longer than N characters.
The ISO C standard says that the `mblen' function must not change
1999-07-28 19:37:40 +00:00
the state of the `mbtowc' function. */
1995-02-18 01:27:10 +00:00
int
1997-02-10 03:19:57 +00:00
mblen (const char *s, size_t n)
1995-02-18 01:27:10 +00:00
{
1997-10-26 20:13:00 +00:00
int result;
/* 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. */
1997-10-26 20:13:00 +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
/* Reset the state. */
memset (&state, '\0', sizeof state);
result = fcts->towc->__stateful;
1998-04-21 19:09:49 +00:00
}
1998-04-20 18:41:05 +00:00
else if (*s == '\0')
1999-07-28 19:37:40 +00:00
/* According to the ISO C 89 standard this is the expected behaviour. */
1998-04-20 18:41:05 +00:00
result = 0;
else
{
2000-04-07 16:45:34 +00:00
memset (&state, '\0', sizeof state);
1997-10-26 20:13:00 +00:00
1998-04-20 18:41:05 +00:00
result = __mbrtowc (NULL, s, n, &state);
1997-10-26 20:13:00 +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;
}
1997-10-26 20:13:00 +00:00
return result;
1995-02-18 01:27:10 +00:00
}