1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  *    (See accompanying file LICENSE_1_0.txt or copy at
4  *          http://www.boost.org/LICENSE_1_0.txt)
5  */
6 module glib.giochannel;
7 
8 import glib.gtypes;
9 import glib.gconvert;
10 import glib.gmain;
11 import glib.gstring;
12 import glib.gerror;
13 import glib.gunicode;
14 import glib.gpoll;
15 
16 
17 enum GIOError
18 {
19   G_IO_ERROR_NONE,
20   G_IO_ERROR_AGAIN,
21   G_IO_ERROR_INVAL,
22   G_IO_ERROR_UNKNOWN
23 }
24 
25 //enum G_IO_CHANNEL_ERROR = g_io_channel_error_quark();
26 
27 enum GIOChannelError
28 {
29   /* Derived from errno */
30   G_IO_CHANNEL_ERROR_FBIG,
31   G_IO_CHANNEL_ERROR_INVAL,
32   G_IO_CHANNEL_ERROR_IO,
33   G_IO_CHANNEL_ERROR_ISDIR,
34   G_IO_CHANNEL_ERROR_NOSPC,
35   G_IO_CHANNEL_ERROR_NXIO,
36   G_IO_CHANNEL_ERROR_OVERFLOW,
37   G_IO_CHANNEL_ERROR_PIPE,
38   /* Other */
39   G_IO_CHANNEL_ERROR_FAILED
40 }
41 
42 enum GIOStatus
43 {
44   G_IO_STATUS_ERROR,
45   G_IO_STATUS_NORMAL,
46   G_IO_STATUS_EOF,
47   G_IO_STATUS_AGAIN
48 }
49 
50 enum GSeekType
51 {
52   G_SEEK_CUR,
53   G_SEEK_SET,
54   G_SEEK_END
55 }
56 
57 enum GIOFlags
58 {
59   G_IO_FLAG_APPEND = 1 << 0,
60   G_IO_FLAG_NONBLOCK = 1 << 1,
61   G_IO_FLAG_IS_READABLE = 1 << 2,	/* Read only flag */
62   G_IO_FLAG_IS_WRITABLE = 1 << 3,	/* Read only flag */
63   G_IO_FLAG_IS_WRITEABLE = 1 << 3,      /* Misspelling in 2.29.10 and earlier */
64   G_IO_FLAG_IS_SEEKABLE = 1 << 4,	/* Read only flag */
65   G_IO_FLAG_MASK = (1 << 5) - 1,
66   G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
67   G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
68 }
69 
70 
71 // TODO: define GIOChannel with bitfields
72 struct GIOChannel;
73 // struct _GIOChannel
74 // {
75 //   /*< private >*/
76 //   gint ref_count;
77 //   GIOFuncs *funcs;
78 //
79 //   gchar *encoding;
80 //   GIConv read_cd;
81 //   GIConv write_cd;
82 //   gchar *line_term;		/* String which indicates the end of a line of text */
83 //   guint line_term_len;		/* So we can have null in the line term */
84 //
85 //   gsize buf_size;
86 //   GString *read_buf;		/* Raw data from the channel */
87 //   GString *encoded_read_buf;    /* Channel data converted to UTF-8 */
88 //   GString *write_buf;		/* Data ready to be written to the file */
89 //   gchar partial_write_buf[6];	/* UTF-8 partial characters, null terminated */
90 //
91 //   /* Group the flags together, immediately after partial_write_buf, to save memory */
92 //
93 //   guint use_buffer     : 1;	/* The encoding uses the buffers */
94 //   guint do_encode      : 1;	/* The encoding uses the GIConv coverters */
95 //   guint close_on_unref : 1;	/* Close the channel on final unref */
96 //   guint is_readable    : 1;	/* Cached GIOFlag */
97 //   guint is_writeable   : 1;	/* ditto */
98 //   guint is_seekable    : 1;	/* ditto */
99 //
100 //   gpointer reserved1;
101 //   gpointer reserved2;
102 // };
103 
104 extern (C) {
105 
106 
107     alias GIOFunc = gboolean function (GIOChannel   *source,
108                      GIOCondition  condition,
109                      gpointer      data);
110 
111     struct GIOFuncs
112     {
113         GIOStatus function (GIOChannel   *channel,
114                             gchar        *buf,
115                             gsize         count,
116                             gsize        *bytes_read,
117                             GError      **err) io_read;
118 
119         GIOStatus function (GIOChannel   *channel,
120                             const gchar  *buf,
121                             gsize         count,
122                             gsize        *bytes_written,
123                             GError      **err) io_write;
124 
125         GIOStatus function (GIOChannel   *channel,
126                             gint64        offset,
127                             GSeekType     type,
128                             GError      **err) io_seek;
129 
130         GIOStatus function (GIOChannel   *channel,
131                             GError      **err) io_close;
132 
133         GSource* function (GIOChannel   *channel,
134                            GIOCondition  condition) io_create_watch;
135 
136         void function (GIOChannel   *channel) io_free;
137 
138         GIOStatus function (GIOChannel   *channel,
139                             GIOFlags      flags,
140                             GError      **err) io_set_flags;
141 
142         GIOFlags function (GIOChannel   *channel) io_get_flags;
143     }
144 
145 
146     void        g_io_channel_init   (GIOChannel    *channel);
147 
148     GIOChannel *g_io_channel_ref    (GIOChannel    *channel);
149 
150     void        g_io_channel_unref  (GIOChannel    *channel);
151 
152     deprecated("use g_io_channel_read_chars")
153     GIOError    g_io_channel_read   (GIOChannel    *channel,
154                                      gchar         *buf,
155                                      gsize          count,
156                                      gsize         *bytes_read);
157 
158     deprecated("use g_io_channel_write_chars")
159     GIOError  g_io_channel_write    (GIOChannel    *channel,
160                                      const(gchar)  *buf,
161                                      gsize          count,
162                                      gsize         *bytes_written);
163 
164     deprecated("use g_io_channel_seek_position")
165     GIOError  g_io_channel_seek     (GIOChannel    *channel,
166                                      gint64         offset,
167                                      GSeekType      type);
168 
169     deprecated("use g_io_channel_shutdown")
170     void      g_io_channel_close    (GIOChannel    *channel);
171 
172 
173     GIOStatus g_io_channel_shutdown (GIOChannel      *channel,
174                      gboolean         flush,
175                      GError         **err);
176 
177     guint     g_io_add_watch_full   (GIOChannel      *channel,
178                      gint             priority,
179                      GIOCondition     condition,
180                      GIOFunc          func,
181                      gpointer         user_data,
182                      GDestroyNotify   notify);
183 
184     GSource * g_io_create_watch     (GIOChannel      *channel,
185                      GIOCondition     condition);
186 
187     guint     g_io_add_watch        (GIOChannel      *channel,
188                      GIOCondition     condition,
189                      GIOFunc          func,
190                      gpointer         user_data);
191 
192 
193 
194     void                  g_io_channel_set_buffer_size      (GIOChannel   *channel,
195                                  gsize         size);
196 
197     gsize                 g_io_channel_get_buffer_size      (GIOChannel   *channel);
198 
199     GIOCondition          g_io_channel_get_buffer_condition (GIOChannel   *channel);
200 
201     GIOStatus             g_io_channel_set_flags            (GIOChannel   *channel,
202                                  GIOFlags      flags,
203                                  GError      **error);
204 
205     GIOFlags              g_io_channel_get_flags            (GIOChannel   *channel);
206 
207     void                  g_io_channel_set_line_term        (GIOChannel   *channel,
208                                  const(gchar)  *line_term,
209                                  gint          length);
210 
211     const (gchar)*         g_io_channel_get_line_term        (GIOChannel   *channel,
212                                  gint         *length);
213 
214     void		      g_io_channel_set_buffered		(GIOChannel   *channel,
215                                  gboolean      buffered);
216 
217     gboolean	      g_io_channel_get_buffered		(GIOChannel   *channel);
218 
219     GIOStatus             g_io_channel_set_encoding         (GIOChannel   *channel,
220                                  const(gchar)  *encoding,
221                                  GError      **error);
222 
223     const(gchar) *         g_io_channel_get_encoding         (GIOChannel   *channel);
224 
225     void                  g_io_channel_set_close_on_unref	(GIOChannel   *channel,
226                                  gboolean      do_close);
227 
228     gboolean              g_io_channel_get_close_on_unref	(GIOChannel   *channel);
229 
230 
231 
232     GIOStatus   g_io_channel_flush            (GIOChannel   *channel,
233                            GError      **error);
234 
235     GIOStatus   g_io_channel_read_line        (GIOChannel   *channel,
236                            gchar       **str_return,
237                            gsize        *length,
238                            gsize        *terminator_pos,
239                            GError      **error);
240 
241     GIOStatus   g_io_channel_read_line_string (GIOChannel   *channel,
242                            GString      *buffer,
243                            gsize        *terminator_pos,
244                            GError      **error);
245 
246     GIOStatus   g_io_channel_read_to_end      (GIOChannel   *channel,
247                            gchar       **str_return,
248                            gsize        *length,
249                            GError      **error);
250 
251     GIOStatus   g_io_channel_read_chars       (GIOChannel   *channel,
252                            gchar        *buf,
253                            gsize         count,
254                            gsize        *bytes_read,
255                            GError      **error);
256 
257     GIOStatus   g_io_channel_read_unichar     (GIOChannel   *channel,
258                            gunichar     *thechar,
259                            GError      **error);
260 
261     GIOStatus   g_io_channel_write_chars      (GIOChannel   *channel,
262                            const(gchar)  *buf,
263                            gssize        count,
264                            gsize        *bytes_written,
265                            GError      **error);
266 
267     GIOStatus   g_io_channel_write_unichar    (GIOChannel   *channel,
268                            gunichar      thechar,
269                            GError      **error);
270 
271     GIOStatus   g_io_channel_seek_position    (GIOChannel   *channel,
272                            gint64        offset,
273                            GSeekType     type,
274                            GError      **error);
275 
276 
277     GQuark          g_io_channel_error_quark      ();
278 
279     GIOChannelError g_io_channel_error_from_errno (gint en);
280 
281 
282     GIOChannel* g_io_channel_unix_new    (int         fd);
283 
284     gint        g_io_channel_unix_get_fd (GIOChannel *channel);
285 
286 
287     extern __gshared GSourceFuncs g_io_watch_funcs;
288 
289 
290 
291     version (Win32) {
292 
293 
294 
295         enum G_WIN32_MSG_HANDLE = 19981206;
296 
297 
298         void        g_io_channel_win32_make_pollfd (GIOChannel   *channel,
299                                 GIOCondition  condition,
300                                 GPollFD      *fd);
301 
302         gint        g_io_channel_win32_poll   (GPollFD    *fds,
303                                gint        n_fds,
304                                gint        timeout_);
305 
306         static if (GLIB_SIZEOF_VOID_P == 8) {
307             GIOChannel *g_io_channel_win32_new_messages (gsize hwnd);
308         }
309         else {
310             GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
311         }
312 
313         GIOChannel* g_io_channel_win32_new_fd (gint         fd);
314 
315         gint        g_io_channel_win32_get_fd (GIOChannel *channel);
316 
317         GIOChannel *g_io_channel_win32_new_socket (gint socket);
318 
319         deprecated("use g_io_channel_win32_new_socket")
320         GIOChannel *g_io_channel_win32_new_stream_socket (gint socket);
321 
322 
323         void        g_io_channel_win32_set_debug (GIOChannel *channel,
324                                                   gboolean    flag);
325 
326 
327         alias g_io_channel_new_file = g_io_channel_new_file_utf8;
328 
329 
330         GIOChannel *g_io_channel_new_file_utf8 (const gchar  *filename,
331                                                 const gchar  *mode,
332                                                 GError      **error);
333     }
334     else {
335 
336         GIOChannel* g_io_channel_new_file         (const(gchar)  *filename,
337                                const(gchar)  *mode,
338                                GError      **error);
339     }
340 }
341