From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 5C9B880211 for ; Mon, 16 Oct 2017 08:46:31 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.43,385,1503352800"; d="scan'208";a="241101687" Received: from dhcp-13-128.lip.ens-lyon.fr ([140.77.13.128]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Oct 2017 08:46:31 +0200 From: =?utf-8?Q?Laurent_Th=C3=A9venoux?= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 11.0 \(3445.1.7\)) Date: Mon, 16 Oct 2017 08:46:30 +0200 References: <3380AF33-0E0F-46E9-BF03-E61B65E6B838@inria.fr> <861535789.6026880.1507996456261.JavaMail.zimbra@inria.fr> To: caml-list@inria.fr In-Reply-To: <861535789.6026880.1507996456261.JavaMail.zimbra@inria.fr> Message-Id: X-Mailer: Apple Mail (2.3445.1.7) Subject: Re: [Caml-list] Interfacing C with OCaml and file descriptors Hello, Thank you, everyone, for your very instructive answers! Laurent > Le 14 oct. 2017 =C3=A0 17:54, Thierry Martinez a =C3=A9crit : >=20 > Laurent: >> So, if possible, how to convert the =E2=80=98value channel=E2=80=99 to a= =E2=80=98FILE*=E2=80=99? >=20 > 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. >=20 > #ifdef _WIN32 > #include >=20 > extern int win_CRT_fd_of_filedescr(value handle); >=20 > static FILE * > file_of_file_descr(value file_descr, const char *mode) > { > CAMLparam1(file_descr); > int fd =3D win_CRT_fd_of_filedescr(file_descr); > FILE *result =3D _fdopen(dup(fd), mode); > CAMLreturnT(FILE *, result); > } > #else=20 > static FILE * > file_of_file_descr(value file_descr, const char *mode) > { > CAMLparam1(file_descr); > int fd =3D Int_val(file_descr); > FILE *result =3D fdopen(dup(fd), mode); > CAMLreturnT(FILE *, result); > } > #endif >=20 > Cheers. > --=20 > Thierry. >=20 > ----- Original Message ----- >> From: "Laurent Th=C3=A9venoux" >> To: caml-list@inria.fr >> Sent: Friday, October 13, 2017 12:57:33 PM >> Subject: [Caml-list] Interfacing C with OCaml and file descriptors >>=20 >> Hi, >>=20 >> I have a naive question about file descriptors and C interface (when >> interfacing C with OCaml). >>=20 >> For instance, a file.ml file which looks like: >>=20 >>=20 >> external my_c_interface : out_channel -> int =3D c_function >>=20 >>=20 >> where c_function writes something to a file and return the number of wri= tten >> char. >> The c_function takes a FILE* file descriptor, as for example: >>=20 >>=20 >> CAMLprim value c_function (value channel) >> { >> CAMLparam1 (channel); >> FILE* fd =3D ???; >> CAMLreturn (Val_int (call_to_c_func (fd))); >> } >>=20 >>=20 >> So, if possible, how to convert the =E2=80=98value channel=E2=80=99 to a= =E2=80=98FILE*=E2=80=99? >>=20 >> Thanks for your help, >> Laurent >>=20 >> -- >> 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