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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
37 #define STDOUT_FILENO 1
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
},
61 decompressor_file_info_notify_cb (GZlibDecompressor
*decompressor
,
66 const gchar
*filename
;
68 file_info
= g_zlib_decompressor_get_file_info (decompressor
);
69 if (file_info
== NULL
)
72 filename
= g_file_info_get_name (file_info
);
74 g_printerr ("Decompressor filename: %s\n", filename
);
81 char buffer
[1024 * 8 + 1];
87 GCharsetConverter
*cconv
= NULL
;
90 in
= (GInputStream
*) g_file_read (file
, NULL
, &error
);
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
);
104 conv
= (GConverter
*)g_zlib_decompressor_new (gzip
?G_ZLIB_COMPRESSOR_FORMAT_GZIP
:G_ZLIB_COMPRESSOR_FORMAT_ZLIB
);
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
;
120 g_charset_converter_set_use_fallback (cconv
, fallback
);
123 in
= (GInputStream
*) g_converter_input_stream_new (in
, conv
);
124 g_object_unref (conv
);
125 g_object_unref (old
);
129 g_printerr ("%s: Can't convert between charsets: %s\n",
130 g_get_prgname (), error
->message
);
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
,
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
);
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
);
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
);
165 g_input_stream_read (in
, buffer
, sizeof (buffer
) - 1, NULL
, &error
);
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
));
189 g_printerr ("%s: %s: error reading: %s\n",
190 g_get_prgname (), g_file_get_uri (file
),
192 g_error_free (error
);
202 close_res
= g_input_stream_close (in
, NULL
, &error
);
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
);
214 g_printerr ("Number of fallback errors: %u\n", num
);
219 main (int argc
, char *argv
[])
221 GError
*error
= NULL
;
222 GOptionContext
*context
= NULL
;
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
);
239 g_printerr ("Error parsing commandline options: %s\n", error
->message
);
241 g_printerr ("Try \"%s --help\" for more information.",
250 g_printerr ("%s: missing locations", g_get_prgname ());
252 g_printerr ("Try \"%s --help\" for more information.",
262 file
= g_file_new_for_commandline_arg (locations
[i
]);
264 g_object_unref (file
);
266 while (locations
[++i
] != NULL
);