3 * Copyright 2012, 2013 MinGW.org project
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
24 #include "mb_wc_common.h"
29 #define WIN32_LEAN_AND_MEAN
33 static int __MINGW_ATTRIB_NONNULL(1)
34 __wcrtomb_cp (char *dst, wchar_t wc, const unsigned int cp,
35 const unsigned int mb_max)
51 int size = WideCharToMultiByte (cp, 0 /* Is this correct flag? */,
54 if (size == 0 || invalid_char)
64 wcrtomb (char *dst, wchar_t wc, mbstate_t * __UNUSED_PARAM (ps))
66 char byte_bucket [MB_LEN_MAX];
67 char* tmp_dst = dst ? dst : byte_bucket;
68 return (size_t)__wcrtomb_cp (tmp_dst, wc, get_codepage (),
72 size_t wcsrtombs (char *dst, const wchar_t **src, size_t len,
73 mbstate_t * __UNUSED_PARAM (ps))
77 const unsigned int cp = get_codepage();
78 const unsigned int mb_max = MB_CUR_MAX;
79 const wchar_t *pwc = *src;
81 if (src == NULL || *src == NULL) /* undefined behavior */
88 if ((ret = __wcrtomb_cp (dst, *pwc, cp, mb_max)) <= 0)
92 if (*(dst - 1) == '\0')
94 *src = (wchar_t*) NULL;;
103 char byte_bucket [MB_LEN_MAX];
106 if ((ret = __wcrtomb_cp (byte_bucket, *pwc, cp, mb_max))
110 if (byte_bucket [ret - 1] == '\0')