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

Last change on this file since 9266 was 9179, checked in by piso, 16 years ago

Work in progress (tested).

File size: 2.4 KB
Line 
1;;; byte-io.lisp
2;;;
3;;; Copyright (C) 2004-2005 Peter Graves
4;;; $Id: byte-io.lisp,v 1.6 2005-05-15 19:23:51 piso Exp $
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  (let ((element-type (stream-element-type stream)))
24    (require-type byte element-type)
25    (let ((width (cadr element-type)))
26      (if (= width 8)
27          (write-8-bits byte stream)
28          (let ((bytes ()))
29            (dotimes (i (/ width 8))
30              (push (logand byte #xff) bytes)
31              (setf byte (ash byte -8)))
32            (dolist (b bytes)
33              (write-8-bits b stream)))))
34    byte))
35
36(defun read-byte (stream &optional (eof-error-p t) eof-value)
37  (let* ((element-type (stream-element-type stream)))
38    (unless element-type
39      (if eof-error-p
40          (error 'end-of-file :stream stream)
41          (return-from read-byte eof-value)))
42    (unless (consp element-type)
43      (error 'simple-type-error
44             :format-control "READ-BYTE: unsupported element type ~S."
45             :format-arguments (list element-type)))
46    (let ((width (cadr element-type)))
47      (if (= width 8)
48          (read-8-bits stream eof-error-p eof-value)
49          (let ((result 0))
50            (dotimes (i (/ width 8))
51              (let ((byte (read-8-bits stream eof-error-p eof-value)))
52                (when (eq byte eof-value)
53                  (return-from read-byte eof-value))
54                (setf result (ash result 8))
55                (setf result (+ result byte))))
56            (if (and (eq (car element-type) 'signed-byte)
57                     (not (zerop (logand result (expt 2 (1- width))))))
58                (- result (expt 2 width))
59                result))))))
Note: See TracBrowser for help on using the repository browser.