man-pages/man3/mbstowcs.3.html
2021-03-31 01:06:50 +01:00

322 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MBSTOWCS</TITLE>
</HEAD><BODY>
<H1>MBSTOWCS</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
mbstowcs - convert a multibyte string to a wide-character string
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>size_t mbstowcs(wchar_t *</B><I>dest</I><B>, const char *</B><I>src</I><B>, size_t </B><I>n</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
If
<I>dest</I>
is not NULL,
the
<B>mbstowcs</B>()
function converts the
multibyte string
<I>src</I>
to a wide-character string starting at
<I>dest</I>.
At most
<I>n</I>
wide characters are written to
<I>dest</I>.
The sequence of characters in the string
<I>src</I>
shall begin in the initial shift state.
The conversion can stop for three reasons:
<DL COMPACT>
<DT id="1">1.<DD>
An invalid multibyte sequence has been encountered.
In this case,
<I>(size_t)&nbsp;-1</I>
is returned.
<DT id="2">2.<DD>
<I>n</I>
non-L'\0' wide characters have been stored at
<I>dest</I>.
In this case, the number of wide characters written to
<I>dest</I>
is returned, but the
shift state at this point is lost.
<DT id="3">3.<DD>
The multibyte string has been completely converted, including the
terminating null character ('\0').
In this case, the number of wide characters written to
<I>dest</I>,
excluding the terminating null wide character, is returned.
</DL>
<P>
The programmer must ensure that there is room for at least
<I>n</I>
wide
characters at
<I>dest</I>.
<P>
If
<I>dest</I>
is NULL,
<I>n</I>
is ignored, and the conversion proceeds as
above, except that the converted wide characters are not written out to memory,
and that no length limit exists.
<P>
In order to avoid the case 2 above, the programmer should make sure
<I>n</I>
is
greater than or equal to
<I>mbstowcs(NULL,src,0)+1</I>.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>mbstowcs</B>()
function returns the number of wide characters that make
up the converted part of the wide-character string, not including the
terminating null wide character.
If an invalid multibyte sequence was
encountered,
<I>(size_t)&nbsp;-1</I>
is returned.
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>mbstowcs</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, C99.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The behavior of
<B>mbstowcs</B>()
depends on the
<B>LC_CTYPE</B>
category of the
current locale.
<P>
The function
<B><A HREF="/cgi-bin/man/man2html?3+mbsrtowcs">mbsrtowcs</A></B>(3)
provides a better interface to the same
functionality.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below illustrates the use of
<B>mbstowcs</B>(),
as well as some of the wide character classification functions.
An example run is the following:
<P>
$ ./t_mbstowcs de_DE.UTF-8 Grüße!
Length of source string (excluding terminator):
<BR>&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;bytes
<BR>&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;multibyte&nbsp;characters
<P>
Wide character string is: Grüße! (6 characters)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;G&nbsp;alpha&nbsp;upper
<BR>&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;alpha&nbsp;lower
<BR>&nbsp;&nbsp;&nbsp;&nbsp;ü&nbsp;alpha&nbsp;lower
<BR>&nbsp;&nbsp;&nbsp;&nbsp;ß&nbsp;alpha&nbsp;lower
<BR>&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;alpha&nbsp;lower
<BR>&nbsp;&nbsp;&nbsp;&nbsp;!&nbsp;!alpha
<A NAME="lbAJ">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/wctype.h">wctype.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/locale.h">locale.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/wchar.h">wchar.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;mbslen;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;multibyte&nbsp;characters&nbsp;in&nbsp;source&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;wchar_t&nbsp;*wcs;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Pointer&nbsp;to&nbsp;converted&nbsp;wide&nbsp;character&nbsp;string&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;wchar_t&nbsp;*wp;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;3)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;locale&gt;&nbsp;&lt;string&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Apply&nbsp;the&nbsp;specified&nbsp;locale&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(setlocale(LC_ALL,&nbsp;argv[1])&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;setlocale&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Calculate&nbsp;the&nbsp;length&nbsp;required&nbsp;to&nbsp;hold&nbsp;argv[2]&nbsp;converted&nbsp;to
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;wide&nbsp;character&nbsp;string&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;mbslen&nbsp;=&nbsp;mbstowcs(NULL,&nbsp;argv[2],&nbsp;0);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(mbslen&nbsp;==&nbsp;(size_t)&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;mbstowcs&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Describe&nbsp;the&nbsp;source&nbsp;string&nbsp;to&nbsp;the&nbsp;user&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Length&nbsp;of&nbsp;source&nbsp;string&nbsp;(excluding&nbsp;terminator):\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;%zu&nbsp;bytes\n&quot;,&nbsp;strlen(argv[2]));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;%zu&nbsp;multibyte&nbsp;characters\n\n&quot;,&nbsp;mbslen);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Allocate&nbsp;wide&nbsp;character&nbsp;string&nbsp;of&nbsp;the&nbsp;desired&nbsp;size.&nbsp;&nbsp;Add&nbsp;1
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;allow&nbsp;for&nbsp;terminating&nbsp;null&nbsp;wide&nbsp;character&nbsp;(L'\0').&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;wcs&nbsp;=&nbsp;calloc(mbslen&nbsp;+&nbsp;1,&nbsp;sizeof(wchar_t));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(wcs&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;calloc&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Convert&nbsp;the&nbsp;multibyte&nbsp;character&nbsp;string&nbsp;in&nbsp;argv[2]&nbsp;to&nbsp;a
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wide&nbsp;character&nbsp;string&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(mbstowcs(wcs,&nbsp;argv[2],&nbsp;mbslen&nbsp;+&nbsp;1)&nbsp;==&nbsp;(size_t)&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;mbstowcs&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Wide&nbsp;character&nbsp;string&nbsp;is:&nbsp;%ls&nbsp;(%zu&nbsp;characters)\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wcs,&nbsp;mbslen);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Now&nbsp;do&nbsp;some&nbsp;inspection&nbsp;of&nbsp;the&nbsp;classes&nbsp;of&nbsp;the&nbsp;characters&nbsp;in
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;wide&nbsp;character&nbsp;string&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(wp&nbsp;=&nbsp;wcs;&nbsp;*wp&nbsp;!=&nbsp;0;&nbsp;wp++)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;%lc&nbsp;&quot;,&nbsp;(wint_t)&nbsp;*wp);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!iswalpha(*wp))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;!&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;alpha&nbsp;&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(iswalpha(*wp))&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(iswupper(*wp))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;upper&nbsp;&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(iswlower(*wp))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;lower&nbsp;&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar('\n');
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+mblen">mblen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mbsrtowcs">mbsrtowcs</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mbtowc">mbtowc</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+wcstombs">wcstombs</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+wctomb">wctomb</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="4"><A HREF="#lbAB">NAME</A><DD>
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="7"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="8"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="9"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="10"><A HREF="#lbAH">NOTES</A><DD>
<DT id="11"><A HREF="#lbAI">EXAMPLE</A><DD>
<DL>
<DT id="12"><A HREF="#lbAJ">Program source</A><DD>
</DL>
<DT id="13"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:48 GMT, March 31, 2021
</BODY>
</HTML>