Skip to content

Commit

Permalink
created
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Herman committed Feb 27, 2012
0 parents commit 7fbebbc
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 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 changes: 15 additions & 0 deletions 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 changes: 81 additions & 0 deletions 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 changes: 12 additions & 0 deletions 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 changes: 42 additions & 0 deletions 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))]))

0 comments on commit 7fbebbc

Please sign in to comment.