From 26b3cb7eb2bd84d70da344fff37d6b283128327d Mon Sep 17 00:00:00 2001 From: Matthias Felleisen Date: Thu, 8 Apr 2010 22:14:36 +0000 Subject: [PATCH] batch io expanded svn: r18766 --- collects/2htdp/batch-io.ss | 53 ++++++++++++++++++++++++++------ collects/2htdp/tests/batch-io.ss | 43 ++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 collects/2htdp/tests/batch-io.ss diff --git a/collects/2htdp/batch-io.ss b/collects/2htdp/batch-io.ss index 90e4cf995f..341ebedc03 100644 --- a/collects/2htdp/batch-io.ss +++ b/collects/2htdp/batch-io.ss @@ -3,19 +3,44 @@ (require htdp/error) (define (read-file f) - (check-arg 'read-file (string? f) "string" "first" f) + (check-file f 'read-file) (check-arg 'read-file (file-exists? f) "name of file in program's folder" "first" f) (list->string - (with-input-from-file f - (lambda () - (let loop ([accu '()]) - (define nxt (read-char)) - (if (eof-object? nxt) - (reverse (if (char=? (car accu) #\newline) (cdr accu) accu)) - (loop (cons nxt accu)))))))) - + (with-input-from-file f + (lambda () + (let loop ([accu '()]) + (define nxt (read-char)) + (if (eof-object? nxt) + (reverse (if (char=? (car accu) #\newline) (cdr accu) accu)) + (loop (cons nxt accu)))))))) + +(define (read-file-as-lines f) + (check-file f 'read-file-as-lines) + (with-input-from-file f + (lambda () + (let loop ([accu '()]) + (define nxt (read-line)) + (if (eof-object? nxt) + (reverse accu) + (loop (cons nxt accu))))))) + +(define (read-file-as-1strings f) + (check-file f 'read-file-as-1strings) + (read-chars f string)) + +;; +(define (read-chars f action) + (with-input-from-file f + (lambda () + (let loop ([accu '()]) + (define nxt (read-char)) + (if (eof-object? nxt) + (reverse (if (char=? (car accu) #\newline) (cdr accu) accu)) + (loop (cons nxt accu))))))) + (define (write-file f str) - (check-arg 'read-file (string? f) "string" "first" f) + (check-arg 'write-file (string? f) "name of file (string)" "first" f) + (check-arg 'write-file (string? str) "string" "second" str) (let ([result (not (file-exists? f))]) (with-output-to-file f (lambda () (printf "~a" str)) @@ -25,6 +50,9 @@ ;; ----------------------------------------------------------------------------- (provide + read-file-as-lines ;; String -> [Listof String] + ;; read the fule f (in current-directory) as a list of strings + read-file ;; String -> String ;; read the file f (in current-directory) as a string @@ -34,6 +62,11 @@ ;; true, if f doesn't exist ) +;; String[file name] Symbol -> Void +;; effect: ensure that f is a file in current directory or report error for t +(define (check-file f t) + (check-arg t (string? f) "string" "first" f) + (check-arg t (file-exists? f) "name of file in program's folder" "first" f)) ; ; diff --git a/collects/2htdp/tests/batch-io.ss b/collects/2htdp/tests/batch-io.ss new file mode 100644 index 0000000000..fedf11d209 --- /dev/null +++ b/collects/2htdp/tests/batch-io.ss @@ -0,0 +1,43 @@ +#lang scheme + +(require schemeunit) +(require 2htdp/batch-io) + +(define file "batch-io.txt") + +(define test1 #<