source: trunk/libs/newlib/src/newlib/libc/stdlib/mbstowcs.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.9 KB
Line 
1/*
2FUNCTION
3<<mbstowcs>>---minimal multibyte string to wide char converter
4
5INDEX
6        mbstowcs
7
8SYNOPSIS
9        #include <stdlib.h>
10        int mbstowcs(wchar_t *restrict <[pwc]>, const char *restrict <[s]>, size_t <[n]>);
11
12DESCRIPTION
13When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
14implementation of <<mbstowcs>>.  In this case, the
15only ``multi-byte character sequences'' recognized are single bytes,
16and they are ``converted'' to wide-char versions simply by byte
17extension.
18
19When _MB_CAPABLE is defined, this routine calls <<_mbstowcs_r>> to perform
20the conversion, passing a state variable to allow state dependent
21decoding.  The result is based on the locale setting which may
22be restricted to a defined set of locales.
23
24RETURNS
25This implementation of <<mbstowcs>> returns <<0>> if
26<[s]> is <<NULL>> or is the empty string;
27it returns <<-1>> if _MB_CAPABLE and one of the
28multi-byte characters is invalid or incomplete;
29otherwise it returns the minimum of: <<n>> or the
30number of multi-byte characters in <<s>> plus 1 (to
31compensate for the nul character).
32If the return value is -1, the state of the <<pwc>> string is
33indeterminate.  If the input has a length of 0, the output
34string will be modified to contain a wchar_t nul terminator.
35
36PORTABILITY
37<<mbstowcs>> is required in the ANSI C standard.  However, the precise
38effects vary with the locale.
39
40<<mbstowcs>> requires no supporting OS subroutines.
41*/
42
43#ifndef _REENT_ONLY
44
45#include <newlib.h>
46#include <stdlib.h>
47#include <wchar.h>
48
49size_t
50mbstowcs (wchar_t *__restrict pwcs,
51        const char *__restrict s,
52        size_t n)
53{
54#ifdef _MB_CAPABLE
55  mbstate_t state;
56  state.__count = 0;
57 
58  return _mbstowcs_r (_REENT, pwcs, s, n, &state);
59#else /* not _MB_CAPABLE */
60 
61  int count = 0;
62 
63  if (n != 0) {
64    do {
65      if ((*pwcs++ = (wchar_t) *s++) == 0)
66        break;
67      count++;
68    } while (--n != 0);
69  }
70 
71  return count;
72#endif /* not _MB_CAPABLE */
73}
74
75#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.