From: Andreas Rottmann <a.rottmann@gmx.at>
To: Guile Development <guile-devel@gnu.org>
Subject: [PATCH 4/4] Add implementation of "transcoded ports"
Date: Sun, 21 Nov 2010 23:17:54 +0100 [thread overview]
Message-ID: <1290377874-13808-4-git-send-email-a.rottmann@gmx.at> (raw)
In-Reply-To: <87d3pyz5yo.fsf@delenn.lan>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=UTF-8, Size: 6953 bytes --]
* libguile/r6rs-ports.c (make_tp, tp_write, tp_fill_input, tp_flush)
(tp_close, initialize_transcoded_ports, scm_i_make_transcoded_port): New
functions.
(scm_init_r6rs_ports): Call `initialize_transcoded_ports'.
* module/rnrs/ports.scm (transcoded-port): Actually implement,
using `%make-transcoded-port'.
* test-suite/tests/r6rs-ports.test ("8.2.6 Input and output ports"): Added a
few tests for `transcoded-port'.
---
libguile/r6rs-ports.c | 143 ++++++++++++++++++++++++++++++++++++++
module/rnrs/io/ports.scm | 9 ++-
test-suite/tests/r6rs-ports.test | 23 ++++++
3 files changed, 173 insertions(+), 2 deletions(-)
diff --git a/libguile/r6rs-ports.c b/libguile/r6rs-ports.c
index ea6200f..232509c 100644
--- a/libguile/r6rs-ports.c
+++ b/libguile/r6rs-ports.c
@@ -1076,6 +1076,148 @@ initialize_custom_binary_output_ports (void)
}
\f
+/* Transcoded ports ("tp" for short). */
+static scm_t_bits transcoded_port_type = 0;
+
+#define TP_INPUT_BUFFER_SIZE 4096
+
+#define SCM_TP_BINARY_PORT(_port) SCM_PACK (SCM_STREAM (_port))
+
+static inline SCM
+make_tp (SCM binary_port, unsigned long mode)
+{
+ SCM port;
+ scm_t_port *c_port;
+ const unsigned long mode_bits = SCM_OPN | mode;
+
+ scm_i_pthread_mutex_lock (&scm_i_port_table_mutex);
+
+ port = scm_new_port_table_entry (transcoded_port_type);
+
+ SCM_SETSTREAM (port, SCM_UNPACK (binary_port));
+
+ SCM_SET_CELL_TYPE (port, transcoded_port_type | mode_bits);
+
+ if (SCM_INPUT_PORT_P (port))
+ {
+ c_port = SCM_PTAB_ENTRY (port);
+ c_port->read_buf = scm_gc_malloc_pointerless (TP_INPUT_BUFFER_SIZE,
+ "port buffer");
+ c_port->read_pos = c_port->read_end = c_port->read_buf;
+ c_port->read_buf_size = TP_INPUT_BUFFER_SIZE;
+
+ SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) & ~SCM_BUF0);
+ }
+
+ scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
+
+ return port;
+}
+
+static void
+tp_write (SCM port, const void *data, size_t size)
+{
+ scm_c_write (SCM_TP_BINARY_PORT (port), data, size);
+}
+
+static int
+tp_fill_input (SCM port)
+{
+ size_t count;
+ scm_t_port *c_port = SCM_PTAB_ENTRY (port);
+ SCM bport = SCM_TP_BINARY_PORT (port);
+ scm_t_port *c_bport = SCM_PTAB_ENTRY (bport);
+
+ /* We can't use `scm_c_read' here, since it blocks until the whole
+ block has been read or EOF. */
+
+ if (c_bport->rw_active == SCM_PORT_WRITE)
+ scm_force_output (bport);
+
+ if (c_bport->read_pos >= c_bport->read_end)
+ scm_fill_input (bport);
+
+ count = c_bport->read_end - c_bport->read_pos;
+ if (count > c_port->read_buf_size)
+ count = c_port->read_buf_size;
+
+ memcpy (c_port->read_buf, c_bport->read_pos, count);
+ c_bport->read_pos += count;
+
+ if (c_bport->rw_random)
+ c_bport->rw_active = SCM_PORT_READ;
+
+ if (count == 0)
+ return EOF;
+ else
+ {
+ c_port->read_pos = c_port->read_buf;
+ c_port->read_end = c_port->read_buf + count;
+ return *c_port->read_buf;
+ }
+}
+
+static void
+tp_flush (SCM port)
+{
+ SCM binary_port = SCM_TP_BINARY_PORT (port);
+ scm_t_port *c_port = SCM_PTAB_ENTRY (port);
+ size_t count = c_port->write_pos - c_port->write_buf;
+
+ scm_c_write (binary_port, c_port->write_buf, count);
+
+ c_port->write_pos = c_port->write_buf;
+ c_port->rw_active = SCM_PORT_NEITHER;
+
+ scm_force_output (binary_port);
+}
+
+static int
+tp_close (SCM port)
+{
+ if (SCM_OUTPUT_PORT_P (port))
+ tp_flush (port);
+ return scm_is_true (scm_close_port (SCM_TP_BINARY_PORT (port))) ? 0 : -1;
+}
+
+static inline void
+initialize_transcoded_ports (void)
+{
+ transcoded_port_type =
+ scm_make_port_type ("r6rs-transcoded-port", tp_fill_input, tp_write);
+
+ scm_set_port_flush (transcoded_port_type, tp_flush);
+ scm_set_port_close (transcoded_port_type, tp_close);
+}
+
+SCM_DEFINE (scm_i_make_transcoded_port,
+ "%make-transcoded-port", 1, 0, 0,
+ (SCM port),
+ "Return a new port which reads and writes to @var{port}")
+#define FUNC_NAME s_scm_i_make_transcoded_port
+{
+ SCM result;
+ unsigned long mode = 0;
+
+ SCM_VALIDATE_PORT (SCM_ARG1, port);
+
+ if (scm_is_true (scm_output_port_p (port)))
+ mode |= SCM_WRTNG;
+ else if (scm_is_true (scm_input_port_p (port)))
+ mode |= SCM_RDNG;
+
+ result = make_tp (port, mode);
+
+ /* FIXME: We should actually close `port' "in a special way" here,
+ according to R6RS. As there is no way to do that in Guile without
+ rendering the underlying port unusable for our purposes as well, we
+ just leave it open. */
+
+ return result;
+}
+#undef FUNC_NAME
+
+\f
/* Initialization. */
void
@@ -1096,4 +1238,5 @@ scm_init_r6rs_ports (void)
initialize_custom_binary_input_ports ();
initialize_bytevector_output_ports ();
initialize_custom_binary_output_ports ();
+ initialize_transcoded_ports ();
}
diff --git a/module/rnrs/io/ports.scm b/module/rnrs/io/ports.scm
index 31c1e29..73271ad 100644
--- a/module/rnrs/io/ports.scm
+++ b/module/rnrs/io/ports.scm
@@ -191,8 +191,13 @@
;; So far, we don't support transcoders other than the binary transcoder.
#t)
-(define (transcoded-port port)
- (error "port transcoders are not supported" port))
+(define (transcoded-port port transcoder)
+ "Return a new textual port based on @var{port}, using
+@var{transcoder} to encode and decode data written to or
+read from its underlying binary port @var{port}."
+ (let ((result (%make-transcoded-port port)))
+ (set-port-encoding! result (transcoder-codec transcoder))
+ result))
(define (port-position port)
"Return the offset (an integer) indicating where the next octet will be
diff --git a/test-suite/tests/r6rs-ports.test b/test-suite/tests/r6rs-ports.test
index 56ecbb6..7d746ca 100644
--- a/test-suite/tests/r6rs-ports.test
+++ b/test-suite/tests/r6rs-ports.test
@@ -497,6 +497,29 @@
(not eof?)
(bytevector=? sink source)))))
+(with-test-prefix "8.2.6 Input and output ports"
+ (pass-if "transcoded-port [output]"
+ (let ((s "Hello\nÄÖÜ"))
+ (bytevector=?
+ (string->utf8 s)
+ (call-with-bytevector-output-port
+ (lambda (bv-port)
+ (call-with-port (transcoded-port bv-port (make-transcoder (utf-8-codec)))
+ (lambda (utf8-port)
+ (put-string utf8-port s))))))))
+ (pass-if "transcoded-port [input]"
+ (let ((s "Hello\nÄÖÜ"))
+ (string=?
+ s
+ (get-string-all
+ (transcoded-port (open-bytevector-input-port (string->utf8 s))
+ (make-transcoder (utf-8-codec)))))))
+ (pass-if "transcoded-port [input line]"
+ (string=? "ÄÖÜ"
+ (get-line (transcoded-port
+ (open-bytevector-input-port (string->utf8 "ÄÖÜ\nFooBar"))
+ (make-transcoder (utf-8-codec)))))))
+
;;; Local Variables:
;;; mode: scheme
;;; End:
--
1.7.2.3
next prev parent reply other threads:[~2010-11-21 22:17 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-15 21:45 Some work on the R6RS I/O libraries Andreas Rottmann
2010-11-19 23:59 ` Ludovic Courtès
2010-11-20 17:40 ` [PATCH 1/4] Fix missing port-table locking and bytevector output port segfault Andreas Rottmann
2010-11-20 22:43 ` Ludovic Courtès
2010-11-20 17:40 ` [PATCH 2/4] Add implementation of "transcoded ports" Andreas Rottmann
2010-11-20 22:52 ` Ludovic Courtès
2010-11-21 22:07 ` Andreas Rottmann
2010-11-21 22:17 ` [PATCH 1/4] Turn `(rnrs io ports)' into an R6RS library Andreas Rottmann
2010-11-21 22:17 ` [PATCH 2/4] Reorganize the R6RS I/O condition types Andreas Rottmann
2010-11-21 22:17 ` [PATCH 3/4] Work towards a more complete implementation of `(rnrs io ports)' Andreas Rottmann
2010-11-23 21:13 ` Ludovic Courtès
2010-11-23 23:44 ` Andreas Rottmann
2010-11-24 20:24 ` Ludovic Courtès
2010-11-21 22:17 ` Andreas Rottmann [this message]
2010-11-24 22:29 ` [PATCH 4/4] Add implementation of "transcoded ports" Ludovic Courtès
2010-11-25 0:08 ` Andreas Rottmann
2010-11-25 21:15 ` Ludovic Courtès
2010-11-20 17:40 ` [PATCH 3/4] Reorganize the R6RS I/O condition types Andreas Rottmann
2010-11-20 17:40 ` [PATCH 4/4] Work towards a more complete implementation of `(rnrs io ports)' Andreas Rottmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1290377874-13808-4-git-send-email-a.rottmann@gmx.at \
--to=a.rottmann@gmx.at \
--cc=guile-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).