Click here to Skip to main content
11,479,096 members (57,136 online)
Click here to Skip to main content
Add your own
alternative version

Some useful additions for the C++ standard library

, 11 Apr 2000 73.7K 575 22
Defines some TCHAR compatible STL elements and gives you an std::ostream to send output to the debugger windows.
stltools.zip
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\ stdtools.cpp,  Implementation of the simple C++ standard library additions
/
\ 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.
/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/

#include "stdafx.h"
#include <malloc.h>
#include "stdtools.h"

namespace std {

	
// Maximum lenght of return buffers for the _stringf() functions	
const BUFFER_MAX = 512;


// Define the global debugger out stream objects
// Since one may use them in constructor code of global objects, we
// initialize them very early
#pragma warning( disable: 4074 )	// warning C4074: initializers put in compiler reserved initialization area
#pragma init_seg( compiler )

basic_dbgstreambuf<WCHAR> g_StdDbgStreamBufW;
basic_dbgstreambuf<CHAR>  g_StdDbgStreamBufA;

basic_ostream<WCHAR> _wcdbg( &g_StdDbgStreamBufW );
basic_ostream<CHAR> _cdbg( &g_StdDbgStreamBufA );

#pragma data_seg()

basic_dbgstreambuf<WCHAR>*	_GetStdDbgStreamBufW(
		)
{
	return &g_StdDbgStreamBufW;
}

basic_dbgstreambuf<CHAR>*	_GetStdDbgStreamBufA(
		)
{
	return &g_StdDbgStreamBufA;
}


string	_stringf ( 
		string sFormat, 
		... 
		)
{
	va_list args;
	va_start( args, sFormat );

	LPSTR pszBuffer = (LPSTR) _alloca( BUFFER_MAX );
	
	if( _vsnprintf( pszBuffer, BUFFER_MAX, sFormat.c_str(), args ) == -1 )
		return " <ERROR: _stringf() internal buffer overflow!> ";
	return pszBuffer;
}

wstring	_wstringf( 
		wstring sFormat, 
		... 
		)
{
	va_list args;
	va_start( args, sFormat );

	LPWSTR pszBuffer = (LPWSTR) _alloca( BUFFER_MAX * 2);
	
	if( _vsnwprintf( pszBuffer, BUFFER_MAX, sFormat.c_str(), args ) == -1 )
		return L" <ERROR: _stringf() internal buffer overflow!> ";
	return pszBuffer;
}

string _string_max( 
		string s, 
		int cchMax, 
		string sMore,		// = "~"
		CHAR chFill		// = ' ' 
		)
{
	int cch = s.length();
	if( cch <= cchMax ) {
		s.resize( cchMax, chFill);
	}
	else {
		s.resize( cchMax - sMore.length() );
		s += sMore;
	}
	return s;
}

wstring _wstring_max( 
		wstring s, 
		int cchMax, 
		wstring sMore,		// = "~"
		WCHAR chFill		// = ' ' 
		)
{
	int cch = s.length();
	if( cch <= cchMax ) {
		s.resize( cchMax, chFill);
	}
	else {
		s.resize( cchMax - sMore.length() );
		s += sMore;
	}
	return s;
}

// ostream int64 overloads
template< class int_type, class stream_type >
int _format_int( const stream_type& stream, int_type n, stream_type::char_type*& psz )
{
	stream_type::fmtflags flags = stream.flags();
	if( flags & stream_type::hex ) {
		*psz++ = (stream_type::char_type) '0';
		*psz++ = (stream_type::char_type) 'x';
		return 16;
	}
	else if( flags & stream_type::oct ) {
		*psz++ = (stream_type::char_type) '0';
		return 8;
	}
	else if( flags & stream_type::showpos && n > 0 )
		*psz++ = (stream_type::char_type) '+';
	return 10;
}

ostream& operator <<( ostream& os, __int64 n )
{
	char szBuf[ 32 ];
	char* psz = szBuf;
	int nBase = _format_int( os, n, psz );
	_i64toa( n, psz, nBase );
	return os << szBuf;
}

wostream& operator <<( wostream& os, __int64 n )
{
	wchar_t szBuf[ 32 ];
	wchar_t* psz = szBuf;
	int nBase = _format_int( os, n, psz );
	_i64tow( n, psz, nBase );
	return os << szBuf;
}

ostream& operator <<( ostream& os, unsigned __int64 n )
{
	char szBuf[ 32 ];
	char* psz = szBuf;
	int nBase = _format_int( os, n, psz );
	_ui64toa( n, psz, nBase );
	return os << szBuf;
}

wostream& operator <<( wostream& os, unsigned __int64 n )
{
	wchar_t szBuf[ 32 ];
	wchar_t* psz = szBuf;
	int nBase = _format_int( os, n, psz );
	_ui64tow( n, psz, nBase );
	return os << szBuf;
}

}	// namespace std;

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.150520.1 | Last Updated 12 Apr 2000
Article Copyright 2000 by Daniel Lohmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid