From jeff.horner at vanderbilt.edu  Thu May 31 00:03:15 2007
From: jeff.horner at vanderbilt.edu (Jeffrey Horner)
Date: Wed, 30 May 2007 17:03:15 -0500
Subject: [Rd] Possible changes to connections
In-Reply-To: <Pine.LNX.4.64.0705301726400.11532@gannet.stats.ox.ac.uk>
References: <Pine.LNX.4.64.0705301726400.11532@gannet.stats.ox.ac.uk>
Message-ID: <465DF4A3.9000507@vanderbilt.edu>

Prof Brian Ripley wrote:
> When I originally implemented connections in R 1.2.0, I followed the model 
> in the 'Green Book' closely.  There were a number of features that forced 
> a particular implementation, and one was getConnection() that allows one 
> to recreate a connection object from a number.
[...]
> Another issue is that the current connection objects can be saved and 
> restored but refer to a global table that is session-specific so they lose 
> their meaning (and perhaps gain an unintended one).
> 
> What I suspect is that very few users are aware of the Green Book 
> description and so we have freedom to make some substantial changes
> to the implementation.  Both issues suggest that connection objects should 
> be based on external pointers (which did not exist way back in 1.2.0).

Sounds great! I would also like to see the following interface (all or 
in parts) added for working with connections from C. This is an update 
to the patch I created here:

http://wiki.r-project.org/rwiki/doku.php?id=developers:r_connections_api

/* Acting upon a connection */
void R_CloseConnection(SEXP);
int R_VfprintfConnection(SEXP, const char *format, va_list ap);
int R_FgetcConnection(SEXP);
double R_SeekConnection(SEXP, double where, int origin, int rw);
void R_TruncateConnection(SEXP);
int R_FlushConnection(SEXP);
size_t R_ReadConnection(SEXP, void *buf, size_t size, size_t n);
size_t R_WriteConnection(SEXP, const void *buf, size_t size, size_t n);

/* Querying a connection */
Rboolean R_ConnectionIsText(SEXP);
Rboolean R_ConnectionIsOpen(SEXP);
Rboolean R_ConnectionCanRead(SEXP);
Rboolean R_ConnectionCanWrite(SEXP);
Rboolean R_ConnectionCanSeek(SEXP);
Rboolean R_ConnectionIsBlocking(SEXP);

/* Prototypes for new connections created in C */
typedef Rboolean (*Rc_open)(void *private);
typedef void (*Rc_close)(void *private);
typedef void (*Rc_destroy)(void *private); /* when closing connection */
typedef int (*Rc_vfprintf)(void *private, const char *, va_list);
typedef int (*Rc_fgetc)(void *private);
typedef double (*Rc_seek)(void *private, double, int, int);
typedef void (*Rc_truncate)(void *private);
typedef int (*Rc_fflush)(void *private);
typedef size_t (*Rc_read)(void *, size_t, size_t, void *private);
typedef size_t (*Rc_write)(const void *, size_t, size_t, void *private);

/* Create a Connection */
SEXP R_NewConnection(char *class, char *description, char *mode, 
Rboolean blocking,
     Rc_open, Rc_close, Rc_destroy, Rc_vfprintf, Rc_fgetc, Rc_seek, 
Rc_truncate,
     Rc_fflush, Rc_read, Rc_write, void *private);


/* Swap out the standard C streams. More exotic, but it may clean up the
messy R_ConsoleFile, R_Outputfile, WriteConsole(), WriteConsoleEx(),
etc... confusion. */
Rboolean R_RegisterStdinConnection(SEXP scon);
Rboolean R_RegisterStdoutConnection(SEXP scon);
Rboolean R_RegisterStderrConnection(SEXP scon);

Jeff
-- 
http://biostat.mc.vanderbilt.edu/JeffreyHorner



[Rd] Possible changes to connections
Jeffrey Horner