guile-rsv

R7RS Scheme library for reading and writing RSV (Rows of String Values) data format. Specified in https://github.com/Stenway/RSV-Specification and demonstrated in https://www.youtube.com/watch?v=tb_70o6ohMA
git clone https://kaka.farm/~git/guile-rsv
Log | Files | Refs | README | LICENSE

rows-streams.scm (1764B)


      1 ;;; Scheme implementation of RSV - Rows of String Values.
      2 ;;; Copyright (C) 2024  Yuval Langer.
      3 ;;;
      4 ;;; This program is free software: you can redistribute it and/or modify
      5 ;;; it under the terms of the GNU General Public License as published by
      6 ;;; the Free Software Foundation, either version 3 of the License, or
      7 ;;; (at your option) any later version.
      8 ;;;
      9 ;;; This program is distributed in the hope that it will be useful,
     10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 ;;; GNU General Public License for more details.
     13 ;;;
     14 ;;; You should have received a copy of the GNU General Public License
     15 ;;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     16 
     17 (define-library (rsv rows-streams)
     18   (import (scheme base)
     19           (scheme file)
     20           (scheme write)
     21 
     22           (srfi srfi-41)
     23 
     24           (rsv internal))
     25   (export port->rsv-row-stream)
     26 
     27   (begin
     28     (define-stream (port->rsv-row-stream port)
     29       (unless (binary-port? port)
     30         (error "Provided port should be a binary port."
     31                port))
     32 
     33       (let loop ((row-count 0)
     34                  (column-count 0))
     35         (let ((byte (peek-u8 port)))
     36           (cond
     37            ((eof-object? byte) stream-null)
     38 
     39            ((legal-rsv? byte)
     40             (let-values (((new-row new-row-count new-column-count)
     41                           (read-rsv-row row-count column-count port)))
     42               (stream-cons new-row
     43                            (loop new-row-count
     44                                  new-column-count))))
     45 
     46            (else
     47             (error "Illegal value returned when reading RSV row: (row number, illegal value)"
     48                    row-count
     49                    byte))))))))