Subject: [clisp-list] problem reading bytes as characters

From: Thibault Langlois <[email-masked]>
Date: Fri May 2 09:39:34 2003

Hello,
This is related with a problem I've posted to c.l.l but it seems to be
specific to clisp.
The following function writes bytes to a file and read them back as
characters. 

(defun my-ugly-test ()
   (print (lisp-implementation-type))
   (print (lisp-implementation-version))
   (let* ((file 
            (progn
              (with-open-file (f "/tmp/test.bin" 
                               :direction :output
                               :element-type 'unsigned-byte
                               :if-exists :supersede)
                 (loop for b from 10 to 13 
                       do           
                       (write-byte b f)))                                                                                
              "/tmp/test.bin"))
         (v-code (with-open-file (f file :direction :input)
                     (loop for c = (read-char f nil nil)
                           when c collect (cons c (char-code c))
                           while c)))
         (v-byte (with-open-file (f file :direction :input 
                                  :element-type 'unsigned-byte)
                   (loop for c = (read-byte f nil nil)
                         when c collect c
                         while c))))
     (mapcar #'cons v-byte v-code)))

Although the char-code and code-char function work as expected, I do
obtain the expected result form the test function:

[13]> (char-code #\return)
13
[14]> (char-code #\newline)
10
[15]> (code-char (char-code #\return))
#\Return
[16]> (code-char 10)
#\Newline
[17]> (code-char 13)
#\Return
[19]> (my-ugly-test)
 
"CLISP"
"2.27 (released 2001-07-17) (built 3214131121) (memory 3250927427)"
((10 #\Newline . 10) (11 #\Vt . 11) (12 #\Page . 12) (13 #\Newline .
10))

Why is byte 13 read as #\newline char (and not #\return) ?

Using Lispworks I get:

CL-USER 68 > (my-ugly-test)

"LispWorks" 
"4.2.6" 
((10 #\Newline . 10) (11 #\VT . 11) (12 #\Page . 12) (13 #\Return . 13))

-- 
Thibault Langlois <[email-masked]>
FCUL/DI

---

From: Sam Steingold <[email-masked]>
Date: Fri May 2 09:59:22 2003

> * In message <[email-masked]>
> * On the subject of "[clisp-list] problem reading bytes as characters"
> * Sent on 02 May 2003 17:32:15 +0100
> * Honorable Thibault Langlois <[email-masked]> writes:
>

Consider a file foo.dos created like this:

(with-open-file (s "foo.dos" :direction :output
                 :element-type '(unsigned-byte 8))
  (write-sequence
   (mapcar #'char-code
           '(#\f #\o #\o #\Newline #\b #\a #\r #\Return #\Newline))
  s))

now, what should

(with-open-file (s "foo.dos" :direction :input
                 :element-type 'character
                 :external-format :dos)
  (read-line s))

return?

You might reasonably argue that the right string to return is
"foo\fbar".  Unfortunately, "\f" (== (code-char 10)) is #\Newline in
CLISP, so READ-LINE would return a string with an embedded newline,
which, if not outright non-compliant, would be quite surprising to a
user.

Because of this problem, CLISP reads CR, LF and CRLF as #\Newline.

-- 
Sam Steingold (http://www.podval.org/~sds) running RedHat9 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
I'm a Lisp variable -- bind me!

---

From: Thibault Langlois <[email-masked]>
Date: Fri May 2 10:23:50 2003

> > Why is byte 13 read as #\newline char (and not #\return) ?

Sorry for posting to this list. I should have continue to send to c.l.l

Thank you for your explanations. I think I can solve my problem with a
more recent version of clisp, changing the stream-element-type of
*standard-input* and then use read-byte as you suggest in your post on
c.l.l

Thibault