giochannel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #ifndef __G_IOCHANNEL_H__
  26. #define __G_IOCHANNEL_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <glib/gconvert.h>
  31. #include <glib/gmain.h>
  32. #include <glib/gstring.h>
  33. G_BEGIN_DECLS
  34. /* GIOChannel
  35. */
  36. typedef struct _GIOChannel GIOChannel;
  37. typedef struct _GIOFuncs GIOFuncs;
  38. typedef enum
  39. {
  40. G_IO_ERROR_NONE,
  41. G_IO_ERROR_AGAIN,
  42. G_IO_ERROR_INVAL,
  43. G_IO_ERROR_UNKNOWN
  44. } GIOError;
  45. #define G_IO_CHANNEL_ERROR g_io_channel_error_quark()
  46. typedef enum
  47. {
  48. /* Derived from errno */
  49. G_IO_CHANNEL_ERROR_FBIG,
  50. G_IO_CHANNEL_ERROR_INVAL,
  51. G_IO_CHANNEL_ERROR_IO,
  52. G_IO_CHANNEL_ERROR_ISDIR,
  53. G_IO_CHANNEL_ERROR_NOSPC,
  54. G_IO_CHANNEL_ERROR_NXIO,
  55. G_IO_CHANNEL_ERROR_OVERFLOW,
  56. G_IO_CHANNEL_ERROR_PIPE,
  57. /* Other */
  58. G_IO_CHANNEL_ERROR_FAILED
  59. } GIOChannelError;
  60. typedef enum
  61. {
  62. G_IO_STATUS_ERROR,
  63. G_IO_STATUS_NORMAL,
  64. G_IO_STATUS_EOF,
  65. G_IO_STATUS_AGAIN
  66. } GIOStatus;
  67. typedef enum
  68. {
  69. G_SEEK_CUR,
  70. G_SEEK_SET,
  71. G_SEEK_END
  72. } GSeekType;
  73. typedef enum
  74. {
  75. G_IO_FLAG_NONE GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
  76. G_IO_FLAG_APPEND = 1 << 0,
  77. G_IO_FLAG_NONBLOCK = 1 << 1,
  78. G_IO_FLAG_IS_READABLE = 1 << 2, /* Read only flag */
  79. G_IO_FLAG_IS_WRITABLE = 1 << 3, /* Read only flag */
  80. G_IO_FLAG_IS_WRITEABLE = 1 << 3, /* Misspelling in 2.29.10 and earlier */
  81. G_IO_FLAG_IS_SEEKABLE = 1 << 4, /* Read only flag */
  82. G_IO_FLAG_MASK = (1 << 5) - 1,
  83. G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
  84. G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
  85. } GIOFlags;
  86. struct _GIOChannel
  87. {
  88. /*< private >*/
  89. gint ref_count;
  90. GIOFuncs *funcs;
  91. gchar *encoding;
  92. GIConv read_cd;
  93. GIConv write_cd;
  94. gchar *line_term; /* String which indicates the end of a line of text */
  95. guint line_term_len; /* So we can have null in the line term */
  96. gsize buf_size;
  97. GString *read_buf; /* Raw data from the channel */
  98. GString *encoded_read_buf; /* Channel data converted to UTF-8 */
  99. GString *write_buf; /* Data ready to be written to the file */
  100. gchar partial_write_buf[6]; /* UTF-8 partial characters, null terminated */
  101. /* Group the flags together, immediately after partial_write_buf, to save memory */
  102. guint use_buffer : 1; /* The encoding uses the buffers */
  103. guint do_encode : 1; /* The encoding uses the GIConv converters */
  104. guint close_on_unref : 1; /* Close the channel on final unref */
  105. guint is_readable : 1; /* Cached GIOFlag */
  106. guint is_writeable : 1; /* ditto */
  107. guint is_seekable : 1; /* ditto */
  108. gpointer reserved1;
  109. gpointer reserved2;
  110. };
  111. typedef gboolean (*GIOFunc) (GIOChannel *source,
  112. GIOCondition condition,
  113. gpointer data);
  114. struct _GIOFuncs
  115. {
  116. GIOStatus (*io_read) (GIOChannel *channel,
  117. gchar *buf,
  118. gsize count,
  119. gsize *bytes_read,
  120. GError **err);
  121. GIOStatus (*io_write) (GIOChannel *channel,
  122. const gchar *buf,
  123. gsize count,
  124. gsize *bytes_written,
  125. GError **err);
  126. GIOStatus (*io_seek) (GIOChannel *channel,
  127. gint64 offset,
  128. GSeekType type,
  129. GError **err);
  130. GIOStatus (*io_close) (GIOChannel *channel,
  131. GError **err);
  132. GSource* (*io_create_watch) (GIOChannel *channel,
  133. GIOCondition condition);
  134. void (*io_free) (GIOChannel *channel);
  135. GIOStatus (*io_set_flags) (GIOChannel *channel,
  136. GIOFlags flags,
  137. GError **err);
  138. GIOFlags (*io_get_flags) (GIOChannel *channel);
  139. };
  140. GLIB_AVAILABLE_IN_ALL
  141. void g_io_channel_init (GIOChannel *channel);
  142. GLIB_AVAILABLE_IN_ALL
  143. GIOChannel *g_io_channel_ref (GIOChannel *channel);
  144. GLIB_AVAILABLE_IN_ALL
  145. void g_io_channel_unref (GIOChannel *channel);
  146. GLIB_DEPRECATED_FOR(g_io_channel_read_chars)
  147. GIOError g_io_channel_read (GIOChannel *channel,
  148. gchar *buf,
  149. gsize count,
  150. gsize *bytes_read);
  151. GLIB_DEPRECATED_FOR(g_io_channel_write_chars)
  152. GIOError g_io_channel_write (GIOChannel *channel,
  153. const gchar *buf,
  154. gsize count,
  155. gsize *bytes_written);
  156. GLIB_DEPRECATED_FOR(g_io_channel_seek_position)
  157. GIOError g_io_channel_seek (GIOChannel *channel,
  158. gint64 offset,
  159. GSeekType type);
  160. GLIB_DEPRECATED_FOR(g_io_channel_shutdown)
  161. void g_io_channel_close (GIOChannel *channel);
  162. GLIB_AVAILABLE_IN_ALL
  163. GIOStatus g_io_channel_shutdown (GIOChannel *channel,
  164. gboolean flush,
  165. GError **err);
  166. GLIB_AVAILABLE_IN_ALL
  167. guint g_io_add_watch_full (GIOChannel *channel,
  168. gint priority,
  169. GIOCondition condition,
  170. GIOFunc func,
  171. gpointer user_data,
  172. GDestroyNotify notify);
  173. GLIB_AVAILABLE_IN_ALL
  174. GSource * g_io_create_watch (GIOChannel *channel,
  175. GIOCondition condition);
  176. GLIB_AVAILABLE_IN_ALL
  177. guint g_io_add_watch (GIOChannel *channel,
  178. GIOCondition condition,
  179. GIOFunc func,
  180. gpointer user_data);
  181. /* character encoding conversion involved functions.
  182. */
  183. GLIB_AVAILABLE_IN_ALL
  184. void g_io_channel_set_buffer_size (GIOChannel *channel,
  185. gsize size);
  186. GLIB_AVAILABLE_IN_ALL
  187. gsize g_io_channel_get_buffer_size (GIOChannel *channel);
  188. GLIB_AVAILABLE_IN_ALL
  189. GIOCondition g_io_channel_get_buffer_condition (GIOChannel *channel);
  190. GLIB_AVAILABLE_IN_ALL
  191. GIOStatus g_io_channel_set_flags (GIOChannel *channel,
  192. GIOFlags flags,
  193. GError **error);
  194. GLIB_AVAILABLE_IN_ALL
  195. GIOFlags g_io_channel_get_flags (GIOChannel *channel);
  196. GLIB_AVAILABLE_IN_ALL
  197. void g_io_channel_set_line_term (GIOChannel *channel,
  198. const gchar *line_term,
  199. gint length);
  200. GLIB_AVAILABLE_IN_ALL
  201. const gchar * g_io_channel_get_line_term (GIOChannel *channel,
  202. gint *length);
  203. GLIB_AVAILABLE_IN_ALL
  204. void g_io_channel_set_buffered (GIOChannel *channel,
  205. gboolean buffered);
  206. GLIB_AVAILABLE_IN_ALL
  207. gboolean g_io_channel_get_buffered (GIOChannel *channel);
  208. GLIB_AVAILABLE_IN_ALL
  209. GIOStatus g_io_channel_set_encoding (GIOChannel *channel,
  210. const gchar *encoding,
  211. GError **error);
  212. GLIB_AVAILABLE_IN_ALL
  213. const gchar * g_io_channel_get_encoding (GIOChannel *channel);
  214. GLIB_AVAILABLE_IN_ALL
  215. void g_io_channel_set_close_on_unref (GIOChannel *channel,
  216. gboolean do_close);
  217. GLIB_AVAILABLE_IN_ALL
  218. gboolean g_io_channel_get_close_on_unref (GIOChannel *channel);
  219. GLIB_AVAILABLE_IN_ALL
  220. GIOStatus g_io_channel_flush (GIOChannel *channel,
  221. GError **error);
  222. GLIB_AVAILABLE_IN_ALL
  223. GIOStatus g_io_channel_read_line (GIOChannel *channel,
  224. gchar **str_return,
  225. gsize *length,
  226. gsize *terminator_pos,
  227. GError **error);
  228. GLIB_AVAILABLE_IN_ALL
  229. GIOStatus g_io_channel_read_line_string (GIOChannel *channel,
  230. GString *buffer,
  231. gsize *terminator_pos,
  232. GError **error);
  233. GLIB_AVAILABLE_IN_ALL
  234. GIOStatus g_io_channel_read_to_end (GIOChannel *channel,
  235. gchar **str_return,
  236. gsize *length,
  237. GError **error);
  238. GLIB_AVAILABLE_IN_ALL
  239. GIOStatus g_io_channel_read_chars (GIOChannel *channel,
  240. gchar *buf,
  241. gsize count,
  242. gsize *bytes_read,
  243. GError **error);
  244. GLIB_AVAILABLE_IN_ALL
  245. GIOStatus g_io_channel_read_unichar (GIOChannel *channel,
  246. gunichar *thechar,
  247. GError **error);
  248. GLIB_AVAILABLE_IN_ALL
  249. GIOStatus g_io_channel_write_chars (GIOChannel *channel,
  250. const gchar *buf,
  251. gssize count,
  252. gsize *bytes_written,
  253. GError **error);
  254. GLIB_AVAILABLE_IN_ALL
  255. GIOStatus g_io_channel_write_unichar (GIOChannel *channel,
  256. gunichar thechar,
  257. GError **error);
  258. GLIB_AVAILABLE_IN_ALL
  259. GIOStatus g_io_channel_seek_position (GIOChannel *channel,
  260. gint64 offset,
  261. GSeekType type,
  262. GError **error);
  263. GLIB_AVAILABLE_IN_ALL
  264. GIOChannel* g_io_channel_new_file (const gchar *filename,
  265. const gchar *mode,
  266. GError **error);
  267. /* Error handling */
  268. GLIB_AVAILABLE_IN_ALL
  269. GQuark g_io_channel_error_quark (void);
  270. GLIB_AVAILABLE_IN_ALL
  271. GIOChannelError g_io_channel_error_from_errno (gint en);
  272. /* On Unix, IO channels created with this function for any file
  273. * descriptor or socket.
  274. *
  275. * On Win32, this can be used either for files opened with the MSVCRT
  276. * (the Microsoft run-time C library) _open() or _pipe, including file
  277. * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr),
  278. * or for Winsock SOCKETs. If the parameter is a legal file
  279. * descriptor, it is assumed to be such, otherwise it should be a
  280. * SOCKET. This relies on SOCKETs and file descriptors not
  281. * overlapping. If you want to be certain, call either
  282. * g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket()
  283. * instead as appropriate.
  284. *
  285. * The term file descriptor as used in the context of Win32 refers to
  286. * the emulated Unix-like file descriptors MSVCRT provides. The native
  287. * corresponding concept is file HANDLE. There isn't as of yet a way to
  288. * get GIOChannels for Win32 file HANDLEs.
  289. */
  290. GLIB_AVAILABLE_IN_ALL
  291. GIOChannel* g_io_channel_unix_new (int fd);
  292. GLIB_AVAILABLE_IN_ALL
  293. gint g_io_channel_unix_get_fd (GIOChannel *channel);
  294. /* Hook for GClosure / GSource integration. Don't touch */
  295. GLIB_VAR GSourceFuncs g_io_watch_funcs;
  296. #ifdef G_OS_WIN32
  297. /* You can use this "pseudo file descriptor" in a GPollFD to add
  298. * polling for Windows messages. GTK applications should not do that.
  299. */
  300. #define G_WIN32_MSG_HANDLE 19981206
  301. /* Use this to get a GPollFD from a GIOChannel, so that you can call
  302. * g_io_channel_win32_poll(). After calling this you should only use
  303. * g_io_channel_read() to read from the GIOChannel, i.e. never read()
  304. * from the underlying file descriptor. For SOCKETs, it is possible to call
  305. * recv().
  306. */
  307. GLIB_AVAILABLE_IN_ALL
  308. void g_io_channel_win32_make_pollfd (GIOChannel *channel,
  309. GIOCondition condition,
  310. GPollFD *fd);
  311. /* This can be used to wait until at least one of the channels is readable.
  312. * On Unix you would do a select() on the file descriptors of the channels.
  313. */
  314. GLIB_AVAILABLE_IN_ALL
  315. gint g_io_channel_win32_poll (GPollFD *fds,
  316. gint n_fds,
  317. gint timeout_);
  318. /* Create an IO channel for Windows messages for window handle hwnd. */
  319. #if GLIB_SIZEOF_VOID_P == 8
  320. /* We use gsize here so that it is still an integer type and not a
  321. * pointer, like the guint in the traditional prototype. We can't use
  322. * intptr_t as that is not portable enough.
  323. */
  324. GLIB_AVAILABLE_IN_ALL
  325. GIOChannel *g_io_channel_win32_new_messages (gsize hwnd);
  326. #else
  327. GLIB_AVAILABLE_IN_ALL
  328. GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
  329. #endif
  330. /* Create an IO channel for C runtime (emulated Unix-like) file
  331. * descriptors. After calling g_io_add_watch() on an IO channel
  332. * returned by this function, you shouldn't call read() on the file
  333. * descriptor. This is because adding polling for a file descriptor is
  334. * implemented on Win32 by starting a thread that sits blocked in a
  335. * read() from the file descriptor most of the time. All reads from
  336. * the file descriptor should be done by this internal GLib
  337. * thread. Your code should call only g_io_channel_read_chars().
  338. */
  339. GLIB_AVAILABLE_IN_ALL
  340. GIOChannel* g_io_channel_win32_new_fd (gint fd);
  341. /* Get the C runtime file descriptor of a channel. */
  342. GLIB_AVAILABLE_IN_ALL
  343. gint g_io_channel_win32_get_fd (GIOChannel *channel);
  344. /* Create an IO channel for a winsock socket. The parameter should be
  345. * a SOCKET. Contrary to IO channels for file descriptors (on *Win32),
  346. * you can use normal recv() or recvfrom() on sockets even if GLib
  347. * is polling them.
  348. */
  349. GLIB_AVAILABLE_IN_ALL
  350. GIOChannel *g_io_channel_win32_new_socket (gint socket);
  351. GLIB_DEPRECATED_FOR(g_io_channel_win32_new_socket)
  352. GIOChannel *g_io_channel_win32_new_stream_socket (gint socket);
  353. GLIB_AVAILABLE_IN_ALL
  354. void g_io_channel_win32_set_debug (GIOChannel *channel,
  355. gboolean flag);
  356. #endif
  357. G_END_DECLS
  358. #endif /* __G_IOCHANNEL_H__ */