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/>. */
19 #include "libpspp/u8-istream.h"
28 #include "libpspp/i18n.h"
31 #include "gl/progname.h"
32 #include "gl/xalloc.h"
37 printf ("usage: %s COMMAND [ARG]...\n"
38 "The available commands are:\n"
40 " print this usage message\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",
51 cmd_read (int argc, char *argv[])
53 struct u8_istream *is;
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);
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));
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");
84 n = u8_istream_read (is, buffer, outbufsize);
86 fwrite (buffer, 1, n, stdout);
88 error (1, errno, "u8_istream_read failed");
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);
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]);
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"))
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);
123 error (1, 0, "unknown command `%s'; use `%s help' for help",
124 argv[1], program_name);