| 1 | /* ports.i --- Guile typemaps for handling ports -*- c -*- |
| 2 | Copyright (C) 2000, 2004 Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> |
| 3 | |
| 4 | $Header: /cvsroot/swig/SWIG/Lib/guile/ports.i,v 1.5 2004/04/26 14:09:02 mkoeppe Exp $ |
| 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 | ( int closep ) |
| 25 | { |
| 26 | if (SWIG_ConvertPtr($input, (void**) &($1), $1_descriptor, 0) == 0) { |
| 27 | closep = 0; |
| 28 | } |
| 29 | else if(!(SCM_FPORTP($input))) |
| 30 | scm_wrong_type_arg("$name", $argnum, $input); |
| 31 | else { |
| 32 | int fd; |
| 33 | if (SCM_OUTPUT_PORT_P($input)) |
| 34 | scm_force_output($input); |
| 35 | fd=dup(SCM_FPORT_FDES($input)); |
| 36 | if(fd==-1) |
| 37 | scm_misc_error("$name", strerror(errno), SCM_EOL); |
| 38 | $1=fdopen(fd, |
| 39 | SCM_OUTPUT_PORT_P($input) |
| 40 | ? (SCM_INPUT_PORT_P($input) |
| 41 | ? "r+" : "w") |
| 42 | : "r"); |
| 43 | if($1==NULL) |
| 44 | scm_misc_error("$name", strerror(errno), SCM_EOL); |
| 45 | closep = 1; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | %typemap(freearg) FILE* { |
| 50 | if (closep$argnum) |
| 51 | fclose($1); |
| 52 | } |
| 53 | |