1 /* Test program for the wide character stream functions handling larger
3 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
30 /* Approximate size of the file (must be larger). */
37 char name
[] = "/tmp/widetext.out.XXXXXX";
51 setlocale (LC_ALL
, "de_DE.UTF-8");
52 printf ("locale used: %s\n\n", setlocale (LC_ALL
, NULL
));
54 /* Read the file into memory. */
55 mbsize
= fread (mbbuf
, 1, SIZE
, stdin
);
58 printf ("%u: cannot read input file from standard input: %m\n",
63 printf ("INFO: input file has %Zd bytes\n", mbsize
);
65 /* First convert the text to wide characters. We use iconv here. */
69 size_t inleft
= mbsize
;
70 char *outbuf
= (char *) wcbuf
;
71 size_t outleft
= sizeof (wcbuf
);
74 cd
= iconv_open ("WCHAR_T", "UTF-8");
75 if (cd
== (iconv_t
) -1)
77 printf ("%u: cannot get iconv descriptor for conversion to UCS4\n",
82 /* We must need only one call and there must be no losses. */
83 nonr
= iconv (cd
, &inbuf
, &inleft
, &outbuf
, &outleft
);
84 if (nonr
!= 0 && nonr
!= (size_t) -1)
86 printf ("%u: iconv performed %Zd nonreversible conversions\n",
91 if (nonr
== (size_t) -1)
94 %u: iconv returned with %Zd and errno = %m (inleft: %Zd, outleft: %Zd)\n",
95 __LINE__
, nonr
, inleft
, outleft
);
101 printf ("%u: iconv didn't convert all input\n", __LINE__
);
107 if ((sizeof (wcbuf
) - outleft
) % sizeof (wchar_t) != 0)
109 printf ("%u: iconv converted not complete wchar_t\n", __LINE__
);
113 wcsize
= (sizeof (wcbuf
) - outleft
) / sizeof (wchar_t);
114 assert (wcsize
+ 1 <= SIZE
);
117 /* Now that we finished the preparations, run the first test. We
118 are writing the wide char data out and read it back in. We write
119 and read single characters. */
124 printf ("%u: cannot open temporary file: %m\n", __LINE__
);
130 fp
= fdopen (dup (fd
), "w");
133 printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__
);
137 for (n
= 0; n
< wcsize
; ++n
)
139 if (fputwc (wcbuf
[n
], fp
) == WEOF
)
141 printf ("%u: fputwc failed: %m\n", __LINE__
);
149 printf ("%u: fclose after single-character writing failed (%d): %m\n",
154 lseek (fd
, SEEK_SET
, 0);
155 fp
= fdopen (dup (fd
), "r");
158 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
162 for (n
= 0; n
< wcsize
; ++n
)
164 wint_t wch
= fgetwc (fp
);
167 printf ("%u: fgetwc failed (idx %Zd): %m\n", __LINE__
, n
);
173 /* There should be nothing else. */
174 if (fgetwc (fp
) != WEOF
)
176 printf ("%u: too many characters available with fgetwc\n", __LINE__
);
179 else if (wmemcmp (wcbuf
, wc2buf
, wcsize
) != 0)
181 printf ("%u: buffer read with fgetwc differs\n", __LINE__
);
188 printf ("%u: fclose after single-character reading failed (%d): %m\n",
193 /* Just make sure there are no two errors which hide each other, read the
194 file using the `char' functions. */
196 lseek (fd
, SEEK_SET
, 0);
197 fp
= fdopen (fd
, "r");
200 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
204 if (fread (mb2buf
, 1, mbsize
, fp
) != mbsize
)
206 printf ("%u: cannot read all of the temp file\n", __LINE__
);
211 /* Make sure there is nothing left. */
212 if (fgetc (fp
) != EOF
)
214 printf ("%u: more input available\n", __LINE__
);
218 if (memcmp (mb2buf
, mbbuf
, mbsize
) != 0)
220 printf ("%u: buffer written with fputwc differs\n", __LINE__
);
228 printf ("%u: fclose after single-character reading failed (%d): %m\n",
233 /* Now to reading and writing line-wise. */
235 fd
= mkstemp (strcpy (name
, "/tmp/widetext.out.XXXXXX"));
238 printf ("%u: cannot open temporary file: %m\n", __LINE__
);
244 fp
= fdopen (dup (fd
), "w");
247 printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__
);
251 for (wcp
= wcbuf
; wcp
< &wcbuf
[wcsize
]; )
253 wchar_t *wendp
= wcschr (wcp
, L
'\n');
257 /* Temporarily NUL terminate the line. */
258 wchar_t save
= wendp
[1];
269 wcp
= wcschr (wcp
, L
'\0');
270 assert (wcp
== &wcbuf
[wcsize
]);
277 printf ("%u: fclose after line-wise writing failed (%d): %m\n",
282 lseek (fd
, SEEK_SET
, 0);
283 fp
= fdopen (dup (fd
), "r");
286 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
290 for (wcp
= wc2buf
; wcp
< &wc2buf
[wcsize
]; )
292 if (fgetws (wcp
, &wc2buf
[wcsize
] - wcp
+ 1, fp
) == NULL
)
294 printf ("%u: short read using fgetws (only %Zd of %Zd)\n",
295 __LINE__
, wcp
- wc2buf
, wcsize
);
299 wcp
= wcschr (wcp
, L
'\0');
302 if (wcp
> &wc2buf
[wcsize
])
304 printf ("%u: fgetws read too much\n", __LINE__
);
307 else if (fgetwc (fp
) != WEOF
)
309 /* There should be nothing else. */
310 printf ("%u: too many characters available with fgetws\n", __LINE__
);
314 if (wcp
>= &wc2buf
[wcsize
] && wmemcmp (wcbuf
, wc2buf
, wcsize
) != 0)
316 printf ("%u: buffer read with fgetws differs\n", __LINE__
);
323 printf ("%u: fclose after single-character reading failed (%d): %m\n",
328 /* Just make sure there are no two errors which hide each other, read the
329 file using the `char' functions. */
331 lseek (fd
, SEEK_SET
, 0);
332 fp
= fdopen (fd
, "r");
335 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
339 if (fread (mb2buf
, 1, mbsize
, fp
) != mbsize
)
341 printf ("%u: cannot read all of the temp file\n", __LINE__
);
346 /* Make sure there is nothing left. */
347 if (fgetc (fp
) != EOF
)
349 printf ("%u: more input available\n", __LINE__
);
353 if (memcmp (mb2buf
, mbbuf
, mbsize
) != 0)
355 printf ("%u: buffer written with fputws differs\n", __LINE__
);
363 printf ("%u: fclose after single-character reading failed (%d): %m\n",