#lang racket/base (require "main.rkt") (require racket/match) (require racket/port) (module+ main (require racket/cmdline) (define input-format 'any) (define output-format 'binary) (define indent? #t) (define annotations? #t) (define count +inf.0) (command-line #:once-each ["--atob" "Text to binary" (begin (set! input-format 'text) (set! output-format 'binary))] ["--all" "Read until EOF" (set! count +inf.0)] ["--count" n "Read n items" (set! count (string->number n))] ["--btoa" "Binary to text" (begin (set! input-format 'binary) (set! output-format 'text))] [("--ia" "--input-any") "Autodetect input mode (default)" (set! input-format 'any)] [("--ib" "--input-binary") "Set binary input mode" (set! input-format 'binary)] [("--it" "--input-text") "Set text input mode" (set! input-format 'text)] [("--ob" "--output-binary") "Set binary output mode" (set! output-format 'binary)] [("--ot" "--output-text") "Set text output mode" (set! output-format 'text)] ["--indent" "Enable indent and set text output mode" (set! output-format 'text) (set! indent? #t)] ["--no-indent" "Disable indent and set text output mode" (set! output-format 'text) (set! indent? #f)] ["--annotations" "Output annotations" (set! annotations? #t)] ["--no-annotations" "Strip annotations" (set! annotations? #f)]) (struct embedded (value) #:transparent) (let loop ((count count)) (when (positive? count) (define v ((if annotations? values strip-annotations) (match input-format ['any (read-preserve #:read-syntax? #t #:decode-embedded embedded #:source "")] ['text (read-preserve/text #:read-syntax? #t #:decode-embedded embedded #:source "")] ['binary (read-preserve/binary #:decode-embedded embedded #:read-syntax? #t)]))) (when (not (eof-object? v)) (void (match output-format ['text (write-preserve/text v #:indent indent? #:encode-embedded embedded-value) (newline)] ['binary (write-preserve/binary v #:encode-embedded embedded-value #:write-annotations? #t)])) (flush-output) (loop (- count 1))))))