Imported Upstream version 3.3.2
[debian/amanda] / perl / amglue / filehandles.swg
1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %typemap(in) int fd {
22     IO *io = NULL;
23     PerlIO *pio = NULL;
24     int fd = -1;
25
26     if (SvIOK($input)) {
27         /* plain old integer */
28         $1 = SvIV($input);
29     } else {
30         /* try extracting as filehandle */
31         
32         /* note: sv_2io may call die() */
33         io = sv_2io($input);
34         if (io) {
35             pio = IoIFP(io);
36         }
37         if (pio) {
38             fd = PerlIO_fileno(pio);
39         }
40         if (fd >= 0) {
41             $1 = fd;
42         } else {
43             SWIG_exception(SWIG_TypeError, "Expected integer file descriptor "
44                 "or file handle for argument $argnum");
45         }
46     }
47 }