StrOps::Expand2( StrBuf &, StrPtr &, StrDict & )
Expand “[%var%|alt\]” strings into corresponding
“val” strings from a StrDict
, or “alt” if
“var” is undefined.
Virtual? |
No |
|
Class |
||
Arguments |
|
the output string |
|
the input string |
|
|
the var/value pairs to look up |
|
Returns |
|
Notes
Like Expand()
, this
function provides a way to quickly expand variables from a
StrDict
into a StrBuf
, with the additional
feature of providing alternate text if the value is not defined.
The exact syntax of the expression to be expanded is:
[ text1 %var% text2 | alt
\]
If variable “var” has value "val" in the StrDict d
,
the expression expands to:
text1 val text2
otherwise, it expands to:
alt
See the example for details.
Example
This small program demonstrates the Expand2()
method in an OutputStat()
implementation:
void MyClientUser::OutputStat( StrDict *varList )
{
StrBuf s = StrBuf();
s.Set( "stat: [File: %depotFile%|No file]!" );
StrBuf o = StrBuf();
StrOps::Expand2( o, s, *varList );
StrOps::Dump( o );
}
int main( int argc, char **argv )
{
ClientApi client;
MyClientUser ui;
Error e;
client.SetProtocol( "tag", "" );
client.Init( &e );
client.SetArgv( argc - 2, argv + 2 );
client.Run( argv[1], &ui );
return client.Final( &e );
}
Executing the preceding code produces the following output:
% a.out files * stat: File: //depot/src/file1.c! stat: File: //depot/src/file2.c! % a.out labels stat: No file! stat: No file! stat: No file! stat: No file! stat: No file!