giochannel.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. {*
  2. * giochannel.inc
  3. *
  4. * depends on gconvert.inc, gmain.inc, gstring.inc
  5. *}
  6. type
  7. PGIOError = ^TGIOError;
  8. TGIOError = (G_IO_ERROR_NONE,
  9. G_IO_ERROR_AGAIN,
  10. G_IO_ERROR_INVAL,
  11. G_IO_ERROR_UNKNOWN);
  12. function G_IO_CHANNEL_ERROR : TGQuark;
  13. { Derived from errno }
  14. { Other }
  15. type
  16. PGIOChannelError = ^TGIOChannelError;
  17. TGIOChannelError = (G_IO_CHANNEL_ERROR_FBIG,
  18. G_IO_CHANNEL_ERROR_INVAL,
  19. G_IO_CHANNEL_ERROR_IO,
  20. G_IO_CHANNEL_ERROR_ISDIR,
  21. G_IO_CHANNEL_ERROR_NOSPC,
  22. G_IO_CHANNEL_ERROR_NXIO,
  23. G_IO_CHANNEL_ERROR_OVERFLOW,
  24. G_IO_CHANNEL_ERROR_PIPE,
  25. G_IO_CHANNEL_ERROR_FAILED);
  26. PGIOStatus = ^TGIOStatus;
  27. TGIOStatus = (G_IO_STATUS_ERROR,
  28. G_IO_STATUS_NORMAL,
  29. G_IO_STATUS_EOF,
  30. G_IO_STATUS_AGAIN);
  31. PGSeekType = ^TGSeekType;
  32. TGSeekType = (G_SEEK_CUR,
  33. G_SEEK_SET,
  34. G_SEEK_END);
  35. PGIOCondition = ^TGIOCondition;
  36. TGIOCondition = gint;
  37. const
  38. G_IO_IN = GLIB_SYSDEF_POLLIN;
  39. G_IO_OUT = GLIB_SYSDEF_POLLOUT;
  40. G_IO_PRI = GLIB_SYSDEF_POLLPRI;
  41. G_IO_ERR = GLIB_SYSDEF_POLLERR;
  42. G_IO_HUP = GLIB_SYSDEF_POLLHUP;
  43. G_IO_NVAL = GLIB_SYSDEF_POLLNVAL;
  44. type
  45. PGIOFlags = ^TGIOFlags;
  46. TGIOFlags = gint;
  47. const
  48. G_IO_FLAG_APPEND = 1 shl 0;
  49. G_IO_FLAG_NONBLOCK = 1 shl 1;
  50. G_IO_FLAG_IS_READABLE = 1 shl 2;
  51. G_IO_FLAG_IS_WRITEABLE = 1 shl 3;
  52. G_IO_FLAG_IS_SEEKABLE = 1 shl 4;
  53. G_IO_FLAG_MASK = (1 shl 5) - 1;
  54. G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK;
  55. G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND or G_IO_FLAG_NONBLOCK;
  56. type
  57. {< private > }
  58. PGIOChannel = ^TGIOChannel;
  59. TGIOFunc = function (source:PGIOChannel; condition:TGIOCondition; data:gpointer):gboolean;cdecl;
  60. PGIOFuncs = ^TGIOFuncs;
  61. TGIOFuncs = record
  62. io_read : function (channel:PGIOChannel; buf:Pgchar; count:gsize; bytes_read:Pgsize; err:PPGError):TGIOStatus;cdecl;
  63. io_write : function (channel:PGIOChannel; buf:Pgchar; count:gsize; bytes_written:Pgsize; err:PPGError):TGIOStatus;cdecl;
  64. io_seek : function (channel:PGIOChannel; offset:gint64; _type:TGSeekType; err:PPGError):TGIOStatus;cdecl;
  65. io_close : function (channel:PGIOChannel; err:PPGError):TGIOStatus;cdecl;
  66. io_create_watch : function (channel:PGIOChannel; condition:TGIOCondition):PGSource;cdecl;
  67. io_free : procedure (channel:PGIOChannel);cdecl;
  68. io_set_flags : function (channel:PGIOChannel; flags:TGIOFlags; err:PPGError):TGIOStatus;cdecl;
  69. io_get_flags : function (channel:PGIOChannel):TGIOFlags;cdecl;
  70. end;
  71. TGIOChannel = record
  72. ref_count : guint;
  73. funcs : PGIOFuncs;
  74. encoding : Pgchar;
  75. read_cd : TGIConv;
  76. write_cd : TGIConv;
  77. line_term : Pgchar; // String which indicates the end of a line of text
  78. line_term_len : guint; // So we can have null in the line term
  79. buf_size : gsize;
  80. read_buf : PGString; // Raw data from the channel
  81. encoded_read_buf : PGString; // Channel data converted to UTF-8
  82. write_buf : PGString; // Data ready to be written to the file
  83. partial_write_buf : array[0..5] of gchar; // UTF-8 partial characters, null terminated
  84. { Group the flags together, immediately after partial_write_buf, to save memory }
  85. flag0 : word;
  86. reserved1 : gpointer;
  87. reserved2 : gpointer;
  88. end;
  89. const
  90. bm_TGIOChannel_use_buffer = $1;
  91. bp_TGIOChannel_use_buffer = 0;
  92. bm_TGIOChannel_do_encode = $2;
  93. bp_TGIOChannel_do_encode = 1;
  94. bm_TGIOChannel_close_on_unref = $4;
  95. bp_TGIOChannel_close_on_unref = 2;
  96. bm_TGIOChannel_is_readable = $8;
  97. bp_TGIOChannel_is_readable = 3;
  98. bm_TGIOChannel_is_writeable = $10;
  99. bp_TGIOChannel_is_writeable = 4;
  100. bm_TGIOChannel_is_seekable = $20;
  101. bp_TGIOChannel_is_seekable = 5;
  102. { use this to get/set information to the TGIOChannel struct}
  103. function TGIOChannel_use_buffer (var a : TGIOChannel) : guint;
  104. procedure TGIOChannel_set_use_buffer (var a : TGIOChannel; __use_buffer : guint);
  105. function TGIOChannel_do_encode (var a : TGIOChannel) : guint;
  106. procedure TGIOChannel_set_do_encode (var a : TGIOChannel; __do_encode : guint);
  107. function TGIOChannel_close_on_unref (var a : TGIOChannel) : guint;
  108. procedure TGIOChannel_set_close_on_unref (var a : TGIOChannel; __close_on_unref : guint);
  109. function TGIOChannel_is_readable (var a : TGIOChannel) : guint;
  110. procedure TGIOChannel_set_is_readable (var a : TGIOChannel; __is_readable : guint);
  111. function TGIOChannel_is_writeable (var a : TGIOChannel) : guint;
  112. procedure TGIOChannel_set_is_writeable (var a : TGIOChannel; __is_writeable : guint);
  113. function TGIOChannel_is_seekable (var a : TGIOChannel) : guint;
  114. procedure TGIOChannel_set_is_seekable (var a : TGIOChannel; __is_seekable : guint);
  115. procedure g_io_channel_init(channel:PGIOChannel);cdecl;external gliblib name 'g_io_channel_init';
  116. procedure g_io_channel_ref(channel:PGIOChannel);cdecl;external gliblib name 'g_io_channel_ref';
  117. procedure g_io_channel_unref(channel:PGIOChannel);cdecl;external gliblib name 'g_io_channel_unref';
  118. {DEPRECATED functions}
  119. function g_io_channel_read(channel:PGIOChannel; buf:Pgchar; count:gsize; bytes_read:Pgsize):TGIOError;cdecl;external gliblib name 'g_io_channel_read';
  120. function g_io_channel_write(channel:PGIOChannel; buf:Pgchar; count:gsize; bytes_written:Pgsize):TGIOError;cdecl;external gliblib name 'g_io_channel_write';
  121. function g_io_channel_seek(channel:PGIOChannel; offset:gint64; _type:TGSeekType):TGIOError;cdecl;external gliblib name 'g_io_channel_seek';
  122. procedure g_io_channel_close(channel:PGIOChannel);cdecl;external gliblib name 'g_io_channel_close';
  123. {end of DEPRECATED functions}
  124. function g_io_channel_shutdown(channel:PGIOChannel; flush:gboolean; err:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_shutdown';
  125. function g_io_add_watch_full(channel:PGIOChannel; priority:gint; condition:TGIOCondition; func:TGIOFunc; user_data:gpointer;
  126. notify:TGDestroyNotify):guint;cdecl;external gliblib name 'g_io_add_watch_full';
  127. function g_io_create_watch(channel:PGIOChannel; condition:TGIOCondition):PGSource;cdecl;external gliblib name 'g_io_create_watch';
  128. function g_io_add_watch(channel:PGIOChannel; condition:TGIOCondition; func:TGIOFunc; user_data:gpointer):guint;cdecl;external gliblib name 'g_io_add_watch';
  129. { character encoding conversion involved functions.
  130. }
  131. procedure g_io_channel_set_buffer_size(channel:PGIOChannel; size:gsize);cdecl;external gliblib name 'g_io_channel_set_buffer_size';
  132. function g_io_channel_get_buffer_size(channel:PGIOChannel):gsize;cdecl;external gliblib name 'g_io_channel_get_buffer_size';
  133. function g_io_channel_get_buffer_condition(channel:PGIOChannel):TGIOCondition;cdecl;external gliblib name 'g_io_channel_get_buffer_condition';
  134. function g_io_channel_set_flags(channel:PGIOChannel; flags:TGIOFlags; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_set_flags';
  135. function g_io_channel_get_flags(channel:PGIOChannel):TGIOFlags;cdecl;external gliblib name 'g_io_channel_get_flags';
  136. procedure g_io_channel_set_line_term(channel:PGIOChannel; line_term:Pgchar; length:gint);cdecl;external gliblib name 'g_io_channel_set_line_term';
  137. function g_io_channel_get_line_term(channel:PGIOChannel; length:Pgint):pgchar;cdecl;external gliblib name 'g_io_channel_get_line_term';
  138. procedure g_io_channel_set_buffered(channel:PGIOChannel; buffered:gboolean);cdecl;external gliblib name 'g_io_channel_set_buffered';
  139. function g_io_channel_get_buffered(channel:PGIOChannel):gboolean;cdecl;external gliblib name 'g_io_channel_get_buffered';
  140. function g_io_channel_set_encoding(channel:PGIOChannel; encoding:Pgchar; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_set_encoding';
  141. function g_io_channel_get_encoding(channel:PGIOChannel):pgchar;cdecl;external gliblib name 'g_io_channel_get_encoding';
  142. procedure g_io_channel_set_close_on_unref(channel:PGIOChannel; do_close:gboolean);cdecl;external gliblib name 'g_io_channel_set_close_on_unref';
  143. function g_io_channel_get_close_on_unref(channel:PGIOChannel):gboolean;cdecl;external gliblib name 'g_io_channel_get_close_on_unref';
  144. function g_io_channel_flush(channel:PGIOChannel; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_flush';
  145. function g_io_channel_read_line(channel:PGIOChannel; str_return:PPgchar; length:Pgsize; terminator_pos:Pgsize; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_read_line';
  146. function g_io_channel_read_line_string(channel:PGIOChannel; buffer:PGString; terminator_pos:Pgsize; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_read_line_string';
  147. function g_io_channel_read_to_end(channel:PGIOChannel; str_return:PPgchar; length:Pgsize; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_read_to_end';
  148. function g_io_channel_read_chars(channel:PGIOChannel; buf:Pgchar; count:gsize; bytes_read:Pgsize; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_read_chars';
  149. function g_io_channel_read_unichar(channel:PGIOChannel; thechar:Pgunichar; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_read_unichar';
  150. function g_io_channel_write_chars(channel:PGIOChannel; buf:Pgchar; count:gssize; bytes_written:Pgsize; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_write_chars';
  151. function g_io_channel_write_unichar(channel:PGIOChannel; thechar:gunichar; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_write_unichar';
  152. function g_io_channel_seek_position(channel:PGIOChannel; offset:gint64; _type:TGSeekType; error:PPGError):TGIOStatus;cdecl;external gliblib name 'g_io_channel_seek_position';
  153. function g_io_channel_new_file(filename:Pgchar; mode:Pgchar; error:PPGError):PGIOChannel;cdecl;external gliblib name 'g_io_channel_new_file';
  154. { Error handling }
  155. function g_io_channel_error_quark:TGQuark;cdecl;external gliblib name 'g_io_channel_error_quark';
  156. function g_io_channel_error_from_errno(en:gint):TGIOChannelError;cdecl;external gliblib name 'g_io_channel_error_from_errno';
  157. { On Unix, IO channels created with this function for any file
  158. descriptor or socket.
  159. On Win32, this can be used either for files opened with the MSVCRT
  160. (the Microsoft run-time C library) _open() or _pipe, including file
  161. descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr),
  162. or for Winsock SOCKETs. If the parameter is a legal file
  163. descriptor, it is assumed to be such, otherwise it should be a
  164. SOCKET. This relies on SOCKETs and file descriptors not
  165. overlapping. If you want to be certain, call either
  166. g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket()
  167. instead as appropriate.
  168. The term file descriptor as used in the context of Win32 refers to
  169. the emulated Unix-like file descriptors MSVCRT provides. The native
  170. corresponding concept is file HANDLE. There isn't as of yet a way to
  171. get GIOChannels for Win32 file HANDLEs.
  172. }
  173. function g_io_channel_unix_new(fd:longint):PGIOChannel;cdecl;external gliblib name 'g_io_channel_unix_new';
  174. function g_io_channel_unix_get_fd(channel:PGIOChannel):gint;cdecl;external gliblib name 'g_io_channel_unix_get_fd';
  175. { Hook for GClosure / GSource integration. Don't touch }
  176. //GLIB_VAR GSourceFuncs g_io_watch_funcs;
  177. {$ifdef WIN32}
  178. const
  179. G_WIN32_MSG_HANDLE = 19981206;
  180. { Use this to get a GPollFD from a GIOChannel, so that you can call
  181. g_io_channel_win32_poll(). After calling this you should only use
  182. g_io_channel_read() to read from the GIOChannel, i.e. never read()
  183. from the underlying file descriptor. For SOCKETs, it is possible to call
  184. recv().
  185. }
  186. procedure g_io_channel_win32_make_pollfd(channel:PGIOChannel; condition:TGIOCondition; fd:PGPollFD);cdecl;external gliblib name 'g_io_channel_win32_make_pollfd';
  187. { This can be used to wait a until at least one of the channels is readable.
  188. On Unix you would do a select() on the file descriptors of the channels.
  189. }
  190. function g_io_channel_win32_poll(fds:PGPollFD; n_fds:gint; timeout:gint):gint;cdecl;external gliblib name 'g_io_channel_win32_poll';
  191. { This is used to add polling for Windows messages. GDK (GTk+) programs
  192. should not use this.
  193. }
  194. // procedure g_main_poll_win32_msg_add(priority:gint; fd:PGPollFD; hwnd:guint);cdecl;external gliblib name 'g_main_poll_win32_msg_add';
  195. //
  196. //NOTE: already defined in gmain.inc
  197. { Create an IO channel for Windows messages for window handle hwnd. }
  198. function g_io_channel_win32_new_messages(hwnd:guint):PGIOChannel;cdecl;external gliblib name 'g_io_channel_win32_new_messages';
  199. { Create an IO channel for C runtime (emulated Unix-like) file
  200. descriptors. After calling g_io_add_watch() on a IO channel
  201. returned by this function, you shouldn't call read() on the file
  202. descriptor. This is because adding polling for a file descriptor is
  203. implemented on Win32 by starting a thread that sits blocked in a
  204. read() from the file descriptor most of the time. All reads from
  205. the file descriptor should be done by this internal GLib
  206. thread. Your code should call only g_io_channel_read().
  207. }
  208. function g_io_channel_win32_new_fd(fd:gint):PGIOChannel;cdecl;external gliblib name 'g_io_channel_win32_new_fd';
  209. { Get the C runtime file descriptor of a channel. }
  210. function g_io_channel_win32_get_fd(channel:PGIOChannel):gint;cdecl;external gliblib name 'g_io_channel_win32_get_fd';
  211. { Create an IO channel for a winsock socket. The parameter should be
  212. a SOCKET. Contrary to IO channels for file descriptors (on Win32),
  213. you can use normal recv() or recvfrom() on sockets even if GLib
  214. is polling them.
  215. }
  216. function g_io_channel_win32_new_socket(socket:gint):PGIOChannel;cdecl;external gliblib name 'g_io_channel_win32_new_socket';
  217. {$endif}