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