os/ossrv/glib/tests/iochannel-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
sl@0
     3
#undef G_DISABLE_ASSERT
sl@0
     4
#undef G_LOG_DOMAIN
sl@0
     5
sl@0
     6
#include <sys/types.h>
sl@0
     7
#include <sys/stat.h>
sl@0
     8
#include <fcntl.h>
sl@0
     9
#include <glib.h>
sl@0
    10
#include <string.h>
sl@0
    11
#include <stdio.h>
sl@0
    12
#include <stdlib.h>
sl@0
    13
sl@0
    14
#ifdef __SYMBIAN32__
sl@0
    15
#include "mrt2_glib2_test.h"
sl@0
    16
#endif /*__SYMBIAN32__*/
sl@0
    17
sl@0
    18
sl@0
    19
#define BUFFER_SIZE 1024
sl@0
    20
sl@0
    21
static void
sl@0
    22
test_small_writes (void)
sl@0
    23
{
sl@0
    24
  GIOChannel *io;
sl@0
    25
  GIOStatus status;
sl@0
    26
  guint cnt; 
sl@0
    27
  gchar tmp;
sl@0
    28
  GError *error = NULL;
sl@0
    29
sl@0
    30
  io = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &error);
sl@0
    31
  if (error)
sl@0
    32
    {
sl@0
    33
      g_warning ("Unable to open file %s: %s", 
sl@0
    34
		 "iochannel-test-outfile", 
sl@0
    35
		 error->message);
sl@0
    36
      g_error_free (error);
sl@0
    37
      
sl@0
    38
      exit (1);
sl@0
    39
    }
sl@0
    40
sl@0
    41
  g_io_channel_set_encoding (io, NULL, NULL);
sl@0
    42
  g_io_channel_set_buffer_size (io, 1022);
sl@0
    43
sl@0
    44
  cnt = 2 * g_io_channel_get_buffer_size (io);
sl@0
    45
  tmp = 0;
sl@0
    46
 
sl@0
    47
  while (cnt)
sl@0
    48
    {
sl@0
    49
      status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL);
sl@0
    50
      if (status == G_IO_STATUS_ERROR)
sl@0
    51
	break;
sl@0
    52
      if (status == G_IO_STATUS_NORMAL)
sl@0
    53
	cnt--;
sl@0
    54
    }
sl@0
    55
sl@0
    56
  g_assert (status == G_IO_STATUS_NORMAL);
sl@0
    57
sl@0
    58
  g_io_channel_unref (io);
sl@0
    59
}
sl@0
    60
sl@0
    61
sl@0
    62
gint main (gint argc, gchar * argv[])
sl@0
    63
{
sl@0
    64
    GIOChannel *gio_r, *gio_w ;
sl@0
    65
    GError *gerr = NULL;
sl@0
    66
    GString *buffer;
sl@0
    67
    char *filename;
sl@0
    68
    char *srcdir = getenv ("srcdir");
sl@0
    69
    gint rlength = 0;
sl@0
    70
    glong wlength = 0;
sl@0
    71
    gsize length_out;
sl@0
    72
    const gchar encoding[] = "ISO-8859-5";
sl@0
    73
    GIOStatus status;
sl@0
    74
    GIOFlags flags;
sl@0
    75
sl@0
    76
  #ifdef __SYMBIAN32__
sl@0
    77
  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
    78
  g_set_print_handler(mrtPrintHandler);
sl@0
    79
  #endif /*__SYMBIAN32__*/
sl@0
    80
	  
sl@0
    81
sl@0
    82
    if (!srcdir)
sl@0
    83
      srcdir = "c:";
sl@0
    84
    filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
sl@0
    85
  
sl@0
    86
    setbuf (stdout, NULL); /* For debugging */
sl@0
    87
sl@0
    88
    gio_r = g_io_channel_new_file (filename, "r", &gerr);
sl@0
    89
    if (gerr)
sl@0
    90
      {
sl@0
    91
        g_warning ("Unable to open file %s: %s", filename, gerr->message);
sl@0
    92
        g_error_free (gerr);
sl@0
    93
        
sl@0
    94
        g_assert(FALSE && "iochannel-test failed");
sl@0
    95
        
sl@0
    96
        #if __SYMBIAN32__
sl@0
    97
  		testResultXml("iochannel-test");
sl@0
    98
  		#endif /* EMULATOR */
sl@0
    99
        return 1;
sl@0
   100
      }
sl@0
   101
    gio_w = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &gerr);
sl@0
   102
    if (gerr)
sl@0
   103
      {
sl@0
   104
        g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
sl@0
   105
        g_error_free (gerr);
sl@0
   106
        #if __SYMBIAN32__
sl@0
   107
  		testResultXml("iochannel-test");
sl@0
   108
  		#endif /* EMULATOR */
sl@0
   109
        
sl@0
   110
        return 1;
sl@0
   111
      }
sl@0
   112
sl@0
   113
    g_io_channel_set_encoding (gio_r, encoding, &gerr);
sl@0
   114
    if (gerr)
sl@0
   115
      {
sl@0
   116
        g_warning (gerr->message);
sl@0
   117
        /* Keep going if this is just a case of iconv not supporting EUC-JP, see bug 428048 */
sl@0
   118
        if (gerr->code != G_CONVERT_ERROR_NO_CONVERSION)
sl@0
   119
          return 1;
sl@0
   120
        g_error_free (gerr);
sl@0
   121
        
sl@0
   122
        g_assert(FALSE && "iochannel-test failed");
sl@0
   123
        
sl@0
   124
        #if __SYMBIAN32__
sl@0
   125
  		testResultXml("iochannel-test");
sl@0
   126
  		#endif /* EMULATOR */
sl@0
   127
        
sl@0
   128
        return 1;
sl@0
   129
      }
sl@0
   130
    
sl@0
   131
    g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
sl@0
   132
sl@0
   133
    status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
sl@0
   134
    if (status == G_IO_STATUS_ERROR)
sl@0
   135
      {
sl@0
   136
        g_warning (gerr->message);
sl@0
   137
        g_assert(FALSE && "iochannel-test failed");
sl@0
   138
        g_error_free (gerr);
sl@0
   139
        gerr = NULL;
sl@0
   140
      }
sl@0
   141
    flags = g_io_channel_get_flags (gio_r);
sl@0
   142
    buffer = g_string_sized_new (BUFFER_SIZE);
sl@0
   143
sl@0
   144
    while (TRUE)
sl@0
   145
    {
sl@0
   146
        do
sl@0
   147
          status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
sl@0
   148
        while (status == G_IO_STATUS_AGAIN);
sl@0
   149
        if (status != G_IO_STATUS_NORMAL)
sl@0
   150
          break;
sl@0
   151
sl@0
   152
        rlength += buffer->len;
sl@0
   153
sl@0
   154
        do
sl@0
   155
          status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
sl@0
   156
            &length_out, &gerr);
sl@0
   157
        while (status == G_IO_STATUS_AGAIN);
sl@0
   158
        if (status != G_IO_STATUS_NORMAL)
sl@0
   159
          break;
sl@0
   160
sl@0
   161
        wlength += length_out;
sl@0
   162
sl@0
   163
        if (length_out < buffer->len)
sl@0
   164
        {
sl@0
   165
        	g_warning ("Only wrote part of the line.");
sl@0
   166
        	g_assert(FALSE && "iochannel-test failed");
sl@0
   167
        }
sl@0
   168
          
sl@0
   169
sl@0
   170
#ifdef VERBOSE
sl@0
   171
        g_print ("%s", buffer->str);
sl@0
   172
#endif
sl@0
   173
        g_string_truncate (buffer, 0);
sl@0
   174
    }
sl@0
   175
sl@0
   176
    switch (status)
sl@0
   177
      {
sl@0
   178
        case G_IO_STATUS_EOF:
sl@0
   179
          break;
sl@0
   180
        case G_IO_STATUS_ERROR:
sl@0
   181
          g_warning (gerr->message);
sl@0
   182
          g_error_free (gerr);
sl@0
   183
          gerr = NULL;
sl@0
   184
          break;
sl@0
   185
        default:
sl@0
   186
          g_warning ("Abnormal exit from write loop.");
sl@0
   187
          g_assert(FALSE && "iochannel-test failed");
sl@0
   188
          break;
sl@0
   189
      }
sl@0
   190
sl@0
   191
    do
sl@0
   192
      status = g_io_channel_flush (gio_w, &gerr);
sl@0
   193
    while (status == G_IO_STATUS_AGAIN);
sl@0
   194
sl@0
   195
    if (status == G_IO_STATUS_ERROR)
sl@0
   196
      {
sl@0
   197
        g_warning (gerr->message);
sl@0
   198
        g_assert(FALSE && "iochannel-test failed");
sl@0
   199
        g_error_free (gerr);
sl@0
   200
        gerr = NULL;
sl@0
   201
      }
sl@0
   202
sl@0
   203
#ifdef VERBOSE
sl@0
   204
    g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
sl@0
   205
#endif
sl@0
   206
sl@0
   207
    g_io_channel_unref(gio_r);
sl@0
   208
    g_io_channel_unref(gio_w);
sl@0
   209
sl@0
   210
    test_small_writes ();
sl@0
   211
    
sl@0
   212
   	#if __SYMBIAN32__
sl@0
   213
  	testResultXml("iochannel-test");
sl@0
   214
  	#endif /* EMULATOR */
sl@0
   215
  	    
sl@0
   216
    return 0;
sl@0
   217
}