[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: request for review: Doing direct file I/O in Emacs Lisp
From: |
Kim F. Storm |
Subject: |
Re: request for review: Doing direct file I/O in Emacs Lisp |
Date: |
10 May 2004 08:52:33 +0200 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 |
John Wiegley <address@hidden> writes:
> The following patch implements a file-handle interface for Emacs Lisp,
> which allows files to be directly opened and read/written to without
> an intervening buffer. Eshell can now use this, for example, to
> greatly speed up output redirection (by several orders of magnitude).
>
> It is a simple interface that reads in strings, given a length, and
> writes strings by examining their length:
>
> (let ((handle (file-handle-open "/tmp/some-file" "w")))
> (file-handle-write handle "Test data\n")
> (file-handle-close handle)
>
> (setq handle (file-handle-open "/tmp/some-file" "r"))
> (message (file-handle-read handle 128))
> (file-handle-close handle))
>
This seems like a great idea, but it is not up to me to decide.
Some comments:
The doc string for -open need improvement. I'm not sure about using
"r", "w", etc for the mode; using things like 'read 'write and 'append
seems more lisp like. But I don't prefer either.
The doc strings for -read and -write are bad.
Instead of an explicit CONS as the handle, I would rather declare
the file handle like this:
struct Lisp_File_Handle
{
EMACS_INT size;
struct Lisp_Vector *v_next;
Lisp_Object handle_hi;
Lisp_Object handle_lo;
Lisp_Object file_name;
Lisp_Object open_mode;
};
and use handle_hi and handle_lo directly instead of all the CAR and
CDR'ing.
When you close the file handle, open_mode is set to nil.
The file_name and open_mode can be used to improve the printing of
the file handle as in
else if (FILE_HANDLEP (obj))
{
strout ("#<file-handle ", -1, -1, printcharfun, 0);
if (!NILP (XFILE_HANDLE (obj)->open_mode))
{
strout (XFILE_HANDLE (obj)->file_name, -1, -1, printcharfun, 0);
strout (" ", -1, -1, printcharfun, 0);
strout (XFILE_HANDLE (obj)->open_mode, -1, -1, printcharfun, 0);
}
else
strout (" closed", -1, -1, printcharfun, 0);
PRINTCHAR (')>');
}
You don't address the issue of multibyte in -read and -write.
It seems that you always assume things to be unibyte.
I don't know what the right thing to do is, but maybe you should
at least signal an error if things are not unibyte ?
Otherwise, you should associate a coding system with the file-handle
and use that for read and write. As a first shot at this, you could
add the coding system as an optional third arg to -open, and assume
unibyte/binary if no coding system was specified.
A -seek operation would be nice I guess.
And a -position (aka ftell) would be nice too.
--
Kim F. Storm <address@hidden> http://www.cua.dk
Re: request for review: Doing direct file I/O in Emacs Lisp, John Wiegley, 2004/05/11
Re: request for review: Doing direct file I/O in Emacs Lisp,
Kim F. Storm <=
Re: request for review: Doing direct file I/O in Emacs Lisp, Andreas Schwab, 2004/05/10
Re: request for review: Doing direct file I/O in Emacs Lisp, Stefan Monnier, 2004/05/10
Re: request for review: Doing direct file I/O in Emacs Lisp, Richard Stallman, 2004/05/10