| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This code was written by Rich Felker in 2010; no copyright is claimed. | 2 * This code was written by Rich Felker in 2010; no copyright is claimed. |
| 3 * This code is in the public domain. Attribution is appreciated but | 3 * This code is in the public domain. Attribution is appreciated but |
| 4 * unnecessary. | 4 * unnecessary. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <wchar.h> | 7 #include <wchar.h> |
| 8 | 8 |
| 9 size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, s
ize_t n, mbstate_t *restrict st) | 9 size_t wcsnrtombs(char* restrict dst, |
| 10 { | 10 const wchar_t** restrict wcs, |
| 11 » size_t l, cnt=0, n2; | 11 size_t wn, |
| 12 » char *s, buf[256]; | 12 size_t n, |
| 13 » const wchar_t *ws = *wcs; | 13 mbstate_t* restrict st) { |
| 14 size_t l, cnt = 0, n2; |
| 15 char *s, buf[256]; |
| 16 const wchar_t* ws = *wcs; |
| 14 | 17 |
| 15 » if (!dst) s = buf, n = sizeof buf; | 18 if (!dst) |
| 16 » else s = dst; | 19 s = buf, n = sizeof buf; |
| 20 else |
| 21 s = dst; |
| 17 | 22 |
| 18 » while ( ws && n && ( (n2=wn)>=n || n2>32 ) ) { | 23 while (ws && n && ((n2 = wn) >= n || n2 > 32)) { |
| 19 » » if (n2>=n) n2=n; | 24 if (n2 >= n) |
| 20 » » wn -= n2; | 25 n2 = n; |
| 21 » » l = wcsrtombs(s, &ws, n2, 0); | 26 wn -= n2; |
| 22 » » if (!(l+1)) { | 27 l = wcsrtombs(s, &ws, n2, 0); |
| 23 » » » cnt = l; | 28 if (!(l + 1)) { |
| 24 » » » n = 0; | 29 cnt = l; |
| 25 » » » break; | 30 n = 0; |
| 26 » » } | 31 break; |
| 27 » » if (s != buf) { | 32 } |
| 28 » » » s += l; | 33 if (s != buf) { |
| 29 » » » n -= l; | 34 s += l; |
| 30 » » } | 35 n -= l; |
| 31 » » cnt += l; | 36 } |
| 32 » } | 37 cnt += l; |
| 33 » if (ws) while (n && wn) { | 38 } |
| 34 » » l = wcrtomb(s, *ws, 0); | 39 if (ws) |
| 35 » » if ((l+1)<=1) { | 40 while (n && wn) { |
| 36 » » » if (!l) ws = 0; | 41 l = wcrtomb(s, *ws, 0); |
| 37 » » » else cnt = l; | 42 if ((l + 1) <= 1) { |
| 38 » » » break; | 43 if (!l) |
| 39 » » } | 44 ws = 0; |
| 40 » » ws++; wn--; | 45 else |
| 41 » » /* safe - this loop runs fewer than sizeof(buf) times */ | 46 cnt = l; |
| 42 » » s+=l; n-=l; | 47 break; |
| 43 » » cnt += l; | 48 } |
| 44 » } | 49 ws++; |
| 45 » if (dst) *wcs = ws; | 50 wn--; |
| 46 » return cnt; | 51 /* safe - this loop runs fewer than sizeof(buf) times */ |
| 52 s += l; |
| 53 n -= l; |
| 54 cnt += l; |
| 55 } |
| 56 if (dst) |
| 57 *wcs = ws; |
| 58 return cnt; |
| 47 } | 59 } |
| OLD | NEW |