From c18859eb4bac8bb6fe4d85bb3ac3bf2b4b240221 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 8 Jun 2017 10:28:24 -0700 Subject: [PATCH] streamage --- pitfall/restructure/decodestream.rkt | 12 +++++++----- pitfall/restructure/encodestream.rkt | 19 +++++++++++++++++++ pitfall/restructure/main.rkt | 3 ++- pitfall/restructure/number.rkt | 20 +++++++++----------- pitfall/restructure/struct.rkt | 4 ++-- pitfall/restructure/test.rkt | 6 +++--- 6 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 pitfall/restructure/encodestream.rkt diff --git a/pitfall/restructure/decodestream.rkt b/pitfall/restructure/decodestream.rkt index a00eec85..89c807fc 100644 --- a/pitfall/restructure/decodestream.rkt +++ b/pitfall/restructure/decodestream.rkt @@ -19,9 +19,11 @@ (values (string->symbol (format "~a~a" key endian)) value)))) - +;; basically just a wrapper for a Racket port (define-subclass object% (RDecodeStream [buffer #""]) - (field [pos 0] - [length (bytes-length buffer)] - ) - ) \ No newline at end of file + (field [length (bytes-length buffer)] + [_port (open-input-bytes buffer)]) + (getter-field [pos (port-position _port)]) + + (define/public (read-bytes count) + (read-bytes-exact count _port))) \ No newline at end of file diff --git a/pitfall/restructure/encodestream.rkt b/pitfall/restructure/encodestream.rkt new file mode 100644 index 00000000..3ce103c2 --- /dev/null +++ b/pitfall/restructure/encodestream.rkt @@ -0,0 +1,19 @@ +#lang restructure/racket +(provide (all-defined-out)) + +#| approximates +https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee +|# + +;; basically just a wrapper for a Racket outputport +(define-subclass object% (REncodeStream [bufferSize 65536]) + (field [_port (open-output-bytes)]) + (getter-field [pos (port-position _port)]) + + (define/public (dump) + (get-output-bytes _port)) + + (define/public (write val) + (cond + [(bytes? val) (write-bytes val _port)] + [else (error 'REncodeStream:write:unknown-type)]))) \ No newline at end of file diff --git a/pitfall/restructure/main.rkt b/pitfall/restructure/main.rkt index 4fae76e2..2c05543a 100644 --- a/pitfall/restructure/main.rkt +++ b/pitfall/restructure/main.rkt @@ -2,4 +2,5 @@ (r+p "number.rkt" "struct.rkt" - "decodestream.rkt") \ No newline at end of file + "decodestream.rkt" + "encodestream.rkt") \ No newline at end of file diff --git a/pitfall/restructure/number.rkt b/pitfall/restructure/number.rkt index 63d25363..decf886b 100644 --- a/pitfall/restructure/number.rkt +++ b/pitfall/restructure/number.rkt @@ -1,5 +1,5 @@ #lang restructure/racket -(require "decodestream.rkt") +(require "decodestream.rkt" "encodestream.rkt") ;; approximates https://github.com/mbutterick/restructure/blob/master/src/Number.coffee @@ -23,36 +23,34 @@ (getter-field [size (hash-ref type-sizes fn)]) (define/override (decode stream [res #f]) - (unless (input-port? stream) - (raise-argument-error 'decode "input port" stream)) - (define bstr (read-bytes-exact size stream)) + (unless (is-a? stream RDecodeStream) + (raise-argument-error 'decode "RDecodeStream" stream)) + (define bstr (send stream read-bytes size)) (if (= 1 size) (bytes-ref bstr 0) (integer-bytes->integer bstr (unsigned-type? type) (eq? endian 'BE)))) (define/override (encode stream val) (when stream - (unless (output-port? stream) - (raise-argument-error 'encode "output port" stream))) + (unless (is-a? stream REncodeStream) + (raise-argument-error 'encode "REncodeStream" stream))) (define bstr (if (= 1 size) (bytes val) (integer->integer-bytes val size (unsigned-type? type) (eq? endian 'BE)))) - (if stream (write-bytes bstr stream) bstr))) + (if stream (send stream write bstr) bstr))) (test-module (let ([o (make-object Number 'UInt16 'LE)] - [ip (open-input-bytes (bytes 1 2 3 4))] - [op (open-output-bytes)]) + [ip (make-object RDecodeStream (bytes 1 2 3 4))]) (check-equal? (send o decode ip) 513) ;; 1000 0000 0100 0000 (check-equal? (send o decode ip) 1027) ;; 1100 0000 0010 0000 (check-equal? (send o encode #f 513) (bytes 1 2)) (check-equal? (send o encode #f 1027) (bytes 3 4))) (let ([o (make-object Number 'UInt16 'BE)] - [ip (open-input-bytes (bytes 1 2 3 4))] - [op (open-output-bytes)]) + [ip (make-object RDecodeStream (bytes 1 2 3 4))]) (check-equal? (send o decode ip) 258) ;; 0100 0000 1000 0000 (check-equal? (send o decode ip) 772) ;; 0010 0000 1100 0000 (check-equal? (send o encode #f 258) (bytes 1 2)) diff --git a/pitfall/restructure/struct.rkt b/pitfall/restructure/struct.rkt index 91085678..9f804cc0 100644 --- a/pitfall/restructure/struct.rkt +++ b/pitfall/restructure/struct.rkt @@ -11,7 +11,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (define/override (decode stream [parent #f] [length 0]) (define res (_setup stream parent length)) (_parseFields stream res fields) - (hash-set! (hash-ref res '_props) '_currentOffset (port-position stream)) + (hash-set! (hash-ref res '_props) '_currentOffset (· stream pos)) res) (define/override (encode stream val [parent #f]) @@ -24,7 +24,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee ;; define hidden properties (hash-set! res '_props (mhasheq 'parent (mhasheq 'value parent) - '_startOffset (mhasheq 'value (port-position stream)) + '_startOffset (mhasheq 'value (· stream pos)) '_currentOffset (mhasheq 'value 0 'writable #t) '_length (mhasheq 'value length))) res) diff --git a/pitfall/restructure/test.rkt b/pitfall/restructure/test.rkt index 2f89e476..68c0dc38 100644 --- a/pitfall/restructure/test.rkt +++ b/pitfall/restructure/test.rkt @@ -7,7 +7,7 @@ 'age uint8))) ;; decode a person from a buffer -(define stream (open-input-bytes #"ABC")) +(define stream (make-object RDecodeStream #"ABC")) (define x (send Person decode stream)) (test-module @@ -15,8 +15,8 @@ (check-equal? (hash-ref x 'age) 67)) ;; encode a person from a hash -(define out (open-output-bytes)) +(define out (make-object REncodeStream)) (send Person encode out (hasheq 'name 16961 'age 67)) (test-module - (check-equal? (get-output-bytes out) #"ABC")) \ No newline at end of file + (check-equal? (send out dump) #"ABC")) \ No newline at end of file