summaryrefslogtreecommitdiff
path: root/newlib/libc/stdlib/wctomb_r.c
diff options
context:
space:
mode:
authorPikalaxALT <pikalaxalt@gmail.com>2017-12-15 09:38:53 -0500
committerPikalaxALT <pikalaxalt@gmail.com>2017-12-15 09:39:34 -0500
commitf95a4a932476be2ba99e2fd081e8d2bc6ea12813 (patch)
tree75f67192cb2d7b7b575c94edda318e475239b63c /newlib/libc/stdlib/wctomb_r.c
parentf60aca96985e68c7d8a52eb7bc955fb80e132f73 (diff)
Import newlib and create makefile
Diffstat (limited to 'newlib/libc/stdlib/wctomb_r.c')
-rw-r--r--newlib/libc/stdlib/wctomb_r.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/newlib/libc/stdlib/wctomb_r.c b/newlib/libc/stdlib/wctomb_r.c
new file mode 100644
index 0000000..8fa6e16
--- /dev/null
+++ b/newlib/libc/stdlib/wctomb_r.c
@@ -0,0 +1,110 @@
+#include <stdlib.h>
+#include <locale.h>
+#include "mbctype.h"
+
+int
+_DEFUN (_wctomb_r, (r, s, wchar, state),
+ struct _reent *r _AND
+ char *s _AND
+ wchar_t wchar _AND
+ int *state)
+{
+ if (strlen (r->_current_locale) <= 1)
+ { /* fall-through */ }
+ else if (!strcmp (r->_current_locale, "C-SJIS"))
+ {
+ unsigned char char2 = (unsigned char)wchar;
+ unsigned char char1 = (unsigned char)(wchar >> 8);
+
+ if (s == NULL)
+ return 0; /* not state-dependent */
+
+ if (char1 != 0x00)
+ {
+ /* first byte is non-zero..validate multi-byte char */
+ if (_issjis1(char1) && _issjis2(char2))
+ {
+ *s++ = (char)char1;
+ *s = (char)char2;
+ return 2;
+ }
+ else
+ return -1;
+ }
+ }
+ else if (!strcmp (r->_current_locale, "C-EUCJP"))
+ {
+ unsigned char char2 = (unsigned char)wchar;
+ unsigned char char1 = (unsigned char)(wchar >> 8);
+
+ if (s == NULL)
+ return 0; /* not state-dependent */
+
+ if (char1 != 0x00)
+ {
+ /* first byte is non-zero..validate multi-byte char */
+ if (_iseucjp (char1) && _iseucjp (char2))
+ {
+ *s++ = (char)char1;
+ *s = (char)char2;
+ return 2;
+ }
+ else
+ return -1;
+ }
+ }
+ else if (!strcmp (r->_current_locale, "C-JIS"))
+ {
+ int cnt = 0;
+ unsigned char char2 = (unsigned char)wchar;
+ unsigned char char1 = (unsigned char)(wchar >> 8);
+
+ if (s == NULL)
+ return 1; /* state-dependent */
+
+ if (char1 != 0x00)
+ {
+ /* first byte is non-zero..validate multi-byte char */
+ if (_isjis (char1) && _isjis (char2))
+ {
+ if (*state == 0)
+ {
+ /* must switch from ASCII to JIS state */
+ *state = 1;
+ *s++ = ESC_CHAR;
+ *s++ = '$';
+ *s++ = 'B';
+ cnt = 3;
+ }
+ *s++ = (char)char1;
+ *s = (char)char2;
+ return cnt + 2;
+ }
+ else
+ return -1;
+ }
+ else
+ {
+ if (*state != 0)
+ {
+ /* must switch from JIS to ASCII state */
+ *state = 0;
+ *s++ = ESC_CHAR;
+ *s++ = '(';
+ *s++ = 'B';
+ cnt = 3;
+ }
+ *s = (char)char2;
+ return cnt + 1;
+ }
+ }
+
+ if (s == NULL)
+ return 0;
+
+ /* otherwise we are dealing with a single byte character */
+ *s = (char) wchar;
+ return 1;
+}
+
+