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