glib/tests/unicode-collate.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
       
     2 #undef G_DISABLE_ASSERT
       
     3 #undef G_LOG_DOMAIN
       
     4 
       
     5 #include <glib.h>
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <string.h>
       
     9 #include <locale.h>
       
    10 #ifdef __SYMBIAN32__
       
    11 #include "mrt2_glib2_test.h"
       
    12 #endif /*__SYMBIAN32__*/
       
    13 
       
    14 typedef struct {
       
    15   const char *key;
       
    16   const char *str;
       
    17 } Line;
       
    18   
       
    19 
       
    20 int 
       
    21 compare_collate (const void *a, const void *b)
       
    22 {
       
    23   const Line *line_a = a;
       
    24   const Line *line_b = b;
       
    25 
       
    26   return g_utf8_collate (line_a->str, line_b->str);
       
    27 }
       
    28 
       
    29 int 
       
    30 compare_key (const void *a, const void *b)
       
    31 {
       
    32   const Line *line_a = a;
       
    33   const Line *line_b = b;
       
    34 
       
    35   return strcmp (line_a->key, line_b->key);
       
    36 }
       
    37 
       
    38 int main (int argc, char **argv)
       
    39 {
       
    40   GIOChannel *in;
       
    41   GError *error = NULL;
       
    42   GArray *line_array = g_array_new (FALSE, FALSE, sizeof(Line));
       
    43   guint i;
       
    44   gboolean do_key = FALSE;
       
    45   gboolean do_file = FALSE;
       
    46   gchar *locale;
       
    47   #ifdef __SYMBIAN32__
       
    48   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);
       
    49   g_set_print_handler(mrtPrintHandler);
       
    50   #endif /*__SYMBIAN32__*/
       
    51 
       
    52   /* FIXME: need to modify environment here,
       
    53    * since g_utf8_collate_key calls setlocal (LC_COLLATE, "")
       
    54    */
       
    55   g_setenv ("LC_ALL", "en_US.UTF-8", TRUE);
       
    56   locale = setlocale (LC_ALL, "en_US.UTF-8");
       
    57   if (locale == NULL || strcmp (locale, "en_US.UTF-8") != 0)
       
    58     {
       
    59       fprintf (stderr, "No suitable locale, skipping test\n"); 
       
    60       return 2;
       
    61     }
       
    62 
       
    63   if (argc != 1 && argc != 2 && argc != 3)
       
    64     {
       
    65       fprintf (stderr, "Usage: unicode-collate [--key|--file] [FILE]\n");
       
    66       return 1;
       
    67     }
       
    68 
       
    69   i = 1;
       
    70   if (argc > 1)
       
    71     {
       
    72       if (strcmp (argv[1], "-key") == 0)
       
    73         {
       
    74           do_key = TRUE;
       
    75 	  i = 2;
       
    76         }
       
    77       else if (strcmp (argv[1], "-file") == 0)
       
    78         {
       
    79           do_key = TRUE;
       
    80           do_file = TRUE;
       
    81 	  i = 2;
       
    82         }
       
    83     }
       
    84 
       
    85  if (argc > i)
       
    86     {
       
    87       in = g_io_channel_new_file (argv[i], "r", &error);
       
    88       if (!in)
       
    89 	{
       
    90 	  fprintf (stderr, "Cannot open %s: %s\n", argv[i], error->message);
       
    91   	g_assert(FALSE && "unicode-collate failed");
       
    92   	#ifdef __SYMBIAN32__
       
    93   	testResultXml("unicode-collate");
       
    94   	#endif /* EMULATOR */	
       
    95 	  return 1;
       
    96 	}
       
    97     }
       
    98   else
       
    99     {
       
   100       in = g_io_channel_unix_new (fileno (stdin));
       
   101     }
       
   102 
       
   103   while (TRUE)
       
   104     {
       
   105       gsize term_pos;
       
   106       gchar *str;
       
   107       Line line;
       
   108 
       
   109       if (g_io_channel_read_line (in, &str, NULL, &term_pos, &error) != G_IO_STATUS_NORMAL)
       
   110 	break;
       
   111 
       
   112       str[term_pos] = '\0';
       
   113 
       
   114       if (do_file)
       
   115 	line.key = g_utf8_collate_key_for_filename (str, -1);
       
   116       else
       
   117 	line.key = g_utf8_collate_key (str, -1);
       
   118       line.str = str;
       
   119 
       
   120       g_array_append_val (line_array, line);
       
   121     }
       
   122 
       
   123   if (error)
       
   124     {
       
   125       fprintf (stderr, "Error reading test file, %s\n", error->message);
       
   126       g_assert(FALSE && "unicode-collate failed");
       
   127       #ifdef __SYMBIAN32__
       
   128   	  testResultXml("unicode-collate");
       
   129   	  #endif /* EMULATOR */
       
   130       return 1;
       
   131     }
       
   132 
       
   133   qsort (line_array->data, line_array->len, sizeof (Line), do_key ? compare_key : compare_collate);
       
   134   for (i = 0; i < line_array->len; i++)
       
   135     printf ("%s\n", g_array_index (line_array, Line, i).str);
       
   136 
       
   137   g_io_channel_unref (in);
       
   138   
       
   139   #ifdef __SYMBIAN32__
       
   140   testResultXml("unicode-collate");
       
   141   #endif /* EMULATOR */
       
   142   return 0;
       
   143 }