Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
VarExpander Class Reference

#include <elm/io/VarExpander.h>

+ Inheritance diagram for VarExpander:

Public Member Functions

 VarExpander ()
 
 VarExpander (OutStream &out)
 
char escape () const
 
VarExpanderescape (char e)
 
VarExpanderresetBrace ()
 
VarExpanderbrace (char o, char c)
 
string expand (string s)
 
int write (const char *buffer, int size) override
 
int write (char byte) override
 
int flush (void) override
 
CString lastErrorMessage (void) override
 
bool supportsANSI () const override
 
- Public Member Functions inherited from OutStream
virtual ~OutStream (void)
 

Protected Member Functions

virtual string resolve (string name, bool &record)
 

Additional Inherited Members

- Static Public Attributes inherited from OutStream
static OutStreamnull = _null
 

Detailed Description

This class a character flow filter replacing symbol invocation by the symbol value. Basically, a symbol invocation is of an escape character (default to '$') followed by a brace open character (one of '(', '{'), the symbol name and a brace close character (one of ')', '}'). This string is replaced in the character flow with the string associated with the symbol.

The association is implemented by a call to function VarExpander::resolve(). If required, the association is cached and no more call to this function is performed for this symbol. To specialize the variable expansion, one has to overload this function. The default implementation returns an empty string.

This class may be used as an OutStream or simply by passing a string and getting the result from the function VarExpander::expand().

The code below expands the variable of the chacter flow using OS environment variables:

class OSExpander: public io::VarExpander {
protected:
string resolve(string name, bool& rec) override {
return sys::System::getEnv(&name);
}
};
OSExpander e;
sys::Path p = e.expand("${HOME}/mydir");

Constructor & Destructor Documentation

◆ VarExpander() [1/2]

Default expander constructor: output is performed to starandard output.

◆ VarExpander() [2/2]

VarExpander ( OutStream out)

Expander constructor with the given output.

Parameters
outOutput stream to use.

Member Function Documentation

◆ brace()

VarExpander & brace ( char  o,
char  c 
)
inline

Added the given set of brace characters.

Parameters
oOpen brace character.
cClose brace character.

◆ escape() [1/2]

char escape ( ) const
inline

Get escape character.

Returns
Escape chacter.

◆ escape() [2/2]

VarExpander & escape ( char  e)
inline

Change the escape character.

Parameters
enew escape character.

◆ expand()

string expand ( string  s)

Expand the given string.

Parameters
sString to expand.
Returns
s with symbols expanded.

References CString::chars(), Output::flush(), String::length(), StringBuffer::stream(), String::toCString(), StringBuffer::toString(), and VarExpander::write().

Referenced by elm::io::expandOSVars().

◆ flush()

int flush ( void  )
overridevirtual

Cause the current stream to dump its buffer to the medium.

Returns
0 for success, less than 0 for error.

Implements OutStream.

References OutStream::flush().

◆ lastErrorMessage()

CString lastErrorMessage ( void  )
overridevirtual

Return a message for the last error.

Returns
Message of the last error.

Reimplemented from OutStream.

References OutStream::lastErrorMessage().

◆ resetBrace()

VarExpander & resetBrace ( )
inline

Reset the list of supported open-close characters.

◆ resolve()

string resolve ( string  name,
bool record 
)
protectedvirtual

This function has to be overridden to provide symbol definition. The default implementation just returns the empty string.

Parameters
nameSymbol name to look for.
recordIf set to false, the symbol will not be cached.
Returns
Symbol value corresponding to the given symbol name.

Reimplemented in OSExpander.

Referenced by VarExpander::write().

◆ supportsANSI()

bool supportsANSI ( ) const
overridevirtual

Test if the current stream knows how to decode ANSI special codes. The default implementation returns false.

Returns
True if ANSI code are supported, false else.

Reimplemented from OutStream.

References OutStream::supportsANSI().

◆ write() [1/2]

int write ( char  byte)
overridevirtual

Write a byte to the stream.

Parameters
byteByte to write.
Returns
1 for success or less than 0 for an error.

Reimplemented from OutStream.

References elm::io::byte(), CString::chars(), String::indexOf(), String::length(), elm::io::p(), StringBuffer::reset(), VarExpander::resolve(), String::toCString(), StringBuffer::toString(), and OutStream::write().

◆ write() [2/2]

int write ( const char *  buffer,
int  size 
)
overridevirtual

This is the main method of an output stream: the given buffer is put on the stream.

Parameters
bufferByte buffer to write.
sizeSize of the byte buffer.
Returns
Number of transferred bytes or less than 0 for an error.

Implements OutStream.

Referenced by VarExpander::expand().


The documentation for this class was generated from the following files:
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::sys::System::getEnv
static cstring getEnv(cstring key)
elm::io::VarExpander::resolve
virtual string resolve(string name, bool &record)
Definition: io_VarExpander.cpp:214