From 9d9c66ba82217a3c96febda51cc9ddbb5f8ce3e3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ludovic=20Court=C3=A8s?= Date: Thu, 10 Feb 2011 22:50:51 +0100 Subject: [PATCH] Add `scm_i_set_default_port_encoding' and `scm_i_default_port_encoding'. * libguile/ports.c (scm_i_set_default_port_encoding, scm_i_default_port_encoding): New function. Replace `scm_i_set_port_encoding_x' and `scm_i_get_port_encoding' with PORT == SCM_BOOL_F. (scm_i_set_port_encoding_x): Assume PORT is a port. (scm_i_get_port_encoding): Remove. (scm_port_encoding): Adjust accordingly. (scm_new_port_table_entry): Use `scm_i_default_port_encoding'. * libguile/ports.h (scm_i_get_port_encoding): Remove declarations. (scm_i_default_port_encoding, scm_i_set_default_port_encoding): New declarations. * libguile/posix.c (setlocale): Use `scm_i_set_default_port_encoding'. --- libguile/ports.c | 149 ++++++++++++++++++++++------------------------- libguile/ports.h | 3 +- libguile/posix.c | 4 +- 3 files changed, 74 insertions(+), 82 deletions(-) diff --git a/libguile/ports.c b/libguile/ports.c index 5c004694b..1da6a833b 100644 --- a/libguile/ports.c +++ b/libguile/ports.c @@ -598,12 +598,11 @@ scm_new_port_table_entry (scm_t_bits tag) entry->file_name = SCM_BOOL_F; entry->rw_active = SCM_PORT_NEITHER; entry->port = z; + /* Initialize this port with the thread's current default encoding. */ - if ((enc = scm_i_get_port_encoding (SCM_BOOL_F)) == NULL) - entry->encoding = NULL; - else - entry->encoding = scm_gc_strdup (enc, "port"); + enc = scm_i_default_port_encoding (); + entry->encoding = enc ? scm_gc_strdup (enc, "port") : NULL; /* The conversion descriptors will be opened lazily. */ entry->input_cd = (iconv_t) -1; @@ -1970,32 +1969,43 @@ SCM_VARIABLE (default_port_encoding_var, "%default-port-encoding"); static int scm_port_encoding_init = 0; -/* Return a C string representation of the current encoding. */ +/* Use ENCODING as the default encoding for future ports. */ +void +scm_i_set_default_port_encoding (const char *encoding) +{ + if (!scm_port_encoding_init + || !scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var))) + scm_misc_error (NULL, "tried to set port encoding fluid before it is initialized", + SCM_EOL); + + if (encoding == NULL + || !strcmp (encoding, "ASCII") + || !strcmp (encoding, "ANSI_X3.4-1968") + || !strcmp (encoding, "ISO-8859-1")) + scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F); + else + scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), + scm_from_locale_string (encoding)); +} + +/* Return the name of the default encoding for newly created ports; a + return value of NULL means "ISO-8859-1". */ const char * -scm_i_get_port_encoding (SCM port) +scm_i_default_port_encoding (void) { - SCM encoding; - - if (scm_is_false (port)) + if (!scm_port_encoding_init) + return NULL; + else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var))) + return NULL; + else { - if (!scm_port_encoding_init) - return NULL; - else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var))) + SCM encoding; + + encoding = scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var)); + if (!scm_is_string (encoding)) return NULL; else - { - encoding = scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var)); - if (!scm_is_string (encoding)) - return NULL; - else - return scm_i_string_chars (encoding); - } - } - else - { - scm_t_port *pt; - pt = SCM_PTAB_ENTRY (port); - return pt->encoding; + return scm_i_string_chars (encoding); } } @@ -2003,70 +2013,49 @@ void scm_i_set_port_encoding_x (SCM port, const char *encoding) { scm_t_port *pt; + iconv_t new_input_cd, new_output_cd; - if (scm_is_false (port)) - { - /* Set the default encoding for future ports. */ - if (!scm_port_encoding_init - || !scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var))) - scm_misc_error (NULL, "tried to set port encoding fluid before it is initialized", - SCM_EOL); + new_input_cd = (iconv_t) -1; + new_output_cd = (iconv_t) -1; - if (encoding == NULL - || !strcmp (encoding, "ASCII") - || !strcmp (encoding, "ANSI_X3.4-1968") - || !strcmp (encoding, "ISO-8859-1")) - scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F); - else - scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), - scm_from_locale_string (encoding)); - } - else - { - iconv_t new_input_cd, new_output_cd; - - new_input_cd = (iconv_t) -1; - new_output_cd = (iconv_t) -1; - - /* Set the character encoding for this port. */ - pt = SCM_PTAB_ENTRY (port); + /* Set the character encoding for this port. */ + pt = SCM_PTAB_ENTRY (port); - if (encoding == NULL) - encoding = "ISO-8859-1"; + if (encoding == NULL) + encoding = "ISO-8859-1"; - pt->encoding = scm_gc_strdup (encoding, "port"); + pt->encoding = scm_gc_strdup (encoding, "port"); - if (SCM_CELL_WORD_0 (port) & SCM_RDNG) - { - /* Open an input iconv conversion descriptor, from ENCODING - to UTF-8. We choose UTF-8, not UTF-32, because iconv - implementations can typically convert from anything to - UTF-8, but not to UTF-32 (see - ). */ - new_input_cd = iconv_open ("UTF-8", encoding); - if (new_input_cd == (iconv_t) -1) - goto invalid_encoding; - } + if (SCM_CELL_WORD_0 (port) & SCM_RDNG) + { + /* Open an input iconv conversion descriptor, from ENCODING + to UTF-8. We choose UTF-8, not UTF-32, because iconv + implementations can typically convert from anything to + UTF-8, but not to UTF-32 (see + ). */ + new_input_cd = iconv_open ("UTF-8", encoding); + if (new_input_cd == (iconv_t) -1) + goto invalid_encoding; + } - if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) + if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) + { + new_output_cd = iconv_open (encoding, "UTF-8"); + if (new_output_cd == (iconv_t) -1) { - new_output_cd = iconv_open (encoding, "UTF-8"); - if (new_output_cd == (iconv_t) -1) - { - if (new_input_cd != (iconv_t) -1) - iconv_close (new_input_cd); - goto invalid_encoding; - } + if (new_input_cd != (iconv_t) -1) + iconv_close (new_input_cd); + goto invalid_encoding; } + } - if (pt->input_cd != (iconv_t) -1) - iconv_close (pt->input_cd); - if (pt->output_cd != (iconv_t) -1) - iconv_close (pt->output_cd); + if (pt->input_cd != (iconv_t) -1) + iconv_close (pt->input_cd); + if (pt->output_cd != (iconv_t) -1) + iconv_close (pt->output_cd); - pt->input_cd = new_input_cd; - pt->output_cd = new_output_cd; - } + pt->input_cd = new_input_cd; + pt->output_cd = new_output_cd; return; @@ -2092,7 +2081,7 @@ SCM_DEFINE (scm_port_encoding, "port-encoding", 1, 0, 0, SCM_VALIDATE_PORT (1, port); pt = SCM_PTAB_ENTRY (port); - enc = scm_i_get_port_encoding (port); + enc = pt->encoding; if (enc) return scm_from_locale_string (pt->encoding); else diff --git a/libguile/ports.h b/libguile/ports.h index 7408b9bb1..cc0b98762 100644 --- a/libguile/ports.h +++ b/libguile/ports.h @@ -298,7 +298,8 @@ SCM_API SCM scm_port_column (SCM port); SCM_API SCM scm_set_port_column_x (SCM port, SCM line); SCM_API SCM scm_port_filename (SCM port); SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename); -SCM_INTERNAL const char *scm_i_get_port_encoding (SCM port); +SCM_INTERNAL const char *scm_i_default_port_encoding (void); +SCM_INTERNAL void scm_i_set_default_port_encoding (const char *); SCM_INTERNAL void scm_i_set_port_encoding_x (SCM port, const char *str); SCM_API SCM scm_port_encoding (SCM port); SCM_API SCM scm_set_port_encoding_x (SCM port, SCM encoding); diff --git a/libguile/posix.c b/libguile/posix.c index 939e2488d..97e30df2b 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1631,8 +1631,10 @@ SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0, } enc = locale_charset (); + /* Set the default encoding for new ports. */ - scm_i_set_port_encoding_x (SCM_BOOL_F, enc); + scm_i_set_default_port_encoding (enc); + /* Set the encoding for the stdio ports. */ scm_i_set_port_encoding_x (scm_current_input_port (), enc); scm_i_set_port_encoding_x (scm_current_output_port (), enc); -- 2.20.1