Re: Having trouble with streams....
On Aug 10, 9:55 pm, Pete Becker <p...@versatilecoding.com> wrote:
SpreadTooThin wrote:
This constructor is causing the application to crash on Mac OS X, in
xcode...
dbgFile::dbgFile(void) : std::ostream(0), output_buffer(),
indent_buffer(&output_buffer), isopen(false) {}
here is part of the class definition:
class dbgFile : public std::ostream {
private:
std::filebuf output_buffer;
dbgBuf indent_buffer;
std::stack<std::string> s;
int stack_level;
bool isopen;
}
I suspect that std::ostream(0) is the culprit, but not sure how to
tell...
Read the documentation. std::ostream has only one constructor,
and that takes a pointer to the streambuf object that the
ostream is supposed to manage.
And that pointer can be null, in which case, std::ios should set
badbit, which should in turn guarantee that the pointer is never
dereferenced.
There's no other way to give it a streambuf, so you must do
that with the constructor.
What does std::basic_ios< charT, traits >::rdbuf(
std::basic_streambuf< charT, traits >* ) do, then?
Passing a null pointer is not a good idea. Give it the address
of the std::filebuf object.
If what you want is to output directly to a file:-). (That
seems to be his case, but most of the time, it won't be. And in
fact, I suspect that what he really needs here is a filtering
streambuf---one of the members has indent in the name, and
indent is best handled by a filtering streambuf, with say an
inserter along the lines of:
class IndentInserter
{
public:
explicit IndentInserter( int indent )
: myIsAtStartOfLine( true )
, myIndentBuffer( indent, ' ' ) {}
int insert( std::streambuf& dest, int ch )
{
if ( myIsAtStartOfLine && ! myIndentBuffer.empty() ) {
dest.sputn( ourIndentBuffer, myIndent ) ;
}
myIsAtStartOfLine = (ch == '\n') ;
return dest.sputc( ch ) ;
}
void setIndent( size_t newIndent )
{
myIndentBuffer.resize( newIndent, ' ' ) ;
}
private:
bool myIsAtStartOfLine ;
std::vector< char >
myIndentBuffer ;
} ;
Of course, he still needs a std::filebuf somewhere; the
filtering streambuf does need a sink.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34