31bb7fe6e030b86e0421fe905e305b9bc550b25f
[platform/upstream/glib.git] / docs / reference / glib / tmpl / iochannels.sgml
1 <!-- ##### SECTION Title ##### -->
2 IO Channels
3
4 <!-- ##### SECTION Short_Description ##### -->
5 portable support for using files, pipes and sockets.
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 The #GIOChannel data type aims to provide a portable method for using file
10 descriptors, pipes, and sockets, and integrating them into the
11 <link linkend="glib-The-Main-Event-Loop">main event loop</link>.
12 Currently full support is available on UNIX platforms, support for
13 Windows is only partially complete.
14 </para>
15 <para>
16 To create a new #GIOChannel on UNIX systems use g_io_channel_unix_new().
17 This works for plain file descriptors, pipes and sockets.
18 Alternatively, a channel can be created for a file in a system independent
19 manner using g_io_channel_new_file().
20 </para>
21 <para>
22 Once a #GIOChannel has been created, it can be used in a generic manner
23 with the functions g_io_channel_read_chars(), g_io_channel_write_chars(),
24 g_io_channel_seek_position(), and g_io_channel_close().
25 </para>
26 <para>
27 To add a #GIOChannel to the 
28 <link linkend="glib-The-Main-Event-Loop">main event loop</link>
29 use g_io_add_watch() or g_io_add_watch_full(). Here you specify which events
30 you are interested in on the #GIOChannel, and provide a function to be
31 called whenever these events occur.
32 </para>
33 <para>
34 #GIOChannel instances are created with an initial reference count of 1.
35 g_io_channel_ref() and g_io_channel_unref() can be used to increment or
36 decrement the reference count respectively. When the reference count falls
37 to 0, the #GIOChannel is freed. (Though it isn't closed automatically,
38 unless it was created using g_io_channel_new_from_file().)
39 Using g_io_add_watch() or g_io_add_watch_full() increments a channel's
40 reference count.
41 </para>
42 <para>
43 The new functions g_io_channel_read_chars(), g_io_channel_read_line(),
44 g_io_channel_read_line_string(), g_io_channel_read_to_end(),
45 g_io_channel_write_chars(), g_io_channel_seek_position(),
46 and g_io_channel_flush() should not be mixed with the
47 deprecated functions g_io_channel_read(), g_io_channel_write(),
48 and g_io_channel_seek() on the same channel.
49 </para>
50
51 <!-- ##### SECTION See_Also ##### -->
52 <para>
53 <variablelist>
54
55 <varlistentry>
56 <term>gtk_input_add_full(), gtk_input_remove(), gdk_input_add(),
57 gdk_input_add_full(), gdk_input_remove()</term>
58 <listitem><para>
59 Convenience functions for creating #GIOChannel instances and adding them to the
60 <link linkend="glib-The-Main-Event-Loop">main event loop</link>.
61 </para></listitem>
62 </varlistentry>
63
64 </variablelist>
65 </para>
66
67 <!-- ##### SECTION Stability_Level ##### -->
68
69
70 <!-- ##### STRUCT GIOChannel ##### -->
71 <para>
72 A data structure representing an IO Channel. The fields should be considered
73 private and should only be accessed with the following functions.
74 </para>
75
76
77 <!-- ##### FUNCTION g_io_channel_unix_new ##### -->
78 <para>
79 Creates a new #GIOChannel given a file descriptor.
80 On UNIX systems this works for plain files, pipes, and sockets.
81 </para>
82 <para>
83 The returned #GIOChannel has a reference count of 1.
84 </para>
85 <para>
86 The default encoding for #GIOChannel is UTF-8. If your application
87 is reading output from a command using via pipe, you may need to
88 set the encoding to the encoding of the current locale (see
89 g_get_charset()) with the g_io_channel_set_encoding() function.
90 </para>
91 <para>
92 If you want to read raw binary data without interpretation, then
93 call the g_io_channel_set_encoding() function with %NULL for the
94 encoding argument.
95 </para>
96
97 @fd: a file descriptor.
98 @Returns: a new #GIOChannel.
99
100
101 <!-- ##### FUNCTION g_io_channel_unix_get_fd ##### -->
102 <para>
103 Returns the file descriptor of the UNIX #GIOChannel.
104 </para>
105
106 @channel: a #GIOChannel, created with g_io_channel_unix_new().
107 @Returns: the file descriptor of the #GIOChannel.
108
109
110 <!-- ##### FUNCTION g_io_channel_init ##### -->
111 <para>
112 Initializes a #GIOChannel struct. This is called by each of the above functions
113 when creating a #GIOChannel, and so is not often needed by the application
114 programmer (unless you are creating a new type of #GIOChannel).
115 </para>
116
117 @channel: a #GIOChannel.
118
119
120 <!-- ##### FUNCTION g_io_channel_new_file ##### -->
121 <para>
122 </para>
123
124 @filename: 
125 @mode: 
126 @error: 
127 @Returns: 
128
129
130 <!-- ##### FUNCTION g_io_channel_read_chars ##### -->
131 <para>
132
133 </para>
134
135 @channel: 
136 @buf: 
137 @count: 
138 @bytes_read: 
139 @error: 
140 @Returns: 
141
142
143 <!-- ##### FUNCTION g_io_channel_read_unichar ##### -->
144 <para>
145
146 </para>
147
148 @channel: 
149 @thechar: 
150 @error: 
151 @Returns: 
152
153
154 <!-- ##### FUNCTION g_io_channel_read_line ##### -->
155 <para>
156
157 </para>
158
159 @channel: 
160 @str_return: 
161 @length: 
162 @terminator_pos: 
163 @error: 
164 @Returns: 
165
166
167 <!-- ##### FUNCTION g_io_channel_read_line_string ##### -->
168 <para>
169
170 </para>
171
172 @channel: 
173 @buffer: 
174 @terminator_pos: 
175 @error: 
176 @Returns: 
177
178
179 <!-- ##### FUNCTION g_io_channel_read_to_end ##### -->
180 <para>
181
182 </para>
183
184 @channel: 
185 @str_return: 
186 @length: 
187 @error: 
188 @Returns: 
189
190
191 <!-- ##### FUNCTION g_io_channel_write_chars ##### -->
192 <para>
193
194 </para>
195
196 @channel: 
197 @buf: 
198 @count: 
199 @bytes_written: 
200 @error: 
201 @Returns: 
202
203
204 <!-- ##### FUNCTION g_io_channel_write_unichar ##### -->
205 <para>
206
207 </para>
208
209 @channel: 
210 @thechar: 
211 @error: 
212 @Returns: 
213
214
215 <!-- ##### FUNCTION g_io_channel_flush ##### -->
216 <para>
217
218 </para>
219
220 @channel: 
221 @error: 
222 @Returns: 
223
224
225 <!-- ##### FUNCTION g_io_channel_seek_position ##### -->
226 <para>
227
228 </para>
229
230 @channel: 
231 @offset: 
232 @type: 
233 @error: 
234 @Returns: 
235
236
237 <!-- ##### ENUM GSeekType ##### -->
238 <para>
239 An enumeration specifying the base position for a g_io_channel_seek_position()
240 operation.
241 </para>
242
243 @G_SEEK_CUR: the current position in the file.
244 @G_SEEK_SET: the start of the file.
245 @G_SEEK_END: the end of the file.
246
247 <!-- ##### FUNCTION g_io_channel_shutdown ##### -->
248 <para>
249
250 </para>
251
252 @channel: 
253 @flush: 
254 @err: 
255 @Returns: 
256
257
258 <!-- ##### ENUM GIOStatus ##### -->
259 <para>
260 Stati returned by most of the #GIOFuncs functions. 
261 </para>
262
263 @G_IO_STATUS_ERROR: An error occurred.
264 @G_IO_STATUS_NORMAL: Success.
265 @G_IO_STATUS_EOF: End of file.
266 @G_IO_STATUS_AGAIN: Resource temporarily unavailable.
267
268 <!-- ##### ENUM GIOChannelError ##### -->
269 <para>
270 Error codes returned by #GIOChannel operations.
271 </para>
272
273 @G_IO_CHANNEL_ERROR_FBIG: File too large.
274 @G_IO_CHANNEL_ERROR_INVAL: Invalid argument.
275 @G_IO_CHANNEL_ERROR_IO: IO error.
276 @G_IO_CHANNEL_ERROR_ISDIR: File is a directory.
277 @G_IO_CHANNEL_ERROR_NOSPC: No space left on device.
278 @G_IO_CHANNEL_ERROR_NXIO: No such device or address.
279 @G_IO_CHANNEL_ERROR_OVERFLOW: Value too large for defined datatype.
280 @G_IO_CHANNEL_ERROR_PIPE: Broken pipe.
281 @G_IO_CHANNEL_ERROR_FAILED: Some other error.
282
283 <!-- ##### MACRO G_IO_CHANNEL_ERROR ##### -->
284 <para>
285 Error domain for #GIOChannel operations. Errors in this domain will
286 be from the #GIOChannelError enumeration. See #GError for information on
287 error domains.
288 </para>
289
290
291
292 <!-- ##### FUNCTION g_io_channel_error_from_errno ##### -->
293 <para>
294
295 </para>
296
297 @en: 
298 @Returns: 
299
300
301 <!-- ##### FUNCTION g_io_channel_ref ##### -->
302 <para>
303 Increments the reference count of a #GIOChannel.
304 </para>
305
306 @channel: a #GIOChannel.
307 @Returns:  the @channel that was passed in (since 2.6)
308
309
310 <!-- ##### FUNCTION g_io_channel_unref ##### -->
311 <para>
312 Decrements the reference count of a #GIOChannel.
313 </para>
314
315 @channel: a #GIOChannel.
316
317
318 <!-- ##### FUNCTION g_io_create_watch ##### -->
319 <para>
320 Creates a #GSource that's dispatched when @condition is met for the given
321 @channel. For example, if condition is #G_IO_IN, the source will be dispatched
322 when there's data available for reading.  g_io_add_watch() is a simpler
323 interface to this same functionality, for the case where you want to add the
324 source to the default main loop at the default priority.
325 </para>
326
327 @channel: a #GIOChannel to watch
328 @condition: conditions to watch for
329 @Returns: a new #GSource
330
331
332 <!-- ##### FUNCTION g_io_add_watch ##### -->
333 <para>
334 Adds the #GIOChannel into the
335 <link linkend="glib-The-Main-Event-Loop">main event loop</link>
336 with the default priority.
337 </para>
338
339 @channel: a #GIOChannel.
340 @condition: the condition to watch for.
341 @func: the function to call when the condition is satisfied.
342 @user_data: user data to pass to @func.
343 @Returns: the event source id.
344
345
346 <!-- ##### FUNCTION g_io_add_watch_full ##### -->
347 <para>
348 Adds the #GIOChannel into the
349 <link linkend="glib-The-Main-Event-Loop">main event loop</link>
350 with the given priority.
351 </para>
352
353 @channel: a #GIOChannel.
354 @priority: the priority of the #GIOChannel source.
355 @condition: the condition to watch for.
356 @func: the function to call when the condition is satisfied.
357 @user_data: user data to pass to @func.
358 @notify: the function to call when the source is removed.
359 @Returns: the event source id.
360
361
362 <!-- ##### ENUM GIOCondition ##### -->
363 <para>
364 A bitwise combination representing a condition to watch for on an event
365 source.
366 </para>
367
368 @G_IO_IN: There is data to read.
369 @G_IO_OUT: Data can be written (without blocking).
370 @G_IO_PRI: There is urgent data to read.
371 @G_IO_ERR: Error condition.
372 @G_IO_HUP: Hung up (the connection has been broken, usually for pipes 
373            and sockets).
374 @G_IO_NVAL: Invalid request. The file descriptor is not open.
375
376 <!-- ##### USER_FUNCTION GIOFunc ##### -->
377 <para>
378 Specifies the type of function passed to g_io_add_watch() or
379 g_io_add_watch_full(), which is called when the requested condition on a
380 #GIOChannel is satisfied.
381 </para>
382
383 @source: the #GIOChannel event source.
384 @condition: the condition which has been satisfied.
385 @data: user data set in g_io_add_watch() or g_io_add_watch_full().
386 @Returns: the function should return %FALSE if the event source should be
387 removed.
388
389
390 <!-- ##### STRUCT GIOFuncs ##### -->
391 <para>
392 A table of functions used to handle different types of #GIOChannel in a
393 generic way.
394 </para>
395
396 @io_read: 
397 @io_write: 
398 @io_seek: 
399 @io_close: 
400 @io_create_watch: 
401 @io_free: 
402 @io_set_flags: 
403 @io_get_flags: 
404
405 <!-- ##### FUNCTION g_io_channel_get_buffer_size ##### -->
406 <para>
407
408 </para>
409
410 @channel: 
411 @Returns: 
412
413
414 <!-- ##### FUNCTION g_io_channel_set_buffer_size ##### -->
415 <para>
416
417 </para>
418
419 @channel: 
420 @size: 
421
422
423 <!-- ##### FUNCTION g_io_channel_get_buffer_condition ##### -->
424 <para>
425
426 </para>
427
428 @channel: 
429 @Returns: 
430
431
432 <!-- ##### FUNCTION g_io_channel_get_flags ##### -->
433 <para>
434
435 </para>
436
437 @channel: 
438 @Returns: 
439
440
441 <!-- ##### FUNCTION g_io_channel_set_flags ##### -->
442 <para>
443
444 </para>
445
446 @channel: 
447 @flags: 
448 @error: 
449 @Returns: 
450
451
452 <!-- ##### ENUM GIOFlags ##### -->
453 <para>
454 Specifies properties of a #GIOChannel. Some of the flags can only
455 be read with g_io_channel_get_flags(), but not changed with
456 g_io_channel_set_flags(). 
457 </para>
458
459 @G_IO_FLAG_APPEND: turns on append mode, corresponds to %O_APPEND (see the 
460    documentation of the UNIX <function>open()</function> syscall).
461 @G_IO_FLAG_NONBLOCK: turns on nonblocking mode, corresponds to 
462    %O_NONBLOCK/%O_NDELAY (see the documentation of the UNIX 
463    <function>open()</function> syscall).
464 @G_IO_FLAG_IS_READABLE: indicates that the io channel is readable. This flag 
465    can not be changed.
466 @G_IO_FLAG_IS_WRITEABLE: indicates that the io channel is writable. This flag 
467    can not be changed.
468 @G_IO_FLAG_IS_SEEKABLE: indicates that the io channel is seekable, i.e. that 
469    g_io_channel_seek_position() can be used on it. This flag can not be changed.
470 @G_IO_FLAG_MASK: 
471 @G_IO_FLAG_GET_MASK: 
472 @G_IO_FLAG_SET_MASK: 
473
474 <!-- ##### FUNCTION g_io_channel_get_line_term ##### -->
475 <para>
476
477 </para>
478
479 @channel: 
480 @length: 
481 @Returns: 
482
483
484 <!-- ##### FUNCTION g_io_channel_set_line_term ##### -->
485 <para>
486
487 </para>
488
489 @channel: 
490 @line_term: 
491 @length: 
492
493
494 <!-- ##### FUNCTION g_io_channel_get_buffered ##### -->
495 <para>
496
497 </para>
498
499 @channel: 
500 @Returns: 
501
502
503 <!-- ##### FUNCTION g_io_channel_set_buffered ##### -->
504 <para>
505
506 </para>
507
508 @channel: 
509 @buffered: 
510
511
512 <!-- ##### FUNCTION g_io_channel_get_encoding ##### -->
513 <para>
514
515 </para>
516
517 @channel: 
518 @Returns: 
519
520
521 <!-- ##### FUNCTION g_io_channel_set_encoding ##### -->
522 <para>
523
524 </para>
525
526 @channel: 
527 @encoding: 
528 @error: 
529 @Returns: 
530
531
532 <!-- ##### FUNCTION g_io_channel_get_close_on_unref ##### -->
533 <para>
534
535 </para>
536
537 @channel: 
538 @Returns: 
539
540
541 <!-- ##### FUNCTION g_io_channel_set_close_on_unref ##### -->
542 <para>
543
544 </para>
545
546 @channel: 
547 @do_close: 
548
549
550 <!-- ##### FUNCTION g_io_channel_read ##### -->
551 <para>
552 </para>
553
554 @channel: 
555 @buf: 
556 @count: 
557 @bytes_read: 
558 @Returns: 
559
560
561 <!-- ##### ENUM GIOError ##### -->
562 <para>
563 #GIOError is only used by the deprecated functions g_io_channel_read(),
564 g_io_channel_write(), and g_io_channel_seek().
565 </para>
566
567 @G_IO_ERROR_NONE: no error 
568 @G_IO_ERROR_AGAIN: an EAGAIN error occurred
569 @G_IO_ERROR_INVAL: an EINVAL error occurred
570 @G_IO_ERROR_UNKNOWN: another error occurred
571
572 <!-- ##### FUNCTION g_io_channel_write ##### -->
573 <para>
574 </para>
575
576 @channel: 
577 @buf: 
578 @count: 
579 @bytes_written: 
580 @Returns: 
581
582
583 <!-- ##### FUNCTION g_io_channel_seek ##### -->
584 <para>
585 </para>
586
587 @channel: 
588 @offset: 
589 @type.
590 @type: 
591 @Returns: 
592
593
594 <!-- ##### FUNCTION g_io_channel_close ##### -->
595 <para>
596 </para>
597
598 @channel: 
599
600