Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

367 řádky
12 KiB

  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, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  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. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #ifndef __G_IOCHANNEL_H__
  29. #define __G_IOCHANNEL_H__
  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_IN GLIB_SYSDEF_POLLIN,
  76. G_IO_OUT GLIB_SYSDEF_POLLOUT,
  77. G_IO_PRI GLIB_SYSDEF_POLLPRI,
  78. G_IO_ERR GLIB_SYSDEF_POLLERR,
  79. G_IO_HUP GLIB_SYSDEF_POLLHUP,
  80. G_IO_NVAL GLIB_SYSDEF_POLLNVAL
  81. } GIOCondition;
  82. typedef enum
  83. {
  84. G_IO_FLAG_APPEND = 1 << 0,
  85. G_IO_FLAG_NONBLOCK = 1 << 1,
  86. G_IO_FLAG_IS_READABLE = 1 << 2, /* Read only flag */
  87. G_IO_FLAG_IS_WRITEABLE = 1 << 3, /* Read only flag */
  88. G_IO_FLAG_IS_SEEKABLE = 1 << 4, /* Read only flag */
  89. G_IO_FLAG_MASK = (1 << 5) - 1,
  90. G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
  91. G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
  92. } GIOFlags;
  93. struct _GIOChannel
  94. {
  95. /*< private >*/
  96. gint ref_count;
  97. GIOFuncs *funcs;
  98. gchar *encoding;
  99. GIConv read_cd;
  100. GIConv write_cd;
  101. gchar *line_term; /* String which indicates the end of a line of text */
  102. guint line_term_len; /* So we can have null in the line term */
  103. gsize buf_size;
  104. GString *read_buf; /* Raw data from the channel */
  105. GString *encoded_read_buf; /* Channel data converted to UTF-8 */
  106. GString *write_buf; /* Data ready to be written to the file */
  107. gchar partial_write_buf[6]; /* UTF-8 partial characters, null terminated */
  108. /* Group the flags together, immediately after partial_write_buf, to save memory */
  109. guint use_buffer : 1; /* The encoding uses the buffers */
  110. guint do_encode : 1; /* The encoding uses the GIConv coverters */
  111. guint close_on_unref : 1; /* Close the channel on final unref */
  112. guint is_readable : 1; /* Cached GIOFlag */
  113. guint is_writeable : 1; /* ditto */
  114. guint is_seekable : 1; /* ditto */
  115. gpointer reserved1;
  116. gpointer reserved2;
  117. };
  118. typedef gboolean (*GIOFunc) (GIOChannel *source,
  119. GIOCondition condition,
  120. gpointer data);
  121. struct _GIOFuncs
  122. {
  123. GIOStatus (*io_read) (GIOChannel *channel,
  124. gchar *buf,
  125. gsize count,
  126. gsize *bytes_read,
  127. GError **err);
  128. GIOStatus (*io_write) (GIOChannel *channel,
  129. const gchar *buf,
  130. gsize count,
  131. gsize *bytes_written,
  132. GError **err);
  133. GIOStatus (*io_seek) (GIOChannel *channel,
  134. gint64 offset,
  135. GSeekType type,
  136. GError **err);
  137. GIOStatus (*io_close) (GIOChannel *channel,
  138. GError **err);
  139. GSource* (*io_create_watch) (GIOChannel *channel,
  140. GIOCondition condition);
  141. void (*io_free) (GIOChannel *channel);
  142. GIOStatus (*io_set_flags) (GIOChannel *channel,
  143. GIOFlags flags,
  144. GError **err);
  145. GIOFlags (*io_get_flags) (GIOChannel *channel);
  146. };
  147. void g_io_channel_init (GIOChannel *channel);
  148. GIOChannel *g_io_channel_ref (GIOChannel *channel);
  149. void g_io_channel_unref (GIOChannel *channel);
  150. #ifndef G_DISABLE_DEPRECATED
  151. GIOError g_io_channel_read (GIOChannel *channel,
  152. gchar *buf,
  153. gsize count,
  154. gsize *bytes_read);
  155. GIOError g_io_channel_write (GIOChannel *channel,
  156. const gchar *buf,
  157. gsize count,
  158. gsize *bytes_written);
  159. GIOError g_io_channel_seek (GIOChannel *channel,
  160. gint64 offset,
  161. GSeekType type);
  162. void g_io_channel_close (GIOChannel *channel);
  163. #endif /* G_DISABLE_DEPRECATED */
  164. GIOStatus g_io_channel_shutdown (GIOChannel *channel,
  165. gboolean flush,
  166. GError **err);
  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. GSource * g_io_create_watch (GIOChannel *channel,
  174. GIOCondition condition);
  175. guint g_io_add_watch (GIOChannel *channel,
  176. GIOCondition condition,
  177. GIOFunc func,
  178. gpointer user_data);
  179. /* character encoding conversion involved functions.
  180. */
  181. void g_io_channel_set_buffer_size (GIOChannel *channel,
  182. gsize size);
  183. gsize g_io_channel_get_buffer_size (GIOChannel *channel);
  184. GIOCondition g_io_channel_get_buffer_condition (GIOChannel *channel);
  185. GIOStatus g_io_channel_set_flags (GIOChannel *channel,
  186. GIOFlags flags,
  187. GError **error);
  188. GIOFlags g_io_channel_get_flags (GIOChannel *channel);
  189. void g_io_channel_set_line_term (GIOChannel *channel,
  190. const gchar *line_term,
  191. gint length);
  192. G_CONST_RETURN gchar* g_io_channel_get_line_term (GIOChannel *channel,
  193. gint *length);
  194. void g_io_channel_set_buffered (GIOChannel *channel,
  195. gboolean buffered);
  196. gboolean g_io_channel_get_buffered (GIOChannel *channel);
  197. GIOStatus g_io_channel_set_encoding (GIOChannel *channel,
  198. const gchar *encoding,
  199. GError **error);
  200. G_CONST_RETURN gchar* g_io_channel_get_encoding (GIOChannel *channel);
  201. void g_io_channel_set_close_on_unref (GIOChannel *channel,
  202. gboolean do_close);
  203. gboolean g_io_channel_get_close_on_unref (GIOChannel *channel);
  204. GIOStatus g_io_channel_flush (GIOChannel *channel,
  205. GError **error);
  206. GIOStatus g_io_channel_read_line (GIOChannel *channel,
  207. gchar **str_return,
  208. gsize *length,
  209. gsize *terminator_pos,
  210. GError **error);
  211. GIOStatus g_io_channel_read_line_string (GIOChannel *channel,
  212. GString *buffer,
  213. gsize *terminator_pos,
  214. GError **error);
  215. GIOStatus g_io_channel_read_to_end (GIOChannel *channel,
  216. gchar **str_return,
  217. gsize *length,
  218. GError **error);
  219. GIOStatus g_io_channel_read_chars (GIOChannel *channel,
  220. gchar *buf,
  221. gsize count,
  222. gsize *bytes_read,
  223. GError **error);
  224. GIOStatus g_io_channel_read_unichar (GIOChannel *channel,
  225. gunichar *thechar,
  226. GError **error);
  227. GIOStatus g_io_channel_write_chars (GIOChannel *channel,
  228. const gchar *buf,
  229. gssize count,
  230. gsize *bytes_written,
  231. GError **error);
  232. GIOStatus g_io_channel_write_unichar (GIOChannel *channel,
  233. gunichar thechar,
  234. GError **error);
  235. GIOStatus g_io_channel_seek_position (GIOChannel *channel,
  236. gint64 offset,
  237. GSeekType type,
  238. GError **error);
  239. #ifdef G_OS_WIN32
  240. #define g_io_channel_new_file g_io_channel_new_file_utf8
  241. #endif
  242. GIOChannel* g_io_channel_new_file (const gchar *filename,
  243. const gchar *mode,
  244. GError **error);
  245. /* Error handling */
  246. GQuark g_io_channel_error_quark (void);
  247. GIOChannelError g_io_channel_error_from_errno (gint en);
  248. /* On Unix, IO channels created with this function for any file
  249. * descriptor or socket.
  250. *
  251. * On Win32, this can be used either for files opened with the MSVCRT
  252. * (the Microsoft run-time C library) _open() or _pipe, including file
  253. * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr),
  254. * or for Winsock SOCKETs. If the parameter is a legal file
  255. * descriptor, it is assumed to be such, otherwise it should be a
  256. * SOCKET. This relies on SOCKETs and file descriptors not
  257. * overlapping. If you want to be certain, call either
  258. * g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket()
  259. * instead as appropriate.
  260. *
  261. * The term file descriptor as used in the context of Win32 refers to
  262. * the emulated Unix-like file descriptors MSVCRT provides. The native
  263. * corresponding concept is file HANDLE. There isn't as of yet a way to
  264. * get GIOChannels for Win32 file HANDLEs.
  265. */
  266. GIOChannel* g_io_channel_unix_new (int fd);
  267. gint g_io_channel_unix_get_fd (GIOChannel *channel);
  268. /* Hook for GClosure / GSource integration. Don't touch */
  269. GLIB_VAR GSourceFuncs g_io_watch_funcs;
  270. #ifdef G_OS_WIN32
  271. /* You can use this "pseudo file descriptor" in a GPollFD to add
  272. * polling for Windows messages. GTK applications should not do that.
  273. */
  274. #define G_WIN32_MSG_HANDLE 19981206
  275. /* Use this to get a GPollFD from a GIOChannel, so that you can call
  276. * g_io_channel_win32_poll(). After calling this you should only use
  277. * g_io_channel_read() to read from the GIOChannel, i.e. never read()
  278. * from the underlying file descriptor. For SOCKETs, it is possible to call
  279. * recv().
  280. */
  281. void g_io_channel_win32_make_pollfd (GIOChannel *channel,
  282. GIOCondition condition,
  283. GPollFD *fd);
  284. /* This can be used to wait a until at least one of the channels is readable.
  285. * On Unix you would do a select() on the file descriptors of the channels.
  286. */
  287. gint g_io_channel_win32_poll (GPollFD *fds,
  288. gint n_fds,
  289. gint timeout_);
  290. /* Create an IO channel for Windows messages for window handle hwnd. */
  291. #if GLIB_SIZEOF_VOID_P == 8
  292. /* We use gsize here so that it is still an integer type and not a
  293. * pointer, like the guint in the traditional prototype. We can't use
  294. * intptr_t as that is not portable enough.
  295. */
  296. GIOChannel *g_io_channel_win32_new_messages (gsize hwnd);
  297. #else
  298. GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
  299. #endif
  300. /* Create an IO channel for C runtime (emulated Unix-like) file
  301. * descriptors. After calling g_io_add_watch() on a IO channel
  302. * returned by this function, you shouldn't call read() on the file
  303. * descriptor. This is because adding polling for a file descriptor is
  304. * implemented on Win32 by starting a thread that sits blocked in a
  305. * read() from the file descriptor most of the time. All reads from
  306. * the file descriptor should be done by this internal GLib
  307. * thread. Your code should call only g_io_channel_read_chars().
  308. */
  309. GIOChannel* g_io_channel_win32_new_fd (gint fd);
  310. /* Get the C runtime file descriptor of a channel. */
  311. gint g_io_channel_win32_get_fd (GIOChannel *channel);
  312. /* Create an IO channel for a winsock socket. The parameter should be
  313. * a SOCKET. Contrary to IO channels for file descriptors (on *Win32),
  314. * you can use normal recv() or recvfrom() on sockets even if GLib
  315. * is polling them.
  316. */
  317. GIOChannel *g_io_channel_win32_new_socket (gint socket);
  318. #endif
  319. G_END_DECLS
  320. #endif /* __G_IOCHANNEL_H__ */