summary refs log tree commit diff
path: root/libio/tst-widetext.c
blob: 9826c356fabc80a4b458fdc7c052eaefc63680a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Test program for the wide character stream functions handling larger
   amounts of text.
   Copyright (C) 2000-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@cygnus.com>.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <assert.h>
#include <iconv.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>

/* Approximate size of the file (must be larger).  */
#define SIZE 210000


static int
do_test (void)
{
  char name[] = "/tmp/widetext.out.XXXXXX";
  char mbbuf[SIZE];
  char mb2buf[SIZE];
  wchar_t wcbuf[SIZE];
  wchar_t wc2buf[SIZE];
  size_t mbsize;
  size_t wcsize;
  int fd;
  FILE *fp;
  size_t n;
  int res;
  int status = 0;
  wchar_t *wcp;

  setlocale (LC_ALL, "de_DE.UTF-8");
  printf ("locale used: %s\n\n", setlocale (LC_ALL, NULL));

  /* Read the file into memory.  */
  mbsize = fread (mbbuf, 1, SIZE, stdin);
  if (mbsize == 0)
    {
      printf ("%u: cannot read input file from standard input: %m\n",
	      __LINE__);
      exit (1);
    }

   printf ("INFO: input file has %Zd bytes\n", mbsize);

  /* First convert the text to wide characters.  We use iconv here.  */
  {
    iconv_t cd;
    char *inbuf = mbbuf;
    size_t inleft = mbsize;
    char *outbuf = (char *) wcbuf;
    size_t outleft = sizeof (wcbuf);
    size_t nonr;

    cd = iconv_open ("WCHAR_T", "UTF-8");
    if (cd == (iconv_t) -1)
      {
	printf ("%u: cannot get iconv descriptor for conversion to UCS4\n",
		__LINE__);
	exit (1);
      }

    /* We must need only one call and there must be no losses.  */
    nonr = iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
    if (nonr != 0 && nonr != (size_t) -1)
      {
	printf ("%u: iconv performed %Zd nonreversible conversions\n",
		__LINE__, nonr);
	exit (1);
      }

    if  (nonr == (size_t) -1)
      {
	printf ("\
%u: iconv returned with %Zd and errno = %m (inleft: %Zd, outleft: %Zd)\n",
		__LINE__, nonr, inleft, outleft);
	exit (1);
      }

    if (inleft != 0)
      {
	printf ("%u: iconv didn't convert all input\n", __LINE__);
	exit (1);
      }

    iconv_close (cd);

    if ((sizeof (wcbuf) - outleft) % sizeof (wchar_t) != 0)
      {
	printf ("%u: iconv converted not complete wchar_t\n", __LINE__);
	exit (1);
      }

    wcsize = (sizeof (wcbuf) - outleft) / sizeof (wchar_t);
    assert (wcsize + 1 <= SIZE);
  }

  /* Now that we finished the preparations, run the first test.  We
     are writing the wide char data out and read it back in.  We write
     and read single characters.  */

  fd = mkstemp (name);
  if (fd == -1)
    {
      printf ("%u: cannot open temporary file: %m\n", __LINE__);
      exit (1);
    }

  unlink (name);

  fp = fdopen (dup (fd), "w");
  if (fp == NULL)
    {
      printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__);
      exit (1);
    }

  for (n = 0; n < wcsize; ++n)
    {
      if (fputwc (wcbuf[n], fp) == WEOF)
	{
	  printf ("%u: fputwc failed: %m\n", __LINE__);
	  exit (1);
	}
    }

  res = fclose (fp);
  if (res != 0)
    {
      printf ("%u: fclose after single-character writing failed (%d): %m\n",
	      __LINE__, res);
      exit (1);
    }

  lseek (fd, SEEK_SET, 0);
  fp = fdopen (dup (fd), "r");
  if (fp == NULL)
    {
      printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
      exit (1);
    }

  for (n = 0; n < wcsize; ++n)
    {
      wint_t wch = fgetwc (fp);
      if (wch == WEOF)
	{
	  printf ("%u: fgetwc failed (idx %Zd): %m\n", __LINE__, n);
	  exit (1);
	}
      wc2buf[n] = wch;
    }

  /* There should be nothing else.  */
  if (fgetwc (fp) != WEOF)
    {
      printf ("%u: too many characters available with fgetwc\n", __LINE__);
      status = 1;
    }
  else if (wmemcmp (wcbuf, wc2buf, wcsize) != 0)
    {
      printf ("%u: buffer read with fgetwc differs\n", __LINE__);
      status = 1;
    }

  res = fclose (fp);
  if (res != 0)
    {
      printf ("%u: fclose after single-character reading failed (%d): %m\n",
	      __LINE__, res);
      exit (1);
    }

  /* Just make sure there are no two errors which hide each other, read the
     file using the `char' functions.  */

  lseek (fd, SEEK_SET, 0);
  fp = fdopen (fd, "r");
  if (fp == NULL)
    {
      printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
      exit (1);
    }

  if (fread (mb2buf, 1, mbsize, fp) != mbsize)
    {
      printf ("%u: cannot read all of the temp file\n", __LINE__);
      status = 1;
    }
  else
    {
      /* Make sure there is nothing left.  */
      if (fgetc (fp) != EOF)
	{
	  printf ("%u: more input available\n", __LINE__);
	  status = 1;
	}

      if (memcmp (mb2buf, mbbuf, mbsize) != 0)
	{
	  printf ("%u: buffer written with fputwc differs\n", __LINE__);
	  status = 1;
	}
    }

  res = fclose (fp);
  if (res != 0)
    {
      printf ("%u: fclose after single-character reading failed (%d): %m\n",
	      __LINE__, res);
      exit (1);
    }

  /* Now to reading and writing line-wise.  */

  fd = mkstemp (strcpy (name, "/tmp/widetext.out.XXXXXX"));
  if (fd == -1)
    {
      printf ("%u: cannot open temporary file: %m\n", __LINE__);
      exit (1);
    }

  unlink (name);

  fp = fdopen (dup (fd), "w");
  if (fp == NULL)
    {
      printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__);
      exit (1);
    }

  for (wcp = wcbuf; wcp < &wcbuf[wcsize]; )
    {
      wchar_t *wendp = wcschr (wcp, L'\n');

      if (wendp != NULL)
	{
	  /* Temporarily NUL terminate the line.  */
	  wchar_t save = wendp[1];
	  wendp[1] = L'\0';

	  fputws (wcp, fp);

	  wendp[1] = save;
	  wcp = &wendp[1];
	}
      else
	{
	  fputws (wcp, fp);
	  wcp = wcschr (wcp, L'\0');
	  assert (wcp == &wcbuf[wcsize]);
	}
    }

  res = fclose (fp);
  if (res != 0)
    {
      printf ("%u: fclose after line-wise writing failed (%d): %m\n",
	      __LINE__, res);
      exit (1);
    }

  lseek (fd, SEEK_SET, 0);
  fp = fdopen (dup (fd), "r");
  if (fp == NULL)
    {
      printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
      exit (1);
    }

  for (wcp = wc2buf; wcp < &wc2buf[wcsize]; )
    {
      if (fgetws (wcp, &wc2buf[wcsize] - wcp + 1, fp) == NULL)
	{
	  printf ("%u: short read using fgetws (only %td of %Zd)\n",
		  __LINE__, wcp - wc2buf, wcsize);
	  status = 1;
	  break;
	}
      wcp = wcschr (wcp, L'\0');
    }

  if (wcp > &wc2buf[wcsize])
    {
      printf ("%u: fgetws read too much\n", __LINE__);
      status = 1;
    }
  else if (fgetwc (fp) != WEOF)
    {
      /* There should be nothing else.  */
      printf ("%u: too many characters available with fgetws\n", __LINE__);
      status = 1;
    }

  if (wcp >= &wc2buf[wcsize] && wmemcmp (wcbuf, wc2buf, wcsize) != 0)
    {
      printf ("%u: buffer read with fgetws differs\n", __LINE__);
      status = 1;
    }

  res = fclose (fp);
  if (res != 0)
    {
      printf ("%u: fclose after single-character reading failed (%d): %m\n",
	      __LINE__, res);
      exit (1);
    }

  /* Just make sure there are no two errors which hide each other, read the
     file using the `char' functions.  */

  lseek (fd, SEEK_SET, 0);
  fp = fdopen (fd, "r");
  if (fp == NULL)
    {
      printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
      exit (1);
    }

  if (fread (mb2buf, 1, mbsize, fp) != mbsize)
    {
      printf ("%u: cannot read all of the temp file\n", __LINE__);
      status = 1;
    }
  else
    {
      /* Make sure there is nothing left.  */
      if (fgetc (fp) != EOF)
	{
	  printf ("%u: more input available\n", __LINE__);
	  status = 1;
	}

      if (memcmp (mb2buf, mbbuf, mbsize) != 0)
	{
	  printf ("%u: buffer written with fputws differs\n", __LINE__);
	  status = 1;
	}
    }

  res = fclose (fp);
  if (res != 0)
    {
      printf ("%u: fclose after single-character reading failed (%d): %m\n",
	      __LINE__, res);
      exit (1);
    }

  return status;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"