unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Using a temp file from emacs C code
@ 2024-10-26 23:33 Cecilio Pardo
  2024-10-27  6:17 ` Eli Zaretskii
  0 siblings, 1 reply; 3+ messages in thread
From: Cecilio Pardo @ 2024-10-26 23:33 UTC (permalink / raw)
  To: emacs-devel

Hello,

How should I create and use a temp file from emacs' C code?

I got this working but it doesn't look right:

- Call lisp function make-temp-file, trough funcall
- Convert the returned string to utf-16 (because of Windows)
- Then to read the file use emacs_fopen
- Call lisp function delete-file (funcall)

   const char *prefix = "prefix";
   Lisp_Object tmp_file =
     CALLN (Ffuncall,
	   intern_c_string ("make-temp-file"),
	   make_string (prefix, strlen (prefix)),
	   Qnil, Qnil, Qnil);
   tmp_file = ENCODE_FILE (tmp_file);
   WCHAR wide_filename[MAX_PATH];
   filename_to_utf16 (SDATA (tmp_file), wide_filename);

   // write things to the file..., then read back:
	
   FILE *fp = emacs_fopen (SDATA (tmp_file), "rb");
   if (fp != NULL)
     {
       // ...
       fclose (fp);
     }

   CALLN (Ffuncall, intern_c_string("delete-file"), tmp_file, Qnil);



Thanks.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using a temp file from emacs C code
  2024-10-26 23:33 Using a temp file from emacs C code Cecilio Pardo
@ 2024-10-27  6:17 ` Eli Zaretskii
  2024-10-27  8:22   ` Cecilio Pardo
  0 siblings, 1 reply; 3+ messages in thread
From: Eli Zaretskii @ 2024-10-27  6:17 UTC (permalink / raw)
  To: Cecilio Pardo; +Cc: emacs-devel

> Date: Sun, 27 Oct 2024 01:33:30 +0200
> From: Cecilio Pardo <cpardo@imayhem.com>
> 
> How should I create and use a temp file from emacs' C code?

By first trying to avoid it, and do it from Lisp instead ;-)

But if you absolutely have no other way, then...

> I got this working but it doesn't look right:
> 
> - Call lisp function make-temp-file, trough funcall
> - Convert the returned string to utf-16 (because of Windows)
> - Then to read the file use emacs_fopen

I don't understand: if you create the file, why do you need to read it
back? why not simply pass whatever you write to the file to the code
which needs to read it, as a string or a temp buffer?

Anyway, you could use Fmake_temp_file_internal instead.  You can see
an example of that in the implementation of native-elisp-load.  And
for reading a file (if you really need it; see above) use
Finsert_file_contents, it will handle the issues with encoding the
file name etc. for you.  (But beware of hidden rocks if you do this in
code that needs to be run early during Emacs bootstrap.)



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using a temp file from emacs C code
  2024-10-27  6:17 ` Eli Zaretskii
@ 2024-10-27  8:22   ` Cecilio Pardo
  0 siblings, 0 replies; 3+ messages in thread
From: Cecilio Pardo @ 2024-10-27  8:22 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

On 27/10/2024 7:17, Eli Zaretskii wrote:
>> How should I create and use a temp file from emacs' C code?
> 
> By first trying to avoid it, and do it from Lisp instead ;-)
> 
> But if you absolutely have no other way, then...

I promise.

>> I got this working but it doesn't look right:
>>
>> - Call lisp function make-temp-file, trough funcall
>> - Convert the returned string to utf-16 (because of Windows)
>> - Then to read the file use emacs_fopen
> 
> I don't understand: if you create the file, why do you need to read it
> back? why not simply pass whatever you write to the file to the code
> which needs to read it, as a string or a temp buffer?

Sorry, I oversimplified. I'm calling a winapi function that writes the file.

> Anyway, you could use Fmake_temp_file_internal instead.  You can see
> an example of that in the implementation of native-elisp-load.  And
> for reading a file (if you really need it; see above) use
> Finsert_file_contents, it will handle the issues with encoding the
> file name etc. for you.  (But beware of hidden rocks if you do this in
> code that needs to be run early during Emacs bootstrap.)

Thanks. Not this time.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-27  8:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-26 23:33 Using a temp file from emacs C code Cecilio Pardo
2024-10-27  6:17 ` Eli Zaretskii
2024-10-27  8:22   ` Cecilio Pardo

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).