From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Rottmann Newsgroups: gmane.lisp.guile.devel Subject: [PATCH 2/4] Add implementation of "transcoded ports" Date: Sat, 20 Nov 2010 18:40:31 +0100 Message-ID: <1290274833-24970-2-git-send-email-a.rottmann@gmx.at> References: <87ipzsvp9y.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1290274858 5125 80.91.229.12 (20 Nov 2010 17:40:58 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 20 Nov 2010 17:40:58 +0000 (UTC) To: Guile Development Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Nov 20 18:40:54 2010 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PJrQj-0001M5-O6 for guile-devel@m.gmane.org; Sat, 20 Nov 2010 18:40:54 +0100 Original-Received: from localhost ([127.0.0.1]:35299 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PJrQi-0003XV-Qq for guile-devel@m.gmane.org; Sat, 20 Nov 2010 12:40:52 -0500 Original-Received: from [140.186.70.92] (port=52823 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PJrQW-0003W3-1G for guile-devel@gnu.org; Sat, 20 Nov 2010 12:40:41 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PJrQU-0007D4-Dk for guile-devel@gnu.org; Sat, 20 Nov 2010 12:40:39 -0500 Original-Received: from mailout-de.gmx.net ([213.165.64.22]:39639 helo=mail.gmx.net) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1PJrQT-0007Cc-Tk for guile-devel@gnu.org; Sat, 20 Nov 2010 12:40:38 -0500 Original-Received: (qmail invoked by alias); 20 Nov 2010 17:40:35 -0000 Original-Received: from 83-215-154-5.hage.dyn.salzburg-online.at (EHLO nathot.lan) [83.215.154.5] by mail.gmx.net (mp020) with SMTP; 20 Nov 2010 18:40:35 +0100 X-Authenticated: #3102804 X-Provags-ID: V01U2FsdGVkX19GojcvTtFG3gdqR2gW6CkmjvYC0KhsdkeB3o6i+W EMpLbFeXZAvsol Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by nathot.lan (Postfix) with ESMTP id 5A52C3A6B1 for ; Sat, 20 Nov 2010 18:40:34 +0100 (CET) Original-Received: from nathot.lan ([127.0.0.1]) by localhost (nathot.lan [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id o4qK1Bhwx6IT for ; Sat, 20 Nov 2010 18:40:34 +0100 (CET) Original-Received: from delenn.lan (delenn.lan [192.168.3.11]) by nathot.lan (Postfix) with ESMTP id CD7653A695 for ; Sat, 20 Nov 2010 18:40:33 +0100 (CET) Original-Received: by delenn.lan (Postfix, from userid 1000) id 9ACA62C0319; Sat, 20 Nov 2010 18:40:33 +0100 (CET) X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <87ipzsvp9y.fsf@gnu.org> X-Y-GMX-Trusted: 0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:11162 Archived-At: * libguile/r6rs-ports.c (make_tp, tp_write, tp_fill_input, tp_flush, tp_close, initialize_transcoded_ports, scm_transcoded_port): New functions. (scm_init_r6rs_ports): Call `initialize_transcoded_ports'. * module/rnrs/ports.scm (transcoded-port): Remove, this is now implemented in C. * test-suite/tests/r6rs-ports.test (8.2.6 Input and output ports): Added a few tests for `transcoded-port'. --- libguile/r6rs-ports.c | 146 ++++++++++++++++++++++++++++++++++++++ module/rnrs/io/ports.scm | 3 - test-suite/tests/r6rs-ports.test | 23 ++++++ 3 files changed, 169 insertions(+), 3 deletions(-) diff --git a/libguile/r6rs-ports.c b/libguile/r6rs-ports.c index 87aba3d..dbb5c80 100644 --- a/libguile/r6rs-ports.c +++ b/libguile/r6rs-ports.c @@ -1076,6 +1076,151 @@ initialize_custom_binary_output_ports (void) } +/* 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_transcoded_port, + "transcoded-port", 2, 0, 0, + (SCM port, SCM transcoder), + "") +#define FUNC_NAME s_scm_transcoded_port +{ + SCM codec; + SCM result; + unsigned long mode = 0; + + SCM_VALIDATE_PORT (SCM_ARG1, port); + SCM_VALIDATE_STRUCT (SCM_ARG1, transcoder); + + codec = scm_struct_ref (transcoder, scm_from_int8 (1)); + + 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); + + scm_set_port_encoding_x (result, codec); + + /* SCM_CLR_PORT_OPEN_FLAG (port); */ + + return result; +} +#undef FUNC_NAME + + /* Initialization. */ void @@ -1096,4 +1241,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 04dabe6..ae8d0ea 100644 --- a/module/rnrs/io/ports.scm +++ b/module/rnrs/io/ports.scm @@ -71,9 +71,6 @@ ;; 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 (port-position port) "Return the offset (an integer) indicating where the next octet will be read from/written to in @var{port}." 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