Back to index

glibc  2.9
tst-mbswcs5.c
Go to the documentation of this file.
00001 /* Test restarting behaviour of wcrtomb.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Bruno Haible <haible@ilog.fr>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <wchar.h>
00024 #include <locale.h>
00025 
00026 #define show(expr, nexp, bufexp) \
00027   {                                                            \
00028     size_t res = expr;                                                \
00029     printf (#expr " -> %Zd", res);                             \
00030     dst += res;                                                       \
00031     printf (", dst = buf+%td", dst - (char *) buf);                   \
00032     if (res != nexp || dst != (char *) (bufexp))               \
00033       {                                                               \
00034        printf (", expected %Zd and buf+%td", nexp,                    \
00035               (bufexp) - (unsigned char *) buf);               \
00036        result = 1;                                             \
00037       }                                                               \
00038     putc ('\n', stdout);                                       \
00039   }
00040 
00041 int
00042 main (void)
00043 {
00044   unsigned char buf[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
00045   const unsigned char bufcheck[7] = { 0x25, 0xe2, 0x82, 0xac, 0xce, 0xbb, 0 };
00046   const wchar_t srcbuf[4] = { 0x25, 0x20ac, 0x03bb, 0 };
00047   mbstate_t state;
00048   const wchar_t *src;
00049   char *dst;
00050   int result = 0;
00051   const char *used_locale;
00052 
00053   setlocale (LC_CTYPE, "de_DE.UTF-8");
00054   /* Double check.  */
00055   used_locale = setlocale (LC_CTYPE, NULL);
00056   printf ("used locale: \"%s\"\n", used_locale);
00057   result = strcmp (used_locale, "de_DE.UTF-8");
00058 
00059   memset (&state, '\0', sizeof (state));
00060 
00061   src = srcbuf;
00062   dst = (char *) buf;
00063   show (wcrtomb (dst, *src++, &state), 1, buf + 1);
00064   show (wcrtomb (dst, *src++, &state), 3, buf + 4);
00065   show (wcrtomb (dst, *src++, &state), 2, buf + 6);
00066   show (wcrtomb (dst, *src, &state), 1, buf + 7);
00067 
00068   if (memcmp (buf, bufcheck, 7))
00069     {
00070       puts ("wrong results");
00071       result = 1;
00072     }
00073 
00074   return result;
00075 }