also, here's a patch to Gloss which I've used locally but which may be
generally useful.

Cheers -- Eric

"Eric Schulte" <schulte.e...@gmail.com> writes:

> Thanks for sharing this library, I found reading your code to be very
> useful.
>
> I need to be able to read and write elf files from within Clojure code,
> and Gloss initially looked like a good option.  However much of the
> streaming support and frame-based conception got in the way of my
> particular needs.
>
> In the end I have written a much simpler collection of functions which
> supports parsing binary files into lists of bytes, manipulating these
> lists, and then writing the lists back to binary files.  This is heavily
> influenced by the code in Gloss.  See https://github.com/eschulte/bin-ed
>
> Anyways just thought I'd share, if anyone notices anything in the code
> which doesn't make sense, or could be more easily accomplished in some
> other way please do let me know.
>
> Thanks -- Eric
>From a5888053767a0a3de798e432a15b26d98fa8b78f Mon Sep 17 00:00:00 2001
From: Eric Schulte <schulte.e...@gmail.com>
Date: Mon, 3 Jan 2011 19:33:07 -0500
Subject: [PATCH] adding primitive support for uint16, uint32 and uint64

---
 src/gloss/data/primitives.clj |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/src/gloss/data/primitives.clj b/src/gloss/data/primitives.clj
index 6f8ad4c..f524d21 100644
--- a/src/gloss/data/primitives.clj
+++ b/src/gloss/data/primitives.clj
@@ -26,6 +26,18 @@
     (string? x) (-> x first int byte)
     :else (throw (Exception. (str "Cannot convert " x " to byte.")))))
 
+(defn uint16 "Convert to a uint16" [x]
+  (let [int16 (short x)]
+    (if (neg? int16) (+ 0x10000 int16) int16)))
+
+(defn uint32 "Convert to a uint32" [x]
+  (let [int32 (int x)]
+    (if (neg? int32) (+ 0x100000000 int32) int32)))
+
+(defn uint64 "Convert to a uint64" [x]
+  (let [int64 (long x)]
+    (if (neg? int64) (+ 0x10000000000000000 int64) int64)))
+
 (defmacro primitive-codec [accessor writer size typecast transform-fn]
   `(reify
      Reader
@@ -54,7 +66,10 @@
 (def primitive-codecs
   {:byte (primitive-codec .get .put 1 byte to-byte) 
    :int16 (primitive-codec .getShort .putShort 2 short identity)
+   :uint16 (primitive-codec .getShort .putShort 2 uint16 identity)
    :int32 (primitive-codec .getInt .putInt 4 int identity)
+   :uint32 (primitive-codec .getInt .putInt 4 uint32 identity)
    :int64 (primitive-codec .getLong .putLong 8 long identity)
+   :uint64 (primitive-codec .getLong .putLong 8 uint64 identity)
    :float32 (primitive-codec .getFloat .putFloat 4 float identity)
    :float64 (primitive-codec .getDouble .putDouble 8 double identity)})
-- 
1.7.1

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to