1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #ifndef __G_IOCHANNEL_H__
28 #define __G_IOCHANNEL_H__
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
34 #include <glib/gconvert.h>
35 #include <glib/gmain.h>
36 #include <glib/gstring.h>
43 typedef struct _GIOChannel GIOChannel;
44 typedef struct _GIOFuncs GIOFuncs;
54 #define G_IO_CHANNEL_ERROR g_io_channel_error_quark()
58 /* Derived from errno */
59 G_IO_CHANNEL_ERROR_FBIG,
60 G_IO_CHANNEL_ERROR_INVAL,
61 G_IO_CHANNEL_ERROR_IO,
62 G_IO_CHANNEL_ERROR_ISDIR,
63 G_IO_CHANNEL_ERROR_NOSPC,
64 G_IO_CHANNEL_ERROR_NXIO,
65 G_IO_CHANNEL_ERROR_OVERFLOW,
66 G_IO_CHANNEL_ERROR_PIPE,
68 G_IO_CHANNEL_ERROR_FAILED
88 G_IO_FLAG_APPEND = 1 << 0,
89 G_IO_FLAG_NONBLOCK = 1 << 1,
90 G_IO_FLAG_IS_READABLE = 1 << 2, /* Read only flag */
91 G_IO_FLAG_IS_WRITABLE = 1 << 3, /* Read only flag */
92 G_IO_FLAG_IS_SEEKABLE = 1 << 4, /* Read only flag */
93 G_IO_FLAG_MASK = (1 << 5) - 1,
94 G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
95 G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
98 /* Misspelling in enum in 2.29.10 and earlier */
99 #define G_IO_FLAG_IS_WRITEABLE (G_IO_FLAG_IS_WRITABLE)
110 gchar *line_term; /* String which indicates the end of a line of text */
111 guint line_term_len; /* So we can have null in the line term */
114 GString *read_buf; /* Raw data from the channel */
115 GString *encoded_read_buf; /* Channel data converted to UTF-8 */
116 GString *write_buf; /* Data ready to be written to the file */
117 gchar partial_write_buf[6]; /* UTF-8 partial characters, null terminated */
119 /* Group the flags together, immediately after partial_write_buf, to save memory */
121 guint use_buffer : 1; /* The encoding uses the buffers */
122 guint do_encode : 1; /* The encoding uses the GIConv coverters */
123 guint close_on_unref : 1; /* Close the channel on final unref */
124 guint is_readable : 1; /* Cached GIOFlag */
125 guint is_writeable : 1; /* ditto */
126 guint is_seekable : 1; /* ditto */
132 typedef gboolean (*GIOFunc) (GIOChannel *source,
133 GIOCondition condition,
137 GIOStatus (*io_read) (GIOChannel *channel,
142 GIOStatus (*io_write) (GIOChannel *channel,
145 gsize *bytes_written,
147 GIOStatus (*io_seek) (GIOChannel *channel,
151 GIOStatus (*io_close) (GIOChannel *channel,
153 GSource* (*io_create_watch) (GIOChannel *channel,
154 GIOCondition condition);
155 void (*io_free) (GIOChannel *channel);
156 GIOStatus (*io_set_flags) (GIOChannel *channel,
159 GIOFlags (*io_get_flags) (GIOChannel *channel);
162 GLIB_AVAILABLE_IN_ALL
163 void g_io_channel_init (GIOChannel *channel);
164 GLIB_AVAILABLE_IN_ALL
165 GIOChannel *g_io_channel_ref (GIOChannel *channel);
166 GLIB_AVAILABLE_IN_ALL
167 void g_io_channel_unref (GIOChannel *channel);
169 GLIB_DEPRECATED_FOR(g_io_channel_read_for)
170 GIOError g_io_channel_read (GIOChannel *channel,
175 GLIB_DEPRECATED_FOR(g_io_channel_write_chars)
176 GIOError g_io_channel_write (GIOChannel *channel,
179 gsize *bytes_written);
181 GLIB_DEPRECATED_FOR(g_io_channel_seek_position)
182 GIOError g_io_channel_seek (GIOChannel *channel,
186 GLIB_DEPRECATED_FOR(g_io_channel_shutdown)
187 void g_io_channel_close (GIOChannel *channel);
189 GLIB_AVAILABLE_IN_ALL
190 GIOStatus g_io_channel_shutdown (GIOChannel *channel,
193 GLIB_AVAILABLE_IN_ALL
194 guint g_io_add_watch_full (GIOChannel *channel,
196 GIOCondition condition,
199 GDestroyNotify notify);
200 GLIB_AVAILABLE_IN_ALL
201 GSource * g_io_create_watch (GIOChannel *channel,
202 GIOCondition condition);
203 GLIB_AVAILABLE_IN_ALL
204 guint g_io_add_watch (GIOChannel *channel,
205 GIOCondition condition,
209 /* character encoding conversion involved functions.
212 GLIB_AVAILABLE_IN_ALL
213 void g_io_channel_set_buffer_size (GIOChannel *channel,
215 GLIB_AVAILABLE_IN_ALL
216 gsize g_io_channel_get_buffer_size (GIOChannel *channel);
217 GLIB_AVAILABLE_IN_ALL
218 GIOCondition g_io_channel_get_buffer_condition (GIOChannel *channel);
219 GLIB_AVAILABLE_IN_ALL
220 GIOStatus g_io_channel_set_flags (GIOChannel *channel,
223 GLIB_AVAILABLE_IN_ALL
224 GIOFlags g_io_channel_get_flags (GIOChannel *channel);
225 GLIB_AVAILABLE_IN_ALL
226 void g_io_channel_set_line_term (GIOChannel *channel,
227 const gchar *line_term,
229 GLIB_AVAILABLE_IN_ALL
230 const gchar * g_io_channel_get_line_term (GIOChannel *channel,
232 GLIB_AVAILABLE_IN_ALL
233 void g_io_channel_set_buffered (GIOChannel *channel,
235 GLIB_AVAILABLE_IN_ALL
236 gboolean g_io_channel_get_buffered (GIOChannel *channel);
237 GLIB_AVAILABLE_IN_ALL
238 GIOStatus g_io_channel_set_encoding (GIOChannel *channel,
239 const gchar *encoding,
241 GLIB_AVAILABLE_IN_ALL
242 const gchar * g_io_channel_get_encoding (GIOChannel *channel);
243 GLIB_AVAILABLE_IN_ALL
244 void g_io_channel_set_close_on_unref (GIOChannel *channel,
246 GLIB_AVAILABLE_IN_ALL
247 gboolean g_io_channel_get_close_on_unref (GIOChannel *channel);
250 GLIB_AVAILABLE_IN_ALL
251 GIOStatus g_io_channel_flush (GIOChannel *channel,
253 GLIB_AVAILABLE_IN_ALL
254 GIOStatus g_io_channel_read_line (GIOChannel *channel,
257 gsize *terminator_pos,
259 GLIB_AVAILABLE_IN_ALL
260 GIOStatus g_io_channel_read_line_string (GIOChannel *channel,
262 gsize *terminator_pos,
264 GLIB_AVAILABLE_IN_ALL
265 GIOStatus g_io_channel_read_to_end (GIOChannel *channel,
269 GLIB_AVAILABLE_IN_ALL
270 GIOStatus g_io_channel_read_chars (GIOChannel *channel,
275 GLIB_AVAILABLE_IN_ALL
276 GIOStatus g_io_channel_read_unichar (GIOChannel *channel,
279 GLIB_AVAILABLE_IN_ALL
280 GIOStatus g_io_channel_write_chars (GIOChannel *channel,
283 gsize *bytes_written,
285 GLIB_AVAILABLE_IN_ALL
286 GIOStatus g_io_channel_write_unichar (GIOChannel *channel,
289 GLIB_AVAILABLE_IN_ALL
290 GIOStatus g_io_channel_seek_position (GIOChannel *channel,
294 GLIB_AVAILABLE_IN_ALL
295 GIOChannel* g_io_channel_new_file (const gchar *filename,
301 GLIB_AVAILABLE_IN_ALL
302 GQuark g_io_channel_error_quark (void);
303 GLIB_AVAILABLE_IN_ALL
304 GIOChannelError g_io_channel_error_from_errno (gint en);
306 /* On Unix, IO channels created with this function for any file
307 * descriptor or socket.
309 * On Win32, this can be used either for files opened with the MSVCRT
310 * (the Microsoft run-time C library) _open() or _pipe, including file
311 * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr),
312 * or for Winsock SOCKETs. If the parameter is a legal file
313 * descriptor, it is assumed to be such, otherwise it should be a
314 * SOCKET. This relies on SOCKETs and file descriptors not
315 * overlapping. If you want to be certain, call either
316 * g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket()
317 * instead as appropriate.
319 * The term file descriptor as used in the context of Win32 refers to
320 * the emulated Unix-like file descriptors MSVCRT provides. The native
321 * corresponding concept is file HANDLE. There isn't as of yet a way to
322 * get GIOChannels for Win32 file HANDLEs.
324 GLIB_AVAILABLE_IN_ALL
325 GIOChannel* g_io_channel_unix_new (int fd);
326 GLIB_AVAILABLE_IN_ALL
327 gint g_io_channel_unix_get_fd (GIOChannel *channel);
330 /* Hook for GClosure / GSource integration. Don't touch */
331 GLIB_VAR GSourceFuncs g_io_watch_funcs;
335 /* You can use this "pseudo file descriptor" in a GPollFD to add
336 * polling for Windows messages. GTK applications should not do that.
339 #define G_WIN32_MSG_HANDLE 19981206
341 /* Use this to get a GPollFD from a GIOChannel, so that you can call
342 * g_io_channel_win32_poll(). After calling this you should only use
343 * g_io_channel_read() to read from the GIOChannel, i.e. never read()
344 * from the underlying file descriptor. For SOCKETs, it is possible to call
347 GLIB_AVAILABLE_IN_ALL
348 void g_io_channel_win32_make_pollfd (GIOChannel *channel,
349 GIOCondition condition,
352 /* This can be used to wait a until at least one of the channels is readable.
353 * On Unix you would do a select() on the file descriptors of the channels.
355 GLIB_AVAILABLE_IN_ALL
356 gint g_io_channel_win32_poll (GPollFD *fds,
360 /* Create an IO channel for Windows messages for window handle hwnd. */
361 #if GLIB_SIZEOF_VOID_P == 8
362 /* We use gsize here so that it is still an integer type and not a
363 * pointer, like the guint in the traditional prototype. We can't use
364 * intptr_t as that is not portable enough.
366 GLIB_AVAILABLE_IN_ALL
367 GIOChannel *g_io_channel_win32_new_messages (gsize hwnd);
369 GLIB_AVAILABLE_IN_ALL
370 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
373 /* Create an IO channel for C runtime (emulated Unix-like) file
374 * descriptors. After calling g_io_add_watch() on a IO channel
375 * returned by this function, you shouldn't call read() on the file
376 * descriptor. This is because adding polling for a file descriptor is
377 * implemented on Win32 by starting a thread that sits blocked in a
378 * read() from the file descriptor most of the time. All reads from
379 * the file descriptor should be done by this internal GLib
380 * thread. Your code should call only g_io_channel_read_chars().
382 GLIB_AVAILABLE_IN_ALL
383 GIOChannel* g_io_channel_win32_new_fd (gint fd);
385 /* Get the C runtime file descriptor of a channel. */
386 GLIB_AVAILABLE_IN_ALL
387 gint g_io_channel_win32_get_fd (GIOChannel *channel);
389 /* Create an IO channel for a winsock socket. The parameter should be
390 * a SOCKET. Contrary to IO channels for file descriptors (on *Win32),
391 * you can use normal recv() or recvfrom() on sockets even if GLib
394 GLIB_AVAILABLE_IN_ALL
395 GIOChannel *g_io_channel_win32_new_socket (gint socket);
397 GLIB_DEPRECATED_FOR(g_io_channel_win32_new_socket)
398 GIOChannel *g_io_channel_win32_new_stream_socket (gint socket);
400 GLIB_AVAILABLE_IN_ALL
401 void g_io_channel_win32_set_debug (GIOChannel *channel,
407 #define g_io_channel_new_file g_io_channel_new_file_utf8
409 GLIB_AVAILABLE_IN_ALL
410 GIOChannel *g_io_channel_new_file_utf8 (const gchar *filename,
417 #endif /* __G_IOCHANNEL_H__ */