471,613 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,613 software developers and data experts.

Semi-automatic indentation in streams possible?

I would like to be able to do the following:

std::cout // Or any other ostream
<< "Line 1\n"
<< push_tab
<< "Line 2\n"
<< "Line 3\n"
<< push_tab
<< "Line 4\n"
<< pop_tab
<< "Line 5"
<< std::endl;

producing the following output:
Line 1
Line 2
Line 3
Line 4
Line 5

That is, a '\n' character should automatically be replaced by '\n' plus
(4*indentation) spaces (later I want to add a tabsize() modifier).

I came up with the following "semi-solution" (sorry for the long post):

// my_ios.h ***************
#include <iostream>

struct index
{
static int indent();
};

template <class charT, class traits>
std::basic_ostream<charT, traits>& indent(
std::basic_ostream<charT, traits>& strm
)
{
const long& in = strm.iword(index::indent());
for(long i = 0; i < in; ++i)
strm << " ";
return strm;
}

template <class charT, class traits>
std::basic_ostream<charT, traits>& push_indent(
std::basic_ostream<charT, traits>& strm
)
{
strm.iword(index::indent())++;
return strm;
}

template <class charT, class traits>
std::basic_ostream<charT, traits>& pop_indent(
std::basic_ostream<charT, traits>& strm
)
{
long in = strm.iword(index::indent());
if(in > 0)
strm.iword(index::indent()) = in - 1;
return strm;
}
// eof ----------------------------

// my_ios.cpp *************************
#include "my_ios.h"
/*static*/ int index::indent()
{
static const int idx = std::ios_base::xalloc();
return idx;
}

// eof ------------------------------------

Now i can do the following:
#include "my_ios.h"
#include <iostream>

int main(int argc, char* argv[])
{
std::cout
<< indent << "Line 1\n"
<< push_indent
<< indent << "Line 2\n"
<< indent << "Line 3\n"
<< push_indent
<< indent << "Line 4\n"
<< pop_indent
<< indent << "Line 5\n"
;
return 0;
}

i.e., I have to put
<< indent
to the beginning of each output line. Things like
cout << push_indent << "Line 1\nLine 2";
producing the desired result are not possible.

Do you know of any way to do this?

Best regards,
Klaus

Jul 22 '05 #1
2 2934
"Klaus Nowikow" <no*****@decomsysINVALID.com> wrote...
I would like to be able to do the following:

std::cout // Or any other ostream
<< "Line 1\n"
<< push_tab
<< "Line 2\n"
<< "Line 3\n"
<< push_tab
<< "Line 4\n"
<< pop_tab
<< "Line 5"
<< std::endl;

producing the following output:
Line 1
Line 2
Line 3
Line 4
Line 5

That is, a '\n' character should automatically be replaced by '\n' plus
(4*indentation) spaces (later I want to add a tabsize() modifier).

[...]
Do you know of any way to do this?


I'd look into two possible ways: (a) modification of the streambuf which
when \n is output automatically appends the necessary amount of whitespace
and (b) creating your own 'endl' for the same purpose instead of using
'\n'.

Victor
Jul 22 '05 #2
Klaus Nowikow <no*****@decomsysINVALID.com> wrote in message news:<ne*********************@news.sil.at>...
I would like to be able to do the following:

std::cout // Or any other ostream
<< "Line 1\n"
<< push_tab
<< "Line 2\n"
<< "Line 3\n"
<< push_tab
<< "Line 4\n"
<< pop_tab
<< "Line 5"
<< std::endl;


You are heading in the right direction with the use of ios::iword() etc,
but to do it how you want, you really need to implement a streambuf
which intercepts the character stream looking for the start of each
new line and which interjects the appropriate spacing.

Rather than post a lot of code here, go grab a copy of OSE from:

http://ose.sourceforge.net

and look at how its OTC_TraceStream and OTC_TraceBuf classes work.
These classes implement an indenting scheme in the style that you are
looking for.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dennis M. Marks | last post: by
7 posts views Thread by jhomp ssens | last post: by
2 posts views Thread by David Scemama | last post: by
27 posts views Thread by StevePBurgess | last post: by
2 posts views Thread by Trond Michelsen | last post: by
2 posts views Thread by James Stroud | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.