Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Some useful additions for the C++ standard library

, 11 Apr 2000
Defines some TCHAR compatible STL elements and gives you an std::ostream to send output to the debugger windows.
stltools.zip
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\ stdtools.h,  Several usefull additions to the C++ standard library:
/              - TCHAR compatible types and objects (for string, cout, ...)
\              - ostream support for the debugger window. Just print to
/                _tcdbg to use it
\              - several usefull string functions
/              - iostream support for __int64 types
\
/
\ Version:	1.0, created 1999-12-02
/ Author:	Daniel Lohmann (daniel@uni-koblenz.de)
\
/ Please send comments and bugfixes to the above email address.
\
/ This code is provided AS IS, you use it at your own risk! 
\ You may use it for whatever you want.
/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/

#ifndef __STDTOOLS_H__
#define __STDTOOLS_H__

// Note: I add STL-Additions to the std namespace, but prefix identifies that are not
// part of the standard with an underscore ("_"). If you don't like additions to the
// std namespace change it to whatever you want.
namespace std {


// Define TCHAR compatible elements of string/iostream
#if defined _UNICODE || defined UNICODE
	typedef wstring _tstring;
	typedef wostream _tostream;
	typedef wistream _tistream;

	#define _tcout	wcout
	#define _tcin	wcin
	#define _tcerr	wcerr
	#define _tclog	wclog
#else
	typedef string _tstring;
	typedef ostream _tostream;
	typedef istream _tistream;

	#define _tcout	cout
	#define _tcin	cin
	#define _tcerr	cerr
	#define _tclog	clog
#endif

	// The debugger output stream
	// Just dump your objects to _cdbg or _wcdbg. _tcdbg is the TCHAR compatible version
	extern wostream _wcdbg;
	extern ostream _cdbg;


	// ANSI/UNICODE overloaded OutputDebugString()
	inline void _OutDbgString( LPCSTR pszText)
	{
		::OutputDebugStringA( pszText );
	}
	inline void _OutDbgString( LPCWSTR pszText)
	{
		::OutputDebugStringW( pszText );
	}
	
	
	///////////////////////////////////
	// basic_streambuf class, that writes output to the debugger
	//
	//
	template <class E, class T = char_traits<E>, int BUF_SIZE = 512 >
	class basic_dbgstreambuf : public basic_streambuf< E, T >
	{
	public:
		basic_dbgstreambuf()
			: basic_streambuf<E,T>()
		{
			psz = new char_type[ BUF_SIZE ];
			setbuf( psz, BUF_SIZE );
			// leave place for single char + 0 terminator
			setp( psz, psz + BUF_SIZE - 2 );
  			
		}
		virtual ~basic_dbgstreambuf()
		{
			delete psz;
		}
	protected:
		virtual int_type overflow( int_type c = T::eof() )
		{
			char_type* plast = pptr();
			if( c != T::eof() )
				// add c to buffer
				*plast++ = c;
			*plast = char_type();

			_OutDbgString( pbase() );
			setp( pbase(), epptr() );

			return c != T::eof() ? T::not_eof( c ) : T::eof();
		}

		virtual int sync()
		{
			overflow();
			return 0;
		}
		virtual streamsize xsputn(const char_type* pch, streamsize n)
		{
			streamsize nMax, nPut;
			for( nPut = 0; 0 < n; )
				if( pptr() != 0 && 0 < (nMax = epptr() - pptr()) ) {
					if( n < nMax )
						nMax = n;
					traits_type::copy( pptr(), pch, nMax );
					
					// Sync if string contains LF
					bool bSync = traits_type::find( pch, nMax, traits_type::to_char_type( '\n' ) ) != NULL;
					pch += nMax, nPut += nMax, n -= nMax, pbump(nMax);
					if( bSync )
						sync();

					
				}
				else if (traits_type::eq_int_type(traits_type::eof(),
					overflow(traits_type::to_int_type(*pch))))
					break;
				else
					++pch, ++nPut, --n;
			return (nPut); }

		char_type* psz;
	};


	// The default buffers for debugger output
	basic_dbgstreambuf<CHAR>*	_GetStdDbgStreamBufA();
	basic_dbgstreambuf<WCHAR>*	_GetStdDbgStreamBufW();

	///////////////////////////////////
	// string function helpers
	//

	// sprintf - like format functions
	string	_stringf( string sFormat, ... );
	wstring	_wstringf( wstring sFormat, ... );

	// returns a string with exact cchMax chars in lenght. If the input string was
	// shorter, the remaining space will be filled with chFill. If it was larger, it
	// will be truncated and the last part will be sMore to indicate the truncation
	string _string_max( string s, int cchMax, string sMore = "~", CHAR chFill = ' ' );
	wstring _wstring_max( wstring s, int cchMax, wstring sMore = L"~", WCHAR chFill = L' ' );

// Define TCHAR compatible elements of debugger io
// and string helper functions
#if defined _UNICODE || defined UNICODE
	#define _tcdbg	_wcdbg
	#define GetStdDbgStreamBuf GetStdDbgStreamBufW
	#define _tstringf _wstringf
#else
	#define _tcdbg	_cdbg
	#define GetStdDbgStreamBuf GetStdDbgStreamBufA
	#define _tstringf _stringf
#endif



	// ostream int64 support
	ostream& operator <<( ostream& os, __int64 n );
	wostream& operator <<( wostream& os, __int64 n );
	ostream& operator <<( ostream& os, unsigned __int64 n );
	wostream& operator <<( wostream& os, unsigned __int64 n );





}	// namespace std

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Daniel Lohmann

Germany Germany
Daniel Lohmann (daniel@losoft.de) is Assistant Professor at the Distributed Systems and Operating Systems department at Friedrich-Alexander-University Erlangen-Nuremberg, Germany. His main research topic is the design of a highly customizable and scalable operating system product line for deeply embedded systems using static configuration and aspect-oriented techniques. Before joining Universität Erlangen he worked as a freelance trainer and consultant for NT system programming, advanced C++ programming and OOA/OOD. He is interested in upcoming programming techniques like aspect-oriented programming, generative programming and C++ meta coding and has written some nice and handy tools for Windows NT which you can download at his web site.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Apr 2000
Article Copyright 2000 by Daniel Lohmann
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid