;;; File: ;;; read-line.ss ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 1.0 of 20 September 2006 ;;; Summary: ;;; A set of procedures to help you read lines from a file. Taken ;;; from the reading on files. ;;; History: ;;; At end. ; +------------+------------------------------------------------------ ; | Procedures | ; +------------+ ;;; Procedure: ;;; read-line ;;; Parameters: ;;; source, an input port ;;; Purpose: ;;; Read one line of input from a source and return that line ;;; as a string. ;;; Produces: ;;; line, a string ;;; Preconditions: ;;; The source is open for reading. [Unverified] ;;; Postconditions: ;;; Has read characters from the source (thereby affecting ;;; future calls to read-char and peek-char). ;;; line represents the characters in the file from the ;;; "current" point at the time read-line was called ;;; until the first end-of-line or end-of-file character. ;;; line does not contain a newline. (define read-line (lambda (source) ; Read all the characters remaining on the line and ; then convert them to a string. (list->string (read-line-of-chars source)))) ;;; Procedure: ;;; read-line-of-chars ;;; Parameters: ;;; source, an input port ;;; Purpose: ;;; Read one line of input from a source and return that line ;;; as a list of characters. ;;; Produces: ;;; chars, a list of characters. ;;; Preconditions: ;;; The source is open for reading. [Unverified] ;;; Postconditions: ;;; Has read characters from the source (thereby affecting ;;; future calls to read-char and peek-char). ;;; chars represents the characters in the file from the ;;; "current" point at the time read-line was called ;;; until the first end-of-line or end-of-file character. ;;; chars does not contain a newline. (define read-line-of-chars (lambda (source) ; If we're at the end of the line or the end of the file, ; then there are no more characters, so return the empty list. (cond ; If we're at the end of the file, there are no more characters, ; so return the empty list. ((eof-object? (peek-char source)) null) ; If we're at the end of the line, we're done with the line ; skip over the end-of-line character and return the empty list. ((char=? (peek-char source) #\newline) (read-char source) null) ; Otherwise, read the current character, read the remaining ; characters, and join them together. (else (cons (read-char source) (read-line-of-chars source)))))) ; +---------+--------------------------------------------------------- ; | History | ; +---------+ ; Wednesday, 20 September 2006 (v 1.0) [Samuel A. Rebelsky] ; * Created.