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