Blame gio/tests/filter-cat.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2009 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <config.h>
Packit ae235b
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <locale.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>
Packit ae235b
#ifndef STDOUT_FILENO
Packit ae235b
#define STDOUT_FILENO 1
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gchar **locations = NULL;
Packit ae235b
static char *from_charset = NULL;
Packit ae235b
static char *to_charset = NULL;
Packit ae235b
static gboolean decompress = FALSE;
Packit ae235b
static gboolean compress = FALSE;
Packit ae235b
static gboolean gzip = FALSE;
Packit ae235b
static gboolean fallback = FALSE;
Packit ae235b
Packit ae235b
static GOptionEntry entries[] = {
Packit ae235b
  {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
Packit ae235b
  {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
Packit ae235b
  {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
Packit ae235b
  {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
Packit ae235b
  {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
Packit ae235b
  {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
Packit ae235b
  {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
Packit ae235b
  {NULL}
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
Packit ae235b
                                  GParamSpec *pspec,
Packit ae235b
                                  gpointer data)
Packit ae235b
{
Packit ae235b
  GFileInfo *file_info;
Packit ae235b
  const gchar *filename;
Packit ae235b
Packit ae235b
  file_info = g_zlib_decompressor_get_file_info (decompressor);
Packit ae235b
  if (file_info == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  filename = g_file_info_get_name (file_info);
Packit ae235b
  if (filename)
Packit ae235b
    g_printerr ("Decompressor filename: %s\n", filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
cat (GFile * file)
Packit ae235b
{
Packit ae235b
  GInputStream *in;
Packit ae235b
  char buffer[1024 * 8 + 1];
Packit ae235b
  char *p;
Packit ae235b
  gssize res;
Packit ae235b
  gboolean close_res;
Packit ae235b
  GError *error;
Packit ae235b
  GConverter *conv;
Packit ae235b
  GCharsetConverter *cconv = NULL;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  in = (GInputStream *) g_file_read (file, NULL, &error);
Packit ae235b
  if (in == NULL)
Packit ae235b
    {
Packit ae235b
      /* Translators: the first %s is the program name, the second one  */
Packit ae235b
      /* is the URI of the file, the third is the error message.        */
Packit ae235b
      g_printerr ("%s: %s: error opening file: %s\n",
Packit ae235b
		  g_get_prgname (), g_file_get_uri (file), error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (decompress)
Packit ae235b
    {
Packit ae235b
      GInputStream *old;
Packit ae235b
      conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
Packit ae235b
      old = in;
Packit ae235b
      in = (GInputStream *) g_converter_input_stream_new (in, conv);
Packit ae235b
      g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
Packit ae235b
      g_object_unref (conv);
Packit ae235b
      g_object_unref (old);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (from_charset && to_charset)
Packit ae235b
    {
Packit ae235b
      cconv = g_charset_converter_new (to_charset, from_charset, &error);
Packit ae235b
      conv = (GConverter *)cconv;
Packit ae235b
      if (conv)
Packit ae235b
	{
Packit ae235b
	  GInputStream *old;
Packit ae235b
Packit ae235b
	  g_charset_converter_set_use_fallback (cconv, fallback);
Packit ae235b
Packit ae235b
	  old = in;
Packit ae235b
	  in = (GInputStream *) g_converter_input_stream_new (in, conv);
Packit ae235b
	  g_object_unref (conv);
Packit ae235b
	  g_object_unref (old);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  g_printerr ("%s: Can't convert between charsets: %s\n",
Packit ae235b
		      g_get_prgname (), error->message);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (compress)
Packit ae235b
    {
Packit ae235b
      GInputStream *old;
Packit ae235b
      GFileInfo *in_file_info;
Packit ae235b
Packit ae235b
      in_file_info = g_file_query_info (file,
Packit ae235b
                                        G_FILE_ATTRIBUTE_STANDARD_NAME ","
Packit ae235b
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED,
Packit ae235b
                                        G_FILE_QUERY_INFO_NONE,
Packit ae235b
                                        NULL,
Packit ae235b
                                        &error);
Packit ae235b
      if (in_file_info == NULL)
Packit ae235b
        {
Packit ae235b
          g_printerr ("%s: %s: error reading file info: %s\n",
Packit ae235b
                      g_get_prgname (), g_file_get_uri (file), error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
Packit ae235b
      g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
Packit ae235b
      old = in;
Packit ae235b
      in = (GInputStream *) g_converter_input_stream_new (in, conv);
Packit ae235b
      g_object_unref (conv);
Packit ae235b
      g_object_unref (old);
Packit ae235b
      g_object_unref (in_file_info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      res =
Packit ae235b
	g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
Packit ae235b
      if (res > 0)
Packit ae235b
	{
Packit ae235b
	  gssize written;
Packit ae235b
Packit ae235b
	  p = buffer;
Packit ae235b
	  while (res > 0)
Packit ae235b
	    {
Packit ae235b
	      written = write (STDOUT_FILENO, p, res);
Packit ae235b
Packit ae235b
	      if (written == -1 && errno != EINTR)
Packit ae235b
		{
Packit ae235b
		  /* Translators: the first %s is the program name, the */
Packit ae235b
		  /* second one is the URI of the file.                 */
Packit ae235b
		  g_printerr ("%s: %s, error writing to stdout",
Packit ae235b
			      g_get_prgname (), g_file_get_uri (file));
Packit ae235b
		  goto out;
Packit ae235b
		}
Packit ae235b
	      res -= written;
Packit ae235b
	      p += written;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else if (res < 0)
Packit ae235b
	{
Packit ae235b
	  g_printerr ("%s: %s: error reading: %s\n",
Packit ae235b
		      g_get_prgname (), g_file_get_uri (file),
Packit ae235b
		      error->message);
Packit ae235b
	  g_error_free (error);
Packit ae235b
	  error = NULL;
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      else if (res == 0)
Packit ae235b
	break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
Packit ae235b
  close_res = g_input_stream_close (in, NULL, &error);
Packit ae235b
  if (!close_res)
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s: %s:error closing: %s\n",
Packit ae235b
		  g_get_prgname (), g_file_get_uri (file), error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (cconv != NULL && fallback)
Packit ae235b
    {
Packit ae235b
      guint num = g_charset_converter_get_num_fallbacks (cconv);
Packit ae235b
      if (num > 0)
Packit ae235b
	g_printerr ("Number of fallback errors: %u\n", num);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GOptionContext *context = NULL;
Packit ae235b
  GFile *file;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  context =
Packit ae235b
    g_option_context_new ("LOCATION... - concatenate LOCATIONS "
Packit ae235b
			  "to standard output.");
Packit ae235b
Packit ae235b
  g_option_context_set_summary (context, "filter files");
Packit ae235b
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
  g_option_context_parse (context, &argc, &argv, &error);
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  if (error != NULL)
Packit ae235b
    {
Packit ae235b
      g_printerr ("Error parsing commandline options: %s\n", error->message);
Packit ae235b
      g_printerr ("\n");
Packit ae235b
      g_printerr ("Try \"%s --help\" for more information.",
Packit ae235b
		  g_get_prgname ());
Packit ae235b
      g_printerr ("\n");
Packit ae235b
      g_error_free(error);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!locations)
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s: missing locations", g_get_prgname ());
Packit ae235b
      g_printerr ("\n");
Packit ae235b
      g_printerr ("Try \"%s --help\" for more information.",
Packit ae235b
		  g_get_prgname ());
Packit ae235b
      g_printerr ("\n");
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  i = 0;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      file = g_file_new_for_commandline_arg (locations[i]);
Packit ae235b
      cat (file);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
  while (locations[++i] != NULL);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}