summaryrefslogtreecommitdiff
path: root/tests/iochannel-test.c
diff options
context:
space:
mode:
authorOwen Taylor <otaylor@src.gnome.org>2001-06-30 15:22:13 +0000
committerOwen Taylor <otaylor@src.gnome.org>2001-06-30 15:22:13 +0000
commit42a23950f50223eb640a7b5a16e74176ea38df9d (patch)
tree3a4b0cb1d1ffedddf30e801a2c3e1e8850a5814a /tests/iochannel-test.c
parentf571f74616864cd0955ad42a9c905b7d7b358094 (diff)
downloadglib-42a23950f50223eb640a7b5a16e74176ea38df9d.tar.gz
Jun 29 13:36:39 2001 Owen Taylor <otaylor@redhat.com>
* glib/gstring.[ch] (g_string_set_size): Add function to allow setting the length of a string greater than the current length (for buffering usage) * glib/gstring.[ch]: Expose string->allocated_len, since that is useful when using GString simply as a buffer. (Renamed from string->alloc) * glib/giochannel.[ch] glib/giounix.c glib/giowin32.c: Major patch from Hidetoshi Tajima and Ron Steinke reworking GIOChannel to have: - Buffering - Sane and useful error reporting - Streaming encoding conversion with iconv - Convenience functions to read by lines or an entire file. Also fix remaining 64 bit cleanliness issues. * tests/iochannel-test.c tests/Makefile.am: Test case for IO channel streaming conversion. Still needs some fixing up.
Diffstat (limited to 'tests/iochannel-test.c')
-rw-r--r--tests/iochannel-test.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/iochannel-test.c b/tests/iochannel-test.c
new file mode 100644
index 000000000..18ea01de0
--- /dev/null
+++ b/tests/iochannel-test.c
@@ -0,0 +1,138 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <glib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define BUFFER_SIZE 1024
+
+gint main (gint argc, gchar * argv[])
+{
+ GIOChannel *gio_r, *gio_w ;
+ GError *gerr = NULL;
+ GString *buffer;
+ gint rlength = 0, wlength = 0, length_out, line_term_len;
+ gboolean block;
+ const gchar encoding[] = "EUC-JP", line_term[] = G_IO_CHANNEL_UNIX_LINE_TERM;
+ GIOStatus status;
+ GIOFlags flags;
+
+ if (argc < 2)
+ {
+ g_print( "Usage: foo in-file\n" );
+ exit (1);
+ }
+
+ setbuf(stdout, NULL); /* For debugging */
+
+ gio_r = g_io_channel_new_file (argv[1], G_IO_FILE_MODE_READ, &gerr);
+ if(gerr) {
+ g_warning(gerr->message);
+ g_warning("Unable to open file %s", argv[1]);
+ g_free(gerr);
+ return 0;
+ }
+ gio_w = g_io_channel_new_file( "/var/tmp/fooOut.txt", G_IO_FILE_MODE_WRITE, &gerr);
+ if(gerr) {
+ g_warning(gerr->message);
+ g_free(gerr);
+ return 0;
+ }
+
+ g_io_channel_set_encoding (gio_r, encoding, &gerr);
+ if(gerr) {
+ g_warning(gerr->message);
+ g_free(gerr);
+ return 0;
+ }
+
+ g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
+
+ status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
+ if(status == G_IO_STATUS_ERROR) {
+ g_warning(gerr->message);
+ g_error_free(gerr);
+ gerr = NULL;
+ }
+ flags = g_io_channel_get_flags (gio_r);
+ block = ! (flags & G_IO_FLAG_NONBLOCK);
+ if (block)
+ g_print (" BLOCKING TRUE \n\n");
+ else
+ g_print (" BLOCKING FALSE \n\n");
+
+ line_term_len = strlen (line_term);
+ buffer = g_string_sized_new (BUFFER_SIZE);
+
+ while (TRUE)
+ {
+ do
+ status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
+ while (status == G_IO_STATUS_AGAIN);
+ if (status != G_IO_STATUS_NORMAL)
+ break;
+
+ rlength += buffer->len;
+
+ do
+ status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
+ &length_out, &gerr);
+ while (status == G_IO_STATUS_AGAIN);
+ if (status != G_IO_STATUS_NORMAL)
+ break;
+
+ wlength += length_out;
+
+ if (length_out < buffer->len)
+ g_warning ("Only wrote part of the line.");
+
+ do
+ status = g_io_channel_write_chars (gio_w, line_term,
+ line_term_len, &length_out, &gerr);
+ while (status == G_IO_STATUS_AGAIN);
+ if (status != G_IO_STATUS_NORMAL)
+ break;
+
+ if (length_out < line_term_len)
+ g_warning ("Only wrote part of the line term.");
+
+ g_print (": %s\n", buffer->str);
+ g_string_truncate (buffer, 0);
+ }
+
+ switch (status)
+ {
+ case G_IO_STATUS_EOF:
+ break;
+ case G_IO_STATUS_PARTIAL_CHARS:
+ g_warning ("Partial characters at the end of input.");
+ break;
+ case G_IO_STATUS_ERROR:
+ g_warning (gerr->message);
+ g_error_free (gerr);
+ gerr = NULL;
+ break;
+ default:
+ g_warning ("Abnormal exit from write loop.");
+ break;
+ }
+
+ do
+ status = g_io_channel_flush (gio_w, &gerr);
+ while (status == G_IO_STATUS_AGAIN);
+
+ if(status == G_IO_STATUS_ERROR) {
+ g_warning(gerr->message);
+ g_error_free(gerr);
+ gerr = NULL;
+ }
+
+ g_print ("read %d bytes, wrote %d bytes\n", rlength, wlength);
+
+ g_io_channel_unref(gio_r);
+ g_io_channel_unref(gio_w);
+
+ return 0;
+}