diff --git a/headers/posix/_mbstate_t.h b/headers/posix/_mbstate_t.h new file mode 100644 index 0000000000..0491c5962c --- /dev/null +++ b/headers/posix/_mbstate_t.h @@ -0,0 +1,20 @@ +/* + * Copyright 2008-2012 Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef __MBSTATE_T_H +#define __MBSTATE_T_H + + +#include + + +typedef struct { + void* converter; + char charset[64]; + unsigned int count; + char data[1024 + 8]; /* 1024 bytes for data, 8 for alignment space */ +} mbstate_t; + + +#endif /* __MBSTATE_T_H */ diff --git a/headers/posix/_wint_t.h b/headers/posix/_wint_t.h new file mode 100644 index 0000000000..58520c3cf8 --- /dev/null +++ b/headers/posix/_wint_t.h @@ -0,0 +1,30 @@ +/* + * Copyright 2008-2012 Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef __WINT_T_H +#define __WINT_T_H + + +#include + + +/* stddef.h is not supposed to define wint_t, but gcc 2.95.3's one does. + * In all other cases we will do that. */ +#ifndef _WINT_T +#define _WINT_T + +#ifndef __WINT_TYPE__ +#define __WINT_TYPE__ unsigned int +#endif + +typedef __WINT_TYPE__ wint_t; + +#endif /* _WINT_T */ + +#define WEOF ((wint_t)(-1)) + +typedef int wctype_t; + + +#endif /* __WINT_T_H */ diff --git a/headers/posix/stdint.h b/headers/posix/stdint.h index ae6a9134a4..d28c6c068b 100644 --- a/headers/posix/stdint.h +++ b/headers/posix/stdint.h @@ -138,6 +138,8 @@ typedef uint64_t uintmax_t; #define WINT_MIN 0 #define WINT_MAX ((wint_t)-1) +#define WCHAR_MIN 0x00000000UL +#define WCHAR_MAX 0x7FFFFFFFUL /* Macros of Integer Constant Expressions */ #define INT8_C(value) value diff --git a/headers/posix/uchar.h b/headers/posix/uchar.h index 461b87b2db..a4aa5c58d4 100644 --- a/headers/posix/uchar.h +++ b/headers/posix/uchar.h @@ -7,7 +7,14 @@ #include -#include +#include +#include <_mbstate_t.h> + + +#if !defined(__cplusplus) || __cplusplus < 201103L +typedef uint_least32_t char32_t; +typedef uint_least16_t char16_t; +#endif #ifdef __cplusplus @@ -15,10 +22,6 @@ extern "C" { #endif -typedef uint_least32_t char32_t; -typedef uint_least16_t char16_t; - - #define __STD_UTF_32__ 1 #define __STD_UTF_16__ 1 @@ -26,26 +29,9 @@ typedef uint_least16_t char16_t; // TODO implement mbrtoc16 -static __inline size_t -c16rtomb(char *dest, char32_t wc, mbstate_t *mbState) -{ - wchar_t tmp = (wchar_t)wc; - return wcrtomb(dest, tmp, mbState); -} - - -static __inline size_t -mbrtoc32(char32_t *dest, const char *src, size_t srcLength, mbstate_t *mbState) -{ - return mbrtowc((wchar_t*)dest, src, srcLength, mbState); -} - - -static __inline size_t -c32rtomb(char *dest, char32_t wc, mbstate_t *mbState) -{ - return wcrtomb(dest, (wchar_t)wc, mbState); -} +size_t c16rtomb(char *dest, char32_t wc, mbstate_t *mbState); +size_t mbrtoc32(char32_t *dest, const char *src, size_t srcLength, mbstate_t *mbState); +size_t c32rtomb(char *dest, char32_t wc, mbstate_t *mbState); #ifdef __cplusplus diff --git a/headers/posix/wchar.h b/headers/posix/wchar.h index 55a45c8ade..33d8afb602 100644 --- a/headers/posix/wchar.h +++ b/headers/posix/wchar.h @@ -12,34 +12,14 @@ #include #include - -/* stddef.h is not supposed to define wint_t, but gcc 2.95.3's one does. - * In all other cases we will do that. */ -#ifndef _WINT_T -#define _WINT_T - -#ifndef __WINT_TYPE__ -#define __WINT_TYPE__ unsigned int -#endif - -typedef __WINT_TYPE__ wint_t; - -#endif /* _WINT_T */ - -typedef int wctype_t; - -typedef struct { - void* converter; - char charset[64]; - unsigned int count; - char data[1024 + 8]; /* 1024 bytes for data, 8 for alignment space */ -} mbstate_t; +#include <_mbstate_t.h> +#include <_wint_t.h> -#define WEOF ((wint_t)(-1)) - +#ifndef WCHAR_MIN #define WCHAR_MIN 0x00000000UL #define WCHAR_MAX 0x7FFFFFFFUL +#endif /* * Haiku is always using UTF32 in wchars, other encodings can be handled diff --git a/headers/posix/wctype.h b/headers/posix/wctype.h index 96a9d48112..50c004cf3d 100644 --- a/headers/posix/wctype.h +++ b/headers/posix/wctype.h @@ -7,7 +7,7 @@ #include -#include +#include <_wint_t.h> typedef int wctrans_t; diff --git a/headers/private/libroot/locale/LocaleBackend.h b/headers/private/libroot/locale/LocaleBackend.h index e980d6482a..4bf4f08207 100644 --- a/headers/private/libroot/locale/LocaleBackend.h +++ b/headers/private/libroot/locale/LocaleBackend.h @@ -10,6 +10,7 @@ #include #include +#include #include diff --git a/src/system/libroot/posix/glibc/include/wchar.h b/src/system/libroot/posix/glibc/include/wchar.h index 8a4deed8be..25da730acf 100644 --- a/src/system/libroot/posix/glibc/include/wchar.h +++ b/src/system/libroot/posix/glibc/include/wchar.h @@ -2,7 +2,6 @@ #include # ifdef _WCHAR_H -typedef unsigned long int wctype_t; /* Now define the internal interfaces. */ extern int __wcscasecmp (__const wchar_t *__s1, __const wchar_t *__s2) diff --git a/src/system/libroot/posix/glibc/wcsmbs/wchar.h b/src/system/libroot/posix/glibc/wcsmbs/wchar.h index b4d8f2883f..1da5d4d9b7 100644 --- a/src/system/libroot/posix/glibc/wcsmbs/wchar.h +++ b/src/system/libroot/posix/glibc/wcsmbs/wchar.h @@ -89,25 +89,8 @@ typedef struct /* Public type. */ typedef __mbstate_t mbstate_t; -#ifndef WCHAR_MIN -/* These constants might also be defined in . */ -# define WCHAR_MIN __WCHAR_MIN -# define WCHAR_MAX __WCHAR_MAX -#endif -#ifndef WEOF -# define WEOF (0xffffffffu) -#endif - -/* For XPG4 compliance we have to define the stuff from here - as well. */ -#if defined __USE_XOPEN && !defined __USE_UNIX98 # include -#endif - -/* This incomplete type is defined in but needed here because - of `wcsftime'. */ -struct tm; __BEGIN_DECLS diff --git a/src/system/libroot/posix/wchar/Jamfile b/src/system/libroot/posix/wchar/Jamfile index ce58b99b40..1774a9c3a2 100644 --- a/src/system/libroot/posix/wchar/Jamfile +++ b/src/system/libroot/posix/wchar/Jamfile @@ -12,6 +12,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { MergeObject <$(architecture)>posix_wchar.o : btowc.c + uchar.cpp mblen.c mbrlen.c mbrtowc.cpp diff --git a/src/system/libroot/posix/wchar/uchar.cpp b/src/system/libroot/posix/wchar/uchar.cpp new file mode 100644 index 0000000000..3e39c724df --- /dev/null +++ b/src/system/libroot/posix/wchar/uchar.cpp @@ -0,0 +1,43 @@ +/* + * Copyright 2026 Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#include +#include + + +static mbstate_t c16rtombState; + + +extern "C" size_t +c16rtomb(char *dest, char32_t wc, mbstate_t *mbState) +{ + if (mbState == NULL) + mbState = &c16rtombState; + wchar_t tmp = (wchar_t)wc; + return wcrtomb(dest, tmp, mbState); +} + + +static mbstate_t mbrtoc32State; + + +extern "C" size_t +mbrtoc32(char32_t *dest, const char *src, size_t srcLength, mbstate_t *mbState) +{ + if (mbState == NULL) + mbState = &mbrtoc32State; + return mbrtowc((wchar_t*)dest, src, srcLength, mbState); +} + + +static mbstate_t c32rtombState; + + +extern "C" size_t +c32rtomb(char *dest, char32_t wc, mbstate_t *mbState) +{ + if (mbState == NULL) + mbState = &c32rtombState; + return wcrtomb(dest, (wchar_t)wc, mbState); +}