Blame tests/unicode-collate.c

Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <locale.h>
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  const char *key;
Packit ae235b
  const char *str;
Packit ae235b
} Line;
Packit ae235b
  
Packit ae235b
Packit ae235b
static int 
Packit ae235b
compare_collate (const void *a, const void *b)
Packit ae235b
{
Packit ae235b
  const Line *line_a = a;
Packit ae235b
  const Line *line_b = b;
Packit ae235b
Packit ae235b
  return g_utf8_collate (line_a->str, line_b->str);
Packit ae235b
}
Packit ae235b
Packit ae235b
static int 
Packit ae235b
compare_key (const void *a, const void *b)
Packit ae235b
{
Packit ae235b
  const Line *line_a = a;
Packit ae235b
  const Line *line_b = b;
Packit ae235b
Packit ae235b
  return strcmp (line_a->key, line_b->key);
Packit ae235b
}
Packit ae235b
Packit ae235b
int main (int argc, char **argv)
Packit ae235b
{
Packit ae235b
  GIOChannel *in;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GArray *line_array = g_array_new (FALSE, FALSE, sizeof(Line));
Packit ae235b
  guint i;
Packit ae235b
  gboolean do_key = FALSE;
Packit ae235b
  gboolean do_file = FALSE;
Packit ae235b
  gchar *locale;
Packit ae235b
Packit ae235b
  /* FIXME: need to modify environment here,
Packit ae235b
   * since g_utf8_collate_key calls setlocal (LC_COLLATE, "")
Packit ae235b
   */
Packit ae235b
  g_setenv ("LC_ALL", "en_US", TRUE);
Packit ae235b
  locale = setlocale (LC_ALL, "");
Packit ae235b
  if (locale == NULL || strcmp (locale, "en_US") != 0)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "No suitable locale, skipping test\n"); 
Packit ae235b
      return 2;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argc != 1 && argc != 2 && argc != 3)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Usage: unicode-collate [--key|--file] [FILE]\n");
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  i = 1;
Packit ae235b
  if (argc > 1)
Packit ae235b
    {
Packit ae235b
      if (strcmp (argv[1], "--key") == 0)
Packit ae235b
        {
Packit ae235b
          do_key = TRUE;
Packit ae235b
	  i = 2;
Packit ae235b
        }
Packit ae235b
      else if (strcmp (argv[1], "--file") == 0)
Packit ae235b
        {
Packit ae235b
          do_key = TRUE;
Packit ae235b
          do_file = TRUE;
Packit ae235b
	  i = 2;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
 if (argc > i)
Packit ae235b
    {
Packit ae235b
      in = g_io_channel_new_file (argv[i], "r", &error);
Packit ae235b
      if (!in)
Packit ae235b
	{
Packit ae235b
	  fprintf (stderr, "Cannot open %s: %s\n", argv[i], error->message);
Packit ae235b
	  return 1;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      in = g_io_channel_unix_new (fileno (stdin));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      gsize term_pos;
Packit ae235b
      gchar *str;
Packit ae235b
      Line line;
Packit ae235b
Packit ae235b
      if (g_io_channel_read_line (in, &str, NULL, &term_pos, &error) != G_IO_STATUS_NORMAL)
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      str[term_pos] = '\0';
Packit ae235b
Packit ae235b
      if (do_file)
Packit ae235b
	line.key = g_utf8_collate_key_for_filename (str, -1);
Packit ae235b
      else
Packit ae235b
	line.key = g_utf8_collate_key (str, -1);
Packit ae235b
      line.str = str;
Packit ae235b
Packit ae235b
      g_array_append_val (line_array, line);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (error)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error reading test file, %s\n", error->message);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  qsort (line_array->data, line_array->len, sizeof (Line), do_key ? compare_key : compare_collate);
Packit ae235b
  for (i = 0; i < line_array->len; i++)
Packit ae235b
    printf ("%s\n", g_array_index (line_array, Line, i).str);
Packit ae235b
Packit ae235b
  g_io_channel_unref (in);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}