Remove developer script not needed in git repository
[glib.git] / gio / tests / filter-cat.c
blob2cd02b7a2c3f0428444cc988827e001c139c201b
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.1 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>
21 #include <config.h>
23 #include <stdio.h>
24 #include <locale.h>
25 #include <errno.h>
27 #include <glib.h>
28 #include <gio/gio.h>
30 #ifdef G_OS_UNIX
31 #include <unistd.h>
32 #endif
34 #ifdef G_OS_WIN32
35 #include <io.h>
36 #ifndef STDOUT_FILENO
37 #define STDOUT_FILENO 1
38 #endif
39 #endif
41 static gchar **locations = NULL;
42 static char *from_charset = NULL;
43 static char *to_charset = NULL;
44 static gboolean decompress = FALSE;
45 static gboolean compress = FALSE;
46 static gboolean gzip = FALSE;
47 static gboolean fallback = FALSE;
49 static GOptionEntry entries[] = {
50 {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
51 {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
52 {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
53 {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
54 {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
55 {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
56 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
57 {NULL}
60 static void
61 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
62 GParamSpec *pspec,
63 gpointer data)
65 GFileInfo *file_info;
66 const gchar *filename;
68 file_info = g_zlib_decompressor_get_file_info (decompressor);
69 if (file_info == NULL)
70 return;
72 filename = g_file_info_get_name (file_info);
73 if (filename)
74 g_printerr ("Decompressor filename: %s\n", filename);
77 static void
78 cat (GFile * file)
80 GInputStream *in;
81 char buffer[1024 * 8 + 1];
82 char *p;
83 gssize res;
84 gboolean close_res;
85 GError *error;
86 GConverter *conv;
87 GCharsetConverter *cconv = NULL;
89 error = NULL;
90 in = (GInputStream *) g_file_read (file, NULL, &error);
91 if (in == NULL)
93 /* Translators: the first %s is the program name, the second one */
94 /* is the URI of the file, the third is the error message. */
95 g_printerr ("%s: %s: error opening file: %s\n",
96 g_get_prgname (), g_file_get_uri (file), error->message);
97 g_error_free (error);
98 return;
101 if (decompress)
103 GInputStream *old;
104 conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
105 old = in;
106 in = (GInputStream *) g_converter_input_stream_new (in, conv);
107 g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
108 g_object_unref (conv);
109 g_object_unref (old);
112 if (from_charset && to_charset)
114 cconv = g_charset_converter_new (to_charset, from_charset, &error);
115 conv = (GConverter *)cconv;
116 if (conv)
118 GInputStream *old;
120 g_charset_converter_set_use_fallback (cconv, fallback);
122 old = in;
123 in = (GInputStream *) g_converter_input_stream_new (in, conv);
124 g_object_unref (conv);
125 g_object_unref (old);
127 else
129 g_printerr ("%s: Can't convert between charsets: %s\n",
130 g_get_prgname (), error->message);
134 if (compress)
136 GInputStream *old;
137 GFileInfo *in_file_info;
139 in_file_info = g_file_query_info (file,
140 G_FILE_ATTRIBUTE_STANDARD_NAME ","
141 G_FILE_ATTRIBUTE_TIME_MODIFIED,
142 G_FILE_QUERY_INFO_NONE,
143 NULL,
144 &error);
145 if (in_file_info == NULL)
147 g_printerr ("%s: %s: error reading file info: %s\n",
148 g_get_prgname (), g_file_get_uri (file), error->message);
149 g_error_free (error);
150 return;
153 conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
154 g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
155 old = in;
156 in = (GInputStream *) g_converter_input_stream_new (in, conv);
157 g_object_unref (conv);
158 g_object_unref (old);
159 g_object_unref (in_file_info);
162 while (1)
164 res =
165 g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
166 if (res > 0)
168 gssize written;
170 p = buffer;
171 while (res > 0)
173 written = write (STDOUT_FILENO, p, res);
175 if (written == -1 && errno != EINTR)
177 /* Translators: the first %s is the program name, the */
178 /* second one is the URI of the file. */
179 g_printerr ("%s: %s, error writing to stdout",
180 g_get_prgname (), g_file_get_uri (file));
181 goto out;
183 res -= written;
184 p += written;
187 else if (res < 0)
189 g_printerr ("%s: %s: error reading: %s\n",
190 g_get_prgname (), g_file_get_uri (file),
191 error->message);
192 g_error_free (error);
193 error = NULL;
194 break;
196 else if (res == 0)
197 break;
200 out:
202 close_res = g_input_stream_close (in, NULL, &error);
203 if (!close_res)
205 g_printerr ("%s: %s:error closing: %s\n",
206 g_get_prgname (), g_file_get_uri (file), error->message);
207 g_error_free (error);
210 if (cconv != NULL && fallback)
212 guint num = g_charset_converter_get_num_fallbacks (cconv);
213 if (num > 0)
214 g_printerr ("Number of fallback errors: %u\n", num);
219 main (int argc, char *argv[])
221 GError *error = NULL;
222 GOptionContext *context = NULL;
223 GFile *file;
224 int i;
226 context =
227 g_option_context_new ("LOCATION... - concatenate LOCATIONS "
228 "to standard output.");
230 g_option_context_set_summary (context, "filter files");
232 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
233 g_option_context_parse (context, &argc, &argv, &error);
235 g_option_context_free (context);
237 if (error != NULL)
239 g_printerr ("Error parsing commandline options: %s\n", error->message);
240 g_printerr ("\n");
241 g_printerr ("Try \"%s --help\" for more information.",
242 g_get_prgname ());
243 g_printerr ("\n");
244 g_error_free(error);
245 return 1;
248 if (!locations)
250 g_printerr ("%s: missing locations", g_get_prgname ());
251 g_printerr ("\n");
252 g_printerr ("Try \"%s --help\" for more information.",
253 g_get_prgname ());
254 g_printerr ("\n");
255 return 1;
258 i = 0;
262 file = g_file_new_for_commandline_arg (locations[i]);
263 cat (file);
264 g_object_unref (file);
266 while (locations[++i] != NULL);
268 return 0;