LIST: Improve wording of error messages.
[pspp.git] / tests / libpspp / u8-istream-test.c
blob0a16798b0d9dfd52ccccf261902cea93be98311b
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #include <config.h>
19 #include "libpspp/u8-istream.h"
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "libpspp/i18n.h"
30 #include "gl/error.h"
31 #include "gl/progname.h"
32 #include "gl/xalloc.h"
34 static void
35 usage (void)
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);
50 static void
51 cmd_read (int argc, char *argv[])
53 struct u8_istream *is;
54 const char *encoding;
55 const char *filename;
56 int outbufsize;
57 char *buffer;
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);
63 outbufsize = argc > 4 ? atoi (argv[4]) : 4096;
64 buffer = xmalloc (outbufsize);
66 filename = argv[2];
67 encoding = *argv[3] ? argv[3] : NULL;
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");
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");
80 for (;;)
82 ssize_t n;
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;
92 free (buffer);
94 if (u8_istream_is_auto (is))
95 printf ("Auto mode\n");
96 else if (u8_istream_is_utf8 (is))
97 printf ("UTF-8 mode\n");
99 if (!strcmp(filename, "-"))
100 u8_istream_free (is);
101 else
103 if (u8_istream_close (is) != 0)
104 error (1, errno, "u8_istream_close failed");
109 main (int argc, char *argv[])
111 set_program_name (argv[0]);
112 i18n_init ();
114 if (argc < 2)
115 error (1, 0, "missing command name; use `%s help' for help", program_name);
116 else if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help"))
117 usage ();
118 else if (!strcmp(argv[1], "buffer-size"))
119 printf ("%d\n", U8_ISTREAM_BUFFER_SIZE);
120 else if (!strcmp(argv[1], "read"))
121 cmd_read (argc, argv);
122 else
123 error (1, 0, "unknown command `%s'; use `%s help' for help",
124 argv[1], program_name);
126 return 0;