Windows build: Adapted icon names to org.fsf.pspp naming
[pspp.git] / tests / libpspp / line-reader-test.c
blobd7881b5043de113d03dd6c31f26220793e3a4fa5
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/line-reader.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"
29 #include "libpspp/str.h"
31 #include "gl/error.h"
32 #include "gl/progname.h"
33 #include "gl/xalloc.h"
35 static void
36 usage (void)
38 printf ("usage: %s COMMAND [ARG]...\n"
39 "The available commands are:\n"
40 " help\n"
41 " print this usage message\n"
42 " buffer-size\n"
43 " print the buffer size, in bytes, on stdout\n"
44 " read FILE ENCODING\n"
45 " read FILE encoded in ENCODING and print it in UTF-8\n",
46 program_name);
47 exit (0);
50 static void
51 cmd_read (int argc, char *argv[])
53 struct line_reader *r;
54 const char *filename;
55 struct string line;
57 if (argc != 4)
58 error (1, 0, "bad syntax for `%s' command; use `%s help' for help",
59 argv[1], program_name);
61 filename = argv[2];
63 r = (!strcmp(filename, "-")
64 ? line_reader_for_fd (argv[3], STDIN_FILENO)
65 : line_reader_for_file (argv[3], filename, O_RDONLY));
66 if (r == NULL)
67 error (1, errno, "line_reader_open failed");
69 char *encoding = xstrdup (line_reader_get_encoding (r));
70 printf ("encoded in %s", encoding);
71 if (line_reader_is_auto (r))
72 printf (" (auto)");
73 printf ("\n");
75 ds_init_empty (&line);
76 while (line_reader_read (r, &line, SIZE_MAX))
78 const char *new_encoding;
79 char *utf8_line;
81 new_encoding = line_reader_get_encoding (r);
82 if (strcmp (encoding, new_encoding))
84 free (encoding);
85 encoding = xstrdup (new_encoding);
87 printf ("encoded in %s", encoding);
88 if (line_reader_is_auto (r))
89 printf (" (auto)");
90 printf ("\n");
93 utf8_line = recode_string ("UTF-8", encoding,
94 ds_data (&line), ds_length (&line));
95 printf ("\"%s\"\n", utf8_line);
96 free (utf8_line);
98 ds_clear (&line);
100 free (encoding);
101 ds_destroy (&line);
103 if (!strcmp(filename, "-"))
104 line_reader_free (r);
105 else
107 if (line_reader_close (r) != 0)
108 error (1, errno, "line_reader_close failed");
113 main (int argc, char *argv[])
115 set_program_name (argv[0]);
116 i18n_init ();
118 if (argc < 2)
119 error (1, 0, "missing command name; use `%s help' for help", program_name);
120 else if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help"))
121 usage ();
122 else if (!strcmp(argv[1], "buffer-size"))
123 printf ("%d\n", LINE_READER_BUFFER_SIZE);
124 else if (!strcmp(argv[1], "read"))
125 cmd_read (argc, argv);
126 else
127 error (1, 0, "unknown command `%s'; use `%s help' for help",
128 argv[1], program_name);
130 return 0;