Indentation stack

Top  Previous  Next

Scripts > Class elements and c++ instructions > Parser class methods > Plugin methods > Indentation stack

 

A frequent task is to indent some parts of the output. To facilitate this task there is a member in the plugin class, which manages a list (a stack) of values for indentation. One such value denotes the number of characters, e.g. blanks, which shall be sent ahead the real text of a line. A stack of such values is needed, to handle nested indentations.

 

Example:

 

Typical cases are indentations to improve the readability of program code:

 

for ( int i = 0; i < 10; i++)

{

if ( i == 5 )

{

func1 ();

func2 ();

}

}

 

Here at first the for-loop is indented, then the if-clause and then the function calls are indented further again. When a brace closes, the indentation is set back to the previous value. 

If the not indented texts of the lines are stored in a vector (vstr) v, the output shown above can be created by:

 

{{

PushIndent(2);  // indent the whole text by two characters

 

for( int i == 0; i < v.size(); i++)

{

if( v[ I ] == "}";  // if a closing brace follows, set the indentation back

PopIndent( 2 );

 

out << indent << v[ i ];

 

if( v[ I ] == "{";  // increased indentation after an opening brace

IncrIndent( 2 );

}

}}

 

 

The central instruction of this example is:

 

out << indent << v[ i ];

 

 

indent is a class element, which contains the values for the indentations, but also can be used - as shown here - for direct formatting of the output. For this it is passed to a stream, that means, by being inserted in the chain of output elements connected by "<<".  Normally, this insertion will take place at the first position. Otherwise gaps would arise instead of indents.

 

indent can only be used inside of a <<-chain.

 

 

str  IndentStr() const

 

By this function you get a string, which only consists of spaces. The length of the string is determined by the value on top of the indentation-stack.

 

 

Methods, which determine the indentation value, are:

 

void SetIndenter(char xc)

 

Per default blanks are used for the indentation. With the function SetIndenter another character can be set, e.g. the tabulator: '\t'.

 

 

void PushIndent( int xi )

 

By this instruction the new value xi for indentation is pushed on the stack.

 

 

void IncrIndent( int xi )

 

By this instruction also a new value for indentation is pushed on the stack. The new value is the sum of the value on top of the stack and xi.

 

void PopIndent()

 

The value for indentation on top of the stack is removed.

 

 

void ClearIndents()        

 

All values are removed from the indentation stack.

 

 

 



This page belongs to the TextTransformer Documentation

Home  Content  German