1/* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
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/.
25 */
26
27#ifndef __G_IOCHANNEL_H__
28#define __G_IOCHANNEL_H__
29
30#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31#error "Only <glib.h> can be included directly."
32#endif
33
34#include <glib/gconvert.h>
35#include <glib/gmain.h>
36#include <glib/gstring.h>
37
38G_BEGIN_DECLS
39
40/* GIOChannel
41 */
42
43typedef struct _GIOChannel GIOChannel;
44typedef struct _GIOFuncs GIOFuncs;
45
46typedef enum
47{
48 G_IO_ERROR_NONE,
49 G_IO_ERROR_AGAIN,
50 G_IO_ERROR_INVAL,
51 G_IO_ERROR_UNKNOWN
52} GIOError;
53
54#define G_IO_CHANNEL_ERROR g_io_channel_error_quark()
55
56typedef enum
57{
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;
70
71typedef enum
72{
73 G_IO_STATUS_ERROR,
74 G_IO_STATUS_NORMAL,
75 G_IO_STATUS_EOF,
76 G_IO_STATUS_AGAIN
77} GIOStatus;
78
79typedef enum
80{
81 G_SEEK_CUR,
82 G_SEEK_SET,
83 G_SEEK_END
84} GSeekType;
85
86/**
87 * GIOFlags:
88 * @G_IO_FLAG_APPEND: Turns on append mode, corresponds to %O_APPEND
89 * (see the documentation of the UNIX open() syscall)
90 * @G_IO_FLAG_NONBLOCK: Turns on nonblocking mode, corresponds to
91 * %O_NONBLOCK/%O_NDELAY (see the documentation of the UNIX open()
92 * syscall)
93 * @G_IO_FLAG_IS_READABLE: Indicates that the io channel is readable.
94 * This flag cannot be changed.
95 * @G_IO_FLAG_IS_WRITABLE: Indicates that the io channel is writable.
96 * This flag cannot be changed.
97 * @G_IO_FLAG_IS_WRITEABLE: A misspelled version of @G_IO_FLAG_IS_WRITABLE
98 * that existed before the spelling was fixed in GLib 2.30. It is kept
99 * here for compatibility reasons. Deprecated since 2.30
100 * @G_IO_FLAG_IS_SEEKABLE: Indicates that the io channel is seekable,
101 * i.e. that g_io_channel_seek_position() can be used on it.
102 * This flag cannot be changed.
103 * @G_IO_FLAG_MASK: The mask that specifies all the valid flags.
104 * @G_IO_FLAG_GET_MASK: The mask of the flags that are returned from
105 * g_io_channel_get_flags()
106 * @G_IO_FLAG_SET_MASK: The mask of the flags that the user can modify
107 * with g_io_channel_set_flags()
108 *
109 * Specifies properties of a #GIOChannel. Some of the flags can only be
110 * read with g_io_channel_get_flags(), but not changed with
111 * g_io_channel_set_flags().
112 */
113typedef enum
114{
115 /**
116 * G_IO_FLAG_NONE:
117 *
118 * No special flags set.
119 *
120 * Since: 2.74
121 */
122 G_IO_FLAG_NONE GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
123 G_IO_FLAG_APPEND = 1 << 0,
124 G_IO_FLAG_NONBLOCK = 1 << 1,
125 G_IO_FLAG_IS_READABLE = 1 << 2, /* Read only flag */
126 G_IO_FLAG_IS_WRITABLE = 1 << 3, /* Read only flag */
127 G_IO_FLAG_IS_WRITEABLE = 1 << 3, /* Misspelling in 2.29.10 and earlier */
128 G_IO_FLAG_IS_SEEKABLE = 1 << 4, /* Read only flag */
129 G_IO_FLAG_MASK = (1 << 5) - 1,
130 G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
131 G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
132} G_GNUC_FLAG_ENUM GIOFlags;
133
134struct _GIOChannel
135{
136 /*< private >*/
137 gint ref_count;
138 GIOFuncs *funcs;
139
140 gchar *encoding;
141 GIConv read_cd;
142 GIConv write_cd;
143 gchar *line_term; /* String which indicates the end of a line of text */
144 guint line_term_len; /* So we can have null in the line term */
145
146 gsize buf_size;
147 GString *read_buf; /* Raw data from the channel */
148 GString *encoded_read_buf; /* Channel data converted to UTF-8 */
149 GString *write_buf; /* Data ready to be written to the file */
150 gchar partial_write_buf[6]; /* UTF-8 partial characters, null terminated */
151
152 /* Group the flags together, immediately after partial_write_buf, to save memory */
153
154 guint use_buffer : 1; /* The encoding uses the buffers */
155 guint do_encode : 1; /* The encoding uses the GIConv converters */
156 guint close_on_unref : 1; /* Close the channel on final unref */
157 guint is_readable : 1; /* Cached GIOFlag */
158 guint is_writeable : 1; /* ditto */
159 guint is_seekable : 1; /* ditto */
160
161 gpointer reserved1;
162 gpointer reserved2;
163};
164
165typedef gboolean (*GIOFunc) (GIOChannel *source,
166 GIOCondition condition,
167 gpointer data);
168struct _GIOFuncs
169{
170 GIOStatus (*io_read) (GIOChannel *channel,
171 gchar *buf,
172 gsize count,
173 gsize *bytes_read,
174 GError **err);
175 GIOStatus (*io_write) (GIOChannel *channel,
176 const gchar *buf,
177 gsize count,
178 gsize *bytes_written,
179 GError **err);
180 GIOStatus (*io_seek) (GIOChannel *channel,
181 gint64 offset,
182 GSeekType type,
183 GError **err);
184 GIOStatus (*io_close) (GIOChannel *channel,
185 GError **err);
186 GSource* (*io_create_watch) (GIOChannel *channel,
187 GIOCondition condition);
188 void (*io_free) (GIOChannel *channel);
189 GIOStatus (*io_set_flags) (GIOChannel *channel,
190 GIOFlags flags,
191 GError **err);
192 GIOFlags (*io_get_flags) (GIOChannel *channel);
193};
194
195GLIB_AVAILABLE_IN_ALL
196void g_io_channel_init (GIOChannel *channel);
197GLIB_AVAILABLE_IN_ALL
198GIOChannel *g_io_channel_ref (GIOChannel *channel);
199GLIB_AVAILABLE_IN_ALL
200void g_io_channel_unref (GIOChannel *channel);
201
202GLIB_DEPRECATED_FOR(g_io_channel_read_chars)
203GIOError g_io_channel_read (GIOChannel *channel,
204 gchar *buf,
205 gsize count,
206 gsize *bytes_read);
207
208GLIB_DEPRECATED_FOR(g_io_channel_write_chars)
209GIOError g_io_channel_write (GIOChannel *channel,
210 const gchar *buf,
211 gsize count,
212 gsize *bytes_written);
213
214GLIB_DEPRECATED_FOR(g_io_channel_seek_position)
215GIOError g_io_channel_seek (GIOChannel *channel,
216 gint64 offset,
217 GSeekType type);
218
219GLIB_DEPRECATED_FOR(g_io_channel_shutdown)
220void g_io_channel_close (GIOChannel *channel);
221
222GLIB_AVAILABLE_IN_ALL
223GIOStatus g_io_channel_shutdown (GIOChannel *channel,
224 gboolean flush,
225 GError **err);
226GLIB_AVAILABLE_IN_ALL
227guint g_io_add_watch_full (GIOChannel *channel,
228 gint priority,
229 GIOCondition condition,
230 GIOFunc func,
231 gpointer user_data,
232 GDestroyNotify notify);
233GLIB_AVAILABLE_IN_ALL
234GSource * g_io_create_watch (GIOChannel *channel,
235 GIOCondition condition);
236GLIB_AVAILABLE_IN_ALL
237guint g_io_add_watch (GIOChannel *channel,
238 GIOCondition condition,
239 GIOFunc func,
240 gpointer user_data);
241
242/* character encoding conversion involved functions.
243 */
244
245GLIB_AVAILABLE_IN_ALL
246void g_io_channel_set_buffer_size (GIOChannel *channel,
247 gsize size);
248GLIB_AVAILABLE_IN_ALL
249gsize g_io_channel_get_buffer_size (GIOChannel *channel);
250GLIB_AVAILABLE_IN_ALL
251GIOCondition g_io_channel_get_buffer_condition (GIOChannel *channel);
252GLIB_AVAILABLE_IN_ALL
253GIOStatus g_io_channel_set_flags (GIOChannel *channel,
254 GIOFlags flags,
255 GError **error);
256GLIB_AVAILABLE_IN_ALL
257GIOFlags g_io_channel_get_flags (GIOChannel *channel);
258GLIB_AVAILABLE_IN_ALL
259void g_io_channel_set_line_term (GIOChannel *channel,
260 const gchar *line_term,
261 gint length);
262GLIB_AVAILABLE_IN_ALL
263const gchar * g_io_channel_get_line_term (GIOChannel *channel,
264 gint *length);
265GLIB_AVAILABLE_IN_ALL
266void g_io_channel_set_buffered (GIOChannel *channel,
267 gboolean buffered);
268GLIB_AVAILABLE_IN_ALL
269gboolean g_io_channel_get_buffered (GIOChannel *channel);
270GLIB_AVAILABLE_IN_ALL
271GIOStatus g_io_channel_set_encoding (GIOChannel *channel,
272 const gchar *encoding,
273 GError **error);
274GLIB_AVAILABLE_IN_ALL
275const gchar * g_io_channel_get_encoding (GIOChannel *channel);
276GLIB_AVAILABLE_IN_ALL
277void g_io_channel_set_close_on_unref (GIOChannel *channel,
278 gboolean do_close);
279GLIB_AVAILABLE_IN_ALL
280gboolean g_io_channel_get_close_on_unref (GIOChannel *channel);
281
282
283GLIB_AVAILABLE_IN_ALL
284GIOStatus g_io_channel_flush (GIOChannel *channel,
285 GError **error);
286GLIB_AVAILABLE_IN_ALL
287GIOStatus g_io_channel_read_line (GIOChannel *channel,
288 gchar **str_return,
289 gsize *length,
290 gsize *terminator_pos,
291 GError **error);
292GLIB_AVAILABLE_IN_ALL
293GIOStatus g_io_channel_read_line_string (GIOChannel *channel,
294 GString *buffer,
295 gsize *terminator_pos,
296 GError **error);
297GLIB_AVAILABLE_IN_ALL
298GIOStatus g_io_channel_read_to_end (GIOChannel *channel,
299 gchar **str_return,
300 gsize *length,
301 GError **error);
302GLIB_AVAILABLE_IN_ALL
303GIOStatus g_io_channel_read_chars (GIOChannel *channel,
304 gchar *buf,
305 gsize count,
306 gsize *bytes_read,
307 GError **error);
308GLIB_AVAILABLE_IN_ALL
309GIOStatus g_io_channel_read_unichar (GIOChannel *channel,
310 gunichar *thechar,
311 GError **error);
312GLIB_AVAILABLE_IN_ALL
313GIOStatus g_io_channel_write_chars (GIOChannel *channel,
314 const gchar *buf,
315 gssize count,
316 gsize *bytes_written,
317 GError **error);
318GLIB_AVAILABLE_IN_ALL
319GIOStatus g_io_channel_write_unichar (GIOChannel *channel,
320 gunichar thechar,
321 GError **error);
322GLIB_AVAILABLE_IN_ALL
323GIOStatus g_io_channel_seek_position (GIOChannel *channel,
324 gint64 offset,
325 GSeekType type,
326 GError **error);
327GLIB_AVAILABLE_IN_ALL
328GIOChannel* g_io_channel_new_file (const gchar *filename,
329 const gchar *mode,
330 GError **error);
331
332/* Error handling */
333
334GLIB_AVAILABLE_IN_ALL
335GQuark g_io_channel_error_quark (void);
336GLIB_AVAILABLE_IN_ALL
337GIOChannelError g_io_channel_error_from_errno (gint en);
338
339/* On Unix, IO channels created with this function for any file
340 * descriptor or socket.
341 *
342 * On Win32, this can be used either for files opened with the MSVCRT
343 * (the Microsoft run-time C library) _open() or _pipe, including file
344 * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr),
345 * or for Winsock SOCKETs. If the parameter is a legal file
346 * descriptor, it is assumed to be such, otherwise it should be a
347 * SOCKET. This relies on SOCKETs and file descriptors not
348 * overlapping. If you want to be certain, call either
349 * g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket()
350 * instead as appropriate.
351 *
352 * The term file descriptor as used in the context of Win32 refers to
353 * the emulated Unix-like file descriptors MSVCRT provides. The native
354 * corresponding concept is file HANDLE. There isn't as of yet a way to
355 * get GIOChannels for Win32 file HANDLEs.
356 */
357GLIB_AVAILABLE_IN_ALL
358GIOChannel* g_io_channel_unix_new (int fd);
359GLIB_AVAILABLE_IN_ALL
360gint g_io_channel_unix_get_fd (GIOChannel *channel);
361
362
363/* Hook for GClosure / GSource integration. Don't touch */
364GLIB_VAR GSourceFuncs g_io_watch_funcs;
365
366#ifdef G_OS_WIN32
367
368/* You can use this "pseudo file descriptor" in a GPollFD to add
369 * polling for Windows messages. GTK applications should not do that.
370 */
371
372#define G_WIN32_MSG_HANDLE 19981206
373
374/* Use this to get a GPollFD from a GIOChannel, so that you can call
375 * g_io_channel_win32_poll(). After calling this you should only use
376 * g_io_channel_read() to read from the GIOChannel, i.e. never read()
377 * from the underlying file descriptor. For SOCKETs, it is possible to call
378 * recv().
379 */
380GLIB_AVAILABLE_IN_ALL
381void g_io_channel_win32_make_pollfd (GIOChannel *channel,
382 GIOCondition condition,
383 GPollFD *fd);
384
385/* This can be used to wait until at least one of the channels is readable.
386 * On Unix you would do a select() on the file descriptors of the channels.
387 */
388GLIB_AVAILABLE_IN_ALL
389gint g_io_channel_win32_poll (GPollFD *fds,
390 gint n_fds,
391 gint timeout_);
392
393/* Create an IO channel for Windows messages for window handle hwnd. */
394#if GLIB_SIZEOF_VOID_P == 8
395/* We use gsize here so that it is still an integer type and not a
396 * pointer, like the guint in the traditional prototype. We can't use
397 * intptr_t as that is not portable enough.
398 */
399GLIB_AVAILABLE_IN_ALL
400GIOChannel *g_io_channel_win32_new_messages (gsize hwnd);
401#else
402GLIB_AVAILABLE_IN_ALL
403GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
404#endif
405
406/* Create an IO channel for C runtime (emulated Unix-like) file
407 * descriptors. After calling g_io_add_watch() on an IO channel
408 * returned by this function, you shouldn't call read() on the file
409 * descriptor. This is because adding polling for a file descriptor is
410 * implemented on Win32 by starting a thread that sits blocked in a
411 * read() from the file descriptor most of the time. All reads from
412 * the file descriptor should be done by this internal GLib
413 * thread. Your code should call only g_io_channel_read_chars().
414 */
415GLIB_AVAILABLE_IN_ALL
416GIOChannel* g_io_channel_win32_new_fd (gint fd);
417
418/* Get the C runtime file descriptor of a channel. */
419GLIB_AVAILABLE_IN_ALL
420gint g_io_channel_win32_get_fd (GIOChannel *channel);
421
422/* Create an IO channel for a winsock socket. The parameter should be
423 * a SOCKET. Contrary to IO channels for file descriptors (on *Win32),
424 * you can use normal recv() or recvfrom() on sockets even if GLib
425 * is polling them.
426 */
427GLIB_AVAILABLE_IN_ALL
428GIOChannel *g_io_channel_win32_new_socket (gint socket);
429
430GLIB_DEPRECATED_FOR(g_io_channel_win32_new_socket)
431GIOChannel *g_io_channel_win32_new_stream_socket (gint socket);
432
433GLIB_AVAILABLE_IN_ALL
434void g_io_channel_win32_set_debug (GIOChannel *channel,
435 gboolean flag);
436
437#endif
438
439G_END_DECLS
440
441#endif /* __G_IOCHANNEL_H__ */
442