Symbian3/SDK/Source/GUID-817C43E8-9169-4750-818B-B431D138D71A.dita
changeset 7 51a74ef9ed63
parent 0 89d6a7a84779
equal deleted inserted replaced
6:43e37759235e 7:51a74ef9ed63
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-817C43E8-9169-4750-818B-B431D138D71A" xml:lang="en"><title>Using <codeph>GIOChannel</codeph></title><shortdesc>GIOChannel provides a portable method for using file descriptors,
       
    13 sockets and pipes and integrating them into the GLib main loop.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The <codeph>g_io_channel_unix_new()</codeph> API takes a file descriptor
       
    15 as an input parameter and is used to create a new <codeph>GIOChannel</codeph>.
       
    16 Alternatively, the <codeph>g_io_channel_new_file()</codeph> API can be used
       
    17 to create a channel for a file. Once the channel is created, it can be used
       
    18 in a generic manner.</p>
       
    19 <p>To read a channel, APIs such as the following can be used: </p>
       
    20 <ul>
       
    21 <li><p><codeph>g_io_channel_read_chars()</codeph></p></li>
       
    22 <li><p><codeph>g_io_channel_read_line()</codeph></p></li>
       
    23 <li><p><codeph>g_io_channel_read_to_end()</codeph></p></li>
       
    24 </ul>
       
    25 <p>To write to a channel an API like <codeph>g_io_channel_write_chars()</codeph> is
       
    26 used.</p>
       
    27 <p>To set the position in the current <codeph>GIOChannel</codeph>, <codeph>g_io_channel_seek_position()</codeph> can
       
    28 be used.</p>
       
    29 <p>Finally the channel can be closed using <codeph>g_io_channel_shutdown()</codeph>.</p>
       
    30 <p>The following is an example code where a file descriptor to a plain file
       
    31 is used to create a <codeph>GIOChannel</codeph>, and then the contents of
       
    32 the file are read in chunks of 100 bytes and printed using <codeph>g_print</codeph>.
       
    33 Finally the channel is closed. </p>
       
    34 <codeblock xml:space="preserve">#include &lt;glib.h&gt;
       
    35 #include &lt;stdio.h&gt;
       
    36 
       
    37 int main(int argc, char *argv[])
       
    38 {
       
    39 	GIOChannel *channel;
       
    40 	gchar buf[100];
       
    41 	gsize bytes_read;
       
    42 	FILE *fp;
       
    43 	
       
    44 	if(argc != 2)
       
    45 	{
       
    46 		g_print("usage:cat &lt;file_name&gt;\n");
       
    47 		g_print("Press any key to exit\n");
       
    48 		getchar();
       
    49 		return 1;
       
    50 	}
       
    51 	
       
    52 	fp = fopen(argv[1],"r");
       
    53 	
       
    54 	if(!fp)
       
    55 	{
       
    56 		g_print("Unable to open the file %s\n",argv[1]);
       
    57 		return 1;
       
    58 	}
       
    59 		
       
    60 	channel = g_io_channel_unix_new(fileno(fp));
       
    61 	
       
    62  	do
       
    63  	{
       
    64 		g_io_channel_read_chars(channel,buf,100,&amp;bytes_read,NULL);
       
    65 		g_print("%s",buf);
       
    66 		
       
    67 	}
       
    68 	while(bytes_read &gt; 0);
       
    69 	
       
    70 	g_io_channel_shutdown(channel,TRUE,NULL);
       
    71 	
       
    72 	fclose(fp);
       
    73  
       
    74 	return 0;
       
    75 }
       
    76  </codeblock>
       
    77 <p>In the example below, a sample code to copy a file is written. In the code,
       
    78 channels are opened using <codeph>g_io_channel_new_file()</codeph>. The contents
       
    79 of the file are read using <codeph>g_io_channel_read_chars()</codeph>, and
       
    80 the contents are written to a new file using <codeph>g_io_channel_write_chars()</codeph>.</p>
       
    81 <codeblock xml:space="preserve">#include &lt;glib.h&gt;
       
    82 #include &lt;stdio.h&gt;
       
    83 
       
    84 int main(int argc, char *argv[])
       
    85 {
       
    86 	GIOChannel *in_channel,*out_channel;
       
    87 	gchar buf[100];
       
    88 	gsize bytes_read,bytes_written;
       
    89 	GError *error = NULL;
       
    90 	
       
    91 	if(argc != 3)
       
    92 	{
       
    93 		g_print("usage:&lt;cp SOURCE&gt; &lt;DESTINATION&gt;\n");
       
    94 		g_print("Press any key to exit\n");
       
    95 		getchar();
       
    96 		return 1;
       
    97 	}
       
    98 		
       
    99 	in_channel = g_io_channel_new_file(argv[1],"r",&amp;error);
       
   100 	
       
   101 	if(!in_channel)
       
   102 	{
       
   103 		g_print("Unable to open the file %s to read\n",argv[1]);
       
   104 		g_print("Press any key to exit\n");
       
   105 		getchar();
       
   106 		return 1;
       
   107 	}
       
   108 	
       
   109 	out_channel = g_io_channel_new_file(argv[2],"w",&amp;error);
       
   110 	
       
   111 	if(!out_channel)
       
   112 	{
       
   113 		g_print("Unable to open the file %s to write\n",argv[2]);
       
   114 		g_print("Press any key to exit\n");
       
   115 		getchar();
       
   116 		return 1;
       
   117 	}
       
   118 	
       
   119 	do
       
   120 	{
       
   121 		g_io_channel_read_chars(in_channel,buf,100,&amp;bytes_read,&amp;error);
       
   122 		if(error)
       
   123 		{
       
   124 			g_print("Error while reading file %s\n",argv[1]);
       
   125 			g_print("Press any key to exit\n");
       
   126 			getchar();
       
   127 			return 1;
       
   128 		}
       
   129 		
       
   130 		g_io_channel_write_chars(out_channel,buf,bytes_read,&amp;bytes_written,&amp;error);
       
   131 		if(error)
       
   132 		{
       
   133 			g_print("Error while writing file %s\n",argv[2]);
       
   134 			g_print("Press any key to exit\n");
       
   135 			getchar();
       
   136 			return 1;
       
   137 		}
       
   138 	}
       
   139 	while(bytes_read &gt; 0);
       
   140 	
       
   141        g_io_channel_shutdown(in_channel,TRUE,&amp;error);
       
   142 	if(error)
       
   143 	{
       
   144 		g_print("Error has occured\n");
       
   145 		g_print("Press any key to exit\n");
       
   146 		getchar();
       
   147 		return 1;
       
   148 	}
       
   149 	 
       
   150 	g_io_channel_shutdown(out_channel,TRUE,&amp;error);
       
   151  	if(error)
       
   152 	{
       
   153 		g_print("Error has occured\n");
       
   154 		g_print("Press any key to exit\n");
       
   155 		getchar();
       
   156 		return 1;
       
   157 	}
       
   158 	
       
   159 	g_print("File copied successfully...\n");
       
   160 	getchar();
       
   161 	
       
   162 	return 0;
       
   163 }</codeblock>
       
   164 <section id="GUID-96EC4949-D5B6-41A1-A55E-D02390E5CFD5">       <title>Using <codeph>GIOChannel</codeph> in
       
   165 GLib main loops</title>       <p><codeph>GIOChannel</codeph> provides a way
       
   166 to integrate file descriptors to main loops. The following code demonstrates
       
   167 the usage. In the code, a pipe is created. A thread is created which writes
       
   168 to the write end of the pipe. The main loop checks if the other end of the
       
   169 pipe is ready to read. If it is, then the callback function is called. The
       
   170 callback calls <codeph>g_main_loop_quit()</codeph>, and the main loop is terminated
       
   171 and <codeph>g_main_loop_run()</codeph> returns. </p><codeblock xml:space="preserve">#include &lt;glib.h&gt;
       
   172 #include &lt;stdio.h&gt;
       
   173 
       
   174 int fd[2];
       
   175 
       
   176 void *thread_function(void *data)
       
   177 {
       
   178 	// call sleep so that the main loop source is not ready immediately
       
   179 	sleep(10);
       
   180 	
       
   181 	write(fd[1],"GIOChannel main loop example",29);
       
   182 	return NULL;
       
   183 }
       
   184 
       
   185 gboolean my_callback(GIOChannel *source,GIOCondition condition,gpointer data)
       
   186 {
       
   187 	char buf[100];	
       
   188 	
       
   189 	read(fd[0],buf,sizeof(buf));
       
   190 	g_print("%s",buf);
       
   191 	
       
   192 	getchar();
       
   193 	
       
   194 	g_main_loop_quit((GMainLoop *)data);
       
   195 	
       
   196 	return FALSE;
       
   197 }
       
   198 
       
   199 int main()
       
   200 {
       
   201 	GMainLoop *loop;
       
   202 	GIOChannel *channel;
       
   203 	
       
   204 	pipe(fd);
       
   205 	
       
   206 	channel = g_io_channel_unix_new(fd[0]);
       
   207 	
       
   208 	g_thread_init(NULL);
       
   209 	g_thread_create(thread_function,NULL,TRUE,NULL);
       
   210 	
       
   211 	loop = g_main_loop_new(NULL,FALSE);
       
   212 	g_io_add_watch(channel,G_IO_IN | G_IO_HUP | G_IO_ERR,(GIOFunc)my_callback,loop);
       
   213 	g_main_loop_run(loop);
       
   214 	
       
   215 	g_io_channel_shutdown(channel,TRUE,NULL);
       
   216 
       
   217 	return 0;
       
   218 }
       
   219 </codeblock></section>
       
   220 </conbody></concept>