[Rcpp-commits] r953 - in pkg: . Rcpp/R Rcpp/src base64 base64/R base64/man base64/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Apr 3 12:58:35 CEST 2010


Author: romain
Date: 2010-04-03 12:58:34 +0200 (Sat, 03 Apr 2010)
New Revision: 953

Added:
   pkg/base64/
   pkg/base64/DESCRIPTION
   pkg/base64/NAMESPACE
   pkg/base64/R/
   pkg/base64/R/base64.R
   pkg/base64/man/
   pkg/base64/man/base64-package.Rd
   pkg/base64/man/decode.Rd
   pkg/base64/man/encode.Rd
   pkg/base64/man/img.Rd
   pkg/base64/src/
   pkg/base64/src/base64.c
   pkg/base64/src/base64.h
Modified:
   pkg/Rcpp/R/zzz.R
   pkg/Rcpp/src/RcppCommon.cpp
Log:
package base64

Modified: pkg/Rcpp/R/zzz.R
===================================================================
--- pkg/Rcpp/R/zzz.R	2010-04-02 07:58:14 UTC (rev 952)
+++ pkg/Rcpp/R/zzz.R	2010-04-03 10:58:34 UTC (rev 953)
@@ -18,3 +18,12 @@
 .onLoad <- function(libname, pkgname){
 	.Call( "initRcpp", PACKAGE = pkgname )
 }
+
+httpd_delegate <- function(path, query, ...){
+	if( grepl( "^/Rcpp/", path ) ){
+		clazz <- sub( "^/Rcpp/.*[.]html$", "\\1", path )
+		tf <- tempfile()
+		unit_test_examples( clazz, tf )
+		return( list( file = tf ) )
+	}
+}

Modified: pkg/Rcpp/src/RcppCommon.cpp
===================================================================
--- pkg/Rcpp/src/RcppCommon.cpp	2010-04-02 07:58:14 UTC (rev 952)
+++ pkg/Rcpp/src/RcppCommon.cpp	2010-04-03 10:58:34 UTC (rev 953)
@@ -122,6 +122,17 @@
 
 SEXP initRcpp(){
 	initUncaughtExceptionHandler() ;
+	using namespace Rcpp ;
+	Environment tools = Environment::namespace_env( "tools" );
+	Environment::Binding httpd_ = tools["httpd"] ;
+	httpd_.unlock() ;
+	
+	Function httpd = httpd_ ;
+	Language body( httpd.body() ) ;
+	body.insert( 1, ExpressionVector("if( ! is.null( .result <- Rcpp:::httpd_delegate(path, query, ...) ) ){ return( .result ) }")[0] ) ;
+	httpd_ = httpd ;
+	httpd_.lock() ;
+	
 	return R_NilValue ;
 }
    

Added: pkg/base64/DESCRIPTION
===================================================================
--- pkg/base64/DESCRIPTION	                        (rev 0)
+++ pkg/base64/DESCRIPTION	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,11 @@
+Package: base64
+Type: Package
+Title: Base 64 encoder/decoder
+Version: 1.0
+Date: 2010-04-03
+Author: Romain Francois, based on code by Bob Trower available 
+	at http://base64.sourceforge.net/
+Maintainer: Romain Francois <romain at r-enthusiasts.com>
+Description: Base 64 encoder/decoder
+License: GPL (>=2)
+LazyLoad: yes

Added: pkg/base64/NAMESPACE
===================================================================
--- pkg/base64/NAMESPACE	                        (rev 0)
+++ pkg/base64/NAMESPACE	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,6 @@
+useDynLib( "base64" )
+
+export( encode )
+export( decode )
+export( img )
+

Added: pkg/base64/R/base64.R
===================================================================
--- pkg/base64/R/base64.R	                        (rev 0)
+++ pkg/base64/R/base64.R	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,18 @@
+decode <- function(input, output = tempfile() ){
+	invisible( .Call( "decode_", input, output, PACKAGE = "base64" ) )
+}
+
+encode <- function(input, output = tempfile() , linesize = 72L ){
+	invisible( .Call( "encode_", input, output, as.integer(linesize), PACKAGE = "base64" ) )
+}
+
+img <- function( file, Rd = FALSE ){
+	tf <- tempfile()
+	on.exit( unlink(tf) )
+	encode( file, tf )
+	sprintf( '%s<img src="data:image/png;base64,\n%s" />%s', 
+		if( Rd ) "\\out{" else "", 
+		paste( readLines( tf) , collapse = "\n" ), 
+		if( Rd ) "}" else ""
+	)	
+}

Added: pkg/base64/man/base64-package.Rd
===================================================================
--- pkg/base64/man/base64-package.Rd	                        (rev 0)
+++ pkg/base64/man/base64-package.Rd	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,27 @@
+\name{base64-package}
+\alias{base64-package}
+\alias{base64}
+\docType{package}
+\title{
+Base64 encoder/decoder
+}
+\description{
+Base64 encoder/decoder
+}
+\details{
+\tabular{ll}{
+Package: \tab base64\cr
+Type: \tab Package\cr
+Version: \tab 1.0\cr
+Date: \tab 2010-04-03\cr
+License: \tab GPL (>=2)\cr
+LazyLoad: \tab yes\cr
+}
+}
+\author{
+Romain Francois, based on http://base64.sourceforge.net/
+
+Maintainer: Romain Francois <romain at r-enthusiasts.com>
+}
+\keyword{ package }
+

Added: pkg/base64/man/decode.Rd
===================================================================
--- pkg/base64/man/decode.Rd	                        (rev 0)
+++ pkg/base64/man/decode.Rd	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,31 @@
+\name{decode}
+\alias{decode}
+\title{
+decode a file formatted in Base64
+}
+\description{
+decode a file formatted in Base64
+}
+\usage{
+decode(input, output = tempfile() )
+}
+\arguments{
+  \item{input}{Input file}
+  \item{output}{output file}
+}
+\value{
+none. The output file is written
+}
+\references{
+	This is based on the Base64 code available at \url{http://base64.sourceforge.net/}
+	
+	Base 64 page at wikipedia: \url{http://en.wikipedia.org/wiki/Base64}
+}
+\author{
+Romain Francois, based on code by Bob Trower
+}
+\seealso{
+	\link{encode} for the reversed operation
+}
+\keyword{programming}
+

Added: pkg/base64/man/encode.Rd
===================================================================
--- pkg/base64/man/encode.Rd	                        (rev 0)
+++ pkg/base64/man/encode.Rd	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,31 @@
+\name{encode}
+\alias{encode}
+\title{
+Encode a file in Base64 format
+}
+\description{
+Encode a file in Base64 format
+}
+\usage{
+encode(input, output = tempfile(), linesize = 72L)
+}
+\arguments{
+  \item{input}{input file}
+  \item{output}{output file}
+  \item{linesize}{number of characters per line in output file}
+}
+\value{
+nothing. used for the side effect of writing the output file
+}
+\references{
+	This is based on the Base64 code available at \url{http://base64.sourceforge.net/}
+	
+	Base 64 page at wikipedia: \url{http://en.wikipedia.org/wiki/Base64}
+}
+\author{
+Romain Francois, based on code by Bob Trower
+}
+\seealso{
+	\link{decode} for the reversed operation
+}
+

Added: pkg/base64/man/img.Rd
===================================================================
--- pkg/base64/man/img.Rd	                        (rev 0)
+++ pkg/base64/man/img.Rd	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,64 @@
+\name{img}
+\alias{img}
+\title{
+encode a png file as a img data uri
+}
+\description{
+This creates html code to embed a png file into an html document. 
+}
+
+\details{
+\if{html}{
+	The following graph is embedded in the document using the \code{img} function	
+	
+\Sexpr[stage=render,results=rd,echo=FALSE]{
+	library( base64 )
+	library( grDevices )
+	library( graphics )
+	library( stats )
+	
+	pngfile <- tempfile()
+	png( pngfile, width = 600, height = 400 )
+	plot( 1:100, rnorm(100), pch = 21, bg = "red", cex = 2 )
+	dev.off()
+	img( pngfile, Rd = TRUE )
+}
+}
+
+}
+
+\usage{
+img(file, Rd = FALSE)
+}
+\arguments{
+  \item{file}{png file to translate into a data uri}
+  \item{Rd}{if TRUE, extra markup is added to facilitate inclusion of the image in an Rd file}
+}
+\value{
+The html code
+}
+\references{
+Wikipedia entry for data uri	
+}
+
+\author{
+Romain Francois <romain at r-enthusiasts.com>
+}
+\note{
+no checking on the file is performed, so it is up to the user to 
+make sure the input file is indeed a png file
+}
+
+\seealso{
+	\link{encode} is used to encode the file
+}
+\examples{
+\dontrun{
+pngfile <- tempfile()
+png( pngfile, width = 600, height = 600 )
+plot( 1:100, rnorm(100), pch = 21, bg = "red", cex = 2 )
+dev.off()
+img( pngfile )
+}
+}
+\keyword{programming}

Added: pkg/base64/src/base64.c
===================================================================
--- pkg/base64/src/base64.c	                        (rev 0)
+++ pkg/base64/src/base64.c	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,225 @@
+
+#include "base64.h"
+
+/*
+** Translation Table as described in RFC1113
+*/
+static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+/*
+** Translation Table to decode (created by author)
+*/
+static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
+
+/*
+** encodeblock
+**
+** encode 3 8-bit binary bytes as 4 '6-bit' characters
+*/
+void encodeblock( unsigned char in[3], unsigned char out[4], int len )
+{
+    out[0] = cb64[ in[0] >> 2 ];
+    out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
+    out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
+    out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
+}
+
+/*
+** encode
+**
+** base64 encode a stream adding padding and line breaks as per spec.
+*/
+void encode( FILE *infile, FILE *outfile, int linesize )
+{
+    unsigned char in[3], out[4];
+    int i, len, blocksout = 0;
+
+    while( !feof( infile ) ) {
+        len = 0;
+        for( i = 0; i < 3; i++ ) {
+            in[i] = (unsigned char) getc( infile );
+            if( !feof( infile ) ) {
+                len++;
+            }
+            else {
+                in[i] = 0;
+            }
+        }
+        if( len ) {
+            encodeblock( in, out, len );
+            for( i = 0; i < 4; i++ ) {
+                putc( out[i], outfile );
+            }
+            blocksout++;
+        }
+        if( blocksout >= (linesize/4) || feof( infile ) ) {
+            if( blocksout ) {
+                fprintf( outfile, "\r\n" );
+            }
+            blocksout = 0;
+        }
+    }
+}
+
+/*
+** decodeblock
+**
+** decode 4 '6-bit' characters into 3 8-bit binary bytes
+*/
+void decodeblock( unsigned char in[4], unsigned char out[3] )
+{   
+    out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
+    out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
+    out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
+}
+
+/*
+** decode
+**
+** decode a base64 encoded stream discarding padding, line breaks and noise
+*/
+void decode( FILE *infile, FILE *outfile )
+{
+    unsigned char in[4], out[3], v;
+    int i, len;
+
+    while( !feof( infile ) ) {
+        for( len = 0, i = 0; i < 4 && !feof( infile ); i++ ) {
+            v = 0;
+            while( !feof( infile ) && v == 0 ) {
+                v = (unsigned char) getc( infile );
+                v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
+                if( v ) {
+                    v = (unsigned char) ((v == '$') ? 0 : v - 61);
+                }
+            }
+            if( !feof( infile ) ) {
+                len++;
+                if( v ) {
+                    in[ i ] = (unsigned char) (v - 1);
+                }
+            }
+            else {
+                in[i] = 0;
+            }
+        }
+        if( len ) {
+            decodeblock( in, out );
+            for( i = 0; i < len - 1; i++ ) {
+                putc( out[i], outfile );
+            }
+        }
+    }
+}
+
+
+/*
+** b64_message
+**
+** Gather text messages in one place.
+**
+*/
+char *b64_message( int errcode )
+{
+    char *msgs[ B64_MAX_MESSAGES ] = {
+            "b64:000:Invalid Message Code.",
+            "b64:001:Syntax Error -- check help for usage.",
+            "b64:002:File Error Opening/Creating Files.",
+            "b64:003:File I/O Error -- Note: output file not removed.",
+            "b64:004:Error on output file close.",
+            "b64:004:linesize set to minimum."
+    };
+    char *msg = msgs[ 0 ];
+
+    if( errcode > 0 && errcode < B64_MAX_MESSAGES ) {
+        msg = msgs[ errcode ];
+    }
+
+    return( msg );
+}
+
+/*
+** b64
+**
+** 'engine' that opens streams and calls encode/decode
+*/
+
+int b64( int opt, char *infilename, char *outfilename, int linesize )
+{
+    FILE *infile;
+    int retcode = B64_FILE_ERROR;
+
+    if( !infilename ) {
+        infile = stdin;
+    }
+    else {
+        infile = fopen( infilename, "rb" );
+    }
+    if( !infile ) {
+        perror( infilename );
+    }
+    else {
+        FILE *outfile;
+        if( !outfilename ) {
+            outfile = stdout;
+        }
+        else {
+            outfile = fopen( outfilename, "wb" );
+        }
+        if( !outfile ) {
+            perror( outfilename );
+        }
+        else {
+            if( opt == 'e' ) {
+                encode( infile, outfile, linesize );
+            }
+            else {
+                decode( infile, outfile );
+            }
+            if (ferror( infile ) || ferror( outfile )) {
+                retcode = B64_FILE_IO_ERROR;
+            }
+            else {
+                 retcode = 0;
+            }
+            if( outfile != stdout ) {
+                if( fclose( outfile ) != 0 ) {
+                    perror( b64_message( B64_ERROR_OUT_CLOSE ) );
+                    retcode = B64_FILE_IO_ERROR;
+                }
+            }
+        }
+        if( infile != stdin ) {
+            fclose( infile );
+        }
+    }
+
+    return( retcode );
+}
+
+
+/* R functions */
+
+SEXP encode_(SEXP input, SEXP output, SEXP line_size){
+	int res = b64( 'e', 
+		(char*)CHAR(STRING_ELT(input,0)), 
+		(char*)CHAR(STRING_ELT(output,0)), 
+		INTEGER(line_size)[0] ) ;
+	if( res ){
+		error( "%s\n", b64_message( res ) ) ;	
+	}
+	return R_NilValue ;
+}
+
+SEXP decode_(SEXP input, SEXP output){
+	int res = b64( 'd', 
+		(char*)CHAR(STRING_ELT(input,0)), 
+		(char*)CHAR(STRING_ELT(output,0)), 
+		0 ) ;
+	if( res ){
+		error( "%s\n", b64_message( res ) ) ;	
+	}
+	return R_NilValue ;
+}
+
+

Added: pkg/base64/src/base64.h
===================================================================
--- pkg/base64/src/base64.h	                        (rev 0)
+++ pkg/base64/src/base64.h	2010-04-03 10:58:34 UTC (rev 953)
@@ -0,0 +1,33 @@
+#ifndef BASE64__BASE64_H
+#define BASE64__BASE64_H
+
+#include <R.h> 
+#include <Rinternals.h> 
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+** returnable errors
+**
+** Error codes returned to the operating system.
+**
+*/
+#define B64_SYNTAX_ERROR        1
+#define B64_FILE_ERROR          2
+#define B64_FILE_IO_ERROR       3
+#define B64_ERROR_OUT_CLOSE     4
+#define B64_LINE_SIZE_TO_MIN    5
+
+#define B64_DEF_LINE_SIZE   72                                                         
+#define B64_MIN_LINE_SIZE    4
+
+#define THIS_OPT(ac, av) (ac > 1 ? av[1][0] == '-' ? av[1][1] : 0 : 0)
+
+#define B64_MAX_MESSAGES 6
+
+SEXP encode_(SEXP input, SEXP output, SEXP line_size) ;
+SEXP decode_(SEXP input, SEXP output) ;
+
+
+#endif                                                                         



More information about the Rcpp-commits mailing list