crt: split wcrtomb.c to two files wcrtomb.c and wcsrtombs.c
Signed-off-by: Kirill Makurin <maiddaisuki@outlook.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 876b343..ab02568 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -350,6 +350,7 @@
misc/mbrtowc.c \
misc/mbsrtowcs.c \
misc/wcrtomb.c \
+ misc/wcsrtombs.c \
misc/wctrans.c \
misc/wctype.c \
secapi/_vscprintf_p.c \
@@ -917,6 +918,7 @@
misc/strnlen.c \
misc/wassert.c \
misc/wcrtomb.c \
+ misc/wcsrtombs.c \
misc/wcsnlen.c \
secapi/getenv_s.c \
stdio/_fseeki64.c \
diff --git a/mingw-w64-crt/misc/wcrtomb.c b/mingw-w64-crt/misc/wcrtomb.c
index e467434..09a5f35 100644
--- a/mingw-w64-crt/misc/wcrtomb.c
+++ b/mingw-w64-crt/misc/wcrtomb.c
@@ -3,14 +3,12 @@
* This file is part of the mingw-w64 runtime package.
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
-#ifndef WIN32_LEAN_AND_MEAN
-#define WIN32_LEAN_AND_MEAN
-#endif
-#include <locale.h>
-#include <wchar.h>
-#include <stdlib.h>
#include <errno.h>
-#include <limits.h>
+#include <locale.h>
+#include <stdlib.h>
+#include <wchar.h>
+
+#define WIN32_LEAN_AND_MEAN
#include <windows.h>
size_t wcrtomb (
@@ -76,67 +74,3 @@
_set_errno (EILSEQ);
return (size_t) -1;
}
-
-size_t wcsrtombs (
- char *__restrict__ mbs,
- const wchar_t **__restrict__ wcs,
- size_t count,
- mbstate_t *__restrict__ state
-) {
- /* Buffer to store single converted character */
- char mbc[2];
- /* Total number of bytes stored in `mbs` */
- size_t mbcConverted = 0;
-
- /* Next wide character to convert */
- const wchar_t *wc = *wcs;
-
- while (1) {
- const size_t length = wcrtomb (mbc, *wc, state);
-
- /* Conversion failed */
- if (length == (size_t) -1) {
- if (mbs != NULL) {
- *wcs = wc;
- }
- return (size_t) -1;
- }
-
- /* POSIX and ISO C are silent about this */
- if (mbs != NULL && count == 0) {
- return 0;
- }
-
- /* Terminating '\0' has been converted, stop */
- if (mbc[0] == '\0') {
- if (mbs != NULL) {
- *mbs = '\0';
- *wcs = NULL;
- }
- break;
- }
-
- /* Storing `mbc` in `mbs` would exceed `count` */
- if (mbs != NULL && mbcConverted + length > count) {
- *wcs = wc;
- break;
- }
-
- /* Write `mbc` to `mbs` */
- if (mbs != NULL) {
- memcpy (mbs, mbc, length);
- mbs += length;
- }
-
- mbcConverted += length;
- wc += 1;
-
- /* `count` bytes have been written to `mbs`, stop */
- if (mbs != NULL && mbcConverted == count) {
- *wcs = wc;
- break;
- }
- }
-
- return mbcConverted;
-}
diff --git a/mingw-w64-crt/misc/wcsrtombs.c b/mingw-w64-crt/misc/wcsrtombs.c
new file mode 100644
index 0000000..7dfc5e8
--- /dev/null
+++ b/mingw-w64-crt/misc/wcsrtombs.c
@@ -0,0 +1,71 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+#include <string.h>
+#include <wchar.h>
+
+size_t wcsrtombs (
+ char *__restrict__ mbs,
+ const wchar_t **__restrict__ wcs,
+ size_t count,
+ mbstate_t *__restrict__ state
+) {
+ /* Buffer to store single converted character */
+ char mbc[2];
+ /* Total number of bytes stored in `mbs` */
+ size_t mbcConverted = 0;
+
+ /* Next wide character to convert */
+ const wchar_t *wc = *wcs;
+
+ while (1) {
+ const size_t length = wcrtomb (mbc, *wc, state);
+
+ /* Conversion failed */
+ if (length == (size_t) -1) {
+ if (mbs != NULL) {
+ *wcs = wc;
+ }
+ return (size_t) -1;
+ }
+
+ /* POSIX and ISO C are silent about this */
+ if (mbs != NULL && count == 0) {
+ return 0;
+ }
+
+ /* Terminating '\0' has been converted, stop */
+ if (mbc[0] == '\0') {
+ if (mbs != NULL) {
+ *mbs = '\0';
+ *wcs = NULL;
+ }
+ break;
+ }
+
+ /* Storing `mbc` in `mbs` would exceed `count` */
+ if (mbs != NULL && mbcConverted + length > count) {
+ *wcs = wc;
+ break;
+ }
+
+ /* Write `mbc` to `mbs` */
+ if (mbs != NULL) {
+ memcpy (mbs, mbc, length);
+ mbs += length;
+ }
+
+ mbcConverted += length;
+ wc += 1;
+
+ /* `count` bytes have been written to `mbs`, stop */
+ if (mbs != NULL && mbcConverted == count) {
+ *wcs = wc;
+ break;
+ }
+ }
+
+ return mbcConverted;
+}