473,698 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ostr eam<charT, traits>& indent(
std::basic_ostr eam<charT, traits>& strm
)
{
const long& in = strm.iword(inde x::indent());
for(long i = 0; i < in; ++i)
strm << " ";
return strm;
}

template <class charT, class traits>
std::basic_ostr eam<charT, traits>& push_indent(
std::basic_ostr eam<charT, traits>& strm
)
{
strm.iword(inde x::indent())++;
return strm;
}

template <class charT, class traits>
std::basic_ostr eam<charT, traits>& pop_indent(
std::basic_ostr eam<charT, traits>& strm
)
{
long in = strm.iword(inde x::indent());
if(in > 0)
strm.iword(inde x::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 3068
"Klaus Nowikow" <no*****@decoms ysINVALID.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*****@decoms ysINVALID.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3775
by: Dennis M. Marks | last post by:
I am never sure of when a semi-colon is required in javascript. Is there a definite rule? -- Dennis M. Marks -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World! -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
7
20948
by: jhomp ssens | last post by:
I would like to create a pulldown menu which is semi-transparent....that is, you can see the text and graphics behind it when it is pulled down. The effect I'm looking for can be seen at http://www.countrywide.com. Are there any good tutorials which can show me how to do this with CSS?
16
2832
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more semi-circular definitions and
2
3698
by: David Scemama | last post by:
Hi, I'm looking for a way to display semi graphic characters in a multi line text control or in a rich text control. I've tried with all the characters of the extended ASCII table (code page 437), they appear correctly except the semi graphic ones. Please help ... Thanks a lot
27
3169
by: StevePBurgess | last post by:
With a string of authors such as: Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea I would like to write a function to change every other comma into a semi colon (thereby defining where one name ends and the next begins). I could do it by writing a complex (and slow) procedure - but is there a quick way of doing it using regular expressions, for example?
2
2714
by: Trond Michelsen | last post by:
Hi. I have a transparent PNG-image that I would like to display on top of the rest of the web page. I've already got this part working. But, I'd like the background (as in "the part of the image that is transparent"), to be semi transparent instead. So, I've wrapped the image in a div-tag with the style "opacity: .70" instead. This gives me pretty much the semi transparency that I'm looking for, except that this makes the entire image...
2
2315
by: James Stroud | last post by:
Hello All, I am trying to create a semi-standalone with the vendor python on OS X 10.4 (python 2.3.5). I tried to include some packages with both --packages from the command and the 'packages' option in setup.py. While the packages were nicely included in the application bundle in both cases (at Contents/Resources/lib/python2.3/), they were not found by python when the program was launched, giving the error: "ImportError: No module...
0
1922
by: James Arnold | last post by:
I am trying to use a semi transparent PNG as the form background, allowing you to see through certain parts. The intention is similar to a skinnable form like launchy, with semi-transparent pixels (not just .MakeTransparent or Form.Opacity). Also similar to the Adobe splash screens which the feather and drop shadow. By overriding the OnPaint method and manually drawing a PNG onto the form background I can achieve this effect, but it does...
9
4111
by: JamesF1982 | last post by:
Hey everyone, My question is related to HTML, Javascript, CSS and ASP.NET but i think it belongs here! Upon an event i am trying to add a semi-transparent colour across the page so the background is obscurred but i also set a div with a known ID to exist above this. My current method has been to on the fly create a div which gets the screen height/width with javascript and then sets the image. However, my div is created but does not...
26
3040
by: machineghost | last post by:
First off, let me just say that as someone with no DBA training whatsoever, any help I can get with this issue will be very, very much appreciated. My company recently migrated our database from DB2 v7 to DB2 v9. We hired a consultant to help us, and things went pretty smoothly ... up until a few weeks after, when a co-worker tried to insert JavaScript in to our database. That's when we learned that v9, unlike v7, has a problem with...
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8898
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.