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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
38 #define STDOUT_FILENO 1
42 static gchar **locations = NULL;
43 static char *from_charset = NULL;
44 static char *to_charset = NULL;
45 static gboolean decompress = FALSE;
46 static gboolean compress = FALSE;
47 static gboolean gzip = FALSE;
48 static gboolean fallback = FALSE;
50 static GOptionEntry entries[] = {
51 {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
52 {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
53 {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
54 {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
55 {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
56 {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
57 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
62 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
67 const gchar *filename;
69 file_info = g_zlib_decompressor_get_file_info (decompressor);
70 if (file_info == NULL)
73 filename = g_file_info_get_name (file_info);
75 g_printerr ("Decompressor filename: %s\n", filename);
82 char buffer[1024 * 8 + 1];
88 GCharsetConverter *cconv = NULL;
91 in = (GInputStream *) g_file_read (file, NULL, &error);
94 /* Translators: the first %s is the program name, the second one */
95 /* is the URI of the file, the third is the error message. */
96 g_printerr ("%s: %s: error opening file: %s\n",
97 g_get_prgname (), g_file_get_uri (file), error->message);
105 conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
107 in = (GInputStream *) g_converter_input_stream_new (in, conv);
108 g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
109 g_object_unref (conv);
110 g_object_unref (old);
113 if (from_charset && to_charset)
115 cconv = g_charset_converter_new (to_charset, from_charset, &error);
116 conv = (GConverter *)cconv;
121 g_charset_converter_set_use_fallback (cconv, fallback);
124 in = (GInputStream *) g_converter_input_stream_new (in, conv);
125 g_object_unref (conv);
126 g_object_unref (old);
130 g_printerr ("%s: Can't convert between charsets: %s\n",
131 g_get_prgname (), error->message);
138 GFileInfo *in_file_info;
140 in_file_info = g_file_query_info (file,
141 G_FILE_ATTRIBUTE_STANDARD_NAME ","
142 G_FILE_ATTRIBUTE_TIME_MODIFIED,
143 G_FILE_QUERY_INFO_NONE,
146 if (in_file_info == NULL)
148 g_printerr ("%s: %s: error reading file info: %s\n",
149 g_get_prgname (), g_file_get_uri (file), error->message);
150 g_error_free (error);
154 conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
155 g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
157 in = (GInputStream *) g_converter_input_stream_new (in, conv);
158 g_object_unref (conv);
159 g_object_unref (old);
160 g_object_unref (in_file_info);
166 g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
174 written = write (STDOUT_FILENO, p, res);
176 if (written == -1 && errno != EINTR)
178 /* Translators: the first %s is the program name, the */
179 /* second one is the URI of the file. */
180 g_printerr ("%s: %s, error writing to stdout",
181 g_get_prgname (), g_file_get_uri (file));
190 g_printerr ("%s: %s: error reading: %s\n",
191 g_get_prgname (), g_file_get_uri (file),
193 g_error_free (error);
203 close_res = g_input_stream_close (in, NULL, &error);
206 g_printerr ("%s: %s:error closing: %s\n",
207 g_get_prgname (), g_file_get_uri (file), error->message);
208 g_error_free (error);
211 if (cconv != NULL && fallback)
213 guint num = g_charset_converter_get_num_fallbacks (cconv);
215 g_printerr ("Number of fallback errors: %u\n", num);
220 main (int argc, char *argv[])
222 GError *error = NULL;
223 GOptionContext *context = NULL;
228 g_option_context_new ("LOCATION... - concatenate LOCATIONS "
229 "to standard output.");
231 g_option_context_set_summary (context, "filter files");
233 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
234 g_option_context_parse (context, &argc, &argv, &error);
236 g_option_context_free (context);
240 g_printerr ("Error parsing commandline options: %s\n", error->message);
242 g_printerr ("Try \"%s --help\" for more information.",
251 g_printerr ("%s: missing locations", g_get_prgname ());
253 g_printerr ("Try \"%s --help\" for more information.",
263 file = g_file_new_for_commandline_arg (locations[i]);
265 g_object_unref (file);
267 while (locations[++i] != NULL);