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