ネイティブバイトオーダー取得ユーティリティファイル
マシンのネイティブのバイトオーダーを自動的に判定できるユーティリティ関数があると便利かと思ったので作成してみた。
;# <- バイトオーダー判定用文字列 ;; ファイル名: byte-order.lisp (defun guess-byte-order (sample-file) (with-open-file (1byte sample-file :element-type '(unsigned-byte 8)) (with-open-file (2byte sample-file :element-type '(unsigned-byte 16)) (if (= (read-byte 2byte) (+ (ash (read-byte 1byte) 8) (read-byte 1byte))) :big-endian :little-endian)))) (defparameter *native-byte-order* (guess-byte-order *LOAD-PATHNAME*))
上のファイルをロードすると、その環境のバイトオーダーを保持する*native-byte-order*変数が自動的に定義される。
(load "byte-order.lisp") --> T *native-byte-order* --> :LITTLE-ENDIAN
ちなみにsbcl(1.0.44)の場合はsb-c:*backend-byte-order*という変数が同様の情報を保持していた。
sb-c:*backend-byte-order*
--> :LITTLE-ENDIAN