How to write output with an indent

J

Jayden Shui

Hello All,

I'd like to have an itemized output like

1. Topic
1.1 Sub Topic
1.2 Sub Topic
1.2.1 Sub Sub Topic
1.2.2 Sub Sub Topic
1.3 Sub Topic
2. Topic

and so on.

The indent is increased when a sub is introduced. My question is that
how can I use stream to handle such-like output?

I deeply appreciate your kind help and suggestion.

Best regards,

Jayden
 
V

Victor Bazarov

I'd like to have an itemized output like

1. Topic
1.1 Sub Topic
1.2 Sub Topic
1.2.1 Sub Sub Topic
1.2.2 Sub Sub Topic
1.3 Sub Topic
2. Topic

and so on.

The indent is increased when a sub is introduced. My question is that
how can I use stream to handle such-like output?

I deeply appreciate your kind help and suggestion.

You need to write a custom outputter class. It would keep the current
indent and you would tell it when to increase or decrease the indent.
Somehow it would track the lines it outputs and introduce the needed
number of spaces after a newline character...

How is this a C++ language question?

V
 
J

Jayden Shui

You need to write a custom outputter class.  It would keep the current
indent and you would tell it when to increase or decrease the indent.
Somehow it would track the lines it outputs and introduce the needed
number of spaces after a newline character...

How is this a C++ language question?

V

Thank you for your quick response. The question comes from output a
readable tuple (such as boost::tuple) whose element can be a tuple.
I'd like to put the indent parameter in the ostream class. Is that
what you suggested? But I don't know how to do it?

Thanks again!

Jayden
 
L

Larry Evans

Hello All,

I'd like to have an itemized output like

1. Topic
1.1 Sub Topic
1.2 Sub Topic
1.2.1 Sub Sub Topic
1.2.2 Sub Sub Topic
1.3 Sub Topic
2. Topic

and so on.

The indent is increased when a sub is introduced. My question is that
how can I use stream to handle such-like output?

I deeply appreciate your kind help and suggestion.

Best regards,

Jayden

That's what:

http://svn.boost.org/svn/boost/sand...ostreams/utility/indent_scoped_ostreambuf.hpp

An example of it's use is found here:

http://svn.boost.org/svn/boost/sand...libs/array_stepper/examples/solve_tridiag.cpp

HTH.

-Regards,
Larry
 
L

Larry Evans

Hello All,

I'd like to have an itemized output like

1. Topic
1.1 Sub Topic
1.2 Sub Topic
1.2.1 Sub Sub Topic
1.2.2 Sub Sub Topic
1.3 Sub Topic
2. Topic
[snip]
That's what:

http://svn.boost.org/svn/boost/sand...ostreams/utility/indent_scoped_ostreambuf.hpp

An example of it's use is found here:

http://svn.boost.org/svn/boost/sand...libs/array_stepper/examples/solve_tridiag.cpp
A better example is:

http://svn.boost.org/svn/boost/sand...treams/test/indent_scoped_ostreambuf_test.cpp
 
J

Jayden Shui

Hello All,
I'd like to have an itemized output like
1. Topic
    1.1 Sub Topic
    1.2 Sub Topic
          1.2.1 Sub Sub Topic
          1.2.2 Sub Sub Topic
    1.3 Sub Topic
2. Topic
[snip]
That's what:

An example of it's use is found here:

A better example is:

http://svn.boost.org/svn/boost/sandbox/variadic_templates/libs/iostre...

I think this should work. But it is too much for me. How can I writ a
new ostream that derived from std::eek:stream with indent parameter that
I can manipulate like

class MyOstream : public ostream
{
public:
// Constrictor?

private:
ostream mOs.
int mIndent;
static int const INDENT_INCREMENT = 4;
}

How to write the constructor for this class?

Thanks a lot!
 
L

Larry Evans

On 11/13/11 10:04, Jayden Shui wrote:
Hello All,
I'd like to have an itemized output like
1. Topic
1.1 Sub Topic
1.2 Sub Topic
1.2.1 Sub Sub Topic
1.2.2 Sub Sub Topic
1.3 Sub Topic
2. Topic [snip]
That's what:

An example of it's use is found here:

A better example is:

http://svn.boost.org/svn/boost/sandbox/variadic_templates/libs/iostre...

I think this should work. But it is too much for me.

Could you explain what you mean by "too much". I guess you
mean you'd want something so complicated. HOwever, since it's
already written, you don't have to deal with complications.
Well, that's not really true when you get a compile error and
have to decipher the error messages; hence, maybe you've got a
point.
How can I writ a
new ostream that derived from std::eek:stream with indent parameter that
I can manipulate like

class MyOstream : public ostream
{
public:
// Constrictor?

private:
ostream mOs.
int mIndent;
static int const INDENT_INCREMENT = 4;
}

How to write the constructor for this class?

Before the indent_scoped ostream_buf code, there was a class
called marg_ostream. That worked; however, I had to replace every
call to std::cout with a call to a marg_ostream constructed ffrom
std::cout. Also, I had to define operator<< for each primitive type and
marg_ostream. This was way too much trouble; hence, the
indent_scoped_ostream_buf code was produced. Now, you don't have
to replace every call to std::cout with a call to some new class.
That's because the change is made to the buffer pointer within
std::cout. The reason for the scoped part of the name is that
when the instance of indent_scoped_ostream_buf goes out of scope, the
indent_scoped_ostream_buf destructor restores the original buffer
pointer; hence, no more indentation.

I'd guess that after you've tried something like MyOstream and got it
to work, you'll find the difficulty of using it is more of a burden
than any complexity of indent_scoped_ostream_buf.

HTH.
Thanks a lot!

You're welcome.

Good luck.

-regards,
Larry
 
J

Jayden Shui

On 11/13/11 10:33, Larry Evans wrote:
On 11/13/11 10:04, Jayden Shui wrote:
Hello All,
I'd like to have an itemized output like
1. Topic
    1.1 Sub Topic
    1.2 Sub Topic
          1.2.1 Sub Sub Topic
          1.2.2 Sub Sub Topic
    1.3 Sub Topic
2. Topic
[snip]
That's what:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/iostr....
An example of it's use is found here:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/sandbox/ste....
A better example is:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/libs/iostre....
I think this should work. But it is too much for me.

Could you explain what you mean by "too much".  I guess you
mean you'd want something so complicated.  HOwever, since it's
already written, you don't have to deal with complications.
Well, that's not really true when you get a compile error and
have to decipher the error messages; hence, maybe you've got a
point.








How can I writ a
new ostream that derived from std::eek:stream with indent parameter that
I can manipulate like
class MyOstream : public ostream
{
public:
  // Constrictor?
private:
  ostream mOs.
  int mIndent;
  static int const INDENT_INCREMENT = 4;
}
How to write the constructor for this class?

Before the indent_scoped ostream_buf code, there was a class
called marg_ostream.  That worked; however, I had to replace every
call to std::cout with a call to a marg_ostream constructed ffrom
std::cout.  Also, I had to define operator<< for each primitive type and
marg_ostream.  This was way too much trouble; hence, the
indent_scoped_ostream_buf code was produced.  Now, you don't have
to replace every call to std::cout with a call to some new class.
That's because the change is made to the buffer pointer within
std::cout.  The reason for the scoped part of the name is that
when the instance of indent_scoped_ostream_buf goes out of scope, the
indent_scoped_ostream_buf destructor restores the original buffer
pointer; hence, no more indentation.

I'd guess that after you've tried something like MyOstream and got it
to work, you'll find the  difficulty of using it is more of a burden
than any complexity of indent_scoped_ostream_buf.

HTH.


Thanks a lot!

You're welcome.

Good luck.

-regards,
Larry

Hi, Larry,

Your advice opened my mind and I'd like to follow it. I checked my
installed boost version 1.47.0 and didn't find those files. I think I
may need add them to some corresponding directories. And I also need
read input with comments led by some symbol such as '#'. I found one
example in http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

If you have any suggestion, please let me know.

Thanks again and best regards!

Jayden
 
L

Larry Evans

Hi, Larry,

Your advice opened my mind and I'd like to follow it. I checked my
installed boost version 1.47.0 and didn't find those files. I think I
may need add them to some corresponding directories.

They are not in boost, yet. To use them, you'll have to check them out
with svn.

svn checkout
https://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/iostreams
And I also need
read input with comments led by some symbol such as '#'. I found one
example in http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

If you have any suggestion, please let me know.

The indent_scoped_ostreambuf is implemented using the *output* filter:

http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/iostreams/filter/indent.hpp

What you're describing sounds like an *input* filter; hence, I guess
this is a mostly unrelated question.

I've no experience with input filters; hence, I can't be much help here.
Sorry.
Thanks again and best regards!

You're most welcome, Jayden

-regards
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top