![]() |
![]() |



Author: bane
Date Submitted: 2004-05-04 05:48:22
===========
constream.h
===========
/*
* constream.h - Interface for constream, an ostream for writing
* to the Cipher console.
*/
#ifndef constream_h
#define constream_h
#include <streambuf>
#include <iostream>
class conbuf : public std::wstreambuf
{
static const int_type eof = traits_type::eof();
public:
conbuf();
virtual ~conbuf();
protected:
// Outputs a (wide) character to the buffer (that is, the cipher console).
virtual int_type overflow(int_type c = eof);
// Since this is not an input stream, we want both pbackfail() and underflow() to
// return EOF (thus indicating failure). It turns out that the default behavior (the
// base class implementation) does just that.
//virtual int pbackfail(int c = eof);
//virtual int underflow();
// we don?t do input so always return EOF
virtual int_type uflow() { return eof; }
// we don?t do input so always return 0 chars read
virtual std::streamsize xsgetn(char_type *, std::streamsize) {return 0;}
// put n characters
virtual std::streamsize xsputn(const char_type *, std::streamsize);
// We use the default behavior for all remaining virtual protected
// functions. See Sec. 21.6.4 of Stroustrup.
};
extern conbuf console_buffer;
extern std::wostream cphrout;
#endif
=============
constream.cpp
=============
#include "constream.h"
#include "cg_local.h" // or whatever you need for the console funcs.
#include <string>
using namespace std;
// The global buffer and stream
conbuf console_buffer;
wostream cphrout(&console_buffer);
conbuf::conbuf() : wstreambuf()
{
// No state
}
conbuf::~conbuf()
{
}
conbuf::int_type conbuf::overflow(conbuf::int_type c)
{
if (c != eof)
cipher_con_Printf(_T("%c"), (char_type)c);
return c;
}
streamsize conbuf::xsputn(const char_type *p, streamsize n)
{
// We don't really necessarily print n chars of p.
cipher_con_Print(_T("%ls"), p);
// If this bothers you, you could do something like:
// cipher_con_Printf((wstring(_T("%"))+n+wstring(_T("ls"))).c_str(), p);
return n;
}This is kind of a trivial code submission, but it's still a nice idea if you have a bunch of existing techniques or algorithms that depend on STL-style output streams and their myriad formatting abilities and would like the Cipher console to work with them. Just include "constream.h" and you're set for doing things like:
cphrout << _T("Hello, world!") << endl;
and basically anything you can do with cout.
Cheers.
[Recent Contributions] [Recent Source Code]
User Contributed Comments
con_Printf(_T("%ls"), p);con_Print(p);
printf("%s", p);
printf(p);