glib/tests/markup-test.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 <stdio.h>
       
     6 #include <glib.h>
       
     7 
       
     8 #ifdef __SYMBIAN32__
       
     9 #include "mrt2_glib2_test.h"
       
    10 #endif /*__SYMBIAN32__*/
       
    11 
       
    12 
       
    13 static int depth = 0;
       
    14 
       
    15 static void
       
    16 indent (int extra)
       
    17 {
       
    18   int i = 0;
       
    19   while (i < depth)
       
    20     {
       
    21       fputs ("  ", stdout);
       
    22       ++i;
       
    23     }
       
    24 }
       
    25 
       
    26 static void
       
    27 start_element_handler  (GMarkupParseContext *context,
       
    28                         const gchar         *element_name,
       
    29                         const gchar        **attribute_names,
       
    30                         const gchar        **attribute_values,
       
    31                         gpointer             user_data,
       
    32                         GError             **error)
       
    33 {
       
    34   int i;
       
    35   
       
    36   indent (0);
       
    37   printf ("ELEMENT '%s'\n", element_name);
       
    38 
       
    39   i = 0;
       
    40   while (attribute_names[i] != NULL)
       
    41     {
       
    42       indent (1);
       
    43 
       
    44       printf ("%s=\"%s\"\n",
       
    45               attribute_names[i],
       
    46               attribute_values[i]);
       
    47       
       
    48       ++i;
       
    49     }
       
    50   
       
    51   ++depth;
       
    52 }
       
    53 
       
    54 static void
       
    55 end_element_handler    (GMarkupParseContext *context,
       
    56                         const gchar         *element_name,
       
    57                         gpointer             user_data,
       
    58                         GError             **error)
       
    59 {
       
    60   --depth;
       
    61   indent (0);
       
    62   printf ("END '%s'\n", element_name);
       
    63   }
       
    64 
       
    65 static void
       
    66 text_handler                      (GMarkupParseContext *context,
       
    67                         const gchar         *text,
       
    68                         gsize                text_len,
       
    69                         gpointer             user_data,
       
    70                         GError             **error)
       
    71 {
       
    72   indent (0);
       
    73   printf ("TEXT '%.*s'\n", (int)text_len, text);
       
    74 }
       
    75 
       
    76 
       
    77 static void
       
    78 passthrough_handler    (GMarkupParseContext *context,
       
    79                         const gchar         *passthrough_text,
       
    80                         gsize                text_len,
       
    81                         gpointer             user_data,
       
    82                         GError             **error)
       
    83 {
       
    84   indent (0);
       
    85 
       
    86   printf ("PASS '%.*s'\n", (int)text_len, passthrough_text);
       
    87 }
       
    88 
       
    89 static void
       
    90 error_handler          (GMarkupParseContext *context,
       
    91                         GError              *error,
       
    92                         gpointer             user_data)
       
    93 {
       
    94   fprintf (stderr, " %s\n", error->message);
       
    95 }
       
    96 
       
    97 static const GMarkupParser parser = {
       
    98   start_element_handler,
       
    99   end_element_handler,
       
   100   text_handler,
       
   101   passthrough_handler,
       
   102   error_handler
       
   103 };
       
   104 
       
   105 static const GMarkupParser silent_parser = {
       
   106   NULL,
       
   107   NULL,
       
   108   NULL,
       
   109   NULL,
       
   110   error_handler
       
   111 };
       
   112 
       
   113 static int
       
   114 test_in_chunks (const gchar *contents,
       
   115                 gint         length,
       
   116                 gint         chunk_size)
       
   117 {
       
   118   GMarkupParseContext *context;
       
   119   int i = 0;
       
   120   
       
   121   context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
       
   122 
       
   123   while (i < length)
       
   124     {
       
   125       int this_chunk = MIN (length - i, chunk_size);
       
   126 
       
   127       if (!g_markup_parse_context_parse (context,
       
   128                                          contents + i,
       
   129                                          this_chunk,
       
   130                                          NULL))
       
   131         {
       
   132           g_markup_parse_context_free (context);
       
   133           return 1;
       
   134         }
       
   135 
       
   136       i += this_chunk;
       
   137     }
       
   138       
       
   139   if (!g_markup_parse_context_end_parse (context, NULL))
       
   140     {
       
   141       g_markup_parse_context_free (context);
       
   142       return 1;
       
   143     }
       
   144 
       
   145   g_markup_parse_context_free (context);
       
   146 
       
   147   return 0;
       
   148 }
       
   149 
       
   150 static int
       
   151 test_file (const gchar *filename)
       
   152 {
       
   153   gchar *contents;
       
   154   gsize  length;
       
   155   GError *error;
       
   156   GMarkupParseContext *context;
       
   157   
       
   158   error = NULL;
       
   159   if (!g_file_get_contents (filename,
       
   160                             &contents,
       
   161                             &length,
       
   162                             &error))
       
   163     {
       
   164       fprintf (stderr, "%s\n", error->message);
       
   165       g_error_free (error);
       
   166       return 1;
       
   167     }
       
   168 
       
   169   context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
       
   170 
       
   171   if (!g_markup_parse_context_parse (context, contents, length, NULL))
       
   172     {
       
   173       g_markup_parse_context_free (context);
       
   174       return 1;
       
   175     }
       
   176 
       
   177   if (!g_markup_parse_context_end_parse (context, NULL))
       
   178     {
       
   179       g_markup_parse_context_free (context);
       
   180       return 1;
       
   181     }
       
   182 
       
   183   g_markup_parse_context_free (context);
       
   184 
       
   185   /* A byte at a time */
       
   186   if (test_in_chunks (contents, length, 1) != 0)
       
   187     return 1;
       
   188 
       
   189   /* 2 bytes */
       
   190   if (test_in_chunks (contents, length, 2) != 0)
       
   191     return 1;
       
   192 
       
   193   /*5 bytes */
       
   194   if (test_in_chunks (contents, length, 5) != 0)
       
   195     return 1;
       
   196   
       
   197   /* 12 bytes */
       
   198   if (test_in_chunks (contents, length, 12) != 0)
       
   199     return 1;
       
   200   
       
   201   /* 1024 bytes */
       
   202   if (test_in_chunks (contents, length, 1024) != 0)
       
   203     return 1;
       
   204 
       
   205   return 0;
       
   206 }
       
   207 
       
   208 int
       
   209 main (int   argc,
       
   210       char *argv[])
       
   211 {
       
   212   int retval;
       
   213   
       
   214   #ifdef __SYMBIAN32__
       
   215   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);
       
   216   g_set_print_handler(mrtPrintHandler);
       
   217   #endif /*__SYMBIAN32__*/
       
   218 
       
   219   if (argc > 1)
       
   220   {
       
   221     retval = test_file (argv[1]);
       
   222 	  #ifdef __SYMBIAN32__
       
   223   testResultXml("markup-test");
       
   224   #endif /* EMULATOR */
       
   225   return retval;
       
   226 }
       
   227   else
       
   228     {
       
   229       fprintf (stderr, "Give a markup file on the command line\n");
       
   230       return 1;
       
   231     }  
       
   232 }
       
   233