apicompatanamdw/bcdrivers/os/ossrv/glib/tests/iochannel-test.c
changeset 2 0cb2248d0edc
child 8 d8ef7a232001
equal deleted inserted replaced
1:61e9400fe245 2:0cb2248d0edc
       
     1 /*
       
     2 * Copyright (c) 2010 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 #undef G_DISABLE_ASSERT
       
    18 #undef G_LOG_DOMAIN
       
    19 
       
    20 #include <sys/types.h>
       
    21 #include <sys/stat.h>
       
    22 #include <fcntl.h>
       
    23 #include <glib.h>
       
    24 #include <string.h>
       
    25 #include <stdio.h>
       
    26 #include <stdlib.h>
       
    27 
       
    28 #ifdef SYMBIAN
       
    29 #include "mrt2_glib2_test.h"
       
    30 #endif /*SYMBIAN*/
       
    31 
       
    32 
       
    33 #define BUFFER_SIZE 1024
       
    34 
       
    35 gint main (gint argc, gchar * argv[])
       
    36 {
       
    37     GIOChannel *gio_r, *gio_w ;
       
    38     GError *gerr = NULL;
       
    39     GString *buffer;
       
    40     char *filename;
       
    41     char *srcdir = getenv ("srcdir");
       
    42     gint rlength = 0;
       
    43     glong wlength = 0;
       
    44     gsize length_out;
       
    45     const gchar encoding[] = "ISO-8859-5";
       
    46     GIOStatus status;
       
    47     GIOFlags flags;
       
    48 
       
    49   #ifdef SYMBIAN
       
    50   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);
       
    51   g_set_print_handler(mrtPrintHandler);
       
    52   #endif /*SYMBIAN*/
       
    53 	  
       
    54 
       
    55     if (!srcdir)
       
    56       srcdir = "c:";
       
    57     filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
       
    58   
       
    59     setbuf (stdout, NULL); /* For debugging */
       
    60 
       
    61     gio_r = g_io_channel_new_file (filename, "r", &gerr);
       
    62     if (gerr)
       
    63       {
       
    64         g_warning ("Unable to open file %s: %s", filename, gerr->message);
       
    65         g_error_free (gerr);
       
    66         
       
    67         g_assert(FALSE && "iochannel-test failed");
       
    68         
       
    69         #if SYMBIAN
       
    70   		testResultXml("iochannel-test");
       
    71   		#endif /* EMULATOR */
       
    72         
       
    73         return 1;
       
    74       }
       
    75     gio_w = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &gerr);
       
    76     if (gerr)
       
    77       {
       
    78         g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
       
    79         g_error_free (gerr);
       
    80         
       
    81         g_assert(FALSE && "iochannel-test failed");
       
    82         
       
    83         #if SYMBIAN
       
    84   		testResultXml("iochannel-test");
       
    85   		#endif /* EMULATOR */
       
    86         
       
    87         return 1;
       
    88       }
       
    89 
       
    90     g_io_channel_set_encoding (gio_r, encoding, &gerr);
       
    91     if (gerr)
       
    92       {
       
    93         g_warning (gerr->message);
       
    94         g_error_free (gerr);
       
    95         
       
    96         g_assert(FALSE && "iochannel-test failed");
       
    97         
       
    98         #if SYMBIAN
       
    99   		testResultXml("iochannel-test");
       
   100   		#endif /* EMULATOR */
       
   101         
       
   102         return 1;
       
   103       }
       
   104     
       
   105     g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
       
   106 
       
   107     status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
       
   108     if (status == G_IO_STATUS_ERROR)
       
   109       {
       
   110         g_warning (gerr->message);
       
   111         g_assert(FALSE && "iochannel-test failed");
       
   112         g_error_free (gerr);
       
   113         gerr = NULL;
       
   114       }
       
   115     flags = g_io_channel_get_flags (gio_r);
       
   116     buffer = g_string_sized_new (BUFFER_SIZE);
       
   117 
       
   118     while (TRUE)
       
   119     {
       
   120         do
       
   121           status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
       
   122         while (status == G_IO_STATUS_AGAIN);
       
   123         if (status != G_IO_STATUS_NORMAL)
       
   124           break;
       
   125 
       
   126         rlength += buffer->len;
       
   127 
       
   128         do
       
   129           status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
       
   130             &length_out, &gerr);
       
   131         while (status == G_IO_STATUS_AGAIN);
       
   132         if (status != G_IO_STATUS_NORMAL)
       
   133           break;
       
   134 
       
   135         wlength += length_out;
       
   136 
       
   137         if (length_out < buffer->len)
       
   138         {
       
   139         	g_warning ("Only wrote part of the line.");
       
   140         	g_assert(FALSE && "iochannel-test failed");
       
   141         }
       
   142           
       
   143 
       
   144 #ifdef VERBOSE
       
   145         g_print ("%s", buffer->str);
       
   146 #endif
       
   147         g_string_truncate (buffer, 0);
       
   148     }
       
   149 
       
   150     switch (status)
       
   151       {
       
   152         case G_IO_STATUS_EOF:
       
   153           break;
       
   154         case G_IO_STATUS_ERROR:
       
   155           g_warning (gerr->message);
       
   156           g_error_free (gerr);
       
   157           gerr = NULL;
       
   158           break;
       
   159         default:
       
   160           g_warning ("Abnormal exit from write loop.");
       
   161           g_assert(FALSE && "iochannel-test failed");
       
   162           break;
       
   163       }
       
   164 
       
   165     do
       
   166       status = g_io_channel_flush (gio_w, &gerr);
       
   167     while (status == G_IO_STATUS_AGAIN);
       
   168 
       
   169     if (status == G_IO_STATUS_ERROR)
       
   170       {
       
   171         g_warning (gerr->message);
       
   172         g_assert(FALSE && "iochannel-test failed");
       
   173         g_error_free (gerr);
       
   174         gerr = NULL;
       
   175       }
       
   176 
       
   177 #ifdef VERBOSE
       
   178     g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
       
   179 #endif
       
   180 
       
   181     g_io_channel_unref(gio_r);
       
   182     g_io_channel_unref(gio_w);
       
   183     
       
   184    	#if SYMBIAN
       
   185   	testResultXml("iochannel-test");
       
   186   	#endif /* EMULATOR */
       
   187   	    
       
   188     return 0;
       
   189 }