From: dstein64
Subject: Converting input stream
Date: 
Message-ID: <22c7a565-eb8e-4357-9cb8-227e184fd913@a23g2000hsc.googlegroups.com>
I am trying to read from a file input stream. The file is of type .txt
and was produced in Windows. Each read-line looks some thing like:
"example^M"

That is, every line ends with ^M. I believe this is because of the
differences between the way Windows and Linux handle newlines in ASCII
text files. Anyhow, does anyone have any suggestions for dealing with
this? I will not be able to convert the file prior to opening it from
LISP. Is there any lisp functions that can do the conversion, or any
read or read-line functions that can get around this?

Thanks.

From: Edi Weitz
Subject: Re: Converting input stream
Date: 
Message-ID: <u8x02cniq.fsf@agharta.de>
On Fri, 28 Mar 2008 18:26:33 -0700 (PDT), dstein64 <········@gmail.com> wrote:

> I am trying to read from a file input stream. The file is of type
> .txt and was produced in Windows. Each read-line looks some thing
> like: "example^M"
>
> That is, every line ends with ^M. I believe this is because of the
> differences between the way Windows and Linux handle newlines in
> ASCII text files. Anyhow, does anyone have any suggestions for
> dealing with this? I will not be able to convert the file prior to
> opening it from LISP. Is there any lisp functions that can do the
> conversion, or any read or read-line functions that can get around
> this?

That's what external formats are for (which you specify when you open
the file), but this is implementation-dependent.  Here's for example
how LispWorks does it

  http://www.lispworks.com/documentation/lw51/LWUG/html/lwuser-402.htm

and for your case the :EOL-STYLE part would be the most important one.
Most other Lisps have similar facilities.

A somewhat portable solution is this:

  http://weitz.de/flexi-streams/

Edi.

-- 

European Common Lisp Meeting, Amsterdam, April 19/20, 2008

  http://weitz.de/eclm2008/

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: dstein64
Subject: Re: Converting input stream
Date: 
Message-ID: <43c21778-0028-47be-a416-7f995790fe47@m36g2000hse.googlegroups.com>
Is there any way to check if the stream is at the end-of-line? Then if
it is not, I will run (read-line file), which will bring it to the
next line? This method will work fine if I can find some stream
function that checks if EOL is next without actually reading the next
token/line/...
From: Pascal Bourguignon
Subject: Re: Converting input stream
Date: 
Message-ID: <87prtdq142.fsf@thalassa.informatimago.com>
dstein64 <········@gmail.com> writes:

> Is there any way to check if the stream is at the end-of-line? Then if
> it is not, I will run (read-line file), which will bring it to the
> next line? This method will work fine if I can find some stream
> function that checks if EOL is next without actually reading the next
> token/line/...

This won't help you solve your first problem.

(when (char/= #\newline (peek-char nil stream))
   (read-line stream))

clisp converts automatically all type of line terminator to #\newline
when reading text files.  Otherwise, you can also specifiy the
:line-terminator in the encoding (see ext:make-encoding) that you can
pass as :external-type.

But until you give us the precise (lisp-implementation-type) you are
using, we cannot give you the precise implementation manual reference
to search for the precise implementation specific :external-type to
use to read CRLF as #\newline.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
From: dstein64
Subject: Re: Converting input stream
Date: 
Message-ID: <590d9ef6-dca5-4587-af14-a35d93e8e8b7@z38g2000hsc.googlegroups.com>
> clisp converts automatically all type of line terminator to #\newline
> when reading text files.  Otherwise, you can also specifiy the
> :line-terminator in the encoding (see ext:make-encoding) that you can
> pass as :external-type.
>
> But until you give us the precise (lisp-implementation-type) you are
> using, we cannot give you the precise implementation manual reference
> to search for the precise implementation specific :external-type to
> use to read CRLF as #\newline.


I am using SBCL, but I would also like it to work on Allegro. I can
use use #+allegro and #+sbcl though. Thanks.
From: Duane Rettig
Subject: Re: Converting input stream
Date: 
Message-ID: <o0d4pdx9wi.fsf@gemini.franz.com>
dstein64 <········@gmail.com> writes:

>> clisp converts automatically all type of line terminator to #\newline
>> when reading text files.  Otherwise, you can also specifiy the
>> :line-terminator in the encoding (see ext:make-encoding) that you can
>> pass as :external-type.
>>
>> But until you give us the precise (lisp-implementation-type) you are
>> using, we cannot give you the precise implementation manual reference
>> to search for the precise implementation specific :external-type to
>> use to read CRLF as #\newline.
>
>
> I am using SBCL, but I would also like it to work on Allegro. I can
> use use #+allegro and #+sbcl though. Thanks.

Start with

http://www.franz.com/support/documentation/8.1/doc/iacl.htm#external-formats-1

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: William James
Subject: Re: Converting input stream
Date: 
Message-ID: <5f872eb8-c244-4de1-a534-3a7b7b67fab4@d1g2000hsg.googlegroups.com>
dstein64 wrote:
> I am trying to read from a file input stream. The file is of type .txt
> and was produced in Windows. Each read-line looks some thing like:
> "example^M"
>
> That is, every line ends with ^M. I believe this is because of the
> differences between the way Windows and Linux handle newlines in ASCII
> text files. Anyhow, does anyone have any suggestions for dealing with
> this? I will not be able to convert the file prior to opening it from
> LISP. Is there any lisp functions that can do the conversion, or any
> read or read-line functions that can get around this?
>
> Thanks

Check each line.  If the last character is a carriage-return,
remove it..