Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

created

  • Loading branch information...
commit 7fbebbc983756ebae2603fccbdcc95cd2d6c344a 0 parents
@dherman authored
Showing with 159 additions and 0 deletions.
  1. +9 −0 README.md
  2. +15 −0 bytes-vector.rkt
  3. +81 −0 doc.txt
  4. +12 −0 info.rkt
  5. +42 −0 main.rkt
9 README.md
@@ -0,0 +1,9 @@
+## binary.rkt
+
+Racket utilities for binary data.
+
+## License
+
+Copyright © 2012 Dave Herman
+
+Licensed under the [MIT License](http://mit-license.org).
15 bytes-vector.rkt
@@ -0,0 +1,15 @@
+#lang racket/base
+
+(provide read-bytes-list read-bytes-vector)
+
+(define default-chunk-size 1024)
+
+(define (read-bytes-list [chunk-size default-chunk-size] [in (current-input-port)])
+ (let loop ([result '()])
+ (let ([next (read-bytes chunk-size in)])
+ (if (eof-object? next)
+ (reverse result)
+ (loop (cons next result))))))
+
+(define (read-bytes-vector [chunk-size default-chunk-size] [in (current-input-port)])
+ (list->vector (read-bytes-list chunk-size in)))
81 doc.txt
@@ -0,0 +1,81 @@
+_Binary_
+_binary_
+
+This collection provides one file:
+
+ _binary.ss_: utilities for binary data
+
+======================================================================
+
+TYPES ----------------------------------------------------------------
+
+> bytes-list = (listof bytes)
+> bytes-vector = (vector bytes)
+
+
+FUNCTIONS ------------------------------------------------------------
+
+> read-bytes-list :: [integer input-port] -> bytes-list
+
+Reads a list of byte-strings from the given input port. The integer
+indicates the maximum length of each byte string (only the last byte
+string in the list may be smaller than the maximum).
+
+The default byte-string length is 1024.
+
+The default input port is the current value of (current-input-port).
+
+> read-bytes-vector :: [integer input-port] -> bytes-vector
+
+Reads a vector of byte-strings from the given input port. The integer
+indicates the maximum length of each byte string (only the last byte
+string in the vector may be smaller than the maximum).
+
+The default byte-string length is 1024.
+
+The default input port is the current value of (current-input-port).
+
+
+MACROS ---------------------------------------------------------------
+
+> compile-bytes :: (compile-bytes string) => expression<bytes>
+
+Reads up to 1024 bytes of the contents of the file indicated by the
+string, and compiles to a byte-string literal that represents the
+contents of the file.
+
+> compile-bytes :: (compile-bytes integer string) => expression<bytes>
+
+Reads up to the given number of bytes of the contents of the file
+indicated by the string, and compiles to a byte-string literal that
+represents the contents of the file.
+
+> compile-bytes-list :: (compile-bytes-list string) => expression<bytes-list>
+
+Reads the file indicated by the string and compiles to an expression
+that evaluates to a list of byte-strings of length up to 1024 each,
+the concatenation of which represents the contents of the file.
+Only the last byte string in the list may be shorter than 1024 bytes.
+
+> compile-bytes-list :: (compile-bytes-list integer string) => expression<bytes-list>
+
+Reads the file indicated by the string and compiles to an expression
+that evaluates to a list of byte-strings, the concatenation of which
+represents the contents of the file. Each byte string in the list has
+the length given in the integer argument, except possibly the last byte
+string in the list, which may be shorter.
+
+> compile-bytes-vector :: (compile-bytes-list string) => expression<bytes-vector>
+
+Reads the file indicated by the string and compiles to an expression
+that evaluates to a vector of byte-strings of length up to 1024 each,
+the concatenation of which represents the contents of the file.
+Only the last byte string in the vector may be shorter than 1024 bytes.
+
+> compile-bytes-vector :: (compile-bytes-list integer string) => expression<bytes-vector>
+
+Reads the file indicated by the string and compiles to an expression
+that evaluates to a vector of byte-strings, the concatenation of which
+represents the contents of the file. Each byte string in the vector has
+the length given in the integer argument, except possibly the last byte
+string in the vector, which may be shorter.
12 info.rkt
@@ -0,0 +1,12 @@
+#lang setup/infotab
+(define name "binary")
+(define blurb
+ (list "Utilities for binary data."))
+(define primary-file "main.rkt")
+(define doc.txt "doc.txt")
+(define categories '(io))
+(define repositories '("4.x"))
+(define required-core-version "5.0")
+(define version "2")
+(define release-notes
+ (list "Released."))
42 main.rkt
@@ -0,0 +1,42 @@
+#lang racket/base
+
+(require (for-syntax racket/base "bytes-vector.ss"))
+(require "bytes-vector.ss")
+(provide read-bytes-list read-bytes-vector)
+(provide compile-bytes compile-bytes-list compile-bytes-vector)
+
+(define-syntax (compile-bytes-list stx)
+ (syntax-case stx ()
+ [(_ in)
+ #'(compile-bytes-list 1024 in)]
+ [(_ chunk-size in)
+ (and (string? (syntax->datum #'in))
+ (integer? (syntax->datum #'chunk-size)))
+ (with-syntax ([(chunk ...) (with-input-from-file (syntax->datum #'in)
+ (lambda ()
+ (read-bytes-list (syntax->datum #'chunk-size))))])
+ #'(list chunk ...))]))
+
+(define-syntax (compile-bytes-vector stx)
+ (syntax-case stx ()
+ [(_ in)
+ #'(compile-bytes-vector 1024 in)]
+ [(_ chunk-size in)
+ (and (string? (syntax->datum #'in))
+ (integer? (syntax->datum #'chunk-size)))
+ (with-syntax ([(chunk ...) (with-input-from-file (syntax->datum #'in)
+ (lambda ()
+ (read-bytes-list (syntax->datum #'chunk-size))))])
+ #'(vector chunk ...))]))
+
+(define-syntax (compile-bytes stx)
+ (syntax-case stx ()
+ [(_ in)
+ #'(compile-bytes 1024 in)]
+ [(_ max in)
+ (and (string? (syntax->datum #'in))
+ (integer? (syntax->datum #'max)))
+ (with-syntax ([b (with-input-from-file (syntax->datum #'in)
+ (lambda ()
+ (read-bytes (syntax->datum #'max))))])
+ (datum->syntax stx #'b))]))
Please sign in to comment.
Something went wrong with that request. Please try again.