Skip to content

Commit c506e89

Browse files
move Rostream to headers
1 parent 58afc85 commit c506e89

4 files changed

Lines changed: 57 additions & 107 deletions

File tree

inst/include/Rcpp/iostream/Rostream.h

Lines changed: 0 additions & 57 deletions
This file was deleted.

inst/include/Rcpp/iostream/Rstreambuf.h

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Rstreambuf.h: Rcpp R/C++ interface class library -- stream buffer
44
//
5-
// Copyright (C) 2011 - 2012 Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
5+
// Copyright (C) 2011 - 2013 Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
66
//
77
// This file is part of Rcpp.
88
//
@@ -23,12 +23,8 @@
2323
#define RCPP__IOSTREAM__RSTREAMBUF_H
2424

2525
#include <cstdio>
26-
2726
#include <streambuf>
2827

29-
// modified from
30-
// http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
31-
3228
namespace Rcpp {
3329

3430
template <bool OUTPUT>
@@ -41,9 +37,55 @@ namespace Rcpp {
4137

4238
virtual int overflow(int c = EOF );
4339

44-
virtual int sync() ;
40+
virtual int sync() ;
4541
};
4642

43+
template <bool OUTPUT>
44+
class Rostream : public std::ostream {
45+
typedef Rstreambuf<OUTPUT> Buffer ;
46+
Buffer* buf ;
47+
public:
48+
Rostream() :
49+
std::ostream( new Buffer ),
50+
buf( static_cast<Buffer*>( rdbuf() ) )
51+
{}
52+
~Rostream() {
53+
if (buf != NULL) {
54+
delete buf;
55+
buf = NULL;
56+
}
57+
}
58+
};
59+
60+
template <> inline std::streamsize Rstreambuf<true>::xsputn(const char *s, std::streamsize num ) {
61+
Rprintf( "%.*s", num, s ) ;
62+
return num ;
63+
}
64+
template <> inline std::streamsize Rstreambuf<false>::xsputn(const char *s, std::streamsize num ) {
65+
REprintf( "%.*s", num, s ) ;
66+
return num ;
67+
}
68+
69+
template <> inline int Rstreambuf<true>::overflow(int c ) {
70+
if (c != EOF) Rprintf( "%.1s", &c ) ;
71+
return c ;
72+
}
73+
template <> inline int Rstreambuf<false>::overflow(int c ) {
74+
if (c != EOF) REprintf( "%.1s", &c ) ;
75+
return c ;
76+
}
77+
78+
template <> inline int Rstreambuf<true>::sync(){
79+
::R_FlushConsole() ;
80+
return 0 ;
81+
}
82+
template <> inline int Rstreambuf<false>::sync(){
83+
::R_FlushConsole() ;
84+
return 0 ;
85+
}
86+
static Rostream<true> Rcout;
87+
static Rostream<false> Rcerr;
88+
4789

4890
}
4991

inst/include/RcppCommon.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ namespace Rcpp{
130130

131131
#include <Rcpp/cache.h>
132132

133-
// "Rcout" iostream class contributed by Jelmer Ypma
134133
#include <Rcpp/iostream/Rstreambuf.h>
135-
#include <Rcpp/iostream/Rostream.h>
136-
137134
#include <Rcpp/longlong.h>
138135

139136
#endif

src/api.cpp

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,6 @@ namespace Rcpp {
6363
return y ;
6464
}
6565

66-
// {{{ Rostream
67-
template <> inline std::streamsize Rstreambuf<true>::xsputn(const char *s, std::streamsize num ) {
68-
Rprintf( "%.*s", num, s ) ;
69-
return num ;
70-
}
71-
template <> inline std::streamsize Rstreambuf<false>::xsputn(const char *s, std::streamsize num ) {
72-
REprintf( "%.*s", num, s ) ;
73-
return num ;
74-
}
75-
76-
template <> inline int Rstreambuf<true>::overflow(int c ) {
77-
if (c != EOF) Rprintf( "%.1s", &c ) ;
78-
return c ;
79-
}
80-
template <> inline int Rstreambuf<false>::overflow(int c ) {
81-
if (c != EOF) REprintf( "%.1s", &c ) ;
82-
return c ;
83-
}
84-
85-
template <> inline int Rstreambuf<true>::sync(){
86-
::R_FlushConsole() ;
87-
return 0 ;
88-
}
89-
template <> inline int Rstreambuf<false>::sync(){
90-
::R_FlushConsole() ;
91-
return 0 ;
92-
}
93-
Rostream<true> Rcout;
94-
Rostream<false> Rcerr;
95-
// }}}
96-
9766
// {{{ RObject
9867
void RObject::setSEXP(SEXP x){
9968
RCPP_DEBUG_2( "RObject::setSEXP(SEXP = <%p>, TYPEOF=%s )", x, type2name(x) )
@@ -1587,16 +1556,15 @@ static const char* dropTrailing0(char *s, char cdec) {
15871556
/* Note that 's' is modified */
15881557
char *p = s;
15891558
for (p = s; *p; p++) {
1590-
if(*p == cdec) {
1591-
char *replace = p++;
1592-
while ('0' <= *p && *p <= '9')
1593-
if(*(p++) != '0')
1594-
replace = p;
1595-
if(replace != p)
1596-
while((*(replace++) = *(p++)))
1597-
;
1598-
break;
1599-
}
1559+
if(*p == cdec) {
1560+
char *replace = p++;
1561+
while ('0' <= *p && *p <= '9')
1562+
if(*(p++) != '0')
1563+
replace = p;
1564+
if(replace != p)
1565+
while((*(replace++) = *(p++))) ;
1566+
break;
1567+
}
16001568
}
16011569
return s;
16021570
}

0 commit comments

Comments
 (0)