Making some MSL_C progress (#549)

This commit is contained in:
mrshigure 2025-01-27 16:50:48 -08:00 committed by GitHub
parent 32fdc38925
commit 2309176a5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 1456 additions and 263 deletions

View file

@ -1,18 +1,64 @@
#include "PowerPC_EABI_Support/Msl/MSL_C/MSL_Common/mbstring.h"
#include "PowerPC_EABI_Support/Msl/MSL_C/MSL_Common/ansi_files.h"
size_t wcstombs(char* s, const wchar_t* pwcs, size_t n)
{
wchar_t next;
size_t chars_written;
int i;
static int unicode_to_UTF8(char* s, wchar_t wchar) {
int number_of_bytes;
char* target_ptr;
char first_byte_mark[4] = {0x00, 0x00, 0xc0, 0xe0};
chars_written = 0;
for (i = 0; i < n; ++i) {
next = *pwcs++;
*s++ = (char)next;
if ((char)next == '\0')
break;
++chars_written;
}
return chars_written;
if (!s) {
return 0;
}
if (wchar < 0x0080)
number_of_bytes = 1;
else if (wchar < 0x0800)
number_of_bytes = 2;
else
number_of_bytes = 3;
target_ptr = s + number_of_bytes;
switch (number_of_bytes) {
case 3:
*--target_ptr = (wchar & 0x003f) | 0x80;
wchar >>= 6;
case 2:
*--target_ptr = (wchar & 0x003f) | 0x80;
wchar >>= 6;
case 1:
*--target_ptr = wchar | first_byte_mark[number_of_bytes];
}
return (number_of_bytes);
}
static inline int wctomb(char* s, wchar_t wchar) {
return (unicode_to_UTF8(s, wchar));
}
size_t wcstombs(char* s, const wchar_t* pwcs, size_t n) {
int chars_written = 0;
int result;
char temp[3];
wchar_t* source;
if (!s || !pwcs)
return (0);
source = (wchar_t*)pwcs;
while (chars_written <= n) {
if (!*source) {
*(s + chars_written) = '\0';
break;
} else {
result = wctomb(temp, *source++);
if ((chars_written + result) <= n) {
strcat(s + chars_written, temp, result);
chars_written += result;
} else
break;
}
}
return (chars_written);
}