K
Klaus Nowikow
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
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