source: trunk/j/src/org/armedbear/lisp/byte-io.lisp @ 11297

Last change on this file since 11297 was 11297, checked in by ehuelsmann, 13 years ago

Set Id keyword for expansion.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1;;; byte-io.lisp
2;;;
3;;; Copyright (C) 2004-2005 Peter Graves
4;;; $Id: byte-io.lisp 11297 2008-08-31 13:26:45Z ehuelsmann $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package #:system)
21
22(defun write-byte (byte stream)
23  (declare (type stream stream))
24  (let ((element-type (stream-element-type stream)))
25    (require-type byte element-type)
26    (let ((width (cadr element-type)))
27      (if (= width 8)
28          (write-8-bits (the (unsigned-byte 8) byte) stream)
29          (let ((bytes ()))
30            (dotimes (i (/ width 8))
31              (push (logand byte #xff) bytes)
32              (setf byte (ash byte -8)))
33            (dolist (b bytes)
34              (write-8-bits (the (unsigned-byte 8) b) stream)))))
35    byte))
36
37(defun read-byte (stream &optional (eof-error-p t) eof-value)
38  (declare (type stream stream))
39  (let* ((element-type (stream-element-type stream)))
40    (unless element-type
41      (if eof-error-p
42          (error 'end-of-file :stream stream)
43          (return-from read-byte eof-value)))
44    (unless (consp element-type)
45      (error 'simple-type-error
46             :format-control "READ-BYTE: unsupported element type ~S."
47             :format-arguments (list element-type)))
48    (let ((width (cadr element-type)))
49      (if (= width 8)
50          (read-8-bits stream eof-error-p eof-value)
51          (let ((result 0))
52            (dotimes (i (/ width 8))
53              (let ((byte (read-8-bits stream eof-error-p eof-value)))
54                (when (eq byte eof-value)
55                  (return-from read-byte eof-value))
56                (setf result (ash result 8))
57                (setf result (+ result byte))))
58            (if (and (eq (car element-type) 'signed-byte)
59                     (not (zerop (logand result (expt 2 (1- width))))))
60                (- result (expt 2 width))
61                result))))))
Note: See TracBrowser for help on using the repository browser.