mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 03:40:34 +02:00
Replace setbinary' by a public
%default-port-encoding' fluid.
* doc/ref/api-evaluation.texi (Character Encoding of Source Files): Add reference to the "Ports" node. * doc/ref/api-io.texi (Ports): Document `%default-port-encoding'. * libguile/ports.c (scm_port_encoding_var): Rename to... (default_port_encoding_var): ... this; update callers. Make `static'. * libguile/posix.c (scm_setbinary): Remove. * libguile/posix.h: Adjust accordingly. * test-suite/tests/numbers.test: Remove unneeded `setbinary' call. * test-suite/tests/ports.test: Replace `setbinary' call by equivalent `%default-port-encoding' mutation and `set-port-encoding!' calls. * test-suite/tests/r6rs-ports.test: Replace `setbinary' call by equivalent `%default-port-encoding' mutation.
This commit is contained in:
parent
c2be4e891c
commit
d6a6989e08
8 changed files with 38 additions and 53 deletions
|
@ -666,14 +666,17 @@ and @code{UTF-32} may not be used because they are not compatible with
|
|||
ASCII.
|
||||
|
||||
@cindex read
|
||||
@cindex set-port-encoding!
|
||||
@cindex encoding
|
||||
@cindex port encoding
|
||||
@findex set-port-encoding!
|
||||
There might be a scenario in which one would want to read non-ASCII
|
||||
code from a port, such as with the function @code{read}, instead of
|
||||
with @code{load}. If the port's character encoding is the same as the
|
||||
encoding of the code to be read by the port, not other special
|
||||
handling is necessary. The port will automatically do the character
|
||||
encoding conversion. The functions @code{setlocale} or by
|
||||
@code{set-port-encoding!} are used to set port encodings.
|
||||
@code{set-port-encoding!} are used to set port encodings
|
||||
(@pxref{Ports}).
|
||||
|
||||
If a port is used to read code of unknown character encoding, it can
|
||||
accomplish this in three steps. First, the character encoding of the
|
||||
|
|
|
@ -111,6 +111,12 @@ Sets the character encoding that will be used to interpret all port
|
|||
I/O. @var{enc} is a string containing the name of an encoding.
|
||||
@end deffn
|
||||
|
||||
@defvr {Scheme Variable} %default-port-encoding
|
||||
A fluid containing containing @code{#f} or the name of the encoding to
|
||||
be used by default for newly created ports (@pxref{Fluids and Dynamic
|
||||
States}). The value @code{#f} is equivalent to @code{"ISO-8859-1"}.
|
||||
@end defvr
|
||||
|
||||
New ports are created with the encoding appropriate for the current
|
||||
locale if @code{setlocale} has been called or ISO-8859-1 otherwise,
|
||||
and this procedure can be used to modify that encoding.
|
||||
|
|
|
@ -1917,10 +1917,11 @@ SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
|
|||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
/* The default port encoding for this locale. New ports will have this
|
||||
encoding. If it is a string, that is the encoding. If it #f, it
|
||||
is in the native (Latin-1) encoding. */
|
||||
SCM_GLOBAL_VARIABLE (scm_port_encoding_var, "%port-encoding");
|
||||
/* A fluid specifying the default encoding for newly created ports. If it is
|
||||
a string, that is the encoding. If it is #f, it is in the "native"
|
||||
(Latin-1) encoding. */
|
||||
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. */
|
||||
|
@ -1933,11 +1934,11 @@ scm_i_get_port_encoding (SCM port)
|
|||
{
|
||||
if (!scm_port_encoding_init)
|
||||
return NULL;
|
||||
else if (!scm_is_fluid (SCM_VARIABLE_REF (scm_port_encoding_var)))
|
||||
else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var)))
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
encoding = scm_fluid_ref (SCM_VARIABLE_REF (scm_port_encoding_var));
|
||||
encoding = scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var));
|
||||
if (!scm_is_string (encoding))
|
||||
return NULL;
|
||||
else
|
||||
|
@ -2002,7 +2003,7 @@ scm_i_set_port_encoding_x (SCM port, const char *enc)
|
|||
{
|
||||
/* Set the default encoding for future ports. */
|
||||
if (!scm_port_encoding_init
|
||||
|| !scm_is_fluid (SCM_VARIABLE_REF (scm_port_encoding_var)))
|
||||
|| !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);
|
||||
|
||||
|
@ -2010,9 +2011,9 @@ scm_i_set_port_encoding_x (SCM port, const char *enc)
|
|||
|| !strcmp (valid_enc, "ASCII")
|
||||
|| !strcmp (valid_enc, "ANSI_X3.4-1968")
|
||||
|| !strcmp (valid_enc, "ISO-8859-1"))
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (scm_port_encoding_var), SCM_BOOL_F);
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F);
|
||||
else
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (scm_port_encoding_var),
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var),
|
||||
scm_from_locale_string (valid_enc));
|
||||
}
|
||||
else
|
||||
|
@ -2045,7 +2046,7 @@ SCM_DEFINE (scm_port_encoding, "port-encoding", 1, 0, 0,
|
|||
return scm_from_locale_string ("NONE");
|
||||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
|
||||
SCM_DEFINE (scm_set_port_encoding_x, "set-port-encoding!", 2, 0, 0,
|
||||
(SCM port, SCM enc),
|
||||
"Sets the character encoding that will be used to interpret all\n"
|
||||
|
@ -2053,7 +2054,6 @@ SCM_DEFINE (scm_set_port_encoding_x, "set-port-encoding!", 2, 0, 0,
|
|||
"appropriate for the current locale if @code{setlocale} has \n"
|
||||
"been called or ISO-8859-1 otherwise\n"
|
||||
"and this procedure can be used to modify that encoding.\n")
|
||||
|
||||
#define FUNC_NAME s_scm_set_port_encoding_x
|
||||
{
|
||||
char *enc_str;
|
||||
|
@ -2347,12 +2347,14 @@ scm_init_ports ()
|
|||
cur_loadport_fluid = scm_permanent_object (scm_make_fluid ());
|
||||
|
||||
scm_i_port_weak_hash = scm_permanent_object (scm_make_weak_key_hash_table (SCM_I_MAKINUM(31)));
|
||||
|
||||
#include "libguile/ports.x"
|
||||
|
||||
SCM_VARIABLE_SET (scm_port_encoding_var, scm_make_fluid ());
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (scm_port_encoding_var), SCM_BOOL_F);
|
||||
/* Use Latin-1 as the default port encoding. */
|
||||
SCM_VARIABLE_SET (default_port_encoding_var, scm_make_fluid ());
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F);
|
||||
scm_port_encoding_init = 1;
|
||||
|
||||
|
||||
SCM_VARIABLE_SET (scm_conversion_strategy, scm_make_fluid ());
|
||||
scm_fluid_set_x (SCM_VARIABLE_REF (scm_conversion_strategy),
|
||||
scm_from_int ((int) SCM_FAILED_CONVERSION_QUESTION_MARK));
|
||||
|
|
|
@ -1554,34 +1554,6 @@ SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
|
|||
}
|
||||
#undef FUNC_NAME
|
||||
#endif /* HAVE_SETLOCALE */
|
||||
SCM_DEFINE (scm_setbinary, "setbinary", 0, 0, 0,
|
||||
(void),
|
||||
"Sets the encoding for the current input, output, and error\n"
|
||||
"ports to ISO-8859-1. That character encoding allows\n"
|
||||
"ports to operate on binary data.\n"
|
||||
"\n"
|
||||
"It also sets the default encoding for newly created ports\n"
|
||||
"to ISO-8859-1.\n"
|
||||
"\n"
|
||||
"The previous default encoding for new ports is returned\n")
|
||||
#define FUNC_NAME s_scm_setbinary
|
||||
{
|
||||
const char *enc = scm_i_get_port_encoding (SCM_BOOL_F);
|
||||
|
||||
/* Set the default encoding for new ports. */
|
||||
scm_i_set_port_encoding_x (SCM_BOOL_F, NULL);
|
||||
/* Set the encoding for the stdio ports. */
|
||||
scm_i_set_port_encoding_x (scm_current_input_port (), NULL);
|
||||
scm_i_set_port_encoding_x (scm_current_output_port (), NULL);
|
||||
scm_i_set_port_encoding_x (scm_current_error_port (), NULL);
|
||||
|
||||
if (enc)
|
||||
return scm_from_locale_string (enc);
|
||||
|
||||
return scm_from_locale_string ("ISO-8859-1");
|
||||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
|
||||
#ifdef HAVE_MKNOD
|
||||
SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#ifndef SCM_POSIX_H
|
||||
#define SCM_POSIX_H
|
||||
|
||||
/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2006, 2008 Free Software Foundation, Inc.
|
||||
/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2006, 2008, 2009 Free Software Foundation, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public License
|
||||
|
@ -74,7 +74,6 @@ SCM_API SCM scm_access (SCM path, SCM how);
|
|||
SCM_API SCM scm_getpid (void);
|
||||
SCM_API SCM scm_putenv (SCM str);
|
||||
SCM_API SCM scm_setlocale (SCM category, SCM locale);
|
||||
SCM_API SCM scm_setbinary (void);
|
||||
SCM_API SCM scm_mknod (SCM path, SCM type, SCM perms, SCM dev);
|
||||
SCM_API SCM scm_nice (SCM incr);
|
||||
SCM_API SCM scm_sync (void);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
;;;; numbers.test --- tests guile's numbers -*- scheme -*-
|
||||
;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
|
||||
;;;; Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2009 Free Software Foundation, Inc.
|
||||
;;;;
|
||||
;;;; This library is free software; you can redistribute it and/or
|
||||
;;;; modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -22,7 +22,6 @@
|
|||
;;;
|
||||
;;; miscellaneous
|
||||
;;;
|
||||
(setbinary)
|
||||
|
||||
(define exception:numerical-overflow
|
||||
(cons 'numerical-overflow "^Numerical overflow"))
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
;;;; ports.test --- test suite for Guile I/O ports -*- scheme -*-
|
||||
;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
|
||||
;;;;
|
||||
;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007 Free Software Foundation, Inc.
|
||||
;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009 Free Software Foundation, Inc.
|
||||
;;;;
|
||||
;;;; This library is free software; you can redistribute it and/or
|
||||
;;;; modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -33,8 +33,12 @@
|
|||
|
||||
;;;; Some general utilities for testing ports.
|
||||
|
||||
;;; Make sure we are set up for 8-bit data
|
||||
(setbinary)
|
||||
;; Make sure we are set up for 8-bit Latin-1 data.
|
||||
(fluid-set! %default-port-encoding "ISO-8859-1")
|
||||
(for-each (lambda (p)
|
||||
(set-port-encoding! p (fluid-ref %default-port-encoding)))
|
||||
(list (current-input-port) (current-output-port)
|
||||
(current-error-port)))
|
||||
|
||||
;;; Read from PORT until EOF, and return the result as a string.
|
||||
(define (read-all port)
|
||||
|
|
|
@ -27,8 +27,8 @@
|
|||
;;; All these tests assume Guile 1.8's port system, where characters are
|
||||
;;; treated as octets.
|
||||
|
||||
;;; Set the default encoding of future ports to be binary
|
||||
(setbinary)
|
||||
;; Set the default encoding of future ports to be Latin-1.
|
||||
(fluid-set! %default-port-encoding #f)
|
||||
|
||||
|
||||
(with-test-prefix "7.2.5 End-of-File Object"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue