2 Table I/O
(require scone/io) | package: scone |
The header row is a serialized form of the tabledef struct but for ease of authoring it accepts simplified forms according to the columndef-from/c contract. Note that the name of the table is not included in the serialized form.
(define tabledef-from/c (listof columndef-from/c))
The following are both legal serialized table definitions.
(code-point name decomposition) ((code-point number) name (decomposition number #t))
Rows in the serial form are separate datum, there is no enclosing list or vector so that a file can be read and processed row-by-row using the standard Racket reader (see The Reader). The following is a legal serialized table.
((code-point number) name (decomposition number #t)) (0 "NULL" ()) (1 "START OF HEADING" ()) (2 "START OF TEXT" ()) (3 "END OF TEXT" ())
2.1 Reading
procedure
(read-table [ table-definition ext-validator in]) → table? table-definition : read-definition/c = 'first ext-validator : (or/c row-validator/c #f) = #f in : input-port? = (current-input-port)
The table-definition is used to determine how to construct the table’s tabledef struct. See read-definition/c for options.
The ext-validator may be used to provide an external validation procedure for each row as it is read. The procedure is passed the row after validation against the table’s tabledef allowing the procedure to perform any context-specific validation.
procedure
(read-table-from-file file-path [ ext-validator table-definition #:mode mode-flag]) → table? file-path : path-string? ext-validator : (or/c row-validator/c #f) = #f table-definition : read-definition/c = 'first mode-flag : (or/c 'binary 'text) = 'binary
(with-input-from-file file-path (lambda () (read-table table-definition ext-validator)) #:mode mode-flag)
2.2 Display
procedure
(display-table table [out]) → any
table : table? out : output-port? = (current-output-port)
Example
> (display-table (select '(code_point name syntax) #:from (read-table-from-file "./tests/names-list.scone") #:where (λ (cp _ syn . __) (and (< cp 128) (eq? syn 'control)))))
┌──────────┬───────────────────────────┬───────┐
│code_point│name │syntax │
├──────────┼───────────────────────────┼───────┤
│ 127│DELETE │control│
│ 31│INFORMATION SEPARATOR ONE │control│
│ 30│INFORMATION SEPARATOR TWO │control│
│ 29│INFORMATION SEPARATOR THREE│control│
│ 28│INFORMATION SEPARATOR FOUR │control│
│ 27│ESCAPE │control│
│ 26│SUBSTITUTE │control│
│ 25│END OF MEDIUM │control│
│ 24│CANCEL │control│
│ 23│END OF TRANSMISSION BLOCK │control│
│ 22│SYNCHRONOUS IDLE │control│
│ 21│NEGATIVE ACKNOWLEDGE │control│
│ 20│DEVICE CONTROL FOUR │control│
│ 19│DEVICE CONTROL THREE │control│
│ 18│DEVICE CONTROL TWO │control│
│ 17│DEVICE CONTROL ONE │control│
│ 16│DATA LINK ESCAPE │control│
│ 15│SHIFT IN │control│
│ 14│SHIFT OUT │control│
│ 13│CARRIAGE RETURN (CR) │control│
│ 12│FORM FEED (FF) │control│
│ 11│LINE TABULATION │control│
│ 10│LINE FEED (LF) │control│
│ 9│CHARACTER TABULATION │control│
│ 8│BACKSPACE │control│
│ 7│BELL │control│
│ 6│ACKNOWLEDGE │control│
│ 5│ENQUIRY │control│
│ 4│END OF TRANSMISSION │control│
│ 3│END OF TEXT │control│
│ 2│START OF TEXT │control│
│ 1│START OF HEADING │control│
│ 0│NULL │control│
└──────────┴───────────────────────────┴───────┘
2.3 Writing
procedure
(write-table table [out]) → any
table : table? out : output-port? = (current-output-port)
procedure
(write-table-to-file table file-name [ #:mode mode-flag #:exists exists-flag #:permissions permissions]) → any table : table? file-name : (or/c path-string? #f) mode-flag : (or/c 'binary 'text) = 'binary
exists-flag :
(or/c 'error 'append 'update 'can-update 'replace 'truncate 'must-truncate 'truncate/replace) = 'error permissions : (integer-in 0 65535) = 438
Note that the values for mode-flag, exists-flag, and permissions are the same as for the standard library’s with-output-to-file procedure.
If file-name is #f the procedure will synthesize a file name from the table name in tabledef and the value of default-file-extension.
2.4 I/O Contracts
contract
(file-extension/c value) → boolean?
value : any/c
contract
(read-definition/c value) → boolean?
value : any/c
tabledef, a complete existing definition
'first, the first row is a serialized tabledef
'infer, infer a tabledef from the first data row
'none, assume all columns have the type in the default-column-data-type parameter
contract
(row-validator/c [value]) → row/c
value : row = row/c
2.5 I/O Defaults
parameter
(default-file-extension) → file-extension/c
(default-file-extension file-extension) → void? file-extension : file-extension/c
= 'scone