|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * |
|
4 * giochannel.c: IO Channel abstraction |
|
5 * Copyright 1998 Owen Taylor |
|
6 * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
7 * |
|
8 * This library is free software; you can redistribute it and/or |
|
9 * modify it under the terms of the GNU Lesser General Public |
|
10 * License as published by the Free Software Foundation; either |
|
11 * version 2 of the License, or (at your option) any later version. |
|
12 * |
|
13 * This library is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * Lesser General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU Lesser General Public |
|
19 * License along with this library; if not, write to the |
|
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
21 * Boston, MA 02111-1307, USA. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
26 * file for a list of people on the GLib Team. See the ChangeLog |
|
27 * files for a list of changes. These files are distributed with |
|
28 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
29 */ |
|
30 |
|
31 /* |
|
32 * MT safe |
|
33 */ |
|
34 |
|
35 #include "config.h" |
|
36 |
|
37 #include <string.h> |
|
38 #include <errno.h> |
|
39 |
|
40 #ifdef HAVE_UNISTD_H |
|
41 #include <unistd.h> |
|
42 #endif |
|
43 |
|
44 #undef G_DISABLE_DEPRECATED |
|
45 |
|
46 #include "glib.h" |
|
47 |
|
48 #include "giochannel.h" |
|
49 |
|
50 #include "glibintl.h" |
|
51 |
|
52 #include "galias.h" |
|
53 |
|
54 #ifdef __SYMBIAN32__ |
|
55 #include <glib_wsd.h> |
|
56 #endif |
|
57 |
|
58 #define G_IO_NICE_BUF_SIZE 1024 |
|
59 |
|
60 /* This needs to be as wide as the largest character in any possible encoding */ |
|
61 #define MAX_CHAR_SIZE 10 |
|
62 |
|
63 /* Some simplifying macros, which reduce the need to worry whether the |
|
64 * buffers have been allocated. These also make USE_BUF () an lvalue, |
|
65 * which is used in g_io_channel_read_to_end (). |
|
66 */ |
|
67 #define USE_BUF(channel) ((channel)->encoding ? (channel)->encoded_read_buf \ |
|
68 : (channel)->read_buf) |
|
69 #define BUF_LEN(string) ((string) ? (string)->len : 0) |
|
70 |
|
71 static GIOError g_io_error_get_from_g_error (GIOStatus status, |
|
72 GError *err); |
|
73 static void g_io_channel_purge (GIOChannel *channel); |
|
74 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel, |
|
75 GError **err); |
|
76 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel, |
|
77 gsize *length, |
|
78 gsize *terminator_pos, |
|
79 GError **error); |
|
80 |
|
81 EXPORT_C void |
|
82 g_io_channel_init (GIOChannel *channel) |
|
83 { |
|
84 channel->ref_count = 1; |
|
85 channel->encoding = g_strdup ("UTF-8"); |
|
86 channel->line_term = NULL; |
|
87 channel->line_term_len = 0; |
|
88 channel->buf_size = G_IO_NICE_BUF_SIZE; |
|
89 channel->read_cd = (GIConv) -1; |
|
90 channel->write_cd = (GIConv) -1; |
|
91 channel->read_buf = NULL; /* Lazy allocate buffers */ |
|
92 channel->encoded_read_buf = NULL; |
|
93 channel->write_buf = NULL; |
|
94 channel->partial_write_buf[0] = '\0'; |
|
95 channel->use_buffer = TRUE; |
|
96 channel->do_encode = FALSE; |
|
97 channel->close_on_unref = FALSE; |
|
98 } |
|
99 |
|
100 EXPORT_C GIOChannel * |
|
101 g_io_channel_ref (GIOChannel *channel) |
|
102 { |
|
103 g_return_val_if_fail (channel != NULL, NULL); |
|
104 |
|
105 g_atomic_int_inc (FIX_CASTING(int *)&channel->ref_count); |
|
106 |
|
107 return channel; |
|
108 } |
|
109 |
|
110 EXPORT_C void |
|
111 g_io_channel_unref (GIOChannel *channel) |
|
112 { |
|
113 gboolean is_zero; |
|
114 |
|
115 g_return_if_fail (channel != NULL); |
|
116 |
|
117 is_zero = g_atomic_int_dec_and_test (FIX_CASTING(int *)&channel->ref_count); |
|
118 |
|
119 if (G_UNLIKELY (is_zero)) |
|
120 { |
|
121 if (channel->close_on_unref) |
|
122 g_io_channel_shutdown (channel, TRUE, NULL); |
|
123 else |
|
124 g_io_channel_purge (channel); |
|
125 g_free (channel->encoding); |
|
126 if (channel->read_cd != (GIConv) -1) |
|
127 g_iconv_close (channel->read_cd); |
|
128 if (channel->write_cd != (GIConv) -1) |
|
129 g_iconv_close (channel->write_cd); |
|
130 if (channel->line_term) |
|
131 g_free (channel->line_term); |
|
132 if (channel->read_buf) |
|
133 g_string_free (channel->read_buf, TRUE); |
|
134 if (channel->write_buf) |
|
135 g_string_free (channel->write_buf, TRUE); |
|
136 if (channel->encoded_read_buf) |
|
137 g_string_free (channel->encoded_read_buf, TRUE); |
|
138 channel->funcs->io_free (channel); |
|
139 } |
|
140 } |
|
141 |
|
142 static GIOError |
|
143 g_io_error_get_from_g_error (GIOStatus status, |
|
144 GError *err) |
|
145 { |
|
146 switch (status) |
|
147 { |
|
148 case G_IO_STATUS_NORMAL: |
|
149 case G_IO_STATUS_EOF: |
|
150 return G_IO_ERROR_NONE; |
|
151 case G_IO_STATUS_AGAIN: |
|
152 return G_IO_ERROR_AGAIN; |
|
153 case G_IO_STATUS_ERROR: |
|
154 g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN); |
|
155 |
|
156 if (err->domain != G_IO_CHANNEL_ERROR) |
|
157 return G_IO_ERROR_UNKNOWN; |
|
158 switch (err->code) |
|
159 { |
|
160 case G_IO_CHANNEL_ERROR_INVAL: |
|
161 return G_IO_ERROR_INVAL; |
|
162 default: |
|
163 return G_IO_ERROR_UNKNOWN; |
|
164 } |
|
165 default: |
|
166 g_assert_not_reached (); |
|
167 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */ |
|
168 } |
|
169 } |
|
170 |
|
171 /** |
|
172 * g_io_channel_read: |
|
173 * @channel: a #GIOChannel. |
|
174 * @buf: a buffer to read the data into (which should be at least count bytes long). |
|
175 * @count: the number of bytes to read from the #GIOChannel. |
|
176 * @bytes_read: returns the number of bytes actually read. |
|
177 * |
|
178 * Reads data from a #GIOChannel. |
|
179 * |
|
180 * Return value: %G_IO_ERROR_NONE if the operation was successful. |
|
181 * |
|
182 * Deprecated:2.2: Use g_io_channel_read_chars() instead. |
|
183 **/ |
|
184 EXPORT_C GIOError |
|
185 g_io_channel_read (GIOChannel *channel, |
|
186 gchar *buf, |
|
187 gsize count, |
|
188 gsize *bytes_read) |
|
189 { |
|
190 GError *err = NULL; |
|
191 GIOError error; |
|
192 GIOStatus status; |
|
193 |
|
194 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); |
|
195 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN); |
|
196 |
|
197 if (count == 0) |
|
198 { |
|
199 if (bytes_read) |
|
200 *bytes_read = 0; |
|
201 return G_IO_ERROR_NONE; |
|
202 } |
|
203 |
|
204 g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN); |
|
205 |
|
206 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err); |
|
207 |
|
208 error = g_io_error_get_from_g_error (status, err); |
|
209 |
|
210 if (err) |
|
211 g_error_free (err); |
|
212 |
|
213 return error; |
|
214 } |
|
215 |
|
216 /** |
|
217 * g_io_channel_write: |
|
218 * @channel: a #GIOChannel. |
|
219 * @buf: the buffer containing the data to write. |
|
220 * @count: the number of bytes to write. |
|
221 * @bytes_written: the number of bytes actually written. |
|
222 * |
|
223 * Writes data to a #GIOChannel. |
|
224 * |
|
225 * Return value: %G_IO_ERROR_NONE if the operation was successful. |
|
226 * |
|
227 * Deprecated:2.2: Use g_io_channel_write_chars() instead. |
|
228 **/ |
|
229 EXPORT_C GIOError |
|
230 g_io_channel_write (GIOChannel *channel, |
|
231 const gchar *buf, |
|
232 gsize count, |
|
233 gsize *bytes_written) |
|
234 { |
|
235 GError *err = NULL; |
|
236 GIOError error; |
|
237 GIOStatus status; |
|
238 |
|
239 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); |
|
240 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN); |
|
241 |
|
242 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err); |
|
243 |
|
244 error = g_io_error_get_from_g_error (status, err); |
|
245 |
|
246 if (err) |
|
247 g_error_free (err); |
|
248 |
|
249 return error; |
|
250 } |
|
251 |
|
252 /** |
|
253 * g_io_channel_seek: |
|
254 * @channel: a #GIOChannel. |
|
255 * @offset: an offset, in bytes, which is added to the position specified by @type |
|
256 * @type: the position in the file, which can be %G_SEEK_CUR (the current |
|
257 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the |
|
258 * file). |
|
259 * |
|
260 * Sets the current position in the #GIOChannel, similar to the standard library |
|
261 * function fseek(). |
|
262 * |
|
263 * Return value: %G_IO_ERROR_NONE if the operation was successful. |
|
264 * |
|
265 * Deprecated:2.2: Use g_io_channel_seek_position() instead. |
|
266 **/ |
|
267 EXPORT_C GIOError |
|
268 g_io_channel_seek (GIOChannel *channel, |
|
269 gint64 offset, |
|
270 GSeekType type) |
|
271 { |
|
272 GError *err = NULL; |
|
273 GIOError error; |
|
274 GIOStatus status; |
|
275 |
|
276 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); |
|
277 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN); |
|
278 |
|
279 switch (type) |
|
280 { |
|
281 case G_SEEK_CUR: |
|
282 case G_SEEK_SET: |
|
283 case G_SEEK_END: |
|
284 break; |
|
285 default: |
|
286 g_warning ("g_io_channel_seek: unknown seek type"); |
|
287 return G_IO_ERROR_UNKNOWN; |
|
288 } |
|
289 |
|
290 status = channel->funcs->io_seek (channel, offset, type, &err); |
|
291 |
|
292 error = g_io_error_get_from_g_error (status, err); |
|
293 |
|
294 if (err) |
|
295 g_error_free (err); |
|
296 |
|
297 return error; |
|
298 } |
|
299 |
|
300 /* The function g_io_channel_new_file() is prototyped in both |
|
301 * giounix.c and giowin32.c, so we stick its documentation here. |
|
302 */ |
|
303 |
|
304 /** |
|
305 * g_io_channel_new_file: |
|
306 * @filename: A string containing the name of a file. |
|
307 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have |
|
308 * the same meaning as in fopen(). |
|
309 * @error: A location to return an error of type %G_FILE_ERROR. |
|
310 * |
|
311 * Open a file @filename as a #GIOChannel using mode @mode. This |
|
312 * channel will be closed when the last reference to it is dropped, |
|
313 * so there is no need to call g_io_channel_close() (though doing |
|
314 * so will not cause problems, as long as no attempt is made to |
|
315 * access the channel after it is closed). |
|
316 * |
|
317 * Return value: A #GIOChannel on success, %NULL on failure. |
|
318 **/ |
|
319 |
|
320 /** |
|
321 * g_io_channel_close: |
|
322 * @channel: A #GIOChannel |
|
323 * |
|
324 * Close an IO channel. Any pending data to be written will be |
|
325 * flushed, ignoring errors. The channel will not be freed until the |
|
326 * last reference is dropped using g_io_channel_unref(). |
|
327 * |
|
328 * Deprecated:2.2: Use g_io_channel_shutdown() instead. |
|
329 **/ |
|
330 EXPORT_C void |
|
331 g_io_channel_close (GIOChannel *channel) |
|
332 { |
|
333 GError *err = NULL; |
|
334 |
|
335 g_return_if_fail (channel != NULL); |
|
336 |
|
337 g_io_channel_purge (channel); |
|
338 |
|
339 channel->funcs->io_close (channel, &err); |
|
340 |
|
341 if (err) |
|
342 { /* No way to return the error */ |
|
343 g_warning ("Error closing channel: %s", err->message); |
|
344 g_error_free (err); |
|
345 } |
|
346 |
|
347 channel->close_on_unref = FALSE; /* Because we already did */ |
|
348 channel->is_readable = FALSE; |
|
349 channel->is_writeable = FALSE; |
|
350 channel->is_seekable = FALSE; |
|
351 } |
|
352 |
|
353 /** |
|
354 * g_io_channel_shutdown: |
|
355 * @channel: a #GIOChannel |
|
356 * @flush: if %TRUE, flush pending |
|
357 * @err: location to store a #GIOChannelError |
|
358 * |
|
359 * Close an IO channel. Any pending data to be written will be |
|
360 * flushed if @flush is %TRUE. The channel will not be freed until the |
|
361 * last reference is dropped using g_io_channel_unref(). |
|
362 * |
|
363 * Return value: the status of the operation. |
|
364 **/ |
|
365 EXPORT_C GIOStatus |
|
366 g_io_channel_shutdown (GIOChannel *channel, |
|
367 gboolean flush, |
|
368 GError **err) |
|
369 { |
|
370 GIOStatus status, result; |
|
371 GError *tmperr = NULL; |
|
372 |
|
373 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
374 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR); |
|
375 |
|
376 if (channel->write_buf && channel->write_buf->len > 0) |
|
377 { |
|
378 if (flush) |
|
379 { |
|
380 GIOFlags flags; |
|
381 |
|
382 /* Set the channel to blocking, to avoid a busy loop |
|
383 */ |
|
384 flags = g_io_channel_get_flags (channel); |
|
385 /* Ignore any errors here, they're irrelevant */ |
|
386 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL); |
|
387 |
|
388 result = g_io_channel_flush (channel, &tmperr); |
|
389 } |
|
390 else |
|
391 result = G_IO_STATUS_NORMAL; |
|
392 |
|
393 g_string_truncate(channel->write_buf, 0); |
|
394 } |
|
395 else |
|
396 result = G_IO_STATUS_NORMAL; |
|
397 |
|
398 if (channel->partial_write_buf[0] != '\0') |
|
399 { |
|
400 if (flush) |
|
401 g_warning ("Partial character at end of write buffer not flushed.\n"); |
|
402 channel->partial_write_buf[0] = '\0'; |
|
403 } |
|
404 |
|
405 status = channel->funcs->io_close (channel, err); |
|
406 |
|
407 channel->close_on_unref = FALSE; /* Because we already did */ |
|
408 channel->is_readable = FALSE; |
|
409 channel->is_writeable = FALSE; |
|
410 channel->is_seekable = FALSE; |
|
411 |
|
412 if (status != G_IO_STATUS_NORMAL) |
|
413 { |
|
414 g_clear_error (&tmperr); |
|
415 return status; |
|
416 } |
|
417 else if (result != G_IO_STATUS_NORMAL) |
|
418 { |
|
419 g_propagate_error (err, tmperr); |
|
420 return result; |
|
421 } |
|
422 else |
|
423 return G_IO_STATUS_NORMAL; |
|
424 } |
|
425 |
|
426 /* This function is used for the final flush on close or unref */ |
|
427 static void |
|
428 g_io_channel_purge (GIOChannel *channel) |
|
429 { |
|
430 GError *err = NULL; |
|
431 GIOStatus status; |
|
432 |
|
433 g_return_if_fail (channel != NULL); |
|
434 |
|
435 if (channel->write_buf && channel->write_buf->len > 0) |
|
436 { |
|
437 GIOFlags flags; |
|
438 |
|
439 /* Set the channel to blocking, to avoid a busy loop |
|
440 */ |
|
441 flags = g_io_channel_get_flags (channel); |
|
442 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL); |
|
443 |
|
444 status = g_io_channel_flush (channel, &err); |
|
445 |
|
446 if (err) |
|
447 { /* No way to return the error */ |
|
448 g_warning ("Error flushing string: %s", err->message); |
|
449 g_error_free (err); |
|
450 } |
|
451 } |
|
452 |
|
453 /* Flush these in case anyone tries to close without unrefing */ |
|
454 |
|
455 if (channel->read_buf) |
|
456 g_string_truncate (channel->read_buf, 0); |
|
457 if (channel->write_buf) |
|
458 g_string_truncate (channel->write_buf, 0); |
|
459 if (channel->encoding) |
|
460 { |
|
461 if (channel->encoded_read_buf) |
|
462 g_string_truncate (channel->encoded_read_buf, 0); |
|
463 |
|
464 if (channel->partial_write_buf[0] != '\0') |
|
465 { |
|
466 g_warning ("Partial character at end of write buffer not flushed.\n"); |
|
467 channel->partial_write_buf[0] = '\0'; |
|
468 } |
|
469 } |
|
470 } |
|
471 |
|
472 EXPORT_C GSource * |
|
473 g_io_create_watch (GIOChannel *channel, |
|
474 GIOCondition condition) |
|
475 { |
|
476 g_return_val_if_fail (channel != NULL, NULL); |
|
477 |
|
478 return channel->funcs->io_create_watch (channel, condition); |
|
479 } |
|
480 |
|
481 EXPORT_C guint |
|
482 g_io_add_watch_full (GIOChannel *channel, |
|
483 gint priority, |
|
484 GIOCondition condition, |
|
485 GIOFunc func, |
|
486 gpointer user_data, |
|
487 GDestroyNotify notify) |
|
488 { |
|
489 GSource *source; |
|
490 guint id; |
|
491 |
|
492 g_return_val_if_fail (channel != NULL, 0); |
|
493 |
|
494 source = g_io_create_watch (channel, condition); |
|
495 |
|
496 if (priority != G_PRIORITY_DEFAULT) |
|
497 g_source_set_priority (source, priority); |
|
498 g_source_set_callback (source, (GSourceFunc)func, user_data, notify); |
|
499 |
|
500 id = g_source_attach (source, NULL); |
|
501 g_source_unref (source); |
|
502 |
|
503 return id; |
|
504 } |
|
505 |
|
506 EXPORT_C guint |
|
507 g_io_add_watch (GIOChannel *channel, |
|
508 GIOCondition condition, |
|
509 GIOFunc func, |
|
510 gpointer user_data) |
|
511 { |
|
512 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL); |
|
513 } |
|
514 |
|
515 /** |
|
516 * g_io_channel_get_buffer_condition: |
|
517 * @channel: A #GIOChannel |
|
518 * |
|
519 * This function returns a #GIOCondition depending on whether there |
|
520 * is data to be read/space to write data in the |
|
521 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and |
|
522 * %G_IO_OUT may be set. |
|
523 * |
|
524 * Return value: A #GIOCondition |
|
525 **/ |
|
526 EXPORT_C GIOCondition |
|
527 g_io_channel_get_buffer_condition (GIOChannel *channel) |
|
528 { |
|
529 GIOCondition condition = 0; |
|
530 |
|
531 if (channel->encoding) |
|
532 { |
|
533 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0)) |
|
534 condition |= G_IO_IN; /* Only return if we have full characters */ |
|
535 } |
|
536 else |
|
537 { |
|
538 if (channel->read_buf && (channel->read_buf->len > 0)) |
|
539 condition |= G_IO_IN; |
|
540 } |
|
541 |
|
542 if (channel->write_buf && (channel->write_buf->len < channel->buf_size)) |
|
543 condition |= G_IO_OUT; |
|
544 |
|
545 return condition; |
|
546 } |
|
547 |
|
548 /** |
|
549 * g_io_channel_error_from_errno: |
|
550 * @en: an <literal>errno</literal> error number, e.g. %EINVAL. |
|
551 * |
|
552 * Converts an <literal>errno</literal> error number to a #GIOChannelError. |
|
553 * |
|
554 * Return value: a #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL. |
|
555 **/ |
|
556 EXPORT_C GIOChannelError |
|
557 g_io_channel_error_from_errno (gint en) |
|
558 { |
|
559 #ifdef EAGAIN |
|
560 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED); |
|
561 #endif |
|
562 |
|
563 switch (en) |
|
564 { |
|
565 #ifdef EBADF |
|
566 case EBADF: |
|
567 g_warning("Invalid file descriptor.\n"); |
|
568 return G_IO_CHANNEL_ERROR_FAILED; |
|
569 #endif |
|
570 |
|
571 #ifdef EFAULT |
|
572 case EFAULT: |
|
573 g_warning("Buffer outside valid address space.\n"); |
|
574 return G_IO_CHANNEL_ERROR_FAILED; |
|
575 #endif |
|
576 |
|
577 #ifdef EFBIG |
|
578 case EFBIG: |
|
579 return G_IO_CHANNEL_ERROR_FBIG; |
|
580 #endif |
|
581 |
|
582 #ifdef EINTR |
|
583 /* In general, we should catch EINTR before we get here, |
|
584 * but close() is allowed to return EINTR by POSIX, so |
|
585 * we need to catch it here; EINTR from close() is |
|
586 * unrecoverable, because it's undefined whether |
|
587 * the fd was actually closed or not, so we just return |
|
588 * a generic error code. |
|
589 */ |
|
590 case EINTR: |
|
591 return G_IO_CHANNEL_ERROR_FAILED; |
|
592 #endif |
|
593 |
|
594 #ifdef EINVAL |
|
595 case EINVAL: |
|
596 return G_IO_CHANNEL_ERROR_INVAL; |
|
597 #endif |
|
598 |
|
599 #ifdef EIO |
|
600 case EIO: |
|
601 return G_IO_CHANNEL_ERROR_IO; |
|
602 #endif |
|
603 |
|
604 #ifdef EISDIR |
|
605 case EISDIR: |
|
606 return G_IO_CHANNEL_ERROR_ISDIR; |
|
607 #endif |
|
608 |
|
609 #ifdef ENOSPC |
|
610 case ENOSPC: |
|
611 return G_IO_CHANNEL_ERROR_NOSPC; |
|
612 #endif |
|
613 |
|
614 #ifdef ENXIO |
|
615 case ENXIO: |
|
616 return G_IO_CHANNEL_ERROR_NXIO; |
|
617 #endif |
|
618 |
|
619 #ifdef EOVERFLOW |
|
620 case EOVERFLOW: |
|
621 return G_IO_CHANNEL_ERROR_OVERFLOW; |
|
622 #endif |
|
623 |
|
624 #ifdef EPIPE |
|
625 case EPIPE: |
|
626 return G_IO_CHANNEL_ERROR_PIPE; |
|
627 #endif |
|
628 |
|
629 default: |
|
630 return G_IO_CHANNEL_ERROR_FAILED; |
|
631 } |
|
632 } |
|
633 |
|
634 /** |
|
635 * g_io_channel_set_buffer_size: |
|
636 * @channel: a #GIOChannel |
|
637 * @size: the size of the buffer. 0 == pick a good size |
|
638 * |
|
639 * Sets the buffer size. |
|
640 **/ |
|
641 EXPORT_C void |
|
642 g_io_channel_set_buffer_size (GIOChannel *channel, |
|
643 gsize size) |
|
644 { |
|
645 g_return_if_fail (channel != NULL); |
|
646 |
|
647 if (size == 0) |
|
648 size = G_IO_NICE_BUF_SIZE; |
|
649 |
|
650 if (size < MAX_CHAR_SIZE) |
|
651 size = MAX_CHAR_SIZE; |
|
652 |
|
653 channel->buf_size = size; |
|
654 } |
|
655 |
|
656 /** |
|
657 * g_io_channel_get_buffer_size: |
|
658 * @channel: a #GIOChannel |
|
659 * |
|
660 * Gets the buffer size. |
|
661 * |
|
662 * Return value: the size of the buffer. |
|
663 **/ |
|
664 EXPORT_C gsize |
|
665 g_io_channel_get_buffer_size (GIOChannel *channel) |
|
666 { |
|
667 g_return_val_if_fail (channel != NULL, 0); |
|
668 |
|
669 return channel->buf_size; |
|
670 } |
|
671 |
|
672 /** |
|
673 * g_io_channel_set_line_term: |
|
674 * @channel: a #GIOChannel |
|
675 * @line_term: The line termination string. Use %NULL for auto detect. |
|
676 * Auto detection breaks on "\n", "\r\n", "\r", "\0", and |
|
677 * the Unicode paragraph separator. Auto detection should |
|
678 * not be used for anything other than file-based channels. |
|
679 * @length: The length of the termination string. If -1 is passed, the |
|
680 * string is assumed to be nul-terminated. This option allows |
|
681 * termination strings with embeded nuls. |
|
682 * |
|
683 * This sets the string that #GIOChannel uses to determine |
|
684 * where in the file a line break occurs. |
|
685 **/ |
|
686 EXPORT_C void |
|
687 g_io_channel_set_line_term (GIOChannel *channel, |
|
688 const gchar *line_term, |
|
689 gint length) |
|
690 { |
|
691 g_return_if_fail (channel != NULL); |
|
692 g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */ |
|
693 |
|
694 if (line_term == NULL) |
|
695 length = 0; |
|
696 else if (length < 0) |
|
697 length = strlen (line_term); |
|
698 |
|
699 if (channel->line_term) |
|
700 g_free (channel->line_term); |
|
701 channel->line_term = line_term ? g_memdup (line_term, length) : NULL; |
|
702 channel->line_term_len = length; |
|
703 } |
|
704 |
|
705 /** |
|
706 * g_io_channel_get_line_term: |
|
707 * @channel: a #GIOChannel |
|
708 * @length: a location to return the length of the line terminator |
|
709 * |
|
710 * This returns the string that #GIOChannel uses to determine |
|
711 * where in the file a line break occurs. A value of %NULL |
|
712 * indicates auto detection. |
|
713 * |
|
714 * Return value: The line termination string. This value |
|
715 * is owned by GLib and must not be freed. |
|
716 **/ |
|
717 EXPORT_C G_CONST_RETURN gchar* |
|
718 g_io_channel_get_line_term (GIOChannel *channel, |
|
719 gint *length) |
|
720 { |
|
721 g_return_val_if_fail (channel != NULL, NULL); |
|
722 |
|
723 if (length) |
|
724 *length = channel->line_term_len; |
|
725 |
|
726 return channel->line_term; |
|
727 } |
|
728 |
|
729 /** |
|
730 * g_io_channel_set_flags: |
|
731 * @channel: a #GIOChannel. |
|
732 * @flags: the flags to set on the IO channel. |
|
733 * @error: A location to return an error of type #GIOChannelError. |
|
734 * |
|
735 * Sets the (writeable) flags in @channel to (@flags & %G_IO_CHANNEL_SET_MASK). |
|
736 * |
|
737 * Return value: the status of the operation. |
|
738 **/ |
|
739 EXPORT_C GIOStatus |
|
740 g_io_channel_set_flags (GIOChannel *channel, |
|
741 GIOFlags flags, |
|
742 GError **error) |
|
743 { |
|
744 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
745 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
746 G_IO_STATUS_ERROR); |
|
747 |
|
748 return (* channel->funcs->io_set_flags)(channel, |
|
749 flags & G_IO_FLAG_SET_MASK, |
|
750 error); |
|
751 } |
|
752 |
|
753 /** |
|
754 * g_io_channel_get_flags: |
|
755 * @channel: a #GIOChannel |
|
756 * |
|
757 * Gets the current flags for a #GIOChannel, including read-only |
|
758 * flags such as %G_IO_FLAG_IS_READABLE. |
|
759 * |
|
760 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE |
|
761 * are cached for internal use by the channel when it is created. |
|
762 * If they should change at some later point (e.g. partial shutdown |
|
763 * of a socket with the UNIX shutdown() function), the user |
|
764 * should immediately call g_io_channel_get_flags () to update |
|
765 * the internal values of these flags. |
|
766 * |
|
767 * Return value: the flags which are set on the channel |
|
768 **/ |
|
769 EXPORT_C GIOFlags |
|
770 g_io_channel_get_flags (GIOChannel *channel) |
|
771 { |
|
772 GIOFlags flags; |
|
773 |
|
774 g_return_val_if_fail (channel != NULL, 0); |
|
775 |
|
776 flags = (* channel->funcs->io_get_flags) (channel); |
|
777 |
|
778 /* Cross implementation code */ |
|
779 |
|
780 if (channel->is_seekable) |
|
781 flags |= G_IO_FLAG_IS_SEEKABLE; |
|
782 if (channel->is_readable) |
|
783 flags |= G_IO_FLAG_IS_READABLE; |
|
784 if (channel->is_writeable) |
|
785 flags |= G_IO_FLAG_IS_WRITEABLE; |
|
786 |
|
787 return flags; |
|
788 } |
|
789 |
|
790 /** |
|
791 * g_io_channel_set_close_on_unref: |
|
792 * @channel: a #GIOChannel |
|
793 * @do_close: Whether to close the channel on the final unref of |
|
794 * the GIOChannel data structure. The default value of |
|
795 * this is %TRUE for channels created by g_io_channel_new_file (), |
|
796 * and %FALSE for all other channels. |
|
797 * |
|
798 * Setting this flag to %TRUE for a channel you have already closed |
|
799 * can cause problems. |
|
800 **/ |
|
801 EXPORT_C void |
|
802 g_io_channel_set_close_on_unref (GIOChannel *channel, |
|
803 gboolean do_close) |
|
804 { |
|
805 g_return_if_fail (channel != NULL); |
|
806 |
|
807 channel->close_on_unref = do_close; |
|
808 } |
|
809 |
|
810 /** |
|
811 * g_io_channel_get_close_on_unref: |
|
812 * @channel: a #GIOChannel. |
|
813 * |
|
814 * Returns whether the file/socket/whatever associated with @channel |
|
815 * will be closed when @channel receives its final unref and is |
|
816 * destroyed. The default value of this is %TRUE for channels created |
|
817 * by g_io_channel_new_file (), and %FALSE for all other channels. |
|
818 * |
|
819 * Return value: Whether the channel will be closed on the final unref of |
|
820 * the GIOChannel data structure. |
|
821 **/ |
|
822 EXPORT_C gboolean |
|
823 g_io_channel_get_close_on_unref (GIOChannel *channel) |
|
824 { |
|
825 g_return_val_if_fail (channel != NULL, FALSE); |
|
826 |
|
827 return channel->close_on_unref; |
|
828 } |
|
829 |
|
830 /** |
|
831 * g_io_channel_seek_position: |
|
832 * @channel: a #GIOChannel |
|
833 * @offset: The offset in bytes from the position specified by @type |
|
834 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those |
|
835 * cases where a call to g_io_channel_set_encoding () |
|
836 * is allowed. See the documentation for |
|
837 * g_io_channel_set_encoding () for details. |
|
838 * @error: A location to return an error of type #GIOChannelError |
|
839 * |
|
840 * Replacement for g_io_channel_seek() with the new API. |
|
841 * |
|
842 * Return value: the status of the operation. |
|
843 **/ |
|
844 EXPORT_C GIOStatus |
|
845 g_io_channel_seek_position (GIOChannel* channel, |
|
846 gint64 offset, |
|
847 GSeekType type, |
|
848 GError **error) |
|
849 { |
|
850 GIOStatus status; |
|
851 |
|
852 /* For files, only one of the read and write buffers can contain data. |
|
853 * For sockets, both can contain data. |
|
854 */ |
|
855 |
|
856 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
857 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
858 G_IO_STATUS_ERROR); |
|
859 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR); |
|
860 |
|
861 switch (type) |
|
862 { |
|
863 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */ |
|
864 if (channel->use_buffer) |
|
865 { |
|
866 if (channel->do_encode && channel->encoded_read_buf |
|
867 && channel->encoded_read_buf->len > 0) |
|
868 { |
|
869 g_warning ("Seek type G_SEEK_CUR not allowed for this" |
|
870 " channel's encoding.\n"); |
|
871 return G_IO_STATUS_ERROR; |
|
872 } |
|
873 if (channel->read_buf) |
|
874 offset -= channel->read_buf->len; |
|
875 if (channel->encoded_read_buf) |
|
876 { |
|
877 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode); |
|
878 |
|
879 /* If there's anything here, it's because the encoding is UTF-8, |
|
880 * so we can just subtract the buffer length, the same as for |
|
881 * the unencoded data. |
|
882 */ |
|
883 |
|
884 offset -= channel->encoded_read_buf->len; |
|
885 } |
|
886 } |
|
887 break; |
|
888 case G_SEEK_SET: |
|
889 case G_SEEK_END: |
|
890 break; |
|
891 default: |
|
892 g_warning ("g_io_channel_seek_position: unknown seek type"); |
|
893 return G_IO_STATUS_ERROR; |
|
894 } |
|
895 |
|
896 if (channel->use_buffer) |
|
897 { |
|
898 status = g_io_channel_flush (channel, error); |
|
899 if (status != G_IO_STATUS_NORMAL) |
|
900 return status; |
|
901 } |
|
902 |
|
903 status = channel->funcs->io_seek (channel, offset, type, error); |
|
904 |
|
905 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer)) |
|
906 { |
|
907 if (channel->read_buf) |
|
908 g_string_truncate (channel->read_buf, 0); |
|
909 |
|
910 /* Conversion state no longer matches position in file */ |
|
911 if (channel->read_cd != (GIConv) -1) |
|
912 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL); |
|
913 if (channel->write_cd != (GIConv) -1) |
|
914 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL); |
|
915 |
|
916 if (channel->encoded_read_buf) |
|
917 { |
|
918 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode); |
|
919 g_string_truncate (channel->encoded_read_buf, 0); |
|
920 } |
|
921 |
|
922 if (channel->partial_write_buf[0] != '\0') |
|
923 { |
|
924 g_warning ("Partial character at end of write buffer not flushed.\n"); |
|
925 channel->partial_write_buf[0] = '\0'; |
|
926 } |
|
927 } |
|
928 |
|
929 return status; |
|
930 } |
|
931 |
|
932 /** |
|
933 * g_io_channel_flush: |
|
934 * @channel: a #GIOChannel |
|
935 * @error: location to store an error of type #GIOChannelError |
|
936 * |
|
937 * Flushes the write buffer for the GIOChannel. |
|
938 * |
|
939 * Return value: the status of the operation: One of |
|
940 * #G_IO_CHANNEL_NORMAL, #G_IO_CHANNEL_AGAIN, or |
|
941 * #G_IO_CHANNEL_ERROR. |
|
942 **/ |
|
943 EXPORT_C GIOStatus |
|
944 g_io_channel_flush (GIOChannel *channel, |
|
945 GError **error) |
|
946 { |
|
947 GIOStatus status; |
|
948 gsize this_time = 1, bytes_written = 0; |
|
949 |
|
950 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
951 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR); |
|
952 |
|
953 if (channel->write_buf == NULL || channel->write_buf->len == 0) |
|
954 return G_IO_STATUS_NORMAL; |
|
955 |
|
956 do |
|
957 { |
|
958 g_assert (this_time > 0); |
|
959 |
|
960 status = channel->funcs->io_write (channel, |
|
961 channel->write_buf->str + bytes_written, |
|
962 channel->write_buf->len - bytes_written, |
|
963 &this_time, error); |
|
964 bytes_written += this_time; |
|
965 } |
|
966 while ((bytes_written < channel->write_buf->len) |
|
967 && (status == G_IO_STATUS_NORMAL)); |
|
968 |
|
969 g_string_erase (channel->write_buf, 0, bytes_written); |
|
970 |
|
971 return status; |
|
972 } |
|
973 |
|
974 /** |
|
975 * g_io_channel_set_buffered: |
|
976 * @channel: a #GIOChannel |
|
977 * @buffered: whether to set the channel buffered or unbuffered |
|
978 * |
|
979 * The buffering state can only be set if the channel's encoding |
|
980 * is %NULL. For any other encoding, the channel must be buffered. |
|
981 * |
|
982 * A buffered channel can only be set unbuffered if the channel's |
|
983 * internal buffers have been flushed. Newly created channels or |
|
984 * channels which have returned %G_IO_STATUS_EOF |
|
985 * not require such a flush. For write-only channels, a call to |
|
986 * g_io_channel_flush () is sufficient. For all other channels, |
|
987 * the buffers may be flushed by a call to g_io_channel_seek_position (). |
|
988 * This includes the possibility of seeking with seek type %G_SEEK_CUR |
|
989 * and an offset of zero. Note that this means that socket-based |
|
990 * channels cannot be set unbuffered once they have had data |
|
991 * read from them. |
|
992 * |
|
993 * On unbuffered channels, it is safe to mix read and write |
|
994 * calls from the new and old APIs, if this is necessary for |
|
995 * maintaining old code. |
|
996 * |
|
997 * The default state of the channel is buffered. |
|
998 **/ |
|
999 EXPORT_C void |
|
1000 g_io_channel_set_buffered (GIOChannel *channel, |
|
1001 gboolean buffered) |
|
1002 { |
|
1003 g_return_if_fail (channel != NULL); |
|
1004 |
|
1005 if (channel->encoding != NULL) |
|
1006 { |
|
1007 g_warning ("Need to have NULL encoding to set the buffering state of the " |
|
1008 "channel.\n"); |
|
1009 return; |
|
1010 } |
|
1011 |
|
1012 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0); |
|
1013 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0); |
|
1014 |
|
1015 channel->use_buffer = buffered; |
|
1016 } |
|
1017 |
|
1018 /** |
|
1019 * g_io_channel_get_buffered: |
|
1020 * @channel: a #GIOChannel. |
|
1021 * |
|
1022 * Returns whether @channel is buffered. |
|
1023 * |
|
1024 * Return Value: %TRUE if the @channel is buffered. |
|
1025 **/ |
|
1026 EXPORT_C gboolean |
|
1027 g_io_channel_get_buffered (GIOChannel *channel) |
|
1028 { |
|
1029 g_return_val_if_fail (channel != NULL, FALSE); |
|
1030 |
|
1031 return channel->use_buffer; |
|
1032 } |
|
1033 |
|
1034 /** |
|
1035 * g_io_channel_set_encoding: |
|
1036 * @channel: a #GIOChannel |
|
1037 * @encoding: the encoding type |
|
1038 * @error: location to store an error of type #GConvertError. |
|
1039 * |
|
1040 * Sets the encoding for the input/output of the channel. The internal |
|
1041 * encoding is always UTF-8. The default encoding for the |
|
1042 * external file is UTF-8. |
|
1043 * |
|
1044 * The encoding %NULL is safe to use with binary data. |
|
1045 * |
|
1046 * The encoding can only be set if one of the following conditions |
|
1047 * is true: |
|
1048 * |
|
1049 * 1. The channel was just created, and has not been written to |
|
1050 * or read from yet. |
|
1051 * |
|
1052 * 2. The channel is write-only. |
|
1053 * |
|
1054 * 3. The channel is a file, and the file pointer was just |
|
1055 * repositioned by a call to g_io_channel_seek_position(). |
|
1056 * (This flushes all the internal buffers.) |
|
1057 * |
|
1058 * 4. The current encoding is %NULL or UTF-8. |
|
1059 * |
|
1060 * 5. One of the (new API) read functions has just returned %G_IO_STATUS_EOF |
|
1061 * (or, in the case of g_io_channel_read_to_end (), %G_IO_STATUS_NORMAL). |
|
1062 * |
|
1063 * 6. One of the functions g_io_channel_read_chars () or g_io_channel_read_unichar () |
|
1064 * has returned %G_IO_STATUS_AGAIN or %G_IO_STATUS_ERROR. This may be |
|
1065 * useful in the case of %G_CONVERT_ERROR_ILLEGAL_SEQUENCE. |
|
1066 * Returning one of these statuses from g_io_channel_read_line (), |
|
1067 * g_io_channel_read_line_string (), or g_io_channel_read_to_end () |
|
1068 * does <emphasis>not</emphasis> guarantee that the encoding can be changed. |
|
1069 * |
|
1070 * Channels which do not meet one of the above conditions cannot call |
|
1071 * g_io_channel_seek_position () with an offset of %G_SEEK_CUR, |
|
1072 * and, if they are "seekable", cannot |
|
1073 * call g_io_channel_write_chars () after calling one |
|
1074 * of the API "read" functions. |
|
1075 * |
|
1076 * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set. |
|
1077 **/ |
|
1078 EXPORT_C GIOStatus |
|
1079 g_io_channel_set_encoding (GIOChannel *channel, |
|
1080 const gchar *encoding, |
|
1081 GError **error) |
|
1082 { |
|
1083 GIConv read_cd, write_cd; |
|
1084 gboolean did_encode; |
|
1085 |
|
1086 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1087 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR); |
|
1088 |
|
1089 /* Make sure the encoded buffers are empty */ |
|
1090 |
|
1091 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf || |
|
1092 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR); |
|
1093 |
|
1094 if (!channel->use_buffer) |
|
1095 { |
|
1096 g_warning ("Need to set the channel buffered before setting the encoding.\n"); |
|
1097 g_warning ("Assuming this is what you meant and acting accordingly.\n"); |
|
1098 |
|
1099 channel->use_buffer = TRUE; |
|
1100 } |
|
1101 |
|
1102 if (channel->partial_write_buf[0] != '\0') |
|
1103 { |
|
1104 g_warning ("Partial character at end of write buffer not flushed.\n"); |
|
1105 channel->partial_write_buf[0] = '\0'; |
|
1106 } |
|
1107 |
|
1108 did_encode = channel->do_encode; |
|
1109 |
|
1110 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0) |
|
1111 { |
|
1112 channel->do_encode = FALSE; |
|
1113 read_cd = write_cd = (GIConv) -1; |
|
1114 } |
|
1115 else |
|
1116 { |
|
1117 gint err = 0; |
|
1118 const gchar *from_enc = NULL, *to_enc = NULL; |
|
1119 |
|
1120 if (channel->is_readable) |
|
1121 { |
|
1122 read_cd = g_iconv_open ("UTF-8", encoding); |
|
1123 |
|
1124 if (read_cd == (GIConv) -1) |
|
1125 { |
|
1126 err = errno; |
|
1127 from_enc = "UTF-8"; |
|
1128 to_enc = encoding; |
|
1129 } |
|
1130 } |
|
1131 else |
|
1132 read_cd = (GIConv) -1; |
|
1133 |
|
1134 if (channel->is_writeable && err == 0) |
|
1135 { |
|
1136 write_cd = g_iconv_open (encoding, "UTF-8"); |
|
1137 |
|
1138 if (write_cd == (GIConv) -1) |
|
1139 { |
|
1140 err = errno; |
|
1141 from_enc = encoding; |
|
1142 to_enc = "UTF-8"; |
|
1143 } |
|
1144 } |
|
1145 else |
|
1146 write_cd = (GIConv) -1; |
|
1147 |
|
1148 if (err != 0) |
|
1149 { |
|
1150 g_assert (from_enc); |
|
1151 g_assert (to_enc); |
|
1152 |
|
1153 if (err == EINVAL) |
|
1154 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION, |
|
1155 _("Conversion from character set '%s' to '%s' is not supported"), |
|
1156 from_enc, to_enc); |
|
1157 else |
|
1158 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, |
|
1159 _("Could not open converter from '%s' to '%s': %s"), |
|
1160 from_enc, to_enc, g_strerror (err)); |
|
1161 |
|
1162 if (read_cd != (GIConv) -1) |
|
1163 g_iconv_close (read_cd); |
|
1164 if (write_cd != (GIConv) -1) |
|
1165 g_iconv_close (write_cd); |
|
1166 |
|
1167 return G_IO_STATUS_ERROR; |
|
1168 } |
|
1169 |
|
1170 channel->do_encode = TRUE; |
|
1171 } |
|
1172 |
|
1173 /* The encoding is ok, so set the fields in channel */ |
|
1174 |
|
1175 if (channel->read_cd != (GIConv) -1) |
|
1176 g_iconv_close (channel->read_cd); |
|
1177 if (channel->write_cd != (GIConv) -1) |
|
1178 g_iconv_close (channel->write_cd); |
|
1179 |
|
1180 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0) |
|
1181 { |
|
1182 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */ |
|
1183 |
|
1184 /* This is just validated UTF-8, so we can copy it back into read_buf |
|
1185 * so it can be encoded in whatever the new encoding is. |
|
1186 */ |
|
1187 |
|
1188 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str, |
|
1189 channel->encoded_read_buf->len); |
|
1190 g_string_truncate (channel->encoded_read_buf, 0); |
|
1191 } |
|
1192 |
|
1193 channel->read_cd = read_cd; |
|
1194 channel->write_cd = write_cd; |
|
1195 |
|
1196 g_free (channel->encoding); |
|
1197 channel->encoding = g_strdup (encoding); |
|
1198 |
|
1199 return G_IO_STATUS_NORMAL; |
|
1200 } |
|
1201 |
|
1202 /** |
|
1203 * g_io_channel_get_encoding: |
|
1204 * @channel: a #GIOChannel |
|
1205 * |
|
1206 * Gets the encoding for the input/output of the channel. The internal |
|
1207 * encoding is always UTF-8. The encoding %NULL makes the |
|
1208 * channel safe for binary data. |
|
1209 * |
|
1210 * Return value: A string containing the encoding, this string is |
|
1211 * owned by GLib and must not be freed. |
|
1212 **/ |
|
1213 EXPORT_C G_CONST_RETURN gchar* |
|
1214 g_io_channel_get_encoding (GIOChannel *channel) |
|
1215 { |
|
1216 g_return_val_if_fail (channel != NULL, NULL); |
|
1217 |
|
1218 return channel->encoding; |
|
1219 } |
|
1220 |
|
1221 static GIOStatus |
|
1222 g_io_channel_fill_buffer (GIOChannel *channel, |
|
1223 GError **err) |
|
1224 { |
|
1225 gsize read_size, cur_len, oldlen; |
|
1226 GIOStatus status; |
|
1227 |
|
1228 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0) |
|
1229 { |
|
1230 status = g_io_channel_flush (channel, err); |
|
1231 if (status != G_IO_STATUS_NORMAL) |
|
1232 return status; |
|
1233 } |
|
1234 if (channel->is_seekable && channel->partial_write_buf[0] != '\0') |
|
1235 { |
|
1236 g_warning ("Partial character at end of write buffer not flushed.\n"); |
|
1237 channel->partial_write_buf[0] = '\0'; |
|
1238 } |
|
1239 |
|
1240 if (!channel->read_buf) |
|
1241 channel->read_buf = g_string_sized_new (channel->buf_size); |
|
1242 |
|
1243 cur_len = channel->read_buf->len; |
|
1244 |
|
1245 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size); |
|
1246 |
|
1247 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len, |
|
1248 channel->buf_size, &read_size, err); |
|
1249 |
|
1250 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0)); |
|
1251 |
|
1252 g_string_truncate (channel->read_buf, read_size + cur_len); |
|
1253 |
|
1254 if ((status != G_IO_STATUS_NORMAL) |
|
1255 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0))) |
|
1256 return status; |
|
1257 |
|
1258 g_assert (channel->read_buf->len > 0); |
|
1259 |
|
1260 if (channel->encoded_read_buf) |
|
1261 oldlen = channel->encoded_read_buf->len; |
|
1262 else |
|
1263 { |
|
1264 oldlen = 0; |
|
1265 if (channel->encoding) |
|
1266 channel->encoded_read_buf = g_string_sized_new (channel->buf_size); |
|
1267 } |
|
1268 |
|
1269 if (channel->do_encode) |
|
1270 { |
|
1271 size_t errnum, inbytes_left, outbytes_left; |
|
1272 gchar *inbuf, *outbuf; |
|
1273 int errval; |
|
1274 |
|
1275 g_assert (channel->encoded_read_buf); |
|
1276 |
|
1277 reencode: |
|
1278 |
|
1279 inbytes_left = channel->read_buf->len; |
|
1280 outbytes_left = MAX (channel->read_buf->len, |
|
1281 channel->encoded_read_buf->allocated_len |
|
1282 - channel->encoded_read_buf->len - 1); /* 1 for NULL */ |
|
1283 outbytes_left = MAX (outbytes_left, 6); |
|
1284 |
|
1285 inbuf = channel->read_buf->str; |
|
1286 g_string_set_size (channel->encoded_read_buf, |
|
1287 channel->encoded_read_buf->len + outbytes_left); |
|
1288 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len |
|
1289 - outbytes_left; |
|
1290 |
|
1291 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left, |
|
1292 &outbuf, &outbytes_left); |
|
1293 errval = errno; |
|
1294 |
|
1295 g_assert (inbuf + inbytes_left == channel->read_buf->str |
|
1296 + channel->read_buf->len); |
|
1297 g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str |
|
1298 + channel->encoded_read_buf->len); |
|
1299 |
|
1300 g_string_erase (channel->read_buf, 0, |
|
1301 channel->read_buf->len - inbytes_left); |
|
1302 g_string_truncate (channel->encoded_read_buf, |
|
1303 channel->encoded_read_buf->len - outbytes_left); |
|
1304 |
|
1305 if (errnum == (size_t) -1) |
|
1306 { |
|
1307 switch (errval) |
|
1308 { |
|
1309 case EINVAL: |
|
1310 if ((oldlen == channel->encoded_read_buf->len) |
|
1311 && (status == G_IO_STATUS_EOF)) |
|
1312 status = G_IO_STATUS_EOF; |
|
1313 else |
|
1314 status = G_IO_STATUS_NORMAL; |
|
1315 break; |
|
1316 case E2BIG: |
|
1317 /* Buffer size at least 6, wrote at least on character */ |
|
1318 g_assert (inbuf != channel->read_buf->str); |
|
1319 goto reencode; |
|
1320 case EILSEQ: |
|
1321 if (oldlen < channel->encoded_read_buf->len) |
|
1322 status = G_IO_STATUS_NORMAL; |
|
1323 else |
|
1324 { |
|
1325 g_set_error (err, G_CONVERT_ERROR, |
|
1326 G_CONVERT_ERROR_ILLEGAL_SEQUENCE, |
|
1327 _("Invalid byte sequence in conversion input")); |
|
1328 return G_IO_STATUS_ERROR; |
|
1329 } |
|
1330 break; |
|
1331 default: |
|
1332 g_assert (errval != EBADF); /* The converter should be open */ |
|
1333 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, |
|
1334 _("Error during conversion: %s"), g_strerror (errval)); |
|
1335 return G_IO_STATUS_ERROR; |
|
1336 } |
|
1337 } |
|
1338 g_assert ((status != G_IO_STATUS_NORMAL) |
|
1339 || (channel->encoded_read_buf->len > 0)); |
|
1340 } |
|
1341 else if (channel->encoding) /* UTF-8 */ |
|
1342 { |
|
1343 gchar *nextchar, *lastchar; |
|
1344 |
|
1345 g_assert (channel->encoded_read_buf); |
|
1346 |
|
1347 nextchar = channel->read_buf->str; |
|
1348 lastchar = channel->read_buf->str + channel->read_buf->len; |
|
1349 |
|
1350 while (nextchar < lastchar) |
|
1351 { |
|
1352 gunichar val_char; |
|
1353 |
|
1354 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar); |
|
1355 |
|
1356 switch (val_char) |
|
1357 { |
|
1358 case -2: |
|
1359 /* stop, leave partial character in buffer */ |
|
1360 lastchar = nextchar; |
|
1361 break; |
|
1362 case -1: |
|
1363 if (oldlen < channel->encoded_read_buf->len) |
|
1364 status = G_IO_STATUS_NORMAL; |
|
1365 else |
|
1366 { |
|
1367 g_set_error (err, G_CONVERT_ERROR, |
|
1368 G_CONVERT_ERROR_ILLEGAL_SEQUENCE, |
|
1369 _("Invalid byte sequence in conversion input")); |
|
1370 status = G_IO_STATUS_ERROR; |
|
1371 } |
|
1372 lastchar = nextchar; |
|
1373 break; |
|
1374 default: |
|
1375 nextchar = g_utf8_next_char (nextchar); |
|
1376 break; |
|
1377 } |
|
1378 } |
|
1379 |
|
1380 if (lastchar > channel->read_buf->str) |
|
1381 { |
|
1382 gint copy_len = lastchar - channel->read_buf->str; |
|
1383 |
|
1384 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str, |
|
1385 copy_len); |
|
1386 g_string_erase (channel->read_buf, 0, copy_len); |
|
1387 } |
|
1388 } |
|
1389 |
|
1390 return status; |
|
1391 } |
|
1392 |
|
1393 /** |
|
1394 * g_io_channel_read_line: |
|
1395 * @channel: a #GIOChannel |
|
1396 * @str_return: The line read from the #GIOChannel, including the |
|
1397 * line terminator. This data should be freed with g_free() |
|
1398 * when no longer needed. This is a nul-terminated string. |
|
1399 * If a @length of zero is returned, this will be %NULL instead. |
|
1400 * @length: location to store length of the read data, or %NULL |
|
1401 * @terminator_pos: location to store position of line terminator, or %NULL |
|
1402 * @error: A location to return an error of type #GConvertError |
|
1403 * or #GIOChannelError |
|
1404 * |
|
1405 * Reads a line, including the terminating character(s), |
|
1406 * from a #GIOChannel into a newly-allocated string. |
|
1407 * @str_return will contain allocated memory if the return |
|
1408 * is %G_IO_STATUS_NORMAL. |
|
1409 * |
|
1410 * Return value: the status of the operation. |
|
1411 **/ |
|
1412 EXPORT_C GIOStatus |
|
1413 g_io_channel_read_line (GIOChannel *channel, |
|
1414 gchar **str_return, |
|
1415 gsize *length, |
|
1416 gsize *terminator_pos, |
|
1417 GError **error) |
|
1418 { |
|
1419 GIOStatus status; |
|
1420 gsize got_length; |
|
1421 |
|
1422 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1423 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR); |
|
1424 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
1425 G_IO_STATUS_ERROR); |
|
1426 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR); |
|
1427 |
|
1428 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error); |
|
1429 |
|
1430 if (length) |
|
1431 *length = got_length; |
|
1432 |
|
1433 if (status == G_IO_STATUS_NORMAL) |
|
1434 { |
|
1435 g_assert (USE_BUF (channel)); |
|
1436 *str_return = g_strndup (USE_BUF (channel)->str, got_length); |
|
1437 g_string_erase (USE_BUF (channel), 0, got_length); |
|
1438 } |
|
1439 else |
|
1440 *str_return = NULL; |
|
1441 |
|
1442 return status; |
|
1443 } |
|
1444 |
|
1445 /** |
|
1446 * g_io_channel_read_line_string: |
|
1447 * @channel: a #GIOChannel |
|
1448 * @buffer: a #GString into which the line will be written. |
|
1449 * If @buffer already contains data, the old data will |
|
1450 * be overwritten. |
|
1451 * @terminator_pos: location to store position of line terminator, or %NULL |
|
1452 * @error: a location to store an error of type #GConvertError |
|
1453 * or #GIOChannelError |
|
1454 * |
|
1455 * Reads a line from a #GIOChannel, using a #GString as a buffer. |
|
1456 * |
|
1457 * Return value: the status of the operation. |
|
1458 **/ |
|
1459 EXPORT_C GIOStatus |
|
1460 g_io_channel_read_line_string (GIOChannel *channel, |
|
1461 GString *buffer, |
|
1462 gsize *terminator_pos, |
|
1463 GError **error) |
|
1464 { |
|
1465 gsize length; |
|
1466 GIOStatus status; |
|
1467 |
|
1468 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1469 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR); |
|
1470 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
1471 G_IO_STATUS_ERROR); |
|
1472 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR); |
|
1473 |
|
1474 if (buffer->len > 0) |
|
1475 g_string_truncate (buffer, 0); /* clear out the buffer */ |
|
1476 |
|
1477 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error); |
|
1478 |
|
1479 if (status == G_IO_STATUS_NORMAL) |
|
1480 { |
|
1481 g_assert (USE_BUF (channel)); |
|
1482 g_string_append_len (buffer, USE_BUF (channel)->str, length); |
|
1483 g_string_erase (USE_BUF (channel), 0, length); |
|
1484 } |
|
1485 |
|
1486 return status; |
|
1487 } |
|
1488 |
|
1489 |
|
1490 static GIOStatus |
|
1491 g_io_channel_read_line_backend (GIOChannel *channel, |
|
1492 gsize *length, |
|
1493 gsize *terminator_pos, |
|
1494 GError **error) |
|
1495 { |
|
1496 GIOStatus status; |
|
1497 gsize checked_to, line_term_len, line_length, got_term_len; |
|
1498 gboolean first_time = TRUE; |
|
1499 |
|
1500 if (!channel->use_buffer) |
|
1501 { |
|
1502 /* Can't do a raw read in read_line */ |
|
1503 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, |
|
1504 _("Can't do a raw read in g_io_channel_read_line_string")); |
|
1505 return G_IO_STATUS_ERROR; |
|
1506 } |
|
1507 |
|
1508 status = G_IO_STATUS_NORMAL; |
|
1509 |
|
1510 if (channel->line_term) |
|
1511 line_term_len = channel->line_term_len; |
|
1512 else |
|
1513 line_term_len = 3; |
|
1514 /* This value used for setting checked_to, it's the longest of the four |
|
1515 * we autodetect for. |
|
1516 */ |
|
1517 |
|
1518 checked_to = 0; |
|
1519 |
|
1520 while (TRUE) |
|
1521 { |
|
1522 gchar *nextchar, *lastchar; |
|
1523 GString *use_buf; |
|
1524 |
|
1525 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0)) |
|
1526 { |
|
1527 read_again: |
|
1528 status = g_io_channel_fill_buffer (channel, error); |
|
1529 switch (status) |
|
1530 { |
|
1531 case G_IO_STATUS_NORMAL: |
|
1532 if (BUF_LEN (USE_BUF (channel)) == 0) |
|
1533 /* Can happen when using conversion and only read |
|
1534 * part of a character |
|
1535 */ |
|
1536 { |
|
1537 first_time = FALSE; |
|
1538 continue; |
|
1539 } |
|
1540 break; |
|
1541 case G_IO_STATUS_EOF: |
|
1542 if (BUF_LEN (USE_BUF (channel)) == 0) |
|
1543 { |
|
1544 if (length) |
|
1545 *length = 0; |
|
1546 |
|
1547 if (channel->encoding && channel->read_buf->len != 0) |
|
1548 { |
|
1549 g_set_error (error, G_CONVERT_ERROR, |
|
1550 G_CONVERT_ERROR_PARTIAL_INPUT, |
|
1551 _("Leftover unconverted data in read buffer")); |
|
1552 return G_IO_STATUS_ERROR; |
|
1553 } |
|
1554 else |
|
1555 return G_IO_STATUS_EOF; |
|
1556 } |
|
1557 break; |
|
1558 default: |
|
1559 if (length) |
|
1560 *length = 0; |
|
1561 return status; |
|
1562 } |
|
1563 } |
|
1564 |
|
1565 g_assert (BUF_LEN (USE_BUF (channel)) != 0); |
|
1566 |
|
1567 use_buf = USE_BUF (channel); /* The buffer has been created by this point */ |
|
1568 |
|
1569 first_time = FALSE; |
|
1570 |
|
1571 lastchar = use_buf->str + use_buf->len; |
|
1572 |
|
1573 for (nextchar = use_buf->str + checked_to; nextchar < lastchar; |
|
1574 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++) |
|
1575 { |
|
1576 if (channel->line_term) |
|
1577 { |
|
1578 if (memcmp (channel->line_term, nextchar, line_term_len) == 0) |
|
1579 { |
|
1580 line_length = nextchar - use_buf->str; |
|
1581 got_term_len = line_term_len; |
|
1582 goto done; |
|
1583 } |
|
1584 } |
|
1585 else /* auto detect */ |
|
1586 { |
|
1587 switch (*nextchar) |
|
1588 { |
|
1589 case '\n': /* unix */ |
|
1590 line_length = nextchar - use_buf->str; |
|
1591 got_term_len = 1; |
|
1592 goto done; |
|
1593 case '\r': /* Warning: do not use with sockets */ |
|
1594 line_length = nextchar - use_buf->str; |
|
1595 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF) |
|
1596 && (lastchar == use_buf->str + use_buf->len)) |
|
1597 goto read_again; /* Try to read more data */ |
|
1598 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */ |
|
1599 got_term_len = 2; |
|
1600 else /* mac */ |
|
1601 got_term_len = 1; |
|
1602 goto done; |
|
1603 case '\xe2': /* Unicode paragraph separator */ |
|
1604 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0) |
|
1605 { |
|
1606 line_length = nextchar - use_buf->str; |
|
1607 got_term_len = 3; |
|
1608 goto done; |
|
1609 } |
|
1610 break; |
|
1611 case '\0': /* Embeded null in input */ |
|
1612 line_length = nextchar - use_buf->str; |
|
1613 got_term_len = 1; |
|
1614 goto done; |
|
1615 default: /* no match */ |
|
1616 break; |
|
1617 } |
|
1618 } |
|
1619 } |
|
1620 |
|
1621 /* If encoding != NULL, valid UTF-8, didn't overshoot */ |
|
1622 g_assert (nextchar == lastchar); |
|
1623 |
|
1624 /* Check for EOF */ |
|
1625 |
|
1626 if (status == G_IO_STATUS_EOF) |
|
1627 { |
|
1628 if (channel->encoding && channel->read_buf->len > 0) |
|
1629 { |
|
1630 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT, |
|
1631 _("Channel terminates in a partial character")); |
|
1632 return G_IO_STATUS_ERROR; |
|
1633 } |
|
1634 line_length = use_buf->len; |
|
1635 got_term_len = 0; |
|
1636 break; |
|
1637 } |
|
1638 |
|
1639 if (use_buf->len > line_term_len - 1) |
|
1640 checked_to = use_buf->len - (line_term_len - 1); |
|
1641 else |
|
1642 checked_to = 0; |
|
1643 } |
|
1644 |
|
1645 done: |
|
1646 |
|
1647 if (terminator_pos) |
|
1648 *terminator_pos = line_length; |
|
1649 |
|
1650 if (length) |
|
1651 *length = line_length + got_term_len; |
|
1652 |
|
1653 return G_IO_STATUS_NORMAL; |
|
1654 } |
|
1655 |
|
1656 /** |
|
1657 * g_io_channel_read_to_end: |
|
1658 * @channel: a #GIOChannel |
|
1659 * @str_return: Location to store a pointer to a string holding |
|
1660 * the remaining data in the #GIOChannel. This data should |
|
1661 * be freed with g_free() when no longer needed. This |
|
1662 * data is terminated by an extra nul character, but there |
|
1663 * may be other nuls in the intervening data. |
|
1664 * @length: Location to store length of the data |
|
1665 * @error: A location to return an error of type #GConvertError |
|
1666 * or #GIOChannelError |
|
1667 * |
|
1668 * Reads all the remaining data from the file. |
|
1669 * |
|
1670 * Return value: %G_IO_STATUS_NORMAL on success. |
|
1671 * This function never returns %G_IO_STATUS_EOF. |
|
1672 **/ |
|
1673 EXPORT_C GIOStatus |
|
1674 g_io_channel_read_to_end (GIOChannel *channel, |
|
1675 gchar **str_return, |
|
1676 gsize *length, |
|
1677 GError **error) |
|
1678 { |
|
1679 GIOStatus status; |
|
1680 |
|
1681 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1682 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
1683 G_IO_STATUS_ERROR); |
|
1684 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR); |
|
1685 |
|
1686 if (str_return) |
|
1687 *str_return = NULL; |
|
1688 if (length) |
|
1689 *length = 0; |
|
1690 |
|
1691 if (!channel->use_buffer) |
|
1692 { |
|
1693 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, |
|
1694 _("Can't do a raw read in g_io_channel_read_to_end")); |
|
1695 return G_IO_STATUS_ERROR; |
|
1696 } |
|
1697 |
|
1698 do |
|
1699 status = g_io_channel_fill_buffer (channel, error); |
|
1700 while (status == G_IO_STATUS_NORMAL); |
|
1701 |
|
1702 if (status != G_IO_STATUS_EOF) |
|
1703 return status; |
|
1704 |
|
1705 if (channel->encoding && channel->read_buf->len > 0) |
|
1706 { |
|
1707 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT, |
|
1708 _("Channel terminates in a partial character")); |
|
1709 return G_IO_STATUS_ERROR; |
|
1710 } |
|
1711 |
|
1712 if (USE_BUF (channel) == NULL) |
|
1713 { |
|
1714 /* length is already set to zero */ |
|
1715 if (str_return) |
|
1716 *str_return = g_strdup (""); |
|
1717 } |
|
1718 else |
|
1719 { |
|
1720 if (length) |
|
1721 *length = USE_BUF (channel)->len; |
|
1722 |
|
1723 if (str_return) |
|
1724 *str_return = g_string_free (USE_BUF (channel), FALSE); |
|
1725 else |
|
1726 g_string_free (USE_BUF (channel), TRUE); |
|
1727 |
|
1728 if (channel->encoding) |
|
1729 channel->encoded_read_buf = NULL; |
|
1730 else |
|
1731 channel->read_buf = NULL; |
|
1732 } |
|
1733 |
|
1734 return G_IO_STATUS_NORMAL; |
|
1735 } |
|
1736 |
|
1737 /** |
|
1738 * g_io_channel_read_chars: |
|
1739 * @channel: a #GIOChannel |
|
1740 * @buf: a buffer to read data into |
|
1741 * @count: the size of the buffer. Note that the buffer may |
|
1742 * not be complelely filled even if there is data |
|
1743 * in the buffer if the remaining data is not a |
|
1744 * complete character. |
|
1745 * @bytes_read: The number of bytes read. This may be zero even on |
|
1746 * success if count < 6 and the channel's encoding is non-%NULL. |
|
1747 * This indicates that the next UTF-8 character is too wide for |
|
1748 * the buffer. |
|
1749 * @error: A location to return an error of type #GConvertError |
|
1750 * or #GIOChannelError. |
|
1751 * |
|
1752 * Replacement for g_io_channel_read() with the new API. |
|
1753 * |
|
1754 * Return value: the status of the operation. |
|
1755 **/ |
|
1756 EXPORT_C GIOStatus |
|
1757 g_io_channel_read_chars (GIOChannel *channel, |
|
1758 gchar *buf, |
|
1759 gsize count, |
|
1760 gsize *bytes_read, |
|
1761 GError **error) |
|
1762 { |
|
1763 GIOStatus status; |
|
1764 gsize got_bytes; |
|
1765 |
|
1766 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1767 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
1768 G_IO_STATUS_ERROR); |
|
1769 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR); |
|
1770 |
|
1771 if (count == 0) |
|
1772 { |
|
1773 *bytes_read = 0; |
|
1774 return G_IO_STATUS_NORMAL; |
|
1775 } |
|
1776 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR); |
|
1777 |
|
1778 if (!channel->use_buffer) |
|
1779 { |
|
1780 gsize tmp_bytes; |
|
1781 |
|
1782 g_assert (!channel->read_buf || channel->read_buf->len == 0); |
|
1783 |
|
1784 status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error); |
|
1785 |
|
1786 if (bytes_read) |
|
1787 *bytes_read = tmp_bytes; |
|
1788 |
|
1789 return status; |
|
1790 } |
|
1791 |
|
1792 status = G_IO_STATUS_NORMAL; |
|
1793 |
|
1794 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL) |
|
1795 status = g_io_channel_fill_buffer (channel, error); |
|
1796 |
|
1797 /* Only return an error if we have no data */ |
|
1798 |
|
1799 if (BUF_LEN (USE_BUF (channel)) == 0) |
|
1800 { |
|
1801 g_assert (status != G_IO_STATUS_NORMAL); |
|
1802 |
|
1803 if (status == G_IO_STATUS_EOF && channel->encoding |
|
1804 && BUF_LEN (channel->read_buf) > 0) |
|
1805 { |
|
1806 g_set_error (error, G_CONVERT_ERROR, |
|
1807 G_CONVERT_ERROR_PARTIAL_INPUT, |
|
1808 _("Leftover unconverted data in read buffer")); |
|
1809 status = G_IO_STATUS_ERROR; |
|
1810 } |
|
1811 |
|
1812 if (bytes_read) |
|
1813 *bytes_read = 0; |
|
1814 |
|
1815 return status; |
|
1816 } |
|
1817 |
|
1818 if (status == G_IO_STATUS_ERROR) |
|
1819 g_clear_error (error); |
|
1820 |
|
1821 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel))); |
|
1822 |
|
1823 g_assert (got_bytes > 0); |
|
1824 |
|
1825 if (channel->encoding) |
|
1826 /* Don't validate for NULL encoding, binary safe */ |
|
1827 { |
|
1828 gchar *nextchar, *prevchar; |
|
1829 |
|
1830 g_assert (USE_BUF (channel) == channel->encoded_read_buf); |
|
1831 |
|
1832 nextchar = channel->encoded_read_buf->str; |
|
1833 |
|
1834 do |
|
1835 { |
|
1836 prevchar = nextchar; |
|
1837 nextchar = g_utf8_next_char (nextchar); |
|
1838 g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */ |
|
1839 } |
|
1840 while (nextchar < channel->encoded_read_buf->str + got_bytes); |
|
1841 |
|
1842 if (nextchar > channel->encoded_read_buf->str + got_bytes) |
|
1843 got_bytes = prevchar - channel->encoded_read_buf->str; |
|
1844 |
|
1845 g_assert (got_bytes > 0 || count < 6); |
|
1846 } |
|
1847 |
|
1848 memcpy (buf, USE_BUF (channel)->str, got_bytes); |
|
1849 g_string_erase (USE_BUF (channel), 0, got_bytes); |
|
1850 |
|
1851 if (bytes_read) |
|
1852 *bytes_read = got_bytes; |
|
1853 |
|
1854 return G_IO_STATUS_NORMAL; |
|
1855 } |
|
1856 |
|
1857 /** |
|
1858 * g_io_channel_read_unichar: |
|
1859 * @channel: a #GIOChannel |
|
1860 * @thechar: a location to return a character |
|
1861 * @error: A location to return an error of type #GConvertError |
|
1862 * or #GIOChannelError |
|
1863 * |
|
1864 * This function cannot be called on a channel with %NULL encoding. |
|
1865 * |
|
1866 * Return value: a #GIOStatus |
|
1867 **/ |
|
1868 EXPORT_C GIOStatus |
|
1869 g_io_channel_read_unichar (GIOChannel *channel, |
|
1870 gunichar *thechar, |
|
1871 GError **error) |
|
1872 { |
|
1873 GIOStatus status = G_IO_STATUS_NORMAL; |
|
1874 |
|
1875 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1876 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR); |
|
1877 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
1878 G_IO_STATUS_ERROR); |
|
1879 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR); |
|
1880 |
|
1881 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL) |
|
1882 status = g_io_channel_fill_buffer (channel, error); |
|
1883 |
|
1884 /* Only return an error if we have no data */ |
|
1885 |
|
1886 if (BUF_LEN (USE_BUF (channel)) == 0) |
|
1887 { |
|
1888 g_assert (status != G_IO_STATUS_NORMAL); |
|
1889 |
|
1890 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0) |
|
1891 { |
|
1892 g_set_error (error, G_CONVERT_ERROR, |
|
1893 G_CONVERT_ERROR_PARTIAL_INPUT, |
|
1894 _("Leftover unconverted data in read buffer")); |
|
1895 status = G_IO_STATUS_ERROR; |
|
1896 } |
|
1897 |
|
1898 if (thechar) |
|
1899 *thechar = (gunichar) -1; |
|
1900 |
|
1901 return status; |
|
1902 } |
|
1903 |
|
1904 if (status == G_IO_STATUS_ERROR) |
|
1905 g_clear_error (error); |
|
1906 |
|
1907 if (thechar) |
|
1908 *thechar = g_utf8_get_char (channel->encoded_read_buf->str); |
|
1909 |
|
1910 g_string_erase (channel->encoded_read_buf, 0, |
|
1911 g_utf8_next_char (channel->encoded_read_buf->str) |
|
1912 - channel->encoded_read_buf->str); |
|
1913 |
|
1914 return G_IO_STATUS_NORMAL; |
|
1915 } |
|
1916 |
|
1917 /** |
|
1918 * g_io_channel_write_chars: |
|
1919 * @channel: a #GIOChannel |
|
1920 * @buf: a buffer to write data from |
|
1921 * @count: the size of the buffer. If -1, the buffer |
|
1922 * is taken to be a nul-terminated string. |
|
1923 * @bytes_written: The number of bytes written. This can be nonzero |
|
1924 * even if the return value is not %G_IO_STATUS_NORMAL. |
|
1925 * If the return value is %G_IO_STATUS_NORMAL and the |
|
1926 * channel is blocking, this will always be equal |
|
1927 * to @count if @count >= 0. |
|
1928 * @error: A location to return an error of type #GConvertError |
|
1929 * or #GIOChannelError |
|
1930 * |
|
1931 * Replacement for g_io_channel_write() with the new API. |
|
1932 * |
|
1933 * On seekable channels with encodings other than %NULL or UTF-8, generic |
|
1934 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars () |
|
1935 * may only be made on a channel from which data has been read in the |
|
1936 * cases described in the documentation for g_io_channel_set_encoding (). |
|
1937 * |
|
1938 * Return value: the status of the operation. |
|
1939 **/ |
|
1940 EXPORT_C GIOStatus |
|
1941 g_io_channel_write_chars (GIOChannel *channel, |
|
1942 const gchar *buf, |
|
1943 gssize count, |
|
1944 gsize *bytes_written, |
|
1945 GError **error) |
|
1946 { |
|
1947 GIOStatus status; |
|
1948 gssize wrote_bytes = 0; |
|
1949 |
|
1950 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
1951 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
1952 G_IO_STATUS_ERROR); |
|
1953 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR); |
|
1954 |
|
1955 if ((count < 0) && buf) |
|
1956 count = strlen (buf); |
|
1957 |
|
1958 if (count == 0) |
|
1959 { |
|
1960 if (bytes_written) |
|
1961 *bytes_written = 0; |
|
1962 return G_IO_STATUS_NORMAL; |
|
1963 } |
|
1964 |
|
1965 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR); |
|
1966 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR); |
|
1967 |
|
1968 /* Raw write case */ |
|
1969 |
|
1970 if (!channel->use_buffer) |
|
1971 { |
|
1972 gsize tmp_bytes; |
|
1973 |
|
1974 g_assert (!channel->write_buf || channel->write_buf->len == 0); |
|
1975 g_assert (channel->partial_write_buf[0] == '\0'); |
|
1976 |
|
1977 status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error); |
|
1978 |
|
1979 if (bytes_written) |
|
1980 *bytes_written = tmp_bytes; |
|
1981 |
|
1982 return status; |
|
1983 } |
|
1984 |
|
1985 /* General case */ |
|
1986 |
|
1987 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0) |
|
1988 || (BUF_LEN (channel->encoded_read_buf) > 0))) |
|
1989 { |
|
1990 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0) |
|
1991 { |
|
1992 g_warning("Mixed reading and writing not allowed on encoded files"); |
|
1993 return G_IO_STATUS_ERROR; |
|
1994 } |
|
1995 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error); |
|
1996 if (status != G_IO_STATUS_NORMAL) |
|
1997 { |
|
1998 if (bytes_written) |
|
1999 *bytes_written = 0; |
|
2000 return status; |
|
2001 } |
|
2002 } |
|
2003 |
|
2004 if (!channel->write_buf) |
|
2005 channel->write_buf = g_string_sized_new (channel->buf_size); |
|
2006 |
|
2007 while (wrote_bytes < count) |
|
2008 { |
|
2009 gsize space_in_buf; |
|
2010 |
|
2011 /* If the buffer is full, try a write immediately. In |
|
2012 * the nonblocking case, this prevents the user from |
|
2013 * writing just a little bit to the buffer every time |
|
2014 * and never receiving an EAGAIN. |
|
2015 */ |
|
2016 |
|
2017 if (channel->write_buf->len >= channel->buf_size) |
|
2018 { |
|
2019 gsize did_write = 0, this_time; |
|
2020 |
|
2021 do |
|
2022 { |
|
2023 status = channel->funcs->io_write (channel, channel->write_buf->str |
|
2024 + did_write, channel->write_buf->len |
|
2025 - did_write, &this_time, error); |
|
2026 did_write += this_time; |
|
2027 } |
|
2028 while (status == G_IO_STATUS_NORMAL && |
|
2029 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE)); |
|
2030 |
|
2031 g_string_erase (channel->write_buf, 0, did_write); |
|
2032 |
|
2033 if (status != G_IO_STATUS_NORMAL) |
|
2034 { |
|
2035 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0) |
|
2036 status = G_IO_STATUS_NORMAL; |
|
2037 if (bytes_written) |
|
2038 *bytes_written = wrote_bytes; |
|
2039 return status; |
|
2040 } |
|
2041 } |
|
2042 |
|
2043 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1) |
|
2044 - channel->write_buf->len; /* 1 for NULL */ |
|
2045 |
|
2046 /* This is only true because g_io_channel_set_buffer_size () |
|
2047 * ensures that channel->buf_size >= MAX_CHAR_SIZE. |
|
2048 */ |
|
2049 g_assert (space_in_buf >= MAX_CHAR_SIZE); |
|
2050 |
|
2051 if (!channel->encoding) |
|
2052 { |
|
2053 gssize write_this = MIN (space_in_buf, count - wrote_bytes); |
|
2054 |
|
2055 g_string_append_len (channel->write_buf, buf, write_this); |
|
2056 buf += write_this; |
|
2057 wrote_bytes += write_this; |
|
2058 } |
|
2059 else |
|
2060 { |
|
2061 const gchar *from_buf; |
|
2062 gsize from_buf_len, from_buf_old_len, left_len; |
|
2063 size_t err; |
|
2064 gint errnum; |
|
2065 |
|
2066 if (channel->partial_write_buf[0] != '\0') |
|
2067 { |
|
2068 g_assert (wrote_bytes == 0); |
|
2069 |
|
2070 from_buf = channel->partial_write_buf; |
|
2071 from_buf_old_len = strlen (channel->partial_write_buf); |
|
2072 g_assert (from_buf_old_len > 0); |
|
2073 from_buf_len = MIN (6, from_buf_old_len + count); |
|
2074 |
|
2075 memcpy (channel->partial_write_buf + from_buf_old_len, buf, |
|
2076 from_buf_len - from_buf_old_len); |
|
2077 } |
|
2078 else |
|
2079 { |
|
2080 from_buf = buf; |
|
2081 from_buf_len = count - wrote_bytes; |
|
2082 from_buf_old_len = 0; |
|
2083 } |
|
2084 |
|
2085 reconvert: |
|
2086 |
|
2087 if (!channel->do_encode) /* UTF-8 encoding */ |
|
2088 { |
|
2089 const gchar *badchar; |
|
2090 gsize try_len = MIN (from_buf_len, space_in_buf); |
|
2091 |
|
2092 /* UTF-8, just validate, emulate g_iconv */ |
|
2093 |
|
2094 if (!g_utf8_validate (from_buf, try_len, &badchar)) |
|
2095 { |
|
2096 gunichar try_char; |
|
2097 gsize incomplete_len = from_buf + try_len - badchar; |
|
2098 |
|
2099 left_len = from_buf + from_buf_len - badchar; |
|
2100 |
|
2101 try_char = g_utf8_get_char_validated (badchar, incomplete_len); |
|
2102 |
|
2103 switch (try_char) |
|
2104 { |
|
2105 case -2: |
|
2106 g_assert (incomplete_len < 6); |
|
2107 if (try_len == from_buf_len) |
|
2108 { |
|
2109 errnum = EINVAL; |
|
2110 err = (size_t) -1; |
|
2111 } |
|
2112 else |
|
2113 { |
|
2114 errnum = 0; |
|
2115 err = (size_t) 0; |
|
2116 } |
|
2117 break; |
|
2118 case -1: |
|
2119 g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars()."); |
|
2120 /* FIXME bail here? */ |
|
2121 errnum = EILSEQ; |
|
2122 err = (size_t) -1; |
|
2123 break; |
|
2124 default: |
|
2125 g_assert_not_reached (); |
|
2126 err = (size_t) -1; |
|
2127 errnum = 0; /* Don't confunse the compiler */ |
|
2128 } |
|
2129 } |
|
2130 else |
|
2131 { |
|
2132 err = (size_t) 0; |
|
2133 errnum = 0; |
|
2134 left_len = from_buf_len - try_len; |
|
2135 } |
|
2136 |
|
2137 g_string_append_len (channel->write_buf, from_buf, |
|
2138 from_buf_len - left_len); |
|
2139 from_buf += from_buf_len - left_len; |
|
2140 } |
|
2141 else |
|
2142 { |
|
2143 gchar *outbuf; |
|
2144 |
|
2145 left_len = from_buf_len; |
|
2146 g_string_set_size (channel->write_buf, channel->write_buf->len |
|
2147 + space_in_buf); |
|
2148 outbuf = channel->write_buf->str + channel->write_buf->len |
|
2149 - space_in_buf; |
|
2150 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len, |
|
2151 &outbuf, &space_in_buf); |
|
2152 errnum = errno; |
|
2153 g_string_truncate (channel->write_buf, channel->write_buf->len |
|
2154 - space_in_buf); |
|
2155 } |
|
2156 |
|
2157 if (err == (size_t) -1) |
|
2158 { |
|
2159 switch (errnum) |
|
2160 { |
|
2161 case EINVAL: |
|
2162 g_assert (left_len < 6); |
|
2163 |
|
2164 if (from_buf_old_len == 0) |
|
2165 { |
|
2166 /* Not from partial_write_buf */ |
|
2167 |
|
2168 memcpy (channel->partial_write_buf, from_buf, left_len); |
|
2169 channel->partial_write_buf[left_len] = '\0'; |
|
2170 if (bytes_written) |
|
2171 *bytes_written = count; |
|
2172 return G_IO_STATUS_NORMAL; |
|
2173 } |
|
2174 |
|
2175 /* Working in partial_write_buf */ |
|
2176 |
|
2177 if (left_len == from_buf_len) |
|
2178 { |
|
2179 /* Didn't convert anything, must still have |
|
2180 * less than a full character |
|
2181 */ |
|
2182 |
|
2183 g_assert (count == from_buf_len - from_buf_old_len); |
|
2184 |
|
2185 channel->partial_write_buf[from_buf_len] = '\0'; |
|
2186 |
|
2187 if (bytes_written) |
|
2188 *bytes_written = count; |
|
2189 |
|
2190 return G_IO_STATUS_NORMAL; |
|
2191 } |
|
2192 |
|
2193 g_assert (from_buf_len - left_len >= from_buf_old_len); |
|
2194 |
|
2195 /* We converted all the old data. This is fine */ |
|
2196 |
|
2197 break; |
|
2198 case E2BIG: |
|
2199 if (from_buf_len == left_len) |
|
2200 { |
|
2201 /* Nothing was written, add enough space for |
|
2202 * at least one character. |
|
2203 */ |
|
2204 space_in_buf += MAX_CHAR_SIZE; |
|
2205 goto reconvert; |
|
2206 } |
|
2207 break; |
|
2208 case EILSEQ: |
|
2209 g_set_error (error, G_CONVERT_ERROR, |
|
2210 G_CONVERT_ERROR_ILLEGAL_SEQUENCE, |
|
2211 _("Invalid byte sequence in conversion input")); |
|
2212 if (from_buf_old_len > 0 && from_buf_len == left_len) |
|
2213 g_warning ("Illegal sequence due to partial character " |
|
2214 "at the end of a previous write.\n"); |
|
2215 else |
|
2216 wrote_bytes += from_buf_len - left_len - from_buf_old_len; |
|
2217 if (bytes_written) |
|
2218 *bytes_written = wrote_bytes; |
|
2219 channel->partial_write_buf[0] = '\0'; |
|
2220 return G_IO_STATUS_ERROR; |
|
2221 default: |
|
2222 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, |
|
2223 _("Error during conversion: %s"), g_strerror (errnum)); |
|
2224 if (from_buf_len >= left_len + from_buf_old_len) |
|
2225 wrote_bytes += from_buf_len - left_len - from_buf_old_len; |
|
2226 if (bytes_written) |
|
2227 *bytes_written = wrote_bytes; |
|
2228 channel->partial_write_buf[0] = '\0'; |
|
2229 return G_IO_STATUS_ERROR; |
|
2230 } |
|
2231 } |
|
2232 |
|
2233 g_assert (from_buf_len - left_len >= from_buf_old_len); |
|
2234 |
|
2235 wrote_bytes += from_buf_len - left_len - from_buf_old_len; |
|
2236 |
|
2237 if (from_buf_old_len > 0) |
|
2238 { |
|
2239 /* We were working in partial_write_buf */ |
|
2240 |
|
2241 buf += from_buf_len - left_len - from_buf_old_len; |
|
2242 channel->partial_write_buf[0] = '\0'; |
|
2243 } |
|
2244 else |
|
2245 buf = from_buf; |
|
2246 } |
|
2247 } |
|
2248 |
|
2249 if (bytes_written) |
|
2250 *bytes_written = count; |
|
2251 |
|
2252 return G_IO_STATUS_NORMAL; |
|
2253 } |
|
2254 |
|
2255 /** |
|
2256 * g_io_channel_write_unichar: |
|
2257 * @channel: a #GIOChannel |
|
2258 * @thechar: a character |
|
2259 * @error: A location to return an error of type #GConvertError |
|
2260 * or #GIOChannelError |
|
2261 * |
|
2262 * This function cannot be called on a channel with %NULL encoding. |
|
2263 * |
|
2264 * Return value: a #GIOStatus |
|
2265 **/ |
|
2266 EXPORT_C GIOStatus |
|
2267 g_io_channel_write_unichar (GIOChannel *channel, |
|
2268 gunichar thechar, |
|
2269 GError **error) |
|
2270 { |
|
2271 GIOStatus status; |
|
2272 gchar static_buf[6]; |
|
2273 gsize char_len, wrote_len; |
|
2274 |
|
2275 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR); |
|
2276 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR); |
|
2277 g_return_val_if_fail ((error == NULL) || (*error == NULL), |
|
2278 G_IO_STATUS_ERROR); |
|
2279 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR); |
|
2280 |
|
2281 char_len = g_unichar_to_utf8 (thechar, static_buf); |
|
2282 |
|
2283 if (channel->partial_write_buf[0] != '\0') |
|
2284 { |
|
2285 g_warning ("Partial charater written before writing unichar.\n"); |
|
2286 channel->partial_write_buf[0] = '\0'; |
|
2287 } |
|
2288 |
|
2289 status = g_io_channel_write_chars (channel, static_buf, |
|
2290 char_len, &wrote_len, error); |
|
2291 |
|
2292 /* We validate UTF-8, so we can't get a partial write */ |
|
2293 |
|
2294 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL); |
|
2295 |
|
2296 return status; |
|
2297 } |
|
2298 |
|
2299 /** |
|
2300 * g_io_channel_error_quark: |
|
2301 * |
|
2302 * Return value: The quark used as %G_IO_CHANNEL_ERROR |
|
2303 **/ |
|
2304 |
|
2305 #if EMULATOR |
|
2306 |
|
2307 PLS(q,g_io_channel_error_quark ,GQuark) |
|
2308 #define q (*FUNCTION_NAME(q,g_io_channel_error_quark )()) |
|
2309 |
|
2310 #endif /* EMULATOR */ |
|
2311 |
|
2312 EXPORT_C GQuark |
|
2313 g_io_channel_error_quark (void) |
|
2314 { |
|
2315 #if !(EMULATOR) |
|
2316 static GQuark q = 0; |
|
2317 #endif /* EMULATOR */ |
|
2318 |
|
2319 if (q == 0) |
|
2320 q = g_quark_from_static_string ("g-io-channel-error-quark"); |
|
2321 |
|
2322 return q; |
|
2323 } |
|
2324 #if EMULATOR |
|
2325 #undef q |
|
2326 #endif /* EMULATOR */ |
|
2327 |
|
2328 #define __G_IOCHANNEL_C__ |
|
2329 #include "galiasdef.c" |