Read-line and Mac text files?

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Read-line and Mac text files?

Post by cormullion »

Can I use read-line to read old-style Mac text files? These use ASCII 13 as line separator, rather than 10 or 13/10. The manual implies that I can't. These files usually have to be run through (UNIX) tr before they can be piped to other UNIX commands:

tr "\r" "\n" temp.mif > temp1.mif

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You cannot use 'read-line' for old style Mac files with carriage-returns (ASCII 13) as only line separator, but there is a work around:

Code: Select all

(parse (read-file "oldmactextfile") "\r")
This will read the file and break it up into a list of lines with the line terminator stripped:

Code: Select all

> (write-file "test" "abcde\013wxyz\013qwert\013")
17
> (parse (read-file "test") "\r")
("abcde" "wxyz" "qwert" "")
> 
Lutz

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... and I forgot: If the file is very large, then reading it in one chunk might not be the appropiate thing to do. In this case there is a feature in 'read-buffer' that lets you wait for string:

Code: Select all

> (open "test" "read")
3
> (read-buffer 3 'line 256 "\r")
6
> line
"abcde\r"
> (read-buffer 3 'line 256 "\r")
5
> line
"wxyz\r"
> (read-buffer 3 'line 256 "\r")
6
> line
"qwert\r"
> (chop line)
"qwert"
> 
On the last line it is shown how to 'chop' off the last character of the line. See the manual for details.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks - ideally read-line would detect it automatically :-) , but when I'm certain that a file is ASCII 13-delimited I'll be able to read it...

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

How would I make a "pipe" that reads large files where the lines are delimited by "\r" (13)?

I haven't managed to make read-buffer work following your suggestion... I want to read STDIN I think...

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

This works:

Code: Select all

> (write-file "junk" "abc\013def\013xyz\013")
12
> (exit)
~> cat prog
(while (read-buffer 0 'buff 256 "\r") (println "=>" buff))

~> cat junk | newlisp prog 
=>abc
=>def
=>xyz
~>
The first line makes a file with ascii 13 delimited lines. The newlisp program 'prog' contains the line:

(while (read-buffer 0 'buff 256 "\r") (println "=>" buff))

I use 0 for the stdin file handle, then I pipe the file 'junk' through it doing:

cat junk | newlisp prog

If the file is not too long, you also could just read it in in one chunk and then split it using (parse buff {\r} 0):

Code: Select all

> (parse (read-file "junk") {\r} 0)
("abc" "def" "xyz" "")
> 
Lutz


Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks - forgot that 0 meant STDIN! :-)

Locked