Output and text files

Print

As can be seen in previous examples, the core of XSharper output is <print> action, which outputs a string to console.

To make output a bit nicer, mimic operating system streams, and also categorize output, there are 5 output options.

Name Purpose Default direction
^out Normal output Standard output
^bold More visible output, like section names etc. Standard output
^info Optional output Standard output, unless //quiet is specified
^error Error messages Standard error
^debug Debug messages None. Debug output (can be seen with dbgview utility) if //debug switch is specified. Also copied to standard output, if //debugc switch is specified
^nul,^null No output

In many actions it's possible to specify where the output goes via outTo attribute.

<print>This goes to standard output</print>
<print outTo="^bold">This is bold</print>
<print outTo="^info">This won't be shown if executed with //quiet</print>
<print outTo="^debug">This is debug output</print>
<print outTo="^error">This is error</print>

Also the same outTo may redirect the output to a variable

<print outTo="food">Macaroni and cheese</print>
<print>Today's special is ${food}</print>

also if variable name is preceded by +, value is appended to the variable instead of overwriting it

<print outTo="food">Macaroni</print>
<print outTo="+food"> and cheese</print>
<print>Today's special is ${food}</print>

Text file may be used for output using ^# prefix. Also, append is possible by using +^# prefix. Also, after | encoding may be specified (default to UTF8 with BOM)

<print outTo="^#c:\hello.bat|ascii">echo Hello, world</print>
<shell>c:\hello.bat</shell>

By default, print appends a new line to every string output, that may be switched off using newline="false" attribute:

<print newline="false">This is normal.</print>
<print newline="false">This is bold</print>
<print>This is normal again<print> 

Redirection

Output to one stream may be redirected to another:

<redirect outTo="^error">
	<print>This goes to error</print>
</redirect>

Or to a file (in UTF8 w/o BOM):

<set filename="output.txt|utf8/nobom" />
<redirect outTo="^#${filename}">
	<print>Line1</print>
	<print>Line2</print>
</redirect>

Or can add + to append:

<set filename="output.txt|utf8/nobom" />
<redirect outTo="+^#${filename}">
	<print>Line3</print>
	<print>Line4</print>
</redirect>

ReadText

Reading a text file into a variable is easy.

<readtext from="file.txt" outTo="x" />

Or just can out it to console instead:

<readtext from="file.txt" outTo="^out" />

If it's known in advance that the file is in UTF8

<readtext from="file.txt|utf8" outTo="x" />

or

<readtext from="file.txt " outTo="x" encoding="utf8" />

Or, if the file is on the web-server:

<readtext from="http://www.ncjrs.gov/txtfiles/victcost.txt" outTo="x" />

Finally, a multi-expression can be used as well (although XML often better demonstrates writer's intentions)

<set x="${= c.ReadText('http://www.ncjrs.gov/txtfiles/victcost.txt')}" />

WriteText

Writing a text file is just as easy

<set txt="Hello, world" />
<writeText to="c:\file.txt" encoding="utf8/nobom" />

To save as UTF16 with byte order mark:

<set txt="Hello, world" />
<writeText to="c:\file.txt" encoding="utf16/bom" />

Or, can just use print instead

<set txt="Hello, world" />
<print newline='false' outTo='^#c:\file.txt|utf16/bom'>${x}</print>