;;; File: ;;; readutils.ss ;;; Author: ;;; Samuel A. Rebelsky ;;; Summary: ;;; A collection of procedures that may be helpful as you deal with ;;; files. ;;; Version: ;;; 1.0 of November 2000 ;;; Acknowledgements: ;;; See individual methods. ;;; ;;; Contents: ;;; ;;; History: ;;; 7 November 2000 ;;; Created. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Character-based Utilities ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Procedure: ;;; read-line ;;; Parameters: ;;; An open input port. ;;; Purpose: ;;; Read one line of input from a source and return that line ;;; as a string. ;;; Produces: ;;; A string that corresponds to that line of input. If at ;;; the end of file, produces the empty string. ;;; Preconditions: ;;; The source is open for reading. [Unverified] ;;; Post: ;;; Has read characters from the source (thereby affecting ;;; future calls to read-char and peek-char) (define read-line (lambda (source) (list->string (read-line-of-chars source)))) ;;; Procedure: ;;; read-line-of-chars ;;; Purpose: ;;; Read one line of input from a source and return that line ;;; as a list of characters. Does not include the newline. ;;; Produces: ;;; A list of characters. If at the end of the file, returns ;;; the empty list. ;;; Preconditions: ;;; The source is open for reading. [Unverified] ;;; Post: ;;; Has read characters from the source (thereby affecting ;;; future calls to read-char and peek-char) (define read-line-of-chars (lambda (source) (let ((next (read-char source))) (if (or (eof-object? next) (char=? next #\newline)) null (cons next (read-line-of-chars source)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Value-based Utilities ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Procedure: ;;; read-line-of-values ;;; Parameters: ;;; An open port ;;; Purpose: ;;; Read a line of values from a port/file. ;;; Produces: ;;; A list of values. If at the end of file, produces the ;;; empty list. ;;; Preconditions: ;;; The port is open for reading. [Unverified] ;;; Postconditions: ;;; Has read values from the port. (define read-line-of-values (lambda (source) (cond ; If nothing is left in the file, there's nothing on the line. ; So read the eol mark and return an empty list. ((eof-object? (peek-char source)) (begin (read-char source) null)) ; If the next character in the file is end-of-line, there's ; nothing else on the line ; So read the eol mark and return an empty list. ((char=? (peek-char source) #\newline) (begin (read-char source) null)) ; Otherwise, read the next thing and recurse. (else (cons (read source) (read-line-of-values source))))))