Re: [PATCH v3] rdelim: Add new procedure `for-line-in-file`.

Ricardo Wurmus <[email protected]>
Newsgroups gmane.lisp.guile.devel
Message-ID <[email protected]>
Adam Faiz <[email protected]> writes:

> +(define* (for-line-in-file file proc
> +                           #:key (encoding #f) (guess-encoding #f))
> +  "Call PROC for every line in FILE until the eof-object is reached.
> +FILE can either be a filename string or an already opened input port.
> +The corresponding port is closed upon completion.
> +
> +The line provided to PROC is guaranteed to be a string."
> +  (let ((port
> +         (if (input-port? file)
> +             file
> +             (open-input-file file
> +                              #:encoding encoding
> +                              #:guess-encoding guess-encoding))))
> +    (let loop ((line (read-line port)))
> +      (cond ((eof-object? line)
> +             (close-port port))
> +            (else
> +             (proc line)
> +             (loop (read-line port)))))))

I think the port would leak if PROC were to raise an exception.

-- 
Ricardo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.