Output

Every Pnyx writes to output, typically to a Stream, but can be any thing that implements ILineProcessor or IRowProcessor. All output done for a Pnyx is performed during the process state (see State Machine for details). Some of the details for writing output are documented below.

Encoding

Pnyx automatically writes "Byte Order Mark" (BOM) to output files based upon encoding. Even if the source file is missing a BOM, then it will be inserted during output. To use a specific encoding, set Settings.outputEncoding either globally or for specific Pnyx. Also, the setting Settings.outputByteOrderMarks can be set to false to skip writing BOM. The following example shows writing UTF-8 files.

Specific Encoding
using (Pnyx p = new Pnyx())
{
    p.setSettings(outputEncoding: Encoding.UTF8);
    p.read("a\nb\nc");
    p.write("myfile.txt");
}                        
// Writes a UTF-8 file regardless of source

Newlines

When writing to a Stream, the newlines characters used are derived from the first newline found in the source. This preserves the original newlines used in the source file. When outputting the final line, it will only be terminated by a newline if the source was terminated by a newline. To control which type of newlines are used, set Settings.outputNewline, illustrated in the example below:

Writing Newlines
const String input = "1\r\n2\r\n3\r\n4";
using (Pnyx p = new Pnyx())
{
    p.setSettings(outputNewline: StreamInformation.NEWLINE_UNIX);
    p.readString(input);               
    p.writeStdout();
}     
// outputs: converts windows to unix newlines

Custom End

To build a custom end, implement either ILineProcessor or IRowProcessor. This terminates all processing done by Pnyx. When processing is complete, endOfFile method is called, regardless of whether input contains data or not. The following example is a stub of saving Pnyx data to a database.

endLine
public class CustomEndLine : ILineProcessor
{        
    public void processLine(string line)
    {
        // write to databse
    }

    public void endOfFile()
    {
        // commit changes
    }
}

CustomEndLine processor = new CustomEndLine();
using (Pnyx p = new Pnyx())
{
    p.readString("a\nb\nc");                
    p.endLine(processor);
}

Next

Suggested next steps:
  • Line, learn more about Line operations
  • Row, learn more about Row operations
  • Input, learn more about Input operations
  • Settings, learn more about settings