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