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