giochannel.h 14 KB

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