From 510e74fa1cda8c6491a1ba13b2d277cc72d9b943 Mon Sep 17 00:00:00 2001 From: Matthias Felleisen Date: Sat, 5 Jan 2013 14:31:45 -0500 Subject: [PATCH] added stdin and stdout as alternatives --- collects/2htdp/batch-io.rkt | 22 ++++++++++++++----- .../2htdp/scribblings/batch-io.scrbl | 15 +++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/collects/2htdp/batch-io.rkt b/collects/2htdp/batch-io.rkt index 1cdf88ef1f..dd81e852b0 100644 --- a/collects/2htdp/batch-io.rkt +++ b/collects/2htdp/batch-io.rkt @@ -2,6 +2,7 @@ (require racket/function racket/file + racket/format racket/string racket/local (for-syntax racket/base @@ -13,10 +14,17 @@ ;; todo? ;; -- export tokenization? -(define *input-devices* `((standard-in ,current-input-port))) -(define *output-devices* `((standard-out ,current-output-port))) +;; I am tryiing to use these lists to automate the documentation of the +;; functions but my scribble skills are insufficient and my time is running +;; out. +(module devices racket/base + (provide *input-devices* *output-devices*) + (define *input-devices* `((stdin ,current-input-port) (standard-in ,current-input-port))) + (define *output-devices* `((stdout ,current-output-port) (standard-out ,current-output-port)))) -;; ----------------------------------------------------------------------------- +(require (submod "." devices)) + +;;--------------------------------------------------------------------------------------------------- (provide simulate-file) ;; syntax (simulate-file reader string ...) (provide @@ -185,13 +193,17 @@ ;; effect: ensure that f is a file in current directory or report error for t (define (check-input-file f t) (define d? (assq f *input-devices*)) - (check-arg t (or (string? f) d?) (format "string or one of: ~a" (map car *input-devices*)) "first" f) + (check-arg t (or (string? f) d?) (error-message (map car *input-devices*)) "first" f) (check-arg t (or d? (file-exists? f)) "name of file in program's folder" "first" f)) ;; effect: ensure that f is a file in current directory or report error for t (define (check-output-file f t) (define d? (assq f *output-devices*)) - (check-arg t (or (string? f) d?) (format "string or one of: ~a" (map car *output-devices*)) "first" f)) + (check-arg t (or (string? f) d?) (error-message (map car *output-devices*)) "first" f)) + +;; [Listof Symbol] -> String +(define (error-message los) + (string-append "string or one of: " (string-join (map ~e los) ", "))) ;; split : String [Regexp] -> [Listof String] ;; splits a string into a list of substrings using the given delimiter diff --git a/collects/teachpack/2htdp/scribblings/batch-io.scrbl b/collects/teachpack/2htdp/scribblings/batch-io.scrbl index 03f54e9bb4..caba9aa7e5 100644 --- a/collects/teachpack/2htdp/scribblings/batch-io.scrbl +++ b/collects/teachpack/2htdp/scribblings/batch-io.scrbl @@ -4,6 +4,7 @@ scribble/html-properties scribble/latex-properties 2htdp/batch-io "shared.rkt" + (for-syntax racket) (for-label scheme teachpack/2htdp/batch-io)) @(require scheme/runtime-path) @@ -32,14 +33,14 @@ @; ----------------------------------------------------------------------------- -@(define-syntax reading - (syntax-rules () - [(_ name ctc s) - @defproc[(@name [f (or/c 'standard-out (and/c string? file-exists?))]) @ctc ]{ +@(define-syntax (reading stx) + (syntax-case stx () + [(reading name ctc s) + #`@defproc[(@name [f (or/c 'standard-in 'stdin (and/c string? file-exists?))]) @ctc ]{ reads the standard input device (until closed) or the content of file @racket[f] and produces it as @list[s].}] - [(_ name ctc [x ctc2] s ...) - @defproc[(@name [f (or/c 'standard-out (and/c string? file-exists?))] [@x @ctc2]) @ctc ]{ + [(reading name ctc [x ctc2] s ...) + #`@defproc[(@name [f (or/c 'standard-in 'stdin (and/c string? file-exists?))] [@x @ctc2]) @ctc ]{ reads the standard input device (until closed) or the content of file @racket[f] and produces it as @list[s ...].}])) @@ -144,7 +145,7 @@ elements. There is only one writer function at the moment: @itemlist[ -@item{@defproc[(write-file [f (or/c 'standard-out string?)] [cntnt string?]) string?]{ +@item{@defproc[(write-file [f (or/c 'standard-out 'stdout string?)] [cntnt string?]) string?]{ sends @racket[cntnt] to the standard output device or turns @racket[cntnt] into the content of file @racket[f], located in the same folder (directory) as the program. If the write succeeds, the