dofile stream processing
Sometimes, dealing with big structured file such as csv or ldif we don't want to load a whole file into memory, but want to process it record-by-record.
(dofile) iterator behaves like (dolist) but does iteration through file contents.
Syntax:
(dofile (sym str-fname [str-delim [int-strlen]]) body)
(dofile (sym int-fd [str-delim [int-strlen]]) body)
sym – symbol to which records will be placed on iteration
str-fname – filename to be processed (a file will be opened and closed internally)
int-fd – already opened file descriptor – whatever read-buffer can read from (will be closed after a completion either)
str-delim – record delimiter is "\n" by default; use nil to turn it off explicitly
int-strlen – maximal record length (is 65000 by default)
example: (dofile (l «/etc/passwd») (println >" l))
example: (dofile (l (open «/etc/passwd» r)) (inc linecount) (println (l 0)))
Notes
str-delim is a plain string – not regexp
If readed record ends with str-delim it is chopped away before assigning to sym
After finishing, file desctiptor will be closed automatically despite exceptions. Internal catch is used to do it.
Throwed exceptions are catched and returned as function's result.
Throwed/arised errors will be re-throwed again with throw-error.
dofile is now a part of funlib.lsp library.
Separate version isn't published anymore.