1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
36 #define STDOUT_FILENO 1
40 static gchar **locations = NULL;
41 static char *from_charset = NULL;
42 static char *to_charset = NULL;
43 static gboolean decompress = FALSE;
44 static gboolean compress = FALSE;
45 static gboolean gzip = FALSE;
46 static gboolean fallback = FALSE;
48 static GOptionEntry entries[] = {
49 {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
50 {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
51 {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
52 {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
53 {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
54 {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
55 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
60 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
65 const gchar *filename;
67 file_info = g_zlib_decompressor_get_file_info (decompressor);
68 if (file_info == NULL)
71 filename = g_file_info_get_name (file_info);
73 g_printerr ("Decompressor filename: %s\n", filename);
80 char buffer[1024 * 8 + 1];
86 GCharsetConverter *cconv = NULL;
89 in = (GInputStream *) g_file_read (file, NULL, &error);
92 /* Translators: the first %s is the program name, the second one */
93 /* is the URI of the file, the third is the error message. */
94 g_printerr ("%s: %s: error opening file: %s\n",
95 g_get_prgname (), g_file_get_uri (file), error->message);
103 conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
105 in = (GInputStream *) g_converter_input_stream_new (in, conv);
106 g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
107 g_object_unref (conv);
108 g_object_unref (old);
111 if (from_charset && to_charset)
113 cconv = g_charset_converter_new (to_charset, from_charset, &error);
114 conv = (GConverter *)cconv;
119 g_charset_converter_set_use_fallback (cconv, fallback);
122 in = (GInputStream *) g_converter_input_stream_new (in, conv);
123 g_object_unref (conv);
124 g_object_unref (old);
128 g_printerr ("%s: Can't convert between charsets: %s\n",
129 g_get_prgname (), error->message);
136 GFileInfo *in_file_info;
138 in_file_info = g_file_query_info (file,
139 G_FILE_ATTRIBUTE_STANDARD_NAME ","
140 G_FILE_ATTRIBUTE_TIME_MODIFIED,
141 G_FILE_QUERY_INFO_NONE,
144 if (in_file_info == NULL)
146 g_printerr ("%s: %s: error reading file info: %s\n",
147 g_get_prgname (), g_file_get_uri (file), error->message);
148 g_error_free (error);
152 conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
153 g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
155 in = (GInputStream *) g_converter_input_stream_new (in, conv);
156 g_object_unref (conv);
157 g_object_unref (old);
158 g_object_unref (in_file_info);
164 g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
172 written = write (STDOUT_FILENO, p, res);
174 if (written == -1 && errno != EINTR)
176 /* Translators: the first %s is the program name, the */
177 /* second one is the URI of the file. */
178 g_printerr ("%s: %s, error writing to stdout",
179 g_get_prgname (), g_file_get_uri (file));
188 g_printerr ("%s: %s: error reading: %s\n",
189 g_get_prgname (), g_file_get_uri (file),
191 g_error_free (error);
201 close_res = g_input_stream_close (in, NULL, &error);
204 g_printerr ("%s: %s:error closing: %s\n",
205 g_get_prgname (), g_file_get_uri (file), error->message);
206 g_error_free (error);
209 if (cconv != NULL && fallback)
211 guint num = g_charset_converter_get_num_fallbacks (cconv);
213 g_printerr ("Number of fallback errors: %u\n", num);
218 main (int argc, char *argv[])
220 GError *error = NULL;
221 GOptionContext *context = NULL;
226 g_option_context_new ("LOCATION... - concatenate LOCATIONS "
227 "to standard output.");
229 g_option_context_set_summary (context, "filter files");
231 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
232 g_option_context_parse (context, &argc, &argv, &error);
234 g_option_context_free (context);
238 g_printerr ("Error parsing commandline options: %s\n", error->message);
240 g_printerr ("Try \"%s --help\" for more information.",
249 g_printerr ("%s: missing locations", g_get_prgname ());
251 g_printerr ("Try \"%s --help\" for more information.",
261 file = g_file_new_for_commandline_arg (locations[i]);
263 g_object_unref (file);
265 while (locations[++i] != NULL);