1/* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4 *
5 * ports.i
6 *
7 * Guile typemaps for handling ports
8 * ----------------------------------------------------------------------------- */
9
10%{
11  #ifndef _POSIX_SOURCE
12  /* This is needed on Solaris for fdopen(). */
13  #  define _POSIX_SOURCE 199506L
14  #endif
15  #include <stdio.h>
16  #include <errno.h>
17  #include <unistd.h>
18%}
19
20/* This typemap for FILE * accepts
21   (1) FILE * pointer objects,
22   (2) Scheme file ports.  In this case, it creates a temporary C stream
23       which reads or writes from a dup'ed file descriptor.
24 */
25
26%typemap(in, doc="$NAME is a file port or a FILE * pointer") FILE *
27  ( int closep )
28{
29  if (SWIG_ConvertPtr($input, (void**) &($1), $1_descriptor, 0) == 0) {
30    closep = 0;
31  }
32  else if(!(SCM_FPORTP($input)))
33    scm_wrong_type_arg("$name", $argnum, $input);
34  else {
35    int fd;
36    if (SCM_OUTPUT_PORT_P($input))
37      scm_force_output($input);
38    fd=dup(SCM_FPORT_FDES($input));
39    if(fd==-1)
40      scm_misc_error("$name", strerror(errno), SCM_EOL);
41    $1=fdopen(fd,
42		   SCM_OUTPUT_PORT_P($input)
43		   ? (SCM_INPUT_PORT_P($input)
44		      ? "r+" : "w")
45		   : "r");
46    if($1==NULL)
47      scm_misc_error("$name", strerror(errno), SCM_EOL);
48    closep = 1;
49  }
50}
51
52%typemap(freearg) FILE*  {
53  if (closep$argnum)
54    fclose($1);
55}
56
57