u8-istream: New library for reading a text file and recoding to UTF-8.
[pspp-builds.git] / tests / libpspp / u8-istream-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "libpspp/u8-istream.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "libpspp/i18n.h"
29
30 #include "gl/error.h"
31 #include "gl/progname.h"
32 #include "gl/xalloc.h"
33
34 static void
35 usage (void)
36 {
37   printf ("usage: %s COMMAND [ARG]...\n"
38           "The available commands are:\n"
39           "  help\n"
40           "    print this usage message\n"
41           "  buffer-size\n"
42           "    print the buffer size, in bytes, on stdout\n"
43           "  read FILE ENCODING [OUTBUF]\n"
44           "    read FILE encoded in ENCODING (with output buffer size\n"
45           "    OUTBUF) and print it on stdout in UTF-8\n",
46           program_name);
47   exit (0);
48 }
49
50 static void
51 cmd_read (int argc, char *argv[])
52 {
53   struct u8_istream *is;
54   const char *encoding;
55   const char *filename;
56   int outbufsize;
57   char *buffer;
58
59   if (argc < 4 || argc > 5)
60     error (1, 0, "bad syntax for `%s' command; use `%s help' for help",
61            argv[1], program_name);
62
63   outbufsize = argc > 4 ? atoi (argv[4]) : 4096;
64   buffer = xmalloc (outbufsize);
65
66   filename = argv[2];
67   encoding = *argv[3] ? argv[3] : NULL;
68
69   is = (!strcmp(filename, "-")
70         ? u8_istream_for_fd (encoding, STDIN_FILENO)
71         : u8_istream_for_file (encoding, filename, O_RDONLY));
72   if (is == NULL)
73     error (1, errno, "u8_istream_open failed");
74
75   if (u8_istream_is_auto (is))
76     printf ("Auto mode\n");
77   else if (u8_istream_is_utf8 (is))
78     printf ("UTF-8 mode\n");
79
80   for (;;)
81     {
82       ssize_t n;
83
84       n = u8_istream_read (is, buffer, outbufsize);
85       if (n > 0)
86         fwrite (buffer, 1, n, stdout);
87       else if (n < 0)
88         error (1, errno, "u8_istream_read failed");
89       else
90         break;
91     }
92
93   if (u8_istream_is_auto (is))
94     printf ("Auto mode\n");
95   else if (u8_istream_is_utf8 (is))
96     printf ("UTF-8 mode\n");
97
98   if (!strcmp(filename, "-"))
99     u8_istream_free (is);
100   else
101     {
102       if (u8_istream_close (is) != 0)
103         error (1, errno, "u8_istream_close failed");
104     }
105 }
106
107 int
108 main (int argc, char *argv[])
109 {
110   set_program_name (argv[0]);
111   i18n_init ();
112
113   if (argc < 2)
114     error (1, 0, "missing command name; use `%s help' for help", program_name);
115   else if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help"))
116     usage ();
117   else if (!strcmp(argv[1], "buffer-size"))
118     printf ("%d\n", U8_ISTREAM_BUFFER_SIZE);
119   else if (!strcmp(argv[1], "read"))
120     cmd_read (argc, argv);
121   else
122     error (1, 0, "unknown command `%s'; use `%s help' for help",
123            argv[1], program_name);
124
125   return 0;
126 }