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