SVIO Library
1. Design goals
The SVIO library is an attempt to
- unify the API for accessing input data and producting output data on programming competitions
- move the input parsing overhead out of the evaluated program
The SVIO library is an extension to a separate project, programming competition sandbox and supervisor (see Exectime Project). The supervisor and exectime components are not described in this document.
2. API
Thie SVIO library provides a simple API consisting of a following functions:
int read_int(void); unsigned int read_uint(void); long long read_longlong(void); unsigned long long read_ulonglong(void); void read_string(char* buf); int read_strlen(void); char read_char(void); void write_int(int v); void write_uint(unsigned int v); void write_longlong(long long v); void write_ulonglong(unsigned long long v); void write_string(const char* s); void write_char(char c); void write_newline(void); void svio_flush(void);
The above interface can be used in C/C++ programs. Pascal unit provides a bit simpler interface:
unit svio; interface function read_longint(): longint; cdecl; function read_strlen(): longint; cdecl; function read_char(): char; cdecl; procedure write_longint(v: longint); cdecl; procedure write_char(v: char); cdecl; procedure write_newline(); cdecl;
General description
Processing input
From the programmer's point of view, you can assume, that the library works in this way: When the program is being run, the input is preprocessed to obtain an intermediate representation of the input, which can be quickly accessed using the provided functions. This preprocessing is done in the following way:
- all lines of the input file are joined together by replacing newlines with spaces
the input is split into tokens. A token is a nonempty fragment of the input between whitespace.
- each token is classified into one of the following categories
integer (-263 <= n < 263)
- string (any sequence of characters, which cannot be parsed as a number)
- each token is converted to some machine-friendly representation and put in a fragment of memory shared between the supervisor and the executed program
Please note, that trying to read a token of some category (for instance an integer) using input function for some other category (for instance read_string) will lead to undefined behavior.
Producing output
The process of producing the output file is more or less the reverse of the above description. The subtle difference is a possibility to split the output into lines in a controlled manner.
Usage patterns
Integers
Integers can be simply read using read_int and the company.
Writing an integer using write_int and such will output that integer and a single space after it.
Strings
There are two methods for reading strings. The first one looks as follows:
int i, len = read_strlen(); for (i=0; i<len; i++) { char c = read_char(); /* do something with this character */ }
And in Pascal:
var: i, len: longint; c: char; begin len := read_strlen(); for i := 1 to len do begin c := read_char(); { do something } end end
The second method is available in C/C++ only and looks like:
char buf[LARGE_ENOUGH]; read_string(buf);
To output a string, use can use write_char, which simply outputs a single character, which is passed as a parameter. Alternatively, use write_string, which outputs the entire string at once (wihtout appending any spaces after the string, contrary to what is was with the integers).
Newlines
All newlines must be explicitly written using write_newline. write_char('\n') will work too, but is not recommended.