textio.eh

Text I/O streams. More...

use "textio.eh"

Types

type Reader < Any;
type Writer < Any;

Functions

def latin1reader(in: IStream): Reader;
def latin1writer(out: OStream): Writer;
def Reader.close();
def Reader.new(in: IStream, dec: (IStream): Int): Reader;
def Reader.read(): Int;
def Reader.readarray(buf: [Char], ofs: Int, len: Int): Int;
def Reader.readline(): String;
def Reader.readstr(len: Int): String;
def Reader.skip(n: Long): Long;
def utf16reader(in: IStream): Reader;
def utf16writer(out: OStream): Writer;
def utfreader(in: IStream): Reader;
def utfwriter(out: OStream): Writer;
def Writer.close();
def Writer.flush();
def Writer.new(out: OStream, enc: (OStream, Int)): Writer;
def Writer.print(str: String);
def Writer.printf(fmt: String, args: [Any]);
def Writer.println(str: String);
def Writer.write(ch: Int);
def Writer.writearray(buf: [Char], ofs: Int, len: Int);

Description

This header provides wrappers for I/O streams, allowing to process text in different encodings. Reader is a text input stream and Writer is a text output stream. Their API copies that of IStream and OStream with the only difference that they work with characters, not bytes.

To create new reader simply wrap IStream with suitable reader function.

var r = utfreader(fopen_r("sometext.txt"))
var ch = r.read()        // reads a single character
var line = r.readline()  // reads a line of characters 
while (line != null) {
  line = r.readline()
  ...
}
r.close()

Wrappers are provided for the following encodings:

You can create a text stream for arbitrary encoding (even self made) using Reader.new and Writer.new providing encoding functions for them.

UTF-8 is the default encoding used in Alchemy OS. It is used, for example, in terminal.

Type details

type Reader < Any;
Text input stream.

type Writer < Any;
Text output stream.

Function details

def Reader.new(in: IStream, dec: (IStream): Int): Reader;
Creates new reader using the specified input stream and decoding function. On each call the decoding function should return a single character read from the given input stream. If the end of the stream is reached, this function should return EOF.

def Reader.read(): Int;
Reads next character from this reader. If the end of the stream is reached then EOF is returned.

def Reader.readarray(buf: [Char], ofs: Int, len: Int): Int;
Reads len characters from the input stream and writes them to the character array buf starting at index ofs. The function can end up in reading less characters if the end of the stream is reached. Returns total number of actually read characters. If no characters can be read because stream has reached its end then EOF is returned.

def Reader.readstr(len: Int): String;
Reads specified number of characters and returns them as the string. The function can end up reading less characters if the end of the streas is reached. If no characters can be read then null is returned.

def Reader.readline(): String;
Reads a single line of characters from this reader. If the end of the stream is reached then null is returned.

def Reader.skip(n: Long): Long;
Skips over and discards num characters. The function may, for a variety of reasons, end up skipping over some smaller number of characters, possibly 0. Returns total number of actually skipped characters.

def Reader.close();
Closes this reader and frees associated resources.

def Writer.new(out: OStream, enc: (OStream, Int)): Writer;
Creates new writer using the specified output stream and encoding function. On each call the encoding function should write the bytes representing given character to the output stream.

def Writer.write(ch: Int);
Writes given character to the output.

def Writer.writearray(buf: [Char], ofs: Int, len: Int);
Writes len characters from given character array starting from index ofs to the output.

def Writer.print(str: String);
Writes given string to the output.

def Writer.println(str: String);
Writes given string to the output and terminates a line.

def Writer.printf(fmt: String, args: [Any]);
Writes formatted string to the output using given format string and arguments. See String.format description on how formatting is used.

def Writer.flush();
Forces any buffered data to be written immediately.

def Writer.close();
Closes this writer and frees associated resources.

def utfreader(in: IStream): Reader;
Wraps given stream in a reader that uses UTF-8 encoding.

def utfwriter(out: OStream): Writer;
Wraps given stream in a writer that uses UTF-8 encoding.

def latin1reader(in: IStream): Reader;
Wraps given stream in a reader that uses ISO 8859-1 encoding (also known as Latin-1).

def latin1writer(out: OStream): Writer;
Wraps given stream in a writer that uses ISO 8859-1 encoding (also known as Latin-1).

def utf16reader(in: IStream): Reader;
Wraps given stream in a reader that uses UTF-16 encoding (little ending).

def utf16writer(out: OStream): Writer;
Wraps given stream in a writer that uses UTF-16 encoding (little ending).