Simplify glib/glib/tests setup
[glib.git] / glib / giochannel.h
blob0e7c77d4c72abf48956749a5b1b074a176f3f1de
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."
32 #endif
34 #include <glib/gconvert.h>
35 #include <glib/gmain.h>
36 #include <glib/gstring.h>
38 G_BEGIN_DECLS
40 /* GIOChannel
43 typedef struct _GIOChannel GIOChannel;
44 typedef struct _GIOFuncs GIOFuncs;
46 typedef enum
48 G_IO_ERROR_NONE,
49 G_IO_ERROR_AGAIN,
50 G_IO_ERROR_INVAL,
51 G_IO_ERROR_UNKNOWN
52 } GIOError;
54 #define G_IO_CHANNEL_ERROR g_io_channel_error_quark()
56 typedef enum
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,
67 /* Other */
68 G_IO_CHANNEL_ERROR_FAILED
69 } GIOChannelError;
71 typedef enum
73 G_IO_STATUS_ERROR,
74 G_IO_STATUS_NORMAL,
75 G_IO_STATUS_EOF,
76 G_IO_STATUS_AGAIN
77 } GIOStatus;
79 typedef enum
81 G_SEEK_CUR,
82 G_SEEK_SET,
83 G_SEEK_END
84 } GSeekType;
86 typedef enum
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
96 } GIOFlags;
98 /* Misspelling in enum in 2.29.10 and earlier */
99 #define G_IO_FLAG_IS_WRITEABLE (G_IO_FLAG_IS_WRITABLE)
101 struct _GIOChannel
103 /*< private >*/
104 gint ref_count;
105 GIOFuncs *funcs;
107 gchar *encoding;
108 GIConv read_cd;
109 GIConv write_cd;
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 */
113 gsize buf_size;
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 */
128 gpointer reserved1;
129 gpointer reserved2;
132 typedef gboolean (*GIOFunc) (GIOChannel *source,
133 GIOCondition condition,
134 gpointer data);
135 struct _GIOFuncs
137 GIOStatus (*io_read) (GIOChannel *channel,
138 gchar *buf,
139 gsize count,
140 gsize *bytes_read,
141 GError **err);
142 GIOStatus (*io_write) (GIOChannel *channel,
143 const gchar *buf,
144 gsize count,
145 gsize *bytes_written,
146 GError **err);
147 GIOStatus (*io_seek) (GIOChannel *channel,
148 gint64 offset,
149 GSeekType type,
150 GError **err);
151 GIOStatus (*io_close) (GIOChannel *channel,
152 GError **err);
153 GSource* (*io_create_watch) (GIOChannel *channel,
154 GIOCondition condition);
155 void (*io_free) (GIOChannel *channel);
156 GIOStatus (*io_set_flags) (GIOChannel *channel,
157 GIOFlags flags,
158 GError **err);
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,
171 gchar *buf,
172 gsize count,
173 gsize *bytes_read);
175 GLIB_DEPRECATED_FOR(g_io_channel_write_chars)
176 GIOError g_io_channel_write (GIOChannel *channel,
177 const gchar *buf,
178 gsize count,
179 gsize *bytes_written);
181 GLIB_DEPRECATED_FOR(g_io_channel_seek_position)
182 GIOError g_io_channel_seek (GIOChannel *channel,
183 gint64 offset,
184 GSeekType type);
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,
191 gboolean flush,
192 GError **err);
193 GLIB_AVAILABLE_IN_ALL
194 guint g_io_add_watch_full (GIOChannel *channel,
195 gint priority,
196 GIOCondition condition,
197 GIOFunc func,
198 gpointer user_data,
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,
206 GIOFunc func,
207 gpointer user_data);
209 /* character encoding conversion involved functions.
212 GLIB_AVAILABLE_IN_ALL
213 void g_io_channel_set_buffer_size (GIOChannel *channel,
214 gsize size);
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,
221 GIOFlags flags,
222 GError **error);
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,
228 gint length);
229 GLIB_AVAILABLE_IN_ALL
230 const gchar * g_io_channel_get_line_term (GIOChannel *channel,
231 gint *length);
232 GLIB_AVAILABLE_IN_ALL
233 void g_io_channel_set_buffered (GIOChannel *channel,
234 gboolean buffered);
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,
240 GError **error);
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,
245 gboolean do_close);
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,
252 GError **error);
253 GLIB_AVAILABLE_IN_ALL
254 GIOStatus g_io_channel_read_line (GIOChannel *channel,
255 gchar **str_return,
256 gsize *length,
257 gsize *terminator_pos,
258 GError **error);
259 GLIB_AVAILABLE_IN_ALL
260 GIOStatus g_io_channel_read_line_string (GIOChannel *channel,
261 GString *buffer,
262 gsize *terminator_pos,
263 GError **error);
264 GLIB_AVAILABLE_IN_ALL
265 GIOStatus g_io_channel_read_to_end (GIOChannel *channel,
266 gchar **str_return,
267 gsize *length,
268 GError **error);
269 GLIB_AVAILABLE_IN_ALL
270 GIOStatus g_io_channel_read_chars (GIOChannel *channel,
271 gchar *buf,
272 gsize count,
273 gsize *bytes_read,
274 GError **error);
275 GLIB_AVAILABLE_IN_ALL
276 GIOStatus g_io_channel_read_unichar (GIOChannel *channel,
277 gunichar *thechar,
278 GError **error);
279 GLIB_AVAILABLE_IN_ALL
280 GIOStatus g_io_channel_write_chars (GIOChannel *channel,
281 const gchar *buf,
282 gssize count,
283 gsize *bytes_written,
284 GError **error);
285 GLIB_AVAILABLE_IN_ALL
286 GIOStatus g_io_channel_write_unichar (GIOChannel *channel,
287 gunichar thechar,
288 GError **error);
289 GLIB_AVAILABLE_IN_ALL
290 GIOStatus g_io_channel_seek_position (GIOChannel *channel,
291 gint64 offset,
292 GSeekType type,
293 GError **error);
294 GLIB_AVAILABLE_IN_ALL
295 GIOChannel* g_io_channel_new_file (const gchar *filename,
296 const gchar *mode,
297 GError **error);
299 /* Error handling */
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;
333 #ifdef G_OS_WIN32
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
345 * recv().
347 GLIB_AVAILABLE_IN_ALL
348 void g_io_channel_win32_make_pollfd (GIOChannel *channel,
349 GIOCondition condition,
350 GPollFD *fd);
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,
357 gint n_fds,
358 gint timeout_);
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);
368 #else
369 GLIB_AVAILABLE_IN_ALL
370 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
371 #endif
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
392 * is polling them.
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,
402 gboolean flag);
404 #endif
406 #ifdef G_OS_WIN32
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,
411 const gchar *mode,
412 GError **error);
413 #endif
415 G_END_DECLS
417 #endif /* __G_IOCHANNEL_H__ */