1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.IOChannel;
26 
27 private import glib.ConstructionException;
28 private import glib.ErrorG;
29 private import glib.GException;
30 private import glib.Source;
31 private import glib.Str;
32 private import glib.StringG;
33 private import glib.c.functions;
34 public  import glib.c.types;
35 public  import gtkc.glibtypes;
36 private import gtkd.Loader;
37 
38 
39 /**
40  * A data structure representing an IO Channel. The fields should be
41  * considered private and should only be accessed with the following
42  * functions.
43  */
44 public class IOChannel
45 {
46 	/** the main Gtk struct */
47 	protected GIOChannel* gIOChannel;
48 	protected bool ownedRef;
49 
50 	/** Get the main Gtk struct */
51 	public GIOChannel* getIOChannelStruct(bool transferOwnership = false)
52 	{
53 		if (transferOwnership)
54 			ownedRef = false;
55 		return gIOChannel;
56 	}
57 
58 	/** the main Gtk struct as a void* */
59 	protected void* getStruct()
60 	{
61 		return cast(void*)gIOChannel;
62 	}
63 
64 	/**
65 	 * Sets our main struct and passes it to the parent class.
66 	 */
67 	public this (GIOChannel* gIOChannel, bool ownedRef = false)
68 	{
69 		this.gIOChannel = gIOChannel;
70 		this.ownedRef = ownedRef;
71 	}
72 
73 	~this ()
74 	{
75 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
76 			g_io_channel_unref(gIOChannel);
77 	}
78 
79 
80 	/**
81 	 * Open a file @filename as a #GIOChannel using mode @mode. This
82 	 * channel will be closed when the last reference to it is dropped,
83 	 * so there is no need to call g_io_channel_close() (though doing
84 	 * so will not cause problems, as long as no attempt is made to
85 	 * access the channel after it is closed).
86 	 *
87 	 * Params:
88 	 *     filename = A string containing the name of a file
89 	 *     mode = One of "r", "w", "a", "r+", "w+", "a+". These have
90 	 *         the same meaning as in fopen()
91 	 *
92 	 * Returns: A #GIOChannel on success, %NULL on failure.
93 	 *
94 	 * Throws: GException on failure.
95 	 * Throws: ConstructionException GTK+ fails to create the object.
96 	 */
97 	public this(string filename, string mode)
98 	{
99 		GError* err = null;
100 
101 		auto p = g_io_channel_new_file(Str.toStringz(filename), Str.toStringz(mode), &err);
102 
103 		if (err !is null)
104 		{
105 			throw new GException( new ErrorG(err) );
106 		}
107 
108 		if(p is null)
109 		{
110 			throw new ConstructionException("null returned by new_file");
111 		}
112 
113 		this(cast(GIOChannel*) p);
114 	}
115 
116 	/**
117 	 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
118 	 * this works for plain files, pipes, and sockets.
119 	 *
120 	 * The returned #GIOChannel has a reference count of 1.
121 	 *
122 	 * The default encoding for #GIOChannel is UTF-8. If your application
123 	 * is reading output from a command using via pipe, you may need to set
124 	 * the encoding to the encoding of the current locale (see
125 	 * g_get_charset()) with the g_io_channel_set_encoding() function.
126 	 * By default, the fd passed will not be closed when the final reference
127 	 * to the #GIOChannel data structure is dropped.
128 	 *
129 	 * If you want to read raw binary data without interpretation, then
130 	 * call the g_io_channel_set_encoding() function with %NULL for the
131 	 * encoding argument.
132 	 *
133 	 * This function is available in GLib on Windows, too, but you should
134 	 * avoid using it on Windows. The domain of file descriptors and
135 	 * sockets overlap. There is no way for GLib to know which one you mean
136 	 * in case the argument you pass to this function happens to be both a
137 	 * valid file descriptor and socket. If that happens a warning is
138 	 * issued, and GLib assumes that it is the file descriptor you mean.
139 	 *
140 	 * Params:
141 	 *     fd = a file descriptor.
142 	 *
143 	 * Returns: a new #GIOChannel.
144 	 *
145 	 * Throws: ConstructionException GTK+ fails to create the object.
146 	 */
147 	public this(int fd)
148 	{
149 		auto p = g_io_channel_unix_new(fd);
150 
151 		if(p is null)
152 		{
153 			throw new ConstructionException("null returned by unix_new");
154 		}
155 
156 		this(cast(GIOChannel*) p);
157 	}
158 
159 	/**
160 	 * Close an IO channel. Any pending data to be written will be
161 	 * flushed, ignoring errors. The channel will not be freed until the
162 	 * last reference is dropped using g_io_channel_unref().
163 	 *
164 	 * Deprecated: Use g_io_channel_shutdown() instead.
165 	 */
166 	public void close()
167 	{
168 		g_io_channel_close(gIOChannel);
169 	}
170 
171 	/**
172 	 * Flushes the write buffer for the GIOChannel.
173 	 *
174 	 * Returns: the status of the operation: One of
175 	 *     #G_IO_STATUS_NORMAL, #G_IO_STATUS_AGAIN, or
176 	 *     #G_IO_STATUS_ERROR.
177 	 *
178 	 * Throws: GException on failure.
179 	 */
180 	public GIOStatus flush()
181 	{
182 		GError* err = null;
183 
184 		auto p = g_io_channel_flush(gIOChannel, &err);
185 
186 		if (err !is null)
187 		{
188 			throw new GException( new ErrorG(err) );
189 		}
190 
191 		return p;
192 	}
193 
194 	/**
195 	 * This function returns a #GIOCondition depending on whether there
196 	 * is data to be read/space to write data in the internal buffers in
197 	 * the #GIOChannel. Only the flags %G_IO_IN and %G_IO_OUT may be set.
198 	 *
199 	 * Returns: A #GIOCondition
200 	 */
201 	public GIOCondition getBufferCondition()
202 	{
203 		return g_io_channel_get_buffer_condition(gIOChannel);
204 	}
205 
206 	/**
207 	 * Gets the buffer size.
208 	 *
209 	 * Returns: the size of the buffer.
210 	 */
211 	public size_t getBufferSize()
212 	{
213 		return g_io_channel_get_buffer_size(gIOChannel);
214 	}
215 
216 	/**
217 	 * Returns whether @channel is buffered.
218 	 *
219 	 * Returns: %TRUE if the @channel is buffered.
220 	 */
221 	public bool getBuffered()
222 	{
223 		return g_io_channel_get_buffered(gIOChannel) != 0;
224 	}
225 
226 	/**
227 	 * Returns whether the file/socket/whatever associated with @channel
228 	 * will be closed when @channel receives its final unref and is
229 	 * destroyed. The default value of this is %TRUE for channels created
230 	 * by g_io_channel_new_file (), and %FALSE for all other channels.
231 	 *
232 	 * Returns: %TRUE if the channel will be closed, %FALSE otherwise.
233 	 */
234 	public bool getCloseOnUnref()
235 	{
236 		return g_io_channel_get_close_on_unref(gIOChannel) != 0;
237 	}
238 
239 	/**
240 	 * Gets the encoding for the input/output of the channel.
241 	 * The internal encoding is always UTF-8. The encoding %NULL
242 	 * makes the channel safe for binary data.
243 	 *
244 	 * Returns: A string containing the encoding, this string is
245 	 *     owned by GLib and must not be freed.
246 	 */
247 	public string getEncoding()
248 	{
249 		return Str.toString(g_io_channel_get_encoding(gIOChannel));
250 	}
251 
252 	/**
253 	 * Gets the current flags for a #GIOChannel, including read-only
254 	 * flags such as %G_IO_FLAG_IS_READABLE.
255 	 *
256 	 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITABLE
257 	 * are cached for internal use by the channel when it is created.
258 	 * If they should change at some later point (e.g. partial shutdown
259 	 * of a socket with the UNIX shutdown() function), the user
260 	 * should immediately call g_io_channel_get_flags() to update
261 	 * the internal values of these flags.
262 	 *
263 	 * Returns: the flags which are set on the channel
264 	 */
265 	public GIOFlags getFlags()
266 	{
267 		return g_io_channel_get_flags(gIOChannel);
268 	}
269 
270 	/**
271 	 * This returns the string that #GIOChannel uses to determine
272 	 * where in the file a line break occurs. A value of %NULL
273 	 * indicates autodetection.
274 	 *
275 	 * Params:
276 	 *     length = a location to return the length of the line terminator
277 	 *
278 	 * Returns: The line termination string. This value
279 	 *     is owned by GLib and must not be freed.
280 	 */
281 	public string getLineTerm(int* length)
282 	{
283 		return Str.toString(g_io_channel_get_line_term(gIOChannel, length));
284 	}
285 
286 	/**
287 	 * Initializes a #GIOChannel struct.
288 	 *
289 	 * This is called by each of the above functions when creating a
290 	 * #GIOChannel, and so is not often needed by the application
291 	 * programmer (unless you are creating a new type of #GIOChannel).
292 	 */
293 	public void init()
294 	{
295 		g_io_channel_init(gIOChannel);
296 	}
297 
298 	/**
299 	 * Reads data from a #GIOChannel.
300 	 *
301 	 * Deprecated: Use g_io_channel_read_chars() instead.
302 	 *
303 	 * Params:
304 	 *     buf = a buffer to read the data into (which should be at least
305 	 *         count bytes long)
306 	 *     count = the number of bytes to read from the #GIOChannel
307 	 *     bytesRead = returns the number of bytes actually read
308 	 *
309 	 * Returns: %G_IO_ERROR_NONE if the operation was successful.
310 	 */
311 	public GIOError read(string buf, size_t count, size_t* bytesRead)
312 	{
313 		return g_io_channel_read(gIOChannel, Str.toStringz(buf), count, bytesRead);
314 	}
315 
316 	/**
317 	 * Replacement for g_io_channel_read() with the new API.
318 	 *
319 	 * Params:
320 	 *     buf = a buffer to read data into
321 	 *     bytesRead = The number of bytes read. This may be
322 	 *         zero even on success if count < 6 and the channel's encoding
323 	 *         is non-%NULL. This indicates that the next UTF-8 character is
324 	 *         too wide for the buffer.
325 	 *
326 	 * Returns: the status of the operation.
327 	 *
328 	 * Throws: GException on failure.
329 	 */
330 	public GIOStatus readChars(out char[] buf, out size_t bytesRead)
331 	{
332 		GError* err = null;
333 
334 		auto p = g_io_channel_read_chars(gIOChannel, buf.ptr, cast(size_t)buf.length, &bytesRead, &err);
335 
336 		if (err !is null)
337 		{
338 			throw new GException( new ErrorG(err) );
339 		}
340 
341 		return p;
342 	}
343 
344 	/**
345 	 * Reads a line, including the terminating character(s),
346 	 * from a #GIOChannel into a newly-allocated string.
347 	 * @str_return will contain allocated memory if the return
348 	 * is %G_IO_STATUS_NORMAL.
349 	 *
350 	 * Params:
351 	 *     strReturn = The line read from the #GIOChannel, including the
352 	 *         line terminator. This data should be freed with g_free()
353 	 *         when no longer needed. This is a nul-terminated string.
354 	 *         If a @length of zero is returned, this will be %NULL instead.
355 	 *     terminatorPos = location to store position of line terminator, or %NULL
356 	 *
357 	 * Returns: the status of the operation.
358 	 *
359 	 * Throws: GException on failure.
360 	 */
361 	public GIOStatus readLine(out string strReturn, out size_t terminatorPos)
362 	{
363 		char* outstrReturn = null;
364 		size_t length;
365 		GError* err = null;
366 
367 		auto p = g_io_channel_read_line(gIOChannel, &outstrReturn, &length, &terminatorPos, &err);
368 
369 		if (err !is null)
370 		{
371 			throw new GException( new ErrorG(err) );
372 		}
373 
374 		strReturn = Str.toString(outstrReturn, length);
375 
376 		return p;
377 	}
378 
379 	/**
380 	 * Reads a line from a #GIOChannel, using a #GString as a buffer.
381 	 *
382 	 * Params:
383 	 *     buffer = a #GString into which the line will be written.
384 	 *         If @buffer already contains data, the old data will
385 	 *         be overwritten.
386 	 *     terminatorPos = location to store position of line terminator, or %NULL
387 	 *
388 	 * Returns: the status of the operation.
389 	 *
390 	 * Throws: GException on failure.
391 	 */
392 	public GIOStatus readLineString(StringG buffer, out size_t terminatorPos)
393 	{
394 		GError* err = null;
395 
396 		auto p = g_io_channel_read_line_string(gIOChannel, (buffer is null) ? null : buffer.getStringGStruct(), &terminatorPos, &err);
397 
398 		if (err !is null)
399 		{
400 			throw new GException( new ErrorG(err) );
401 		}
402 
403 		return p;
404 	}
405 
406 	/**
407 	 * Reads all the remaining data from the file.
408 	 *
409 	 * Params:
410 	 *     strReturn = Location to
411 	 *         store a pointer to a string holding the remaining data in the
412 	 *         #GIOChannel. This data should be freed with g_free() when no
413 	 *         longer needed. This data is terminated by an extra nul
414 	 *         character, but there may be other nuls in the intervening data.
415 	 *
416 	 * Returns: %G_IO_STATUS_NORMAL on success.
417 	 *     This function never returns %G_IO_STATUS_EOF.
418 	 *
419 	 * Throws: GException on failure.
420 	 */
421 	public GIOStatus readToEnd(out string strReturn)
422 	{
423 		char* outstrReturn = null;
424 		size_t length;
425 		GError* err = null;
426 
427 		auto p = g_io_channel_read_to_end(gIOChannel, &outstrReturn, &length, &err);
428 
429 		if (err !is null)
430 		{
431 			throw new GException( new ErrorG(err) );
432 		}
433 
434 		strReturn = Str.toString(outstrReturn, length);
435 
436 		return p;
437 	}
438 
439 	/**
440 	 * Reads a Unicode character from @channel.
441 	 * This function cannot be called on a channel with %NULL encoding.
442 	 *
443 	 * Params:
444 	 *     thechar = a location to return a character
445 	 *
446 	 * Returns: a #GIOStatus
447 	 *
448 	 * Throws: GException on failure.
449 	 */
450 	public GIOStatus readUnichar(out dchar thechar)
451 	{
452 		GError* err = null;
453 
454 		auto p = g_io_channel_read_unichar(gIOChannel, &thechar, &err);
455 
456 		if (err !is null)
457 		{
458 			throw new GException( new ErrorG(err) );
459 		}
460 
461 		return p;
462 	}
463 
464 	/**
465 	 * Increments the reference count of a #GIOChannel.
466 	 *
467 	 * Returns: the @channel that was passed in (since 2.6)
468 	 */
469 	public IOChannel doref()
470 	{
471 		auto p = g_io_channel_ref(gIOChannel);
472 
473 		if(p is null)
474 		{
475 			return null;
476 		}
477 
478 		return new IOChannel(cast(GIOChannel*) p, true);
479 	}
480 
481 	/**
482 	 * Sets the current position in the #GIOChannel, similar to the standard
483 	 * library function fseek().
484 	 *
485 	 * Deprecated: Use g_io_channel_seek_position() instead.
486 	 *
487 	 * Params:
488 	 *     offset = an offset, in bytes, which is added to the position specified
489 	 *         by @type
490 	 *     type = the position in the file, which can be %G_SEEK_CUR (the current
491 	 *         position), %G_SEEK_SET (the start of the file), or %G_SEEK_END
492 	 *         (the end of the file)
493 	 *
494 	 * Returns: %G_IO_ERROR_NONE if the operation was successful.
495 	 */
496 	public GIOError seek(long offset, GSeekType type)
497 	{
498 		return g_io_channel_seek(gIOChannel, offset, type);
499 	}
500 
501 	/**
502 	 * Replacement for g_io_channel_seek() with the new API.
503 	 *
504 	 * Params:
505 	 *     offset = The offset in bytes from the position specified by @type
506 	 *     type = a #GSeekType. The type %G_SEEK_CUR is only allowed in those
507 	 *         cases where a call to g_io_channel_set_encoding ()
508 	 *         is allowed. See the documentation for
509 	 *         g_io_channel_set_encoding () for details.
510 	 *
511 	 * Returns: the status of the operation.
512 	 *
513 	 * Throws: GException on failure.
514 	 */
515 	public GIOStatus seekPosition(long offset, GSeekType type)
516 	{
517 		GError* err = null;
518 
519 		auto p = g_io_channel_seek_position(gIOChannel, offset, type, &err);
520 
521 		if (err !is null)
522 		{
523 			throw new GException( new ErrorG(err) );
524 		}
525 
526 		return p;
527 	}
528 
529 	/**
530 	 * Sets the buffer size.
531 	 *
532 	 * Params:
533 	 *     size = the size of the buffer, or 0 to let GLib pick a good size
534 	 */
535 	public void setBufferSize(size_t size)
536 	{
537 		g_io_channel_set_buffer_size(gIOChannel, size);
538 	}
539 
540 	/**
541 	 * The buffering state can only be set if the channel's encoding
542 	 * is %NULL. For any other encoding, the channel must be buffered.
543 	 *
544 	 * A buffered channel can only be set unbuffered if the channel's
545 	 * internal buffers have been flushed. Newly created channels or
546 	 * channels which have returned %G_IO_STATUS_EOF
547 	 * not require such a flush. For write-only channels, a call to
548 	 * g_io_channel_flush () is sufficient. For all other channels,
549 	 * the buffers may be flushed by a call to g_io_channel_seek_position ().
550 	 * This includes the possibility of seeking with seek type %G_SEEK_CUR
551 	 * and an offset of zero. Note that this means that socket-based
552 	 * channels cannot be set unbuffered once they have had data
553 	 * read from them.
554 	 *
555 	 * On unbuffered channels, it is safe to mix read and write
556 	 * calls from the new and old APIs, if this is necessary for
557 	 * maintaining old code.
558 	 *
559 	 * The default state of the channel is buffered.
560 	 *
561 	 * Params:
562 	 *     buffered = whether to set the channel buffered or unbuffered
563 	 */
564 	public void setBuffered(bool buffered)
565 	{
566 		g_io_channel_set_buffered(gIOChannel, buffered);
567 	}
568 
569 	/**
570 	 * Whether to close the channel on the final unref of the #GIOChannel
571 	 * data structure. The default value of this is %TRUE for channels
572 	 * created by g_io_channel_new_file (), and %FALSE for all other channels.
573 	 *
574 	 * Setting this flag to %TRUE for a channel you have already closed
575 	 * can cause problems when the final reference to the #GIOChannel is dropped.
576 	 *
577 	 * Params:
578 	 *     doClose = Whether to close the channel on the final unref of
579 	 *         the GIOChannel data structure.
580 	 */
581 	public void setCloseOnUnref(bool doClose)
582 	{
583 		g_io_channel_set_close_on_unref(gIOChannel, doClose);
584 	}
585 
586 	/**
587 	 * Sets the encoding for the input/output of the channel.
588 	 * The internal encoding is always UTF-8. The default encoding
589 	 * for the external file is UTF-8.
590 	 *
591 	 * The encoding %NULL is safe to use with binary data.
592 	 *
593 	 * The encoding can only be set if one of the following conditions
594 	 * is true:
595 	 *
596 	 * - The channel was just created, and has not been written to or read from yet.
597 	 *
598 	 * - The channel is write-only.
599 	 *
600 	 * - The channel is a file, and the file pointer was just repositioned
601 	 * by a call to g_io_channel_seek_position(). (This flushes all the
602 	 * internal buffers.)
603 	 *
604 	 * - The current encoding is %NULL or UTF-8.
605 	 *
606 	 * - One of the (new API) read functions has just returned %G_IO_STATUS_EOF
607 	 * (or, in the case of g_io_channel_read_to_end(), %G_IO_STATUS_NORMAL).
608 	 *
609 	 * -  One of the functions g_io_channel_read_chars() or
610 	 * g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or
611 	 * %G_IO_STATUS_ERROR. This may be useful in the case of
612 	 * %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
613 	 * Returning one of these statuses from g_io_channel_read_line(),
614 	 * g_io_channel_read_line_string(), or g_io_channel_read_to_end()
615 	 * does not guarantee that the encoding can be changed.
616 	 *
617 	 * Channels which do not meet one of the above conditions cannot call
618 	 * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if
619 	 * they are "seekable", cannot call g_io_channel_write_chars() after
620 	 * calling one of the API "read" functions.
621 	 *
622 	 * Params:
623 	 *     encoding = the encoding type
624 	 *
625 	 * Returns: %G_IO_STATUS_NORMAL if the encoding was successfully set
626 	 *
627 	 * Throws: GException on failure.
628 	 */
629 	public GIOStatus setEncoding(string encoding)
630 	{
631 		GError* err = null;
632 
633 		auto p = g_io_channel_set_encoding(gIOChannel, Str.toStringz(encoding), &err);
634 
635 		if (err !is null)
636 		{
637 			throw new GException( new ErrorG(err) );
638 		}
639 
640 		return p;
641 	}
642 
643 	/**
644 	 * Sets the (writeable) flags in @channel to (@flags & %G_IO_FLAG_SET_MASK).
645 	 *
646 	 * Params:
647 	 *     flags = the flags to set on the IO channel
648 	 *
649 	 * Returns: the status of the operation.
650 	 *
651 	 * Throws: GException on failure.
652 	 */
653 	public GIOStatus setFlags(GIOFlags flags)
654 	{
655 		GError* err = null;
656 
657 		auto p = g_io_channel_set_flags(gIOChannel, flags, &err);
658 
659 		if (err !is null)
660 		{
661 			throw new GException( new ErrorG(err) );
662 		}
663 
664 		return p;
665 	}
666 
667 	/**
668 	 * This sets the string that #GIOChannel uses to determine
669 	 * where in the file a line break occurs.
670 	 *
671 	 * Params:
672 	 *     lineTerm = The line termination string. Use %NULL for
673 	 *         autodetect.  Autodetection breaks on "\n", "\r\n", "\r", "\0",
674 	 *         and the Unicode paragraph separator. Autodetection should not be
675 	 *         used for anything other than file-based channels.
676 	 *     length = The length of the termination string. If -1 is passed, the
677 	 *         string is assumed to be nul-terminated. This option allows
678 	 *         termination strings with embedded nuls.
679 	 */
680 	public void setLineTerm(string lineTerm, int length)
681 	{
682 		g_io_channel_set_line_term(gIOChannel, Str.toStringz(lineTerm), length);
683 	}
684 
685 	/**
686 	 * Close an IO channel. Any pending data to be written will be
687 	 * flushed if @flush is %TRUE. The channel will not be freed until the
688 	 * last reference is dropped using g_io_channel_unref().
689 	 *
690 	 * Params:
691 	 *     flush = if %TRUE, flush pending
692 	 *
693 	 * Returns: the status of the operation.
694 	 *
695 	 * Throws: GException on failure.
696 	 */
697 	public GIOStatus shutdown(bool flush)
698 	{
699 		GError* err = null;
700 
701 		auto p = g_io_channel_shutdown(gIOChannel, flush, &err);
702 
703 		if (err !is null)
704 		{
705 			throw new GException( new ErrorG(err) );
706 		}
707 
708 		return p;
709 	}
710 
711 	/**
712 	 * Returns the file descriptor of the #GIOChannel.
713 	 *
714 	 * On Windows this function returns the file descriptor or socket of
715 	 * the #GIOChannel.
716 	 *
717 	 * Returns: the file descriptor of the #GIOChannel.
718 	 */
719 	public int unixGetFd()
720 	{
721 		return g_io_channel_unix_get_fd(gIOChannel);
722 	}
723 
724 	/**
725 	 * Decrements the reference count of a #GIOChannel.
726 	 */
727 	public void unref()
728 	{
729 		g_io_channel_unref(gIOChannel);
730 	}
731 
732 	/**
733 	 * Writes data to a #GIOChannel.
734 	 *
735 	 * Deprecated: Use g_io_channel_write_chars() instead.
736 	 *
737 	 * Params:
738 	 *     buf = the buffer containing the data to write
739 	 *     count = the number of bytes to write
740 	 *     bytesWritten = the number of bytes actually written
741 	 *
742 	 * Returns: %G_IO_ERROR_NONE if the operation was successful.
743 	 */
744 	public GIOError write(string buf, size_t count, size_t* bytesWritten)
745 	{
746 		return g_io_channel_write(gIOChannel, Str.toStringz(buf), count, bytesWritten);
747 	}
748 
749 	/**
750 	 * Replacement for g_io_channel_write() with the new API.
751 	 *
752 	 * On seekable channels with encodings other than %NULL or UTF-8, generic
753 	 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
754 	 * may only be made on a channel from which data has been read in the
755 	 * cases described in the documentation for g_io_channel_set_encoding ().
756 	 *
757 	 * Params:
758 	 *     buf = a buffer to write data from
759 	 *     bytesWritten = The number of bytes written. This can be nonzero
760 	 *         even if the return value is not %G_IO_STATUS_NORMAL.
761 	 *         If the return value is %G_IO_STATUS_NORMAL and the
762 	 *         channel is blocking, this will always be equal
763 	 *         to @count if @count >= 0.
764 	 *
765 	 * Returns: the status of the operation.
766 	 *
767 	 * Throws: GException on failure.
768 	 */
769 	public GIOStatus writeChars(string buf, out size_t bytesWritten)
770 	{
771 		GError* err = null;
772 
773 		auto p = g_io_channel_write_chars(gIOChannel, Str.toStringz(buf), cast(ptrdiff_t)buf.length, &bytesWritten, &err);
774 
775 		if (err !is null)
776 		{
777 			throw new GException( new ErrorG(err) );
778 		}
779 
780 		return p;
781 	}
782 
783 	/**
784 	 * Writes a Unicode character to @channel.
785 	 * This function cannot be called on a channel with %NULL encoding.
786 	 *
787 	 * Params:
788 	 *     thechar = a character
789 	 *
790 	 * Returns: a #GIOStatus
791 	 *
792 	 * Throws: GException on failure.
793 	 */
794 	public GIOStatus writeUnichar(dchar thechar)
795 	{
796 		GError* err = null;
797 
798 		auto p = g_io_channel_write_unichar(gIOChannel, thechar, &err);
799 
800 		if (err !is null)
801 		{
802 			throw new GException( new ErrorG(err) );
803 		}
804 
805 		return p;
806 	}
807 
808 	/**
809 	 * Converts an `errno` error number to a #GIOChannelError.
810 	 *
811 	 * Params:
812 	 *     en = an `errno` error number, e.g. `EINVAL`
813 	 *
814 	 * Returns: a #GIOChannelError error number, e.g.
815 	 *     %G_IO_CHANNEL_ERROR_INVAL.
816 	 */
817 	public static GIOChannelError errorFromErrno(int en)
818 	{
819 		return g_io_channel_error_from_errno(en);
820 	}
821 
822 	/** */
823 	public static GQuark errorQuark()
824 	{
825 		return g_io_channel_error_quark();
826 	}
827 
828 	/**
829 	 * Adds the #GIOChannel into the default main loop context
830 	 * with the default priority.
831 	 *
832 	 * Params:
833 	 *     channel = a #GIOChannel
834 	 *     condition = the condition to watch for
835 	 *     func = the function to call when the condition is satisfied
836 	 *     userData = user data to pass to @func
837 	 *
838 	 * Returns: the event source id
839 	 */
840 	public static uint ioAddWatch(IOChannel channel, GIOCondition condition, GIOFunc func, void* userData)
841 	{
842 		return g_io_add_watch((channel is null) ? null : channel.getIOChannelStruct(), condition, func, userData);
843 	}
844 
845 	/**
846 	 * Adds the #GIOChannel into the default main loop context
847 	 * with the given priority.
848 	 *
849 	 * This internally creates a main loop source using g_io_create_watch()
850 	 * and attaches it to the main loop context with g_source_attach().
851 	 * You can do these steps manually if you need greater control.
852 	 *
853 	 * Params:
854 	 *     channel = a #GIOChannel
855 	 *     priority = the priority of the #GIOChannel source
856 	 *     condition = the condition to watch for
857 	 *     func = the function to call when the condition is satisfied
858 	 *     userData = user data to pass to @func
859 	 *     notify = the function to call when the source is removed
860 	 *
861 	 * Returns: the event source id
862 	 */
863 	public static uint ioAddWatchFull(IOChannel channel, int priority, GIOCondition condition, GIOFunc func, void* userData, GDestroyNotify notify)
864 	{
865 		return g_io_add_watch_full((channel is null) ? null : channel.getIOChannelStruct(), priority, condition, func, userData, notify);
866 	}
867 
868 	/**
869 	 * Creates a #GSource that's dispatched when @condition is met for the
870 	 * given @channel. For example, if condition is #G_IO_IN, the source will
871 	 * be dispatched when there's data available for reading.
872 	 *
873 	 * g_io_add_watch() is a simpler interface to this same functionality, for
874 	 * the case where you want to add the source to the default main loop context
875 	 * at the default priority.
876 	 *
877 	 * On Windows, polling a #GSource created to watch a channel for a socket
878 	 * puts the socket in non-blocking mode. This is a side-effect of the
879 	 * implementation and unavoidable.
880 	 *
881 	 * Params:
882 	 *     channel = a #GIOChannel to watch
883 	 *     condition = conditions to watch for
884 	 *
885 	 * Returns: a new #GSource
886 	 */
887 	public static Source ioCreateWatch(IOChannel channel, GIOCondition condition)
888 	{
889 		auto p = g_io_create_watch((channel is null) ? null : channel.getIOChannelStruct(), condition);
890 
891 		if(p is null)
892 		{
893 			return null;
894 		}
895 
896 		return new Source(cast(GSource*) p, true);
897 	}
898 }