Output

Every Pnyx writes to output, typically to a Stream, but can be anything that implements ILineProcessor, IRowProcessor, IObjectProcessor or INameValuePairProcessor. 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
await 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";
await using (Pnyx p = new Pnyx())
{
    p.setSettings(outputNewline: StreamInformation.NEWLINE_UNIX);
    p.readString(input);               
    p.writeStdout();
}     
// outputs: converts Windows or Mac to Unix newlines

Custom End

To build a custom end, implement either ILineProcessor, IRowProcessor, IObjectProcessor or INameValuePairProcessor. 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 Task processLine(string line)
    {
        // write to database
        return Task.CompletedTask;
    }

    public Task endOfFile()
    {
        // commit changes
        return Task.CompletedTask;
    }
}

CustomEndLine processor = new CustomEndLine();
await 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
  • Object, examples of Object operations
  • NameValuePair, examples of Name-Value Pair operations
  • Input, learn more about Input operations
  • Settings, learn more about settings