Bump version
[platform/upstream/glib.git] / tests / unicode-collate.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <glib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 typedef struct {
10   const char *key;
11   const char *str;
12 } Line;
13   
14
15 int 
16 compare_collate (const void *a, const void *b)
17 {
18   const Line *line_a = a;
19   const Line *line_b = b;
20
21   return g_utf8_collate (line_a->str, line_b->str);
22 }
23
24 int 
25 compare_key (const void *a, const void *b)
26 {
27   const Line *line_a = a;
28   const Line *line_b = b;
29
30   return strcmp (line_a->key, line_b->key);
31 }
32
33 int main (int argc, char **argv)
34 {
35   GIOChannel *in;
36   GError *error = NULL;
37   GArray *line_array = g_array_new (FALSE, FALSE, sizeof(Line));
38   guint i;
39
40   if (argc != 1 && argc != 2)
41     {
42       fprintf (stderr, "Usage: unicode-collate [FILE]\n");
43       return 1;
44     }
45
46   if (argc == 2)
47     {
48       in = g_io_channel_new_file (argv[1], "r", &error);
49       if (!in)
50         {
51           fprintf (stderr, "Cannot open %s: %s\n", argv[1], error->message);
52           return 1;
53         }
54     }
55   else
56     {
57       in = g_io_channel_unix_new (fileno (stdin));
58     }
59
60   while (TRUE)
61     {
62       gsize term_pos;
63       gchar *str;
64       Line line;
65
66       if (g_io_channel_read_line (in, &str, NULL, &term_pos, &error) != G_IO_STATUS_NORMAL)
67         break;
68
69       str[term_pos] = '\0';
70
71       line.key = g_utf8_collate_key (str, -1);
72       line.str = str;
73
74       g_array_append_val (line_array, line);
75     }
76
77   if (error)
78     {
79       fprintf (stderr, "Error reading test file, %s\n", error->message);
80       return 1;
81     }
82
83   printf ("== g_utf8_collate ==\n");
84
85   qsort (line_array->data, line_array->len, sizeof (Line), compare_collate);
86   for (i = 0; i < line_array->len; i++)
87     printf ("%s\n", g_array_index (line_array, Line, i).str);
88
89   printf ("== g_utf8_collate_key ==\n");
90
91   qsort (line_array->data, line_array->len, sizeof (Line), compare_key);
92   for (i = 0; i < line_array->len; i++)
93     printf ("%s\n", g_array_index (line_array, Line, i).str);
94
95   g_io_channel_unref (in);
96
97   return 0;
98 }