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
Read-line and Mac text files?
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
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:
This will read the file and break it up into a list of lines with the line terminator stripped:
Lutz
Code: Select all
(parse (read-file "oldmactextfile") "\r")
Code: Select all
> (write-file "test" "abcde\013wxyz\013qwert\013")
17
> (parse (read-file "test") "\r")
("abcde" "wxyz" "qwert" "")
>
... 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:
On the last line it is shown how to 'chop' off the last character of the line. See the manual for details.
Lutz
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"
>
Lutz
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
This works:
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):
Lutz
Lutz
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
~>
(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
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact: