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
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"
18 #define ENCODING_LEN 31
24 typedef int mbtowc_f (struct _reent
*, wchar_t *, const char *, size_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
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
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 */
75 wcitoascii(char *dst
, wint_t *src
)
77 while ((*dst
++ = *src
++));
80 /* like wcslen, just for wint_t */
82 wcilen (const wint_t *wcs
)
92 /* like wcschr, just for wint_t */
93 static inline wint_t *
94 wcichr (const wint_t *str
, wint_t chr
)
99 return (wint_t *) str
;
105 /* like wcscmp, just for wint_t */
107 wcicmp (const wint_t *s1
, const wint_t *s2
)
112 return (*s1
- *--s2
);
115 /* like wcsncmp, just for wint_t */
117 wcincmp (const wint_t *s1
, const wint_t *s2
, size_t n
)
125 return (*s1
- *--s2
);
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
++))
143 /* like wcpncpy, just for wint_t */
144 static inline wint_t *
145 wcipncpy (wint_t *dst
, const wint_t *src
, size_t count
)
152 if ((*dst
++ = *src
++) == L
'\0')
161 return ret
? ret
: dst
;
168 #ifdef __INSIDE_CYGWIN__
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
);
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);
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);
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);
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);
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);
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
);
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 */