Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to write output with an indent

Reply
Thread Tools

How to write output with an indent

 
 
Jayden Shui
Guest
Posts: n/a
 
      11-13-2011
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-13-2011
On 11/13/2011 11:04 AM, Jayden Shui wrote:
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Jayden Shui
Guest
Posts: n/a
 
      11-13-2011
On Nov 13, 11:14*am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 11/13/2011 11:04 AM, Jayden Shui wrote:
>
>
>
>
>
>
>
>
>
> > 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
> --
> I do not respond to top-posted replies, please don't ask


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
 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      11-13-2011
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
>
> 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/sandb...ostreambuf.hpp

An example of it's use is found here:

http://svn.boost.org/svn/boost/sandb...ve_tridiag.cpp

HTH.

-Regards,
Larry

 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      11-13-2011
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/sandb...ostreambuf.hpp
>
> An example of it's use is found here:
>
> http://svn.boost.org/svn/boost/sandb...ve_tridiag.cpp
>

A better example is:

http://svn.boost.org/svn/boost/sandb...ambuf_test.cpp
 
Reply With Quote
 
Jayden Shui
Guest
Posts: n/a
 
      11-13-2011
On Nov 13, 11:49*am, Larry Evans <cppljev...@suddenlink.net> wrote:
> 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/sandb...es/boost/iostr...

>
> > An example of it's use is found here:

>
> >http://svn.boost.org/svn/boost/sandb...es/sandbox/ste...

>
> A better example is:
>
> http://svn.boost.org/svn/boost/sandb...es/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: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!
 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      11-13-2011
On 11/13/11 15:43, Jayden Shui wrote:
> On Nov 13, 11:49 am, Larry Evans <cppljev...@suddenlink.net> wrote:
>> 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/sandb...es/boost/iostr...

>>
>>> An example of it's use is found here:

>>
>>> http://svn.boost.org/svn/boost/sandb...es/sandbox/ste...

>>
>> A better example is:
>>
>> http://svn.boost.org/svn/boost/sandb...es/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: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


 
Reply With Quote
 
Jayden Shui
Guest
Posts: n/a
 
      11-13-2011
On Nov 13, 5:23*pm, Larry Evans <cppljev...@suddenlink.net> wrote:
> On 11/13/11 15:43, Jayden Shui wrote:
>
>
>
>
>
>
>
>
>
> > On Nov 13, 11:49 am, Larry Evans <cppljev...@suddenlink.net> wrote:
> >> 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/sandb...es/boost/iostr....

>
> >>> An example of it's use is found here:

>
> >>>http://svn.boost.org/svn/boost/sandb...es/sandbox/ste....

>
> >> A better example is:

>
> >>http://svn.boost.org/svn/boost/sandb...es/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: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...doc/index.html

If you have any suggestion, please let me know.

Thanks again and best regards!

Jayden


 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      11-13-2011
On 11/13/11 16:48, Jayden Shui wrote:
> On Nov 13, 5:23 pm, Larry Evans <cppljev...@suddenlink.net> wrote:

[snip]
> 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/sand...oost/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...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/sandb...ter/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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
java 1.5.0_11: How to indent xml output by DOMImplementationLS and LSSerializer Rolf.Kemper@eu.necel.com Java 0 05-31-2007 08:12 AM
gnu indent: how to avoid extra indent? dt C Programming 4 12-31-2006 11:31 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
How can I set the TreeView.Indent = 0? owais ASP .Net 1 01-21-2004 04:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57