OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [stdlib/] [wcsnrtombs.c] - Blame information for rev 309

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
2
FUNCTION
3
<<wcsrtombs>>, <<wcsnrtombs>>---convert a wide-character string to a character string
4
 
5
INDEX
6
        wcsrtombs
7
INDEX
8
        _wcsrtombs_r
9
INDEX
10
        wcsnrtombs
11
INDEX
12
        _wcsnrtombs_r
13
 
14
ANSI_SYNOPSIS
15
        #include <wchar.h>
16
        size_t wcsrtombs(char *<[dst]>, const wchar_t **<[src]>, size_t <[len]>,
17
                         mbstate_t *<[ps]>);
18
 
19
        #include <wchar.h>
20
        size_t _wcsrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
21
                            const wchar_t **<[src]>, size_t <[len]>,
22
                            mbstate_t *<[ps]>);
23
 
24
        #include <wchar.h>
25
        size_t wcsnrtombs(char *<[dst]>, const wchar_t **<[src]>,
26
                          size_t <[nwc]>, size_t <[len]>, mbstate_t *<[ps]>);
27
 
28
        #include <wchar.h>
29
        size_t _wcsnrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
30
                             const wchar_t **<[src]>, size_t <[nwc]>,
31
                             size_t <[len]>, mbstate_t *<[ps]>);
32
 
33
TRAD_SYNOPSIS
34
        #include <wchar.h>
35
        size_t wcsrtombs(<[dst]>, <[src]>, <[len]>, <[ps]>)
36
        char *<[dst]>;
37
        const wchar_t **<[src]>;
38
        size_t <[len]>;
39
        mbstate_t *<[ps]>;
40
 
41
        #include <wchar.h>
42
        size_t _wcsrtombs_r(<[ptr]>, <[dst]>, <[src]>, <[len]>, <[ps]>)
43
        struct _rent *<[ptr]>;
44
        char *<[dst]>;
45
        const wchar_t **<[src]>;
46
        size_t <[len]>;
47
        mbstate_t *<[ps]>;
48
 
49
        #include <wchar.h>
50
        size_t wcsnrtombs(<[dst]>, <[src]>, <[nwc]>, <[len]>, <[ps]>)
51
        char *<[dst]>;
52
        const wchar_t **<[src]>;
53
        size_t <[nwc]>;
54
        size_t <[len]>;
55
        mbstate_t *<[ps]>;
56
 
57
        #include <wchar.h>
58
        size_t _wcsnrtombs_r(<[ptr]>, <[dst]>, <[src]>, <[nwc]>, <[len]>, <[ps]>)
59
        struct _rent *<[ptr]>;
60
        char *<[dst]>;
61
        const wchar_t **<[src]>;
62
        size_t <[nwc]>;
63
        size_t <[len]>;
64
        mbstate_t *<[ps]>;
65
 
66
DESCRIPTION
67
The <<wcsrtombs>> function converts a string of wide characters indirectly
68
pointed to by <[src]> to a corresponding multibyte character string stored in
69
the array pointed to by <[dst}>.  No more than <[len]> bytes are written to
70
<[dst}>.
71
 
72
If <[dst}> is NULL, no characters are stored.
73
 
74
If <[dst}> is not NULL, the pointer pointed to by <[src]> is updated to point
75
to the character after the one that conversion stopped at.  If conversion
76
stops because a null character is encountered, *<[src]> is set to NULL.
77
 
78
The mbstate_t argument, <[ps]>, is used to keep track of the shift state.  If
79
it is NULL, <<wcsrtombs>> uses an internal, static mbstate_t object, which
80
is initialized to the initial conversion state at program startup.
81
 
82
The <<wcsnrtombs>> function behaves identically to <<wcsrtombs>>, except that
83
conversion stops after reading at most <[nwc]> characters from the buffer
84
pointed to by <[src]>.
85
 
86
RETURNS
87
The <<wcsrtombs>> and <<wcsnrtombs>> functions return the number of bytes
88
stored in the array pointed to by <[dst]> (not including any terminating
89
null), if successful, otherwise it returns (size_t)-1.
90
 
91
PORTABILITY
92
<<wcsrtombs>> is defined by C99 standard.
93
<<wcsnrtombs>> is defined by the POSIX.1-2008 standard.
94
*/
95
 
96
#include <reent.h>
97
#include <newlib.h>
98
#include <wchar.h>
99
#include <stdlib.h>
100
#include <stdio.h>
101
#include <errno.h>
102
#include "local.h"
103
 
104
size_t
105
_DEFUN (_wcsnrtombs_r, (r, dst, src, nwc, len, ps),
106
        struct _reent *r _AND
107
        char *dst _AND
108
        const wchar_t **src _AND
109
        size_t nwc _AND
110
        size_t len _AND
111
        mbstate_t *ps)
112
{
113
  char *ptr = dst;
114
  char buff[10];
115
  wchar_t *pwcs;
116
  size_t n;
117
  int i;
118
 
119
#ifdef _MB_CAPABLE
120
  if (ps == NULL)
121
    {
122
      _REENT_CHECK_MISC(r);
123
      ps = &(_REENT_WCSRTOMBS_STATE(r));
124
    }
125
#endif
126
 
127
  /* If no dst pointer, treat len as maximum possible value. */
128
  if (dst == NULL)
129
    len = (size_t)-1;
130
 
131
  n = 0;
132
  pwcs = (wchar_t *)(*src);
133
 
134
  while (n < len && nwc-- > 0)
135
    {
136
      int count = ps->__count;
137
      wint_t wch = ps->__value.__wch;
138
      int bytes = __wctomb (r, buff, *pwcs, __locale_charset (), ps);
139
      if (bytes == -1)
140
        {
141
          r->_errno = EILSEQ;
142
          ps->__count = 0;
143
          return (size_t)-1;
144
        }
145
      if (n + bytes <= len)
146
        {
147
          n += bytes;
148
          if (dst)
149
            {
150
              for (i = 0; i < bytes; ++i)
151
                *ptr++ = buff[i];
152
              ++(*src);
153
            }
154
          if (*pwcs++ == 0x00)
155
            {
156
              if (dst)
157
                *src = NULL;
158
              ps->__count = 0;
159
              return n - 1;
160
            }
161
        }
162
      else
163
        {
164
          /* not enough room, we must back up state to before __wctomb call */
165
          ps->__count = count;
166
          ps->__value.__wch = wch;
167
          len = 0;
168
        }
169
    }
170
 
171
  return n;
172
}
173
 
174
#ifndef _REENT_ONLY
175
size_t
176
_DEFUN (wcsnrtombs, (dst, src, nwc, len, ps),
177
        char *dst _AND
178
        const wchar_t **src _AND
179
        size_t nwc _AND
180
        size_t len _AND
181
        mbstate_t *ps)
182
{
183
  return _wcsnrtombs_r (_REENT, dst, src, nwc, len, ps);
184
}
185
#endif /* !_REENT_ONLY */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.