Mercurial > lasercutter
view src/clojure/contrib/test_contrib/pprint/examples/hexdump.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;;; hexdump.clj -- part of the pretty printer for Clojure3 ;; by Tom Faulhaber4 ;; April 3, 20096 ; Copyright (c) Tom Faulhaber, Dec 2008. All rights reserved.7 ; The use and distribution terms for this software are covered by the8 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)9 ; which can be found in the file epl-v10.html at the root of this distribution.10 ; By using this software in any fashion, you are agreeing to be bound by11 ; the terms of this license.12 ; You must not remove this notice, or any other, from this software.14 ;; This example is a classic hexdump program written using cl-format.16 ;; For some local color, it was written in Dulles Airport while waiting for a flight17 ;; home to San Francisco.19 (ns clojure.contrib.pprint.examples.hexdump20 (:use clojure.contrib.pprint21 clojure.contrib.pprint.utilities)22 (:gen-class (:main true)))24 (def *buffer-length* 1024)26 (defn zip-array [base-offset arr]27 (let [grouped (partition 16 arr)]28 (first (map-passing-context29 (fn [line offset]30 [[offset31 (map #(if (neg? %) (+ % 256) %) line)32 (- 16 (count line))33 (map #(if (<= 32 % 126) (char %) \.) line)]34 (+ 16 offset)])35 base-offset grouped))))38 (defn hexdump39 ([in-stream] (hexdump in-stream true 0))40 ([in-stream out-stream] (hexdump [in-stream out-stream 0]))41 ([in-stream out-stream offset]42 (let [buf (make-array Byte/TYPE *buffer-length*)]43 (loop [offset offset44 count (.read in-stream buf)]45 (if (neg? count)46 nil47 (let [bytes (take count buf)48 zipped (zip-array offset bytes)]49 (cl-format out-stream50 "~:{~8,'0X: ~2{~8@{~#[ ~:;~2,'0X ~]~} ~}~v@{ ~}~2{~8@{~A~} ~}~%~}"51 zipped)52 (recur (+ offset *buffer-length*) (.read in-stream buf))))))))54 (defn hexdump-file55 ([file-name] (hexdump-file file-name true))56 ([file-name stream]57 (with-open [s (java.io.FileInputStream. file-name)]58 (hexdump s))))60 ;; I don't quite understand how to invoke main funcs w/o AOT yet61 (defn -main [& args]62 (hexdump-file (first args)))