docs: Add GIOModuleScope and GIOModuleScopeFlags
[glib.git] / gio / tests / filter-cat.c
blobff7ddd3c71753ce2ac8d9f8c82089c336f3d3928
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>
23 #include <config.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <locale.h>
28 #include <errno.h>
30 #include <glib.h>
31 #include <gio/gio.h>
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;
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}
52 static void
53 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
54 GParamSpec *pspec,
55 gpointer data)
57 GFileInfo *file_info;
58 const gchar *filename;
60 file_info = g_zlib_decompressor_get_file_info (decompressor);
61 if (file_info == NULL)
62 return;
64 filename = g_file_info_get_name (file_info);
65 if (filename)
66 g_printerr ("Decompressor filename: %s\n", filename);
69 static void
70 cat (GFile * file)
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;
81 error = NULL;
82 in = (GInputStream *) g_file_read (file, NULL, &error);
83 if (in == NULL)
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;
93 if (decompress)
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);
104 if (from_charset && to_charset)
106 cconv = g_charset_converter_new (to_charset, from_charset, &error);
107 conv = (GConverter *)cconv;
108 if (conv)
110 GInputStream *old;
112 g_charset_converter_set_use_fallback (cconv, fallback);
114 old = in;
115 in = (GInputStream *) g_converter_input_stream_new (in, conv);
116 g_object_unref (conv);
117 g_object_unref (old);
119 else
121 g_printerr ("%s: Can't convert between charsets: %s\n",
122 g_get_prgname (), error->message);
126 if (compress)
128 GInputStream *old;
129 GFileInfo *in_file_info;
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)
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;
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);
154 while (1)
156 res =
157 g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
158 if (res > 0)
160 ssize_t written;
162 p = buffer;
163 while (res > 0)
165 written = write (STDOUT_FILENO, p, res);
167 if (written == -1 && errno != EINTR)
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;
175 res -= written;
176 p += written;
179 else if (res < 0)
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;
188 else if (res == 0)
189 break;
192 out:
194 close_res = g_input_stream_close (in, NULL, &error);
195 if (!close_res)
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);
202 if (cconv != NULL && fallback)
204 guint num = g_charset_converter_get_num_fallbacks (cconv);
205 if (num > 0)
206 g_printerr ("Number of fallback errors: %u\n", num);
211 main (int argc, char *argv[])
213 GError *error = NULL;
214 GOptionContext *context = NULL;
215 GFile *file;
216 int i;
218 g_type_init ();
220 context =
221 g_option_context_new ("LOCATION... - concatenate LOCATIONS "
222 "to standard output.");
224 g_option_context_set_summary (context, "filter files");
226 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
227 g_option_context_parse (context, &argc, &argv, &error);
229 g_option_context_free (context);
231 if (error != NULL)
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;
242 if (!locations)
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;
252 i = 0;
256 file = g_file_new_for_commandline_arg (locations[i]);
257 cat (file);
258 g_object_unref (file);
260 while (locations[++i] != NULL);
262 return 0;