Add GZIP header processing to GZlibCompressor/GZlibDecompressor
[platform/upstream/glib.git] / gio / tests / filter-cat.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <locale.h>
28 #include <errno.h>
29
30 #include <glib.h>
31 #include <gio/gio.h>
32
33 static gchar **locations = NULL;
34 static char *from_charset = NULL;
35 static char *to_charset = NULL;
36 static gboolean decompress = FALSE;
37 static gboolean compress = FALSE;
38 static gboolean gzip = FALSE;
39 static gboolean fallback = FALSE;
40
41 static GOptionEntry entries[] = {
42   {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
43   {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
44   {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
45   {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
46   {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
47   {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
48   {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
49   {NULL}
50 };
51
52 static void
53 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
54                                   GParamSpec *pspec,
55                                   gpointer data)
56 {
57   GFileInfo *file_info;
58   const gchar *filename;
59
60   file_info = g_zlib_decompressor_get_file_info (decompressor);
61   if (file_info == NULL)
62     return;
63
64   filename = g_file_info_get_name (file_info);
65   if (filename)
66     g_printerr ("Decompressor filename: %s\n", filename);
67 }
68
69 static void
70 cat (GFile * file)
71 {
72   GInputStream *in;
73   char buffer[1024 * 8 + 1];
74   char *p;
75   gssize res;
76   gboolean close_res;
77   GError *error;
78   GConverter *conv;
79   GCharsetConverter *cconv = NULL;
80
81   error = NULL;
82   in = (GInputStream *) g_file_read (file, NULL, &error);
83   if (in == NULL)
84     {
85       /* Translators: the first %s is the program name, the second one  */
86       /* is the URI of the file, the third is the error message.        */
87       g_printerr ("%s: %s: error opening file: %s\n",
88                   g_get_prgname (), g_file_get_uri (file), error->message);
89       g_error_free (error);
90       return;
91     }
92
93   if (decompress)
94     {
95       GInputStream *old;
96       conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
97       old = in;
98       in = (GInputStream *) g_converter_input_stream_new (in, conv);
99       g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
100       g_object_unref (conv);
101       g_object_unref (old);
102     }
103
104   if (from_charset && to_charset)
105     {
106       cconv = g_charset_converter_new (to_charset, from_charset, &error);
107       conv = (GConverter *)cconv;
108       if (conv)
109         {
110           GInputStream *old;
111
112           g_charset_converter_set_use_fallback (cconv, fallback);
113
114           old = in;
115           in = (GInputStream *) g_converter_input_stream_new (in, conv);
116           g_object_unref (conv);
117           g_object_unref (old);
118         }
119       else
120         {
121           g_printerr ("%s: Can't convert between charsets: %s\n",
122                       g_get_prgname (), error->message);
123         }
124     }
125
126   if (compress)
127     {
128       GInputStream *old;
129       GFileInfo *in_file_info;
130
131       in_file_info = g_file_query_info (file,
132                                         G_FILE_ATTRIBUTE_STANDARD_NAME ","
133                                         G_FILE_ATTRIBUTE_TIME_MODIFIED,
134                                         G_FILE_QUERY_INFO_NONE,
135                                         NULL,
136                                         &error);
137       if (in_file_info == NULL)
138         {
139           g_printerr ("%s: %s: error reading file info: %s\n",
140                       g_get_prgname (), g_file_get_uri (file), error->message);
141           g_error_free (error);
142           return;
143         }
144
145       conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
146       g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
147       old = in;
148       in = (GInputStream *) g_converter_input_stream_new (in, conv);
149       g_object_unref (conv);
150       g_object_unref (old);
151       g_object_unref (in_file_info);
152     }
153
154   while (1)
155     {
156       res =
157         g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
158       if (res > 0)
159         {
160           ssize_t written;
161
162           p = buffer;
163           while (res > 0)
164             {
165               written = write (STDOUT_FILENO, p, res);
166
167               if (written == -1 && errno != EINTR)
168                 {
169                   /* Translators: the first %s is the program name, the */
170                   /* second one is the URI of the file.                 */
171                   g_printerr ("%s: %s, error writing to stdout",
172                               g_get_prgname (), g_file_get_uri (file));
173                   goto out;
174                 }
175               res -= written;
176               p += written;
177             }
178         }
179       else if (res < 0)
180         {
181           g_printerr ("%s: %s: error reading: %s\n",
182                       g_get_prgname (), g_file_get_uri (file),
183                       error->message);
184           g_error_free (error);
185           error = NULL;
186           break;
187         }
188       else if (res == 0)
189         break;
190     }
191
192  out:
193
194   close_res = g_input_stream_close (in, NULL, &error);
195   if (!close_res)
196     {
197       g_printerr ("%s: %s:error closing: %s\n",
198                   g_get_prgname (), g_file_get_uri (file), error->message);
199       g_error_free (error);
200     }
201
202   if (cconv != NULL && fallback)
203     {
204       guint num = g_charset_converter_get_num_fallbacks (cconv);
205       if (num > 0)
206         g_printerr ("Number of fallback errors: %u\n", num);
207     }
208 }
209
210 int
211 main (int argc, char *argv[])
212 {
213   GError *error = NULL;
214   GOptionContext *context = NULL;
215   GFile *file;
216   int i;
217
218   g_type_init ();
219
220   context =
221     g_option_context_new ("LOCATION... - concatenate LOCATIONS "
222                           "to standard output.");
223
224   g_option_context_set_summary (context, "filter files");
225
226   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
227   g_option_context_parse (context, &argc, &argv, &error);
228
229   g_option_context_free (context);
230
231   if (error != NULL)
232     {
233       g_printerr ("Error parsing commandline options: %s\n", error->message);
234       g_printerr ("\n");
235       g_printerr ("Try \"%s --help\" for more information.",
236                   g_get_prgname ());
237       g_printerr ("\n");
238       g_error_free(error);
239       return 1;
240     }
241
242   if (!locations)
243     {
244       g_printerr ("%s: missing locations", g_get_prgname ());
245       g_printerr ("\n");
246       g_printerr ("Try \"%s --help\" for more information.",
247                   g_get_prgname ());
248       g_printerr ("\n");
249       return 1;
250     }
251
252   i = 0;
253
254   do
255     {
256       file = g_file_new_for_commandline_arg (locations[i]);
257       cat (file);
258       g_object_unref (file);
259     }
260   while (locations[++i] != NULL);
261
262   return 0;
263 }