sl@0: /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
sl@0: #undef G_DISABLE_ASSERT
sl@0: #undef G_LOG_DOMAIN
sl@0: 
sl@0: #include <glib.h>
sl@0: #include <stdio.h>
sl@0: #include <stdlib.h>
sl@0: #include <string.h>
sl@0: #include <locale.h>
sl@0: #ifdef __SYMBIAN32__
sl@0: #include "mrt2_glib2_test.h"
sl@0: #endif /*__SYMBIAN32__*/
sl@0: 
sl@0: typedef struct {
sl@0:   const char *key;
sl@0:   const char *str;
sl@0: } Line;
sl@0:   
sl@0: 
sl@0: int 
sl@0: compare_collate (const void *a, const void *b)
sl@0: {
sl@0:   const Line *line_a = a;
sl@0:   const Line *line_b = b;
sl@0: 
sl@0:   return g_utf8_collate (line_a->str, line_b->str);
sl@0: }
sl@0: 
sl@0: int 
sl@0: compare_key (const void *a, const void *b)
sl@0: {
sl@0:   const Line *line_a = a;
sl@0:   const Line *line_b = b;
sl@0: 
sl@0:   return strcmp (line_a->key, line_b->key);
sl@0: }
sl@0: 
sl@0: int main (int argc, char **argv)
sl@0: {
sl@0:   GIOChannel *in;
sl@0:   GError *error = NULL;
sl@0:   GArray *line_array = g_array_new (FALSE, FALSE, sizeof(Line));
sl@0:   guint i;
sl@0:   gboolean do_key = FALSE;
sl@0:   gboolean do_file = FALSE;
sl@0:   gchar *locale;
sl@0:   #ifdef __SYMBIAN32__
sl@0:   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
sl@0:   g_set_print_handler(mrtPrintHandler);
sl@0:   #endif /*__SYMBIAN32__*/
sl@0: 
sl@0:   /* FIXME: need to modify environment here,
sl@0:    * since g_utf8_collate_key calls setlocal (LC_COLLATE, "")
sl@0:    */
sl@0:   g_setenv ("LC_ALL", "en_US.UTF-8", TRUE);
sl@0:   locale = setlocale (LC_ALL, "en_US.UTF-8");
sl@0:   if (locale == NULL || strcmp (locale, "en_US.UTF-8") != 0)
sl@0:     {
sl@0:       fprintf (stderr, "No suitable locale, skipping test\n"); 
sl@0:       return 2;
sl@0:     }
sl@0: 
sl@0:   if (argc != 1 && argc != 2 && argc != 3)
sl@0:     {
sl@0:       fprintf (stderr, "Usage: unicode-collate [--key|--file] [FILE]\n");
sl@0:       return 1;
sl@0:     }
sl@0: 
sl@0:   i = 1;
sl@0:   if (argc > 1)
sl@0:     {
sl@0:       if (strcmp (argv[1], "-key") == 0)
sl@0:         {
sl@0:           do_key = TRUE;
sl@0: 	  i = 2;
sl@0:         }
sl@0:       else if (strcmp (argv[1], "-file") == 0)
sl@0:         {
sl@0:           do_key = TRUE;
sl@0:           do_file = TRUE;
sl@0: 	  i = 2;
sl@0:         }
sl@0:     }
sl@0: 
sl@0:  if (argc > i)
sl@0:     {
sl@0:       in = g_io_channel_new_file (argv[i], "r", &error);
sl@0:       if (!in)
sl@0: 	{
sl@0: 	  fprintf (stderr, "Cannot open %s: %s\n", argv[i], error->message);
sl@0:   	g_assert(FALSE && "unicode-collate failed");
sl@0:   	#ifdef __SYMBIAN32__
sl@0:   	testResultXml("unicode-collate");
sl@0:   	#endif /* EMULATOR */	
sl@0: 	  return 1;
sl@0: 	}
sl@0:     }
sl@0:   else
sl@0:     {
sl@0:       in = g_io_channel_unix_new (fileno (stdin));
sl@0:     }
sl@0: 
sl@0:   while (TRUE)
sl@0:     {
sl@0:       gsize term_pos;
sl@0:       gchar *str;
sl@0:       Line line;
sl@0: 
sl@0:       if (g_io_channel_read_line (in, &str, NULL, &term_pos, &error) != G_IO_STATUS_NORMAL)
sl@0: 	break;
sl@0: 
sl@0:       str[term_pos] = '\0';
sl@0: 
sl@0:       if (do_file)
sl@0: 	line.key = g_utf8_collate_key_for_filename (str, -1);
sl@0:       else
sl@0: 	line.key = g_utf8_collate_key (str, -1);
sl@0:       line.str = str;
sl@0: 
sl@0:       g_array_append_val (line_array, line);
sl@0:     }
sl@0: 
sl@0:   if (error)
sl@0:     {
sl@0:       fprintf (stderr, "Error reading test file, %s\n", error->message);
sl@0:       g_assert(FALSE && "unicode-collate failed");
sl@0:       #ifdef __SYMBIAN32__
sl@0:   	  testResultXml("unicode-collate");
sl@0:   	  #endif /* EMULATOR */
sl@0:       return 1;
sl@0:     }
sl@0: 
sl@0:   qsort (line_array->data, line_array->len, sizeof (Line), do_key ? compare_key : compare_collate);
sl@0:   for (i = 0; i < line_array->len; i++)
sl@0:     printf ("%s\n", g_array_index (line_array, Line, i).str);
sl@0: 
sl@0:   g_io_channel_unref (in);
sl@0:   
sl@0:   #ifdef __SYMBIAN32__
sl@0:   testResultXml("unicode-collate");
sl@0:   #endif /* EMULATOR */
sl@0:   return 0;
sl@0: }