added stdin and stdout as alternatives

This commit is contained in:
Matthias Felleisen 2013-01-05 14:31:45 -05:00
parent f78e4c7142
commit 510e74fa1c
2 changed files with 25 additions and 12 deletions

View File

@ -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

View File

@ -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