·····@sip.medizin.uni-ulm.de (kp gores) writes:
< i am writing something like a file converter to "translate" text file "a"
< to a modified "b".
< of course "a" is no lisp file, thus using read doesnt work, because it
< tries to evaluate what is read. so i am stuck with read-char and have to
< construct from my char-by-char input further processable words, numbers,
< delimiters and so on.
<
< is there a more elegant/efficient way to do this?
< or is there somewhere downloadable source code (must be the scanner part
< of a compiler written in lisp)
<
< regards
< kp
Depends on what translations you would like to perform on the input
file. You can use read-sequence and read large chunks of the file into
a buffer and perform the modifications there and then use
write-sequence to dump it to file `b'. If the input file is small
enough you could possibly read the entire thing into core at one time.
(let ((buffer (make-sequence 'simple-string 1024)))
(with-open-file (in "meadself.txt" :direction :input)
(with-open-file (out "temp.txt" :direction :output
:if-exists :supersede)
(loop for n = (read-sequence buffer in)
while (> n 0)
do (write-sequence buffer
out :start 0 :end n)))))
This would just copy the file; just do some transformation on BUFFER
before the write-sequence and adjust N (which is the index of the
first element not updated in the sequence BUFFER).
If the file is small enough you could do something like this instead.
(with-open-file (in "meadself.txt" :direction :input)
(let ((buffer (make-sequence 'simple-string (file-length in))))
(with-open-file (out "temp.txt" :direction :output
:if-exists :supersede)
(let ((size (read-sequence buffer in)))
(write-sequence buffer out :start 0 :end size))))
(values))
The `values' is not really important. Read-sequence is usually pretty
fast not to mention it's much easier to use, I think, than read-char.
It also keeps the symbol count down as well (read interns everything
that is sees which may cause some bloatation). If you find
read-sequence inefficient you can crank up the speed - although it
usually isn't necessary.
Hope this helps - I'd really hate to see you re-implement the lisp
reader.