Merge remote branch 'gvdb/master'
[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 cat (GFile * file)
54 {
55   GInputStream *in;
56   char buffer[1024 * 8 + 1];
57   char *p;
58   gssize res;
59   gboolean close_res;
60   GError *error;
61   GConverter *conv;
62   GCharsetConverter *cconv = NULL;
63
64   error = NULL;
65   in = (GInputStream *) g_file_read (file, NULL, &error);
66   if (in == NULL)
67     {
68       /* Translators: the first %s is the program name, the second one  */
69       /* is the URI of the file, the third is the error message.        */
70       g_printerr ("%s: %s: error opening file: %s\n",
71                   g_get_prgname (), g_file_get_uri (file), error->message);
72       g_error_free (error);
73       return;
74     }
75
76   if (decompress)
77     {
78       GInputStream *old;
79       conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
80       old = in;
81       in = (GInputStream *) g_converter_input_stream_new (in, conv);
82       g_object_unref (conv);
83       g_object_unref (old);
84     }
85
86   if (from_charset && to_charset)
87     {
88       cconv = g_charset_converter_new (to_charset, from_charset, &error);
89       conv = (GConverter *)cconv;
90       if (conv)
91         {
92           GInputStream *old;
93
94           g_charset_converter_set_use_fallback (cconv, fallback);
95
96           old = in;
97           in = (GInputStream *) g_converter_input_stream_new (in, conv);
98           g_object_unref (conv);
99           g_object_unref (old);
100         }
101       else
102         {
103           g_printerr ("%s: Can't convert between charsets: %s\n",
104                       g_get_prgname (), error->message);
105         }
106     }
107
108   if (compress)
109     {
110       GInputStream *old;
111       conv = (GConverter *)g_zlib_compressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
112       old = in;
113       in = (GInputStream *) g_converter_input_stream_new (in, conv);
114       g_object_unref (conv);
115       g_object_unref (old);
116     }
117
118   while (1)
119     {
120       res =
121         g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
122       if (res > 0)
123         {
124           ssize_t written;
125
126           p = buffer;
127           while (res > 0)
128             {
129               written = write (STDOUT_FILENO, p, res);
130
131               if (written == -1 && errno != EINTR)
132                 {
133                   /* Translators: the first %s is the program name, the */
134                   /* second one is the URI of the file.                 */
135                   g_printerr ("%s: %s, error writing to stdout",
136                               g_get_prgname (), g_file_get_uri (file));
137                   goto out;
138                 }
139               res -= written;
140               p += written;
141             }
142         }
143       else if (res < 0)
144         {
145           g_printerr ("%s: %s: error reading: %s\n",
146                       g_get_prgname (), g_file_get_uri (file),
147                       error->message);
148           g_error_free (error);
149           error = NULL;
150           break;
151         }
152       else if (res == 0)
153         break;
154     }
155
156  out:
157
158   close_res = g_input_stream_close (in, NULL, &error);
159   if (!close_res)
160     {
161       g_printerr ("%s: %s:error closing: %s\n",
162                   g_get_prgname (), g_file_get_uri (file), error->message);
163       g_error_free (error);
164     }
165
166   if (cconv != NULL && fallback)
167     {
168       guint num = g_charset_converter_get_num_fallbacks (cconv);
169       if (num > 0)
170         g_printerr ("Number of fallback errors: %u\n", num);
171     }
172 }
173
174 int
175 main (int argc, char *argv[])
176 {
177   GError *error = NULL;
178   GOptionContext *context = NULL;
179   GFile *file;
180   int i;
181
182   g_type_init ();
183
184   context =
185     g_option_context_new ("LOCATION... - concatenate LOCATIONS "
186                           "to standard output.");
187
188   g_option_context_set_summary (context, "filter files");
189
190   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
191   g_option_context_parse (context, &argc, &argv, &error);
192
193   g_option_context_free (context);
194
195   if (error != NULL)
196     {
197       g_printerr ("Error parsing commandline options: %s\n", error->message);
198       g_printerr ("\n");
199       g_printerr ("Try \"%s --help\" for more information.",
200                   g_get_prgname ());
201       g_printerr ("\n");
202       g_error_free(error);
203       return 1;
204     }
205
206   if (!locations)
207     {
208       g_printerr ("%s: missing locations", g_get_prgname ());
209       g_printerr ("\n");
210       g_printerr ("Try \"%s --help\" for more information.",
211                   g_get_prgname ());
212       g_printerr ("\n");
213       return 1;
214     }
215
216   i = 0;
217
218   do
219     {
220       file = g_file_new_for_commandline_arg (locations[i]);
221       cat (file);
222       g_object_unref (file);
223     }
224   while (locations[++i] != NULL);
225
226   return 0;
227 }