source: trunk/libs/newlib/src/newlib/libc/stdlib/mbsnrtowcs.c @ 543

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

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

File size: 3.4 KB
Line 
1/*
2FUNCTION
3<<mbsrtowcs>>, <<mbsnrtowcs>>---convert a character string to a wide-character string
4
5INDEX
6        mbsrtowcs
7INDEX
8        _mbsrtowcs_r
9INDEX
10        mbsnrtowcs
11INDEX
12        _mbsnrtowcs_r
13
14SYNOPSIS
15        #include <wchar.h>
16        size_t mbsrtowcs(wchar_t *__restrict <[dst]>,
17                         const char **__restrict <[src]>,
18                         size_t <[len]>,
19                         mbstate_t *__restrict <[ps]>);
20
21        #include <wchar.h>
22        size_t _mbsrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>,
23                            const char **<[src]>, size_t <[len]>,
24                            mbstate_t *<[ps]>);
25
26        #include <wchar.h>
27        size_t mbsnrtowcs(wchar_t *__ restrict <[dst]>,
28                          const char **__restrict <[src]>, size_t <[nms]>,
29                          size_t <[len]>, mbstate_t *__restrict <[ps]>);
30
31        #include <wchar.h>
32        size_t _mbsnrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>,
33                             const char **<[src]>, size_t <[nms]>,
34                             size_t <[len]>, mbstate_t *<[ps]>);
35
36DESCRIPTION
37The <<mbsrtowcs>> function converts a sequence of multibyte characters
38pointed to indirectly by <[src]> into a sequence of corresponding wide
39characters and stores at most <[len]> of them in the wchar_t array pointed
40to by <[dst]>, until it encounters a terminating null character ('\0').
41
42If <[dst]> is NULL, no characters are stored.
43
44If <[dst]> is not NULL, the pointer pointed to by <[src]> is updated to point
45to the character after the one that conversion stopped at.  If conversion
46stops because a null character is encountered, *<[src]> is set to NULL.
47
48The mbstate_t argument, <[ps]>, is used to keep track of the shift state.  If
49it is NULL, <<mbsrtowcs>> uses an internal, static mbstate_t object, which
50is initialized to the initial conversion state at program startup.
51
52The <<mbsnrtowcs>> function behaves identically to <<mbsrtowcs>>, except that
53conversion stops after reading at most <[nms]> bytes from the buffer pointed
54to by <[src]>.
55
56RETURNS
57The <<mbsrtowcs>> and <<mbsnrtowcs>> functions return the number of wide
58characters stored in the array pointed to by <[dst]> if successful, otherwise
59it returns (size_t)-1.
60
61PORTABILITY
62<<mbsrtowcs>> is defined by the C99 standard.
63<<mbsnrtowcs>> is defined by the POSIX.1-2008 standard.
64*/
65
66#include <reent.h>
67#include <newlib.h>
68#include <wchar.h>
69#include <stdlib.h>
70#include <stdio.h>
71#include <errno.h>
72
73size_t
74_mbsnrtowcs_r (struct _reent *r,
75        wchar_t *dst,
76        const char **src,
77        size_t nms,
78        size_t len,
79        mbstate_t *ps)
80{
81  wchar_t *ptr = dst;
82  const char *tmp_src;
83  size_t max;
84  size_t count = 0;
85  int bytes;
86
87#ifdef _MB_CAPABLE
88  if (ps == NULL)
89    {
90      _REENT_CHECK_MISC(r);
91      ps = &(_REENT_MBSRTOWCS_STATE(r));
92    }
93#endif
94
95  if (dst == NULL)
96    {
97      /* Ignore original len value and do not alter src pointer if the
98         dst pointer is NULL.  */
99      len = (size_t)-1;
100      tmp_src = *src;
101      src = &tmp_src;
102    }     
103 
104  max = len;
105  while (len > 0)
106    {
107      bytes = _mbrtowc_r (r, ptr, *src, nms, ps);
108      if (bytes > 0)
109        {
110          *src += bytes;
111          nms -= bytes;
112          ++count;
113          ptr = (dst == NULL) ? NULL : ptr + 1;
114          --len;
115        }
116      else if (bytes == -2)
117        {
118          *src += nms;
119          return count;
120        }
121      else if (bytes == 0)
122        {
123          *src = NULL;
124          return count;
125        }
126      else
127        {
128          ps->__count = 0;
129          r->_errno = EILSEQ;
130          return (size_t)-1;
131        }
132    }
133
134  return (size_t)max;
135}
136
137#ifndef _REENT_ONLY
138size_t
139mbsnrtowcs (wchar_t *__restrict dst,
140        const char **__restrict src,
141        size_t nms,
142        size_t len,
143        mbstate_t *__restrict ps)
144{
145  return _mbsnrtowcs_r (_REENT, dst, src, nms, len, ps);
146}
147#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.