[Rcpp-commits] r3936 - in pkg/Rcpp: . inst/include/Rcpp/iostream src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Nov 11 14:24:14 CET 2012


Author: romain
Date: 2012-11-11 14:24:14 +0100 (Sun, 11 Nov 2012)
New Revision: 3936

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h
   pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h
   pkg/Rcpp/src/Rostream.cpp
   pkg/Rcpp/src/Rstreambuf.cpp
Log:
added Rcerr

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-11 11:24:04 UTC (rev 3935)
+++ pkg/Rcpp/ChangeLog	2012-11-11 13:24:14 UTC (rev 3936)
@@ -2,6 +2,7 @@
 
         * include/Rcpp/iostream/Rstreambuf.h: implementing sync() so that flush works
         * src/Rstreambuf.cpp: implementation
+        * src/Rostream.cpp : added Rcerr that forwards to REprintf
 
 2012-11-10  JJ Allaire <jj at rstudio.org>
 

Modified: pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h	2012-11-11 11:24:04 UTC (rev 3935)
+++ pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h	2012-11-11 13:24:14 UTC (rev 3936)
@@ -32,56 +32,19 @@
 namespace Rcpp {
 
     class Rostream : public std::ostream {
+    public:
+        Rostream( bool output ) :  buf(output), std::ostream( &buf ){}
         
     protected:
         Rstreambuf buf;
 	
-    public:
-        Rostream();
     };
     
     // declare global variable
     extern Rostream Rcout;
 
-    // template <int RTYPE> std::ostream& operator<<(std::ostream& out, const Rcpp::Vector< RTYPE >& v) {
-    //     out << "[1] " << v[0];
-    //     for (int i=1;i<v.size();i++) {
-    //         out << " " << v[i];
-    //     }
-    //     return out;  // for multiple << operators.
-    // }
-
-    // template <int RTYPE> std::ostream& operator<<(std::ostream& out, const Rcpp::Matrix< RTYPE >& m) {
-    //     // width of columns showing values
-    //     int val_col_width = 12;
-	
-    //     // width of first column, showing row index
-    //     int max_row_idx = m.rows() + 1;
-    //     int idx_col_width = 0;
-    //     while(max_row_idx > 0) {
-    //         max_row_idx /= 10;
-    //         idx_col_width++;
-    //     }
-	
-    //     // print column headers (add 1 to have R indexing)
-    //     out << std::setw( val_col_width + idx_col_width + 3 ) << "[,1]";
-    //     for (int jcol=1;jcol<m.ncol();jcol++) {
-    //         out << std::setw( val_col_width-2 ) << "[," << jcol+1 << "]";
-    //     }
-    //     out << std::endl;
-    //     for (int irow=0;irow<m.nrow();irow++) {
-    //         // print row header (add 1 to have R indexing)
-    //         out << "[" << std::setw( idx_col_width ) << irow+1 << ",]";
-	
-    //         // print values in current row
-    //         for (int jcol=0;jcol<m.ncol();jcol++) {
-    //             out << std::setw( val_col_width ) << m( irow, jcol );
-    //         }
-    //         out << std::endl;
-    //     }
-    //     return out;  // for multiple << operators.
-    // }
-
+    // declare global variable
+    extern Rostream Rcerr;
 }
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h	2012-11-11 11:24:04 UTC (rev 3935)
+++ pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h	2012-11-11 13:24:14 UTC (rev 3936)
@@ -2,7 +2,7 @@
 //
 // Rostream.h: Rcpp R/C++ interface class library -- stream buffer
 //
-// Copyright (C) 2011           Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
+// Copyright (C) 2011 - 2012    Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
 //
 // This file is part of Rcpp.
 //
@@ -32,6 +32,7 @@
 
     class Rstreambuf : public std::streambuf {
     public:
+        Rstreambuf(bool output_): output(output_){}
 		
     protected:
         virtual std::streamsize xsputn(const char *s, std::streamsize n );
@@ -39,6 +40,8 @@
         virtual int overflow(int c = EOF );
         
         virtual int sync() ;
+    private:
+        bool output ;
   };
 
 }

Modified: pkg/Rcpp/src/Rostream.cpp
===================================================================
--- pkg/Rcpp/src/Rostream.cpp	2012-11-11 11:24:04 UTC (rev 3935)
+++ pkg/Rcpp/src/Rostream.cpp	2012-11-11 13:24:14 UTC (rev 3936)
@@ -22,7 +22,7 @@
 #include <RcppCommon.h>
 #include <Rcpp/iostream/Rostream.h>
 
-Rcpp::Rostream::Rostream() : std::ostream( &buf ) {}
+// define global variable Rcout
+Rcpp::Rostream Rcpp::Rcout(true);
+Rcpp::Rostream Rcpp::Rcerr(false);
 
-// define global variable Rcout
-Rcpp::Rostream Rcpp::Rcout;

Modified: pkg/Rcpp/src/Rstreambuf.cpp
===================================================================
--- pkg/Rcpp/src/Rstreambuf.cpp	2012-11-11 11:24:04 UTC (rev 3935)
+++ pkg/Rcpp/src/Rstreambuf.cpp	2012-11-11 13:24:14 UTC (rev 3936)
@@ -2,7 +2,7 @@
 //
 // Rostream.h: Rcpp R/C++ interface class library -- stream buffer
 //
-// Copyright (C) 2011           Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
+// Copyright (C) 2011 - 2012    Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
 //
 // This file is part of Rcpp.
 //
@@ -23,13 +23,21 @@
 #include <Rcpp/iostream/Rstreambuf.h>
 
 std::streamsize Rcpp::Rstreambuf::xsputn(const char *s, std::streamsize num ) {
-    Rprintf( "%.*s", num, s );
+    if(output) {
+        Rprintf( "%.*s", num, s ) ; 
+    } else {
+        REprintf( "%.*s", num, s) ;
+    }
     return num;
 }
 
 int Rcpp::Rstreambuf::overflow(int c ) {
     if (c != EOF) {
-        Rprintf( "%.1s", &c );
+        if( output ) {
+            Rprintf( "%.1s", &c ) ;
+        } else {
+            REprintf( "%.1s", &c ) ;
+        }
     }
     return c;
 }



More information about the Rcpp-commits mailing list