On this page:
2.1 Reading
read-table
read-table-from-file
2.2 Display
display-table
2.3 Writing
write-table
write-table-to-file
2.4 I/  O Contracts
file-extension/  c
read-definition/  c
row-validator/  c
2.5 I/  O Defaults
default-file-extension
8.12

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)
Read a table from the provided 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
Read a table from the file at file-path; this is equivalent to the following:

(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)
This procedure will print the table as formatted output using the Text Table module. By default numeric values are right-aligned, strings and symbols are left- aligned and booleans are centered.

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)
Write the in-memory table in scone form to the provided 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
Write the in-memory table in scone form to the provided file path.

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
A contract that describes how read and read-from-file are to create the new table’s definition.

contract

(row-validator/c [value])  row/c

  value : row = row/c
A contract that defines a procedure which will take a row (row/c) and return row if it succeeds. If the procedure fails it is expected to signal an error.

2.5 I/O Defaults🔗ℹ

parameter

(default-file-extension)  file-extension/c

(default-file-extension file-extension)  void?
  file-extension : file-extension/c
 = 'scone
...