Cygwin: (mostly) drop NT4 and Samba < 3.0 support
[newlib-cygwin.git] / winsup / cygwin / local_includes / wchar.h
blob606559a6abc8d9c5cd8cffb3787c970d7b8da2c0
1 /* wchar.h: Extra wchar defs
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #ifndef _CYGWIN_WCHAR_H
10 #define _CYGWIN_WCHAR_H
12 #include_next <wchar.h>
14 /* Internal headers from newlib */
15 #include "../locale/setlocale.h"
16 #include <uchar.h>
18 #define ENCODING_LEN 31
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
24 typedef int mbtowc_f (struct _reent *, wchar_t *, const char *, size_t,
25 mbstate_t *);
26 typedef mbtowc_f *mbtowc_p;
28 extern mbtowc_f __ascii_mbtowc;
29 extern mbtowc_f __utf8_mbtowc;
30 extern mbtowc_p __iso_mbtowc (int);
31 extern mbtowc_p __cp_mbtowc (int);
33 #define __MBTOWC (__get_current_locale ()->mbtowc)
35 typedef int wctomb_f (struct _reent *, char *, wchar_t, mbstate_t *);
36 typedef wctomb_f *wctomb_p;
38 extern wctomb_f __ascii_wctomb;
39 extern wctomb_f __utf8_wctomb;
41 #define __WCTOMB (__get_current_locale ()->wctomb)
43 /* convert wint_t string to wchar_t string. Make sure dest
44 has room for at least twice as much characters to account
45 for surrogate pairs, plus a wchar_t NUL. */
46 void wcintowcs (wchar_t *, wint_t *, size_t);
48 /* replacement function for wcrtomb, converting a UTF-32 char to a
49 multibyte string. */
50 static inline size_t
51 wirtomb (char *s, wint_t wc, mbstate_t *ps)
53 return c32rtomb (s,(char32_t) wc, ps);
56 /* replacement function for mbrtowc, returning a wint_t representing
57 a UTF-32 value. */
58 static inline size_t
59 mbrtowi (wint_t *pwc, const char *s, size_t n, mbstate_t *ps)
61 return mbrtoc32 ((char32_t *) pwc, s, n, ps);
64 /* replacement function for mbsnrtowcs, returning a wint_t representing
65 a UTF-32 value. Defined in strfuncs.cc.
66 Deviation from standard: If the input is broken, the output will be
67 broken. I. e., we just copy the current byte over into the wint_t
68 destination and try to pick up on the next byte. This is in line
69 with the way fnmatch works. */
70 extern size_t mbsnrtowci(wint_t *, const char **, size_t, size_t, mbstate_t *);
72 /* convert wint_t string to char string, but *only* if the string consists
73 entirely of ASCII chars */
74 static inline void
75 wcitoascii(char *dst, wint_t *src)
77 while ((*dst++ = *src++));
80 /* like wcslen, just for wint_t */
81 static inline size_t
82 wcilen (const wint_t *wcs)
84 size_t ret = 0;
86 if (wcs)
87 while (*wcs++)
88 ++ret;
89 return ret;
92 /* like wcschr, just for wint_t */
93 static inline wint_t *
94 wcichr (const wint_t *str, wint_t chr)
98 if (*str == chr)
99 return (wint_t *) str;
101 while (*str++);
102 return NULL;
105 /* like wcscmp, just for wint_t */
106 static inline int
107 wcicmp (const wint_t *s1, const wint_t *s2)
109 while (*s1 == *s2++)
110 if (*s1++ == 0)
111 return (0);
112 return (*s1 - *--s2);
115 /* like wcsncmp, just for wint_t */
116 static inline int
117 wcincmp (const wint_t *s1, const wint_t *s2, size_t n)
119 if (n == 0)
120 return (0);
123 if (*s1 != *s2++)
125 return (*s1 - *--s2);
127 if (*s1++ == 0)
128 break;
130 while (--n != 0);
131 return (0);
134 /* like wcpcpy, just for wint_t */
135 static inline wint_t *
136 wcipcpy (wint_t *s1, const wint_t *s2)
138 while ((*s1++ = *s2++))
140 return --s1;
143 /* like wcpncpy, just for wint_t */
144 static inline wint_t *
145 wcipncpy (wint_t *dst, const wint_t *src, size_t count)
147 wint_t *ret = NULL;
149 while (count > 0)
151 --count;
152 if ((*dst++ = *src++) == L'\0')
154 ret = dst - 1;
155 break;
158 while (count-- > 0)
159 *dst++ = L'\0';
161 return ret ? ret : dst;
164 #ifdef __cplusplus
166 #endif
168 #ifdef __INSIDE_CYGWIN__
169 #ifdef __cplusplus
170 extern size_t _sys_wcstombs (char *dst, size_t len, const wchar_t *src,
171 size_t nwc, bool is_path);
172 extern size_t _sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src,
173 size_t nwc, bool is_path);
175 static inline size_t
176 sys_wcstombs (char *dst, size_t len, const wchar_t * src,
177 size_t nwc = (size_t) -1)
179 return _sys_wcstombs (dst, len, src, nwc, true);
182 static inline size_t
183 sys_wcstombs_no_path (char *dst, size_t len, const wchar_t * src,
184 size_t nwc = (size_t) -1)
186 return _sys_wcstombs (dst, len, src, nwc, false);
189 static inline size_t
190 sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src,
191 size_t nwc = (size_t) -1)
193 return _sys_wcstombs_alloc (dst_p, type, src, nwc, true);
196 static inline size_t
197 sys_wcstombs_alloc_no_path (char **dst_p, int type, const wchar_t *src,
198 size_t nwc = (size_t) -1)
200 return _sys_wcstombs_alloc (dst_p, type, src, nwc, false);
203 size_t _sys_mbstowcs (mbtowc_p, wchar_t *, size_t, const char *,
204 size_t = (size_t) -1);
206 static inline size_t
207 sys_mbstowcs (wchar_t * dst, size_t dlen, const char *src,
208 size_t nms = (size_t) -1)
210 mbtowc_p f_mbtowc = (__MBTOWC == __ascii_mbtowc) ? __utf8_mbtowc : __MBTOWC;
211 return _sys_mbstowcs (f_mbtowc, dst, dlen, src, nms);
214 size_t sys_mbstowcs_alloc (wchar_t **, int, const char *, size_t = (size_t) -1);
216 static inline size_t
217 sys_mbstouni (PUNICODE_STRING dst, int type, const char *src,
218 size_t nms = (size_t) -1)
220 /* sys_mbstowcs returns length *excluding* trailing \0 */
221 size_t len = sys_mbstowcs (dst->Buffer, type, src, nms);
222 dst->Length = len * sizeof (WCHAR);
223 dst->MaximumLength = dst->Length + sizeof (WCHAR);
224 return dst->Length;
227 static inline size_t
228 sys_mbstouni_alloc (PUNICODE_STRING dst, int type, const char *src,
229 size_t nms = (size_t) -1)
231 /* sys_mbstowcs_alloc returns length *including* trailing \0 */
232 size_t len = sys_mbstowcs_alloc (&dst->Buffer, type, src, nms);
233 dst->MaximumLength = len * sizeof (WCHAR);
234 dst->Length = dst->MaximumLength - sizeof (WCHAR);
235 return dst->MaximumLength;
238 #endif /* __cplusplus */
239 #endif /* __INSIDE_CYGWIN__ */
241 #endif /* _CYGWIN_WCHAR_H */