1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
39 #define STDOUT_FILENO 1
43 static gchar **locations = NULL;
44 static char *from_charset = NULL;
45 static char *to_charset = NULL;
46 static gboolean decompress = FALSE;
47 static gboolean compress = FALSE;
48 static gboolean gzip = FALSE;
49 static gboolean fallback = FALSE;
51 static GOptionEntry entries[] = {
52 {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
53 {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
54 {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
55 {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
56 {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
57 {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
58 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
63 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
68 const gchar *filename;
70 file_info = g_zlib_decompressor_get_file_info (decompressor);
71 if (file_info == NULL)
74 filename = g_file_info_get_name (file_info);
76 g_printerr ("Decompressor filename: %s\n", filename);
83 char buffer[1024 * 8 + 1];
89 GCharsetConverter *cconv = NULL;
92 in = (GInputStream *) g_file_read (file, NULL, &error);
95 /* Translators: the first %s is the program name, the second one */
96 /* is the URI of the file, the third is the error message. */
97 g_printerr ("%s: %s: error opening file: %s\n",
98 g_get_prgname (), g_file_get_uri (file), error->message);
106 conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
108 in = (GInputStream *) g_converter_input_stream_new (in, conv);
109 g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
110 g_object_unref (conv);
111 g_object_unref (old);
114 if (from_charset && to_charset)
116 cconv = g_charset_converter_new (to_charset, from_charset, &error);
117 conv = (GConverter *)cconv;
122 g_charset_converter_set_use_fallback (cconv, fallback);
125 in = (GInputStream *) g_converter_input_stream_new (in, conv);
126 g_object_unref (conv);
127 g_object_unref (old);
131 g_printerr ("%s: Can't convert between charsets: %s\n",
132 g_get_prgname (), error->message);
139 GFileInfo *in_file_info;
141 in_file_info = g_file_query_info (file,
142 G_FILE_ATTRIBUTE_STANDARD_NAME ","
143 G_FILE_ATTRIBUTE_TIME_MODIFIED,
144 G_FILE_QUERY_INFO_NONE,
147 if (in_file_info == NULL)
149 g_printerr ("%s: %s: error reading file info: %s\n",
150 g_get_prgname (), g_file_get_uri (file), error->message);
151 g_error_free (error);
155 conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
156 g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
158 in = (GInputStream *) g_converter_input_stream_new (in, conv);
159 g_object_unref (conv);
160 g_object_unref (old);
161 g_object_unref (in_file_info);
167 g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
175 written = write (STDOUT_FILENO, p, res);
177 if (written == -1 && errno != EINTR)
179 /* Translators: the first %s is the program name, the */
180 /* second one is the URI of the file. */
181 g_printerr ("%s: %s, error writing to stdout",
182 g_get_prgname (), g_file_get_uri (file));
191 g_printerr ("%s: %s: error reading: %s\n",
192 g_get_prgname (), g_file_get_uri (file),
194 g_error_free (error);
204 close_res = g_input_stream_close (in, NULL, &error);
207 g_printerr ("%s: %s:error closing: %s\n",
208 g_get_prgname (), g_file_get_uri (file), error->message);
209 g_error_free (error);
212 if (cconv != NULL && fallback)
214 guint num = g_charset_converter_get_num_fallbacks (cconv);
216 g_printerr ("Number of fallback errors: %u\n", num);
221 main (int argc, char *argv[])
223 GError *error = NULL;
224 GOptionContext *context = NULL;
229 g_option_context_new ("LOCATION... - concatenate LOCATIONS "
230 "to standard output.");
232 g_option_context_set_summary (context, "filter files");
234 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
235 g_option_context_parse (context, &argc, &argv, &error);
237 g_option_context_free (context);
241 g_printerr ("Error parsing commandline options: %s\n", error->message);
243 g_printerr ("Try \"%s --help\" for more information.",
252 g_printerr ("%s: missing locations", g_get_prgname ());
254 g_printerr ("Try \"%s --help\" for more information.",
264 file = g_file_new_for_commandline_arg (locations[i]);
266 g_object_unref (file);
268 while (locations[++i] != NULL);