source: trunk/libs/newlib/src/newlib/libc/stdlib/wcsnrtombs.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: 3.8 KB
Line 
1/*
2FUNCTION
3<<wcsrtombs>>, <<wcsnrtombs>>---convert a wide-character string to a character string
4
5INDEX
6        wcsrtombs
7INDEX
8        _wcsrtombs_r
9INDEX
10        wcsnrtombs
11INDEX
12        _wcsnrtombs_r
13
14SYNOPSIS
15        #include <wchar.h>
16        size_t wcsrtombs(char *__restrict <[dst]>,
17                         const wchar_t **__restrict <[src]>, size_t <[len]>,
18                         mbstate_t *__restrict <[ps]>);
19
20        #include <wchar.h>
21        size_t _wcsrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
22                            const wchar_t **<[src]>, size_t <[len]>,
23                            mbstate_t *<[ps]>);
24
25        #include <wchar.h>
26        size_t wcsnrtombs(char *__restrict <[dst]>,
27                          const wchar_t **__restrict <[src]>,
28                          size_t <[nwc]>, size_t <[len]>,
29                          mbstate_t *__restrict <[ps]>);
30
31        #include <wchar.h>
32        size_t _wcsnrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
33                             const wchar_t **<[src]>, size_t <[nwc]>,
34                             size_t <[len]>, mbstate_t *<[ps]>);
35
36DESCRIPTION
37The <<wcsrtombs>> function converts a string of wide characters indirectly
38pointed to by <[src]> to a corresponding multibyte character string stored in
39the array pointed to by <[dst]>.  No more than <[len]> bytes are written to
40<[dst]>.
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, <<wcsrtombs>> uses an internal, static mbstate_t object, which
50is initialized to the initial conversion state at program startup.
51
52The <<wcsnrtombs>> function behaves identically to <<wcsrtombs>>, except that
53conversion stops after reading at most <[nwc]> characters from the buffer
54pointed to by <[src]>.
55
56RETURNS
57The <<wcsrtombs>> and <<wcsnrtombs>> functions return the number of bytes
58stored in the array pointed to by <[dst]> (not including any terminating
59null), if successful, otherwise it returns (size_t)-1.
60
61PORTABILITY
62<<wcsrtombs>> is defined by C99 standard.
63<<wcsnrtombs>> 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#include "local.h"
73#include "../locale/setlocale.h"
74
75size_t
76_wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
77               size_t len, mbstate_t *ps, struct __locale_t *loc)
78{
79  char *ptr = dst;
80  char buff[10];
81  wchar_t *pwcs;
82  size_t n;
83  int i;
84
85#ifdef _MB_CAPABLE
86  if (ps == NULL)
87    {
88      _REENT_CHECK_MISC(r);
89      ps = &(_REENT_WCSRTOMBS_STATE(r));
90    }
91#endif
92
93  /* If no dst pointer, treat len as maximum possible value. */
94  if (dst == NULL)
95    len = (size_t)-1;
96
97  n = 0;
98  pwcs = (wchar_t *)(*src);
99
100  while (n < len && nwc-- > 0)
101    {
102      int count = ps->__count;
103      wint_t wch = ps->__value.__wch;
104      int bytes = loc->wctomb (r, buff, *pwcs, ps);
105      if (bytes == -1)
106        {
107          r->_errno = EILSEQ;
108          ps->__count = 0;
109          return (size_t)-1;
110        }
111      if (n + bytes <= len)
112        {
113          n += bytes;
114          if (dst)
115            {
116              for (i = 0; i < bytes; ++i)
117                *ptr++ = buff[i];
118              ++(*src);
119            }
120          if (*pwcs++ == 0x00)
121            {
122              if (dst)
123                *src = NULL;
124              ps->__count = 0;
125              return n - 1;
126            }
127        }
128      else
129        {
130          /* not enough room, we must back up state to before __WCTOMB call */
131          ps->__count = count;
132          ps->__value.__wch = wch;
133          len = 0;
134        }
135    }
136
137  return n;
138} 
139
140size_t
141_wcsnrtombs_r (struct _reent *r,
142        char *dst,
143        const wchar_t **src,
144        size_t nwc,
145        size_t len,
146        mbstate_t *ps)
147{
148  return _wcsnrtombs_l (_REENT, dst, src, nwc, len, ps,
149                        __get_current_locale ());
150}
151
152#ifndef _REENT_ONLY
153size_t
154wcsnrtombs (char *__restrict dst,
155        const wchar_t **__restrict src,
156        size_t nwc,
157        size_t len,
158        mbstate_t *__restrict ps)
159{
160  return _wcsnrtombs_l (_REENT, dst, src, nwc, len, ps,
161                        __get_current_locale ());
162}
163#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.