HomeNewsFeaturesLicensingDownloadsScreenshotsFAQRoadmap Contact Us
Search:
8 Online

Community

Discussion Topics Recent Postings User Contributions General Articles Example Documentation Credits

Licensed Developer

Programmer's Manual Artists Manual Tutorials and Articles

Programming: Game Code

Game Code Overview Server Side Game Code Client Side Game Code

Programming: System

Alphabetical Function List Renderer File System Collision & Ray Casting Low Level Audio Game Audio The Console Console Variable List Multiplayer Localisation Maths Library Memory Manager Model File Formats Texture Formats

Art: Overviews

Specifications Shaders Particle Systems Lens Flares Cipher console Cipher file types Tutorials Reference

Art: Tools

Shader Designer Particle Designer 3dsmax tools Model Conversion Font Generator

Cipher Engine
Game Development Search Engine
GameDev.net
You are not signed in - [sign in] [register]

STL Cipher Console Stream

Overview

Author: bane
Date Submitted: 2004-05-04 05:48:22

Source Code

===========
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;
}


Description

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

rikh 4th May, 2004 07:09
Cool.


seryu 4th May, 2004 07:54
It looks very handy :-)

ill give it a try


bane 4th May, 2004 16:21
Just a quick correction - the print statement in conbuf::xsputn() should obviously be printf.
Edited 4th May, 2004 16:21


ebrownlee 4th May, 2004 16:37
If char_type is a wide string, why use
con_Printf(_T("%ls"), p);

...instead of...
con_Print(p);

for xsputn?


bane 4th May, 2004 20:00
If p is a user-supplied string, it is standard practice with printf-style functions to use

printf("%s", p);

rather than

printf(p);

for security and stability reasons. The second form is one of the most common avoidable programmer errors that can lead to buffer overruns and all sorts of nastiness. Just consider what would happen if p happens to contain the '%' character...


ebrownlee 5th May, 2004 02:23
You seem to have misread my post. My suggestion was to avoid using a formatted print function altogether.


bane 5th May, 2004 04:19
Ah, good point :)
I guess I had also assumed that con_Print was implemented in terms of con_Printf, and so con_Printf would be just a bit faster, but checking the cipher sources, it seems to be the other way around. So, yeah, assuming you don't care about outputting precisely n characters, you're better off using con_Print(p).
Thanks for catching that :)




Register and Sign In to discuss this article