2 ** Copyright 2011, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
10 #include <errno_private.h>
11 #include <LocaleBackend.h>
12 #include <wchar_private.h>
15 //#define TRACE_WCSRTOMBS
16 #ifdef TRACE_WCSRTOMBS
18 # define TRACE(x) debug_printf x
24 using BPrivate::Libroot::gLocaleBackend
;
28 __wcsnrtombs(char* dst
, const wchar_t** src
, size_t nwc
, size_t len
,
31 TRACE(("wcsnrtombs(%p, %p, %lu, %lu) - lb:%p\n", dst
, *src
, nwc
, len
,
35 static mbstate_t internalMbState
;
36 ps
= &internalMbState
;
39 if (gLocaleBackend
== NULL
) {
41 * The POSIX locale is active. Since the POSIX locale only contains
42 * chars 0-127 and those ASCII chars are compatible with the UTF32
43 * values used in wint_t, we can just copy the codepoint values.
46 const wchar_t* srcEnd
= *src
+ nwc
;
48 // only count number of required wide characters
49 const wchar_t* wcSrc
= *src
;
50 for (; wcSrc
< srcEnd
; ++wcSrc
, ++count
) {
58 memset(ps
, 0, sizeof(mbstate_t));
63 // "convert" the characters
64 for (; *src
< srcEnd
&& count
< len
; ++*src
, ++count
) {
73 memset(ps
, 0, sizeof(mbstate_t));
80 TRACE(("wcsnrtombs returns %lx and src %p\n", count
, *src
));
86 status_t status
= gLocaleBackend
->WcharStringToMultibyte(dst
, len
, src
, nwc
,
89 if (status
== B_BAD_DATA
) {
90 TRACE(("wcsnrtomb(): setting errno to EILSEQ\n"));
93 } else if (status
!= B_OK
) {
94 TRACE(("wcsnrtomb(): setting errno to EINVAL (status: %lx)\n", status
));
99 TRACE(("wcsnrtombs returns %lx and src %p\n", result
, *src
));
106 B_DEFINE_WEAK_ALIAS(__wcsnrtombs
, wcsnrtombs
);
110 __wcsrtombs(char* dst
, const wchar_t** src
, size_t len
, mbstate_t* ps
)
113 static mbstate_t internalMbState
;
114 ps
= &internalMbState
;
117 return __wcsnrtombs(dst
, src
, __wcslen(*src
) + 1, len
, ps
);
122 B_DEFINE_WEAK_ALIAS(__wcsrtombs
, wcsrtombs
);