How to write output with an indent

Discussion in 'C++' started by Jayden Shui, Nov 13, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    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
    Jayden Shui, Nov 13, 2011
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Nov 13, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Nov 13, 11:14 am, Victor Bazarov <> 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
    Jayden Shui, Nov 13, 2011
    #3
  4. Jayden Shui

    Larry Evans Guest

    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/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
    Larry Evans, Nov 13, 2011
    #4
  5. Jayden Shui

    Larry Evans Guest

    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/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
    Larry Evans, Nov 13, 2011
    #5
  6. Jayden Shui

    Jayden Shui Guest

    On Nov 13, 11:49 am, Larry Evans <> 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/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. 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!
    Jayden Shui, Nov 13, 2011
    #6
  7. Jayden Shui

    Larry Evans Guest

    On 11/13/11 15:43, Jayden Shui wrote:
    > On Nov 13, 11:49 am, Larry Evans <> 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/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
    Larry Evans, Nov 13, 2011
    #7
  8. Jayden Shui

    Jayden Shui Guest

    On Nov 13, 5:23 pm, Larry Evans <> wrote:
    > On 11/13/11 15:43, Jayden Shui wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Nov 13, 11:49 am, Larry Evans <> 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/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
    Jayden Shui, Nov 13, 2011
    #8
  9. Jayden Shui

    Larry Evans Guest

    On 11/13/11 16:48, Jayden Shui wrote:
    > On Nov 13, 5:23 pm, Larry Evans <> 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/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
    Larry Evans, Nov 13, 2011
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. owais
    Replies:
    1
    Views:
    1,977
    Ed Kaim [MSFT]
    Jan 21, 2004
  2. Replies:
    0
    Views:
    1,399
  3. Steve Franks
    Replies:
    5
    Views:
    33,418
    Juan T. Llibre
    Sep 24, 2005
  4. dt
    Replies:
    4
    Views:
    469
    CBFalconer
    Dec 31, 2006
  5. Replies:
    0
    Views:
    1,064
Loading...

Share This Page