desktopappinfo: add Exec to searchable keys
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <locale.h>
25 #include <errno.h>
26
27 #include <glib.h>
28 #include <gio/gio.h>
29
30 #ifdef G_OS_UNIX
31 #include <unistd.h>
32 #endif
33
34 #ifdef G_OS_WIN32
35 #ifndef STDOUT_FILENO
36 #define STDOUT_FILENO 1
37 #endif
38 #endif
39
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;
47
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},
56   {NULL}
57 };
58
59 static void
60 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
61                                   GParamSpec *pspec,
62                                   gpointer data)
63 {
64   GFileInfo *file_info;
65   const gchar *filename;
66
67   file_info = g_zlib_decompressor_get_file_info (decompressor);
68   if (file_info == NULL)
69     return;
70
71   filename = g_file_info_get_name (file_info);
72   if (filename)
73     g_printerr ("Decompressor filename: %s\n", filename);
74 }
75
76 static void
77 cat (GFile * file)
78 {
79   GInputStream *in;
80   char buffer[1024 * 8 + 1];
81   char *p;
82   gssize res;
83   gboolean close_res;
84   GError *error;
85   GConverter *conv;
86   GCharsetConverter *cconv = NULL;
87
88   error = NULL;
89   in = (GInputStream *) g_file_read (file, NULL, &error);
90   if (in == NULL)
91     {
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);
96       g_error_free (error);
97       return;
98     }
99
100   if (decompress)
101     {
102       GInputStream *old;
103       conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
104       old = in;
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);
109     }
110
111   if (from_charset && to_charset)
112     {
113       cconv = g_charset_converter_new (to_charset, from_charset, &error);
114       conv = (GConverter *)cconv;
115       if (conv)
116         {
117           GInputStream *old;
118
119           g_charset_converter_set_use_fallback (cconv, fallback);
120
121           old = in;
122           in = (GInputStream *) g_converter_input_stream_new (in, conv);
123           g_object_unref (conv);
124           g_object_unref (old);
125         }
126       else
127         {
128           g_printerr ("%s: Can't convert between charsets: %s\n",
129                       g_get_prgname (), error->message);
130         }
131     }
132
133   if (compress)
134     {
135       GInputStream *old;
136       GFileInfo *in_file_info;
137
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,
142                                         NULL,
143                                         &error);
144       if (in_file_info == NULL)
145         {
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);
149           return;
150         }
151
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);
154       old = in;
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);
159     }
160
161   while (1)
162     {
163       res =
164         g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
165       if (res > 0)
166         {
167           gssize written;
168
169           p = buffer;
170           while (res > 0)
171             {
172               written = write (STDOUT_FILENO, p, res);
173
174               if (written == -1 && errno != EINTR)
175                 {
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));
180                   goto out;
181                 }
182               res -= written;
183               p += written;
184             }
185         }
186       else if (res < 0)
187         {
188           g_printerr ("%s: %s: error reading: %s\n",
189                       g_get_prgname (), g_file_get_uri (file),
190                       error->message);
191           g_error_free (error);
192           error = NULL;
193           break;
194         }
195       else if (res == 0)
196         break;
197     }
198
199  out:
200
201   close_res = g_input_stream_close (in, NULL, &error);
202   if (!close_res)
203     {
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);
207     }
208
209   if (cconv != NULL && fallback)
210     {
211       guint num = g_charset_converter_get_num_fallbacks (cconv);
212       if (num > 0)
213         g_printerr ("Number of fallback errors: %u\n", num);
214     }
215 }
216
217 int
218 main (int argc, char *argv[])
219 {
220   GError *error = NULL;
221   GOptionContext *context = NULL;
222   GFile *file;
223   int i;
224
225   context =
226     g_option_context_new ("LOCATION... - concatenate LOCATIONS "
227                           "to standard output.");
228
229   g_option_context_set_summary (context, "filter files");
230
231   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
232   g_option_context_parse (context, &argc, &argv, &error);
233
234   g_option_context_free (context);
235
236   if (error != NULL)
237     {
238       g_printerr ("Error parsing commandline options: %s\n", error->message);
239       g_printerr ("\n");
240       g_printerr ("Try \"%s --help\" for more information.",
241                   g_get_prgname ());
242       g_printerr ("\n");
243       g_error_free(error);
244       return 1;
245     }
246
247   if (!locations)
248     {
249       g_printerr ("%s: missing locations", g_get_prgname ());
250       g_printerr ("\n");
251       g_printerr ("Try \"%s --help\" for more information.",
252                   g_get_prgname ());
253       g_printerr ("\n");
254       return 1;
255     }
256
257   i = 0;
258
259   do
260     {
261       file = g_file_new_for_commandline_arg (locations[i]);
262       cat (file);
263       g_object_unref (file);
264     }
265   while (locations[++i] != NULL);
266
267   return 0;
268 }