[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
map-file-lines
From: |
Ted Zlatanov |
Subject: |
map-file-lines |
Date: |
Mon, 02 Feb 2009 11:20:07 -0600 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux) |
Emacs Lisp lacks a good way to iterate over all the lines of a file,
especially for a large file. The following code tries to provide a
solution, concentrating on reading a block of data in one shot and then
processing it line by line. It may be more efficient to write this in
C. Also, it does not deal with cases where the first line read is
bigger than the buffer size, and may have other bugs, but it works for
me so I thought I'd post it for comments and criticism.
Thanks
Ted
(defun map-file-lines (file func &optional bufsize)
(let ((filepos 0)
(linenum 0)
(bufsize (or bufsize 4096)))
(with-temp-buffer
(while
(let*
((inserted (insert-file-contents
file nil
filepos (+ filepos bufsize)
t))
(numlines (count-lines (point-min) (point-max)))
(read (nth 1 inserted))
(done (< 1 read)))
(dotimes (n (count-lines (point-min) (point-max)))
(goto-char (point-min))
(funcall func
(buffer-substring
(line-beginning-position)
(line-end-position))
(incf linenum))
(incf filepos (line-end-position))
(forward-line))
done)))
linenum))
;;(map-file-lines "/tmp/test" (lambda (line num) (message "%d: %s" line)))
- map-file-lines,
Ted Zlatanov <=
- Re: map-file-lines, Stefan Monnier, 2009/02/02
- Re: map-file-lines, Stefan Monnier, 2009/02/02
- Re: map-file-lines, joakim, 2009/02/02
- Re: map-file-lines, Stefan Monnier, 2009/02/02
- Re: map-file-lines, joakim, 2009/02/03