Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sdk_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ def EquivalentFor(*versions):
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/usr_put.c"),
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/dispatch.c"),
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/msghndlr.c"),
Object(NonMatching, "TRK_MINNOW_DOLPHIN/support.c"),
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/support.c"),
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/mutex_TRK.c"),
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/notify.c"),
Object(MatchingFor("PROTO"), "TRK_MINNOW_DOLPHIN/flush_cache.c"),
Expand Down Expand Up @@ -1490,7 +1490,7 @@ def EquivalentFor(*versions):
Object(MatchingFor("PROTO"), "Runtime/direct_io.c"),
Object(MatchingFor("PROTO"), "Runtime/file_io.c"),
Object(MatchingFor("PROTO"), "Runtime/FILE_POS.c"),
Object(NonMatching, "Runtime/mbstring.c"),
Object(MatchingFor("PROTO"), "Runtime/mbstring.c"),
Object(MatchingFor("PROTO"), "Runtime/mem.c"),
Object(MatchingFor("PROTO"), "Runtime/mem_funcs.c"),
Object(MatchingFor("PROTO"), "Runtime/misc_io.c"),
Expand All @@ -1507,7 +1507,7 @@ def EquivalentFor(*versions):
Object(MatchingFor("PROTO"), "Runtime/wstrtoul.c"),
Object(MatchingFor("PROTO"), "Runtime/wctype.c"),
Object(MatchingFor("PROTO"), "Runtime/wmem.c"),
Object(NonMatching, "Runtime/wprintf.c"),
Object(MatchingFor("PROTO"), "Runtime/wprintf.c"),
Object(MatchingFor("PROTO"), "Runtime/wscanf.c"),
Object(MatchingFor("PROTO"), "Runtime/wstring.c"),
Object(MatchingFor("PROTO"), "Runtime/wchar_io.c"),
Expand Down
241 changes: 72 additions & 169 deletions src/Runtime/mbstring.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
#include <SDKVersion.h>
#include "errno.h"
#include "stdio.h"
#include "string.h"
#include <stdlib.h>
#include <string.h>

#if SDK_VERSION <= SDK_VERSION_DOLPHIN_2003_02
#if SDK_VERSION <= SDK_VERSION_DOLPHIN_2004_04
#pragma ANSI_strict off

#ifndef __WINT_TYPE__
#define __WINT_TYPE__ unsigned int
#endif

typedef struct {
int __count;
union {
__WINT_TYPE__ __wch;
char __wchb[4];
} __value; /* Value so far. */
} mbstate_t;

static int is_utf8_complete(const char* s, size_t n);
static int is_utf8_complete(const char* s, size_t n) {
if (n <= 0) /* 0 or fewer characters do not form a valid multibyte character */
return (-1);
Expand Down Expand Up @@ -51,50 +37,6 @@ static int is_utf8_complete(const char* s, size_t n) {
return (-1);
}

#pragma dont_inline on
static int unicode_to_UTF8(char* s, wchar_t wchar) {
int number_of_bytes;
wchar_t wide_char;
char* target_ptr;
char first_byte_mark[4] = {0x00, 0x00, 0xc0, 0xe0};

if (!s) {
return 0;
}

wide_char = wchar;
if (wide_char < 0x0080)
number_of_bytes = 1;
else if (wide_char < 0x0800)
number_of_bytes = 2;
else
number_of_bytes = 3;

target_ptr = s + number_of_bytes;

switch (number_of_bytes) {
case 3:
*--target_ptr = (wide_char & 0x003f) | 0x80;
wide_char >>= 6;
case 2:
*--target_ptr = (wide_char & 0x003f) | 0x80;
wide_char >>= 6;
case 1:
*--target_ptr = wide_char | first_byte_mark[number_of_bytes];
}

return (number_of_bytes);
}
#pragma dont_inline reset

int wctomb(char* s, wchar_t wchar) { return (unicode_to_UTF8(s, wchar)); }
#else
#include <locale.h>

#if defined(RVL_PROTO_RUNTIME)
// Guessed name
static int is_utf8_complete(const char* s, size_t n);

static int utf8_to_unicode(wchar_t* dst, const char* src, size_t n) {
int bytes, expected;
char* p;
Expand Down Expand Up @@ -139,90 +81,14 @@ static int utf8_to_unicode(wchar_t* dst, const char* src, size_t n) {

return bytes;
}
#endif

int mbtowc(wchar_t *dst, const char *src, size_t n) {
#if defined(RVL_PROTO_RUNTIME)
int mbtowc(wchar_t* dst, const char* src, size_t n) {
return utf8_to_unicode(dst, src, n);
#else
return _current_locale.ctype_cmpt_ptr->decode_mb(dst, src, n);
#endif
}

// Guessed name
int __mbtowc_noconv(wchar_t *dst, const char *src, size_t n) {
if (!src)
return 0;

if (!n)
return -1;

if (dst)
*dst = (unsigned char)*src;

if (!*src)
return 0;

return 1;
}

// Guessed name
int __wctomb_noconv(char *dst, wchar_t ch) {
if (!dst)
return 0;

*dst = ch;
return 1;
}

#if SDK_VERSION >= SDK_VERSION_DOLPHIN_2003_04 && SDK_VERSION <= SDK_VERSION_DOLPHIN_2004_04
#pragma ANSI_strict off

#ifndef __WINT_TYPE__
#define __WINT_TYPE__ unsigned int
#if SDK_VERSION <= SDK_VERSION_DOLPHIN_2003_02
#pragma dont_inline on
#endif

typedef struct {
int __count;
union {
__WINT_TYPE__ __wch;
char __wchb[4];
} __value; /* Value so far. */
} mbstate_t;

static int is_utf8_complete(const char* s, size_t n);
static int is_utf8_complete(const char* s, size_t n) {
if (n <= 0) /* 0 or fewer characters do not form a valid multibyte character */
return (-1);

if (*s == 0x00)
return (0);

if ((*s & 0x80) == 0x00)
return (1);
else if ((*s & 0xe0) == 0xc0) /* need 2 bytes */
if (n >= 2)
if (((*(s + 1) & 0x80) == 0x80))
return (2);
else
return (-1);
else
return (-2);

else if ((*s & 0xf0) == 0xe0)
if (n >= 3)
if (((*(s + 1) & 0x80) == 0x80) && ((*(s + 2) & 0x80) == 0x80))
return (3);
else
return (-1);
else if (((n == 2) && ((*(s + 1) & 0x80) == 0x80)) || (n == 1))
return (-2);
else
return (-1);
else
return (-1);
}

static int unicode_to_UTF8(char* s, wchar_t wchar) {
int number_of_bytes;
wchar_t wide_char;
Expand Down Expand Up @@ -256,15 +122,78 @@ static int unicode_to_UTF8(char* s, wchar_t wchar) {

return (number_of_bytes);
}
#if SDK_VERSION <= SDK_VERSION_DOLPHIN_2003_02
#pragma dont_inline reset
#endif

int wctomb(char* s, wchar_t wchar) { return (unicode_to_UTF8(s, wchar)); }
int wctomb(char* dst, wchar_t ch) { return unicode_to_UTF8(dst, ch); }

#else
static inline int wctomb(char *dst, wchar_t ch) {
#include <locale.h>

int mbtowc(wchar_t* dst, const char* src, size_t n) {
return _current_locale.ctype_cmpt_ptr->decode_mb(dst, src, n);
}

// Guessed name
int __mbtowc_noconv(wchar_t *dst, const char *src, size_t n) {
if (!src)
return 0;

if (!n)
return -1;

if (dst)
*dst = (unsigned char)*src;

if (!*src)
return 0;

return 1;
}

// Guessed name
int __wctomb_noconv(char *dst, wchar_t ch) {
if (!dst)
return 0;

*dst = ch;
return 1;
}

static inline int wctomb(char* dst, wchar_t ch) {
return _current_locale.ctype_cmpt_ptr->encode_wc(dst, ch);
}
#endif
#endif

size_t mbstowcs(wchar_t* dest, const char* src, size_t count) {
int result;
const char* p;
int written;
size_t remaining;

remaining = strlen(src);
if (dest) {
p = src;
for (written = 0; written < count; ++written) {
if (*p) {
result = mbtowc(dest++, p, remaining);
if (result > 0) {
p += result;
remaining -= result;
} else {
return (size_t)-1;
}
} else {
*dest = 0;
break;
}
}
} else {
written = 0;
}
return written;
}

size_t wcstombs(char* s, const wchar_t* pwcs, size_t n) {
int chars_written = 0;
Expand Down Expand Up @@ -292,29 +221,3 @@ size_t wcstombs(char* s, const wchar_t* pwcs, size_t n) {

return (chars_written);
}

#if defined(RVL_PROTO_RUNTIME)
size_t mbstowcs(wchar_t* dest, const char* src, size_t count) {
size_t remaining = strlen(src);
int written;

if (dest) {
const char* p = src;
for (written = 0; written < count; ++written) {
if (*p) {
int result = mbtowc(dest++, p, remaining);
if (result <= 0)
return -1;
p += result;
remaining -= result;
} else {
*dest = 0;
break;
}
}
} else {
written = 0;
}
return written;
}
#endif
Loading
Loading