14 template <
bool OUTPUT>
15 class Rstreambuf :
public std::streambuf {
20 virtual std::streamsize xsputn(
const char *s, std::streamsize n );
21 virtual int overflow(
int c = EOF );
25 template <
bool OUTPUT>
26 class Rostream :
public std::ostream {
27 typedef Rstreambuf<OUTPUT> Buffer ;
31 std::ostream( new Buffer ),
32 buf( static_cast<Buffer*>( rdbuf() ) )
42 template <>
inline std::streamsize Rstreambuf<true>::xsputn(
const char *s, std::streamsize num ) {
43 Rprintf(
"%.*s", static_cast<int>(num), s ) ;
46 template <>
inline std::streamsize Rstreambuf<false>::xsputn(
const char *s, std::streamsize num ) {
47 REprintf(
"%.*s", static_cast<int>(num), s ) ;
50 template <>
inline int Rstreambuf<true>::overflow(
int c) {
51 if (c != traits_type::eof()) {
52 char_type ch = traits_type::to_char_type(c);
53 return xsputn(&ch, 1) == 1 ? c : traits_type::eof();
57 template <>
inline int Rstreambuf<false>::overflow(
int c) {
58 if (c != traits_type::eof()) {
59 char_type ch = traits_type::to_char_type(c);
60 return xsputn(&ch, 1) == 1 ? c : traits_type::eof();
64 template <>
inline int Rstreambuf<true>::sync(){
68 template <>
inline int Rstreambuf<false>::sync(){
72 TMB_EXTERN Rostream<true> Rcout;
73 TMB_EXTERN Rostream<false> Rcerr;