From: "Laurent Thévenoux" <laurent.thevenoux@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Interfacing C with OCaml and file descriptors
Date: Mon, 16 Oct 2017 08:46:30 +0200 [thread overview]
Message-ID: <B55C491C-6061-4715-8ED9-692ED4ECEA3B@inria.fr> (raw)
In-Reply-To: <861535789.6026880.1507996456261.JavaMail.zimbra@inria.fr>
Hello,
Thank you, everyone, for your very instructive answers!
Laurent
> Le 14 oct. 2017 à 17:54, Thierry Martinez <thierry.martinez@inria.fr> a écrit :
>
> Laurent:
>> So, if possible, how to convert the ‘value channel’ to a ‘FILE*’?
>
> As the other answers already said, there does not seem to exist a
> portable way to do that.
> FWIW, here is what I do for Pyml, which appears to works both on
> Unix and Windows:
> the function file_of_file_descr takes two arguments:
> - an OCaml value which can be either in_channel or out_channel,
> - and a corresponding mode ("r" or "w", typically),
> and returns a FILE *.
> Of course, since the API for channels is not documented, this
> solution can break on future OCaml versions.
>
> #ifdef _WIN32
> #include <windows.h>
>
> extern int win_CRT_fd_of_filedescr(value handle);
>
> static FILE *
> file_of_file_descr(value file_descr, const char *mode)
> {
> CAMLparam1(file_descr);
> int fd = win_CRT_fd_of_filedescr(file_descr);
> FILE *result = _fdopen(dup(fd), mode);
> CAMLreturnT(FILE *, result);
> }
> #else
> static FILE *
> file_of_file_descr(value file_descr, const char *mode)
> {
> CAMLparam1(file_descr);
> int fd = Int_val(file_descr);
> FILE *result = fdopen(dup(fd), mode);
> CAMLreturnT(FILE *, result);
> }
> #endif
>
> Cheers.
> --
> Thierry.
>
> ----- Original Message -----
>> From: "Laurent Thévenoux" <laurent.thevenoux@inria.fr>
>> To: caml-list@inria.fr
>> Sent: Friday, October 13, 2017 12:57:33 PM
>> Subject: [Caml-list] Interfacing C with OCaml and file descriptors
>>
>> Hi,
>>
>> I have a naive question about file descriptors and C interface (when
>> interfacing C with OCaml).
>>
>> For instance, a file.ml file which looks like:
>>
>>
>> external my_c_interface : out_channel -> int = c_function
>>
>>
>> where c_function writes something to a file and return the number of written
>> char.
>> The c_function takes a FILE* file descriptor, as for example:
>>
>>
>> CAMLprim value c_function (value channel)
>> {
>> CAMLparam1 (channel);
>> FILE* fd = ???;
>> CAMLreturn (Val_int (call_to_c_func (fd)));
>> }
>>
>>
>> So, if possible, how to convert the ‘value channel’ to a ‘FILE*’?
>>
>> Thanks for your help,
>> Laurent
>>
>> --
>> Caml-list mailing list. Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
prev parent reply other threads:[~2017-10-16 6:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-13 10:57 Laurent Thévenoux
2017-10-13 11:10 ` Nicolás Ojeda Bär
2017-10-13 21:37 ` Richard W.M. Jones
2017-10-13 22:42 ` David Allsopp
2017-10-14 6:10 ` Richard W.M. Jones
2017-10-14 15:18 ` Malcolm Matalka
2017-10-14 15:54 ` Thierry Martinez
2017-10-16 6:46 ` Laurent Thévenoux [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=B55C491C-6061-4715-8ED9-692ED4ECEA3B@inria.fr \
--to=laurent.thevenoux@inria.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox