Imported Upstream version 3.3.2
[debian/amanda] / perl / amglue / dumpspecs.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 %{
22 #include "cmdline.h"
23 %}
24
25 /*
26  * Typemaps for lists of dumpspecs
27  *
28  * To use:
29  *   %include "amglue/dumpspecs.swg"
30  *   %import "Amanda/Logfile.swg"
31  * and declare functions as either taking or returning "amglue_dumpspec_list *".
32  */
33
34 %inline %{
35 typedef GSList amglue_dumpspec_list;
36 %}
37
38 /* Typemap to convert a GSList of dumpspec_t's into an array of same. This uses
39  * some SWIG trickery to manage to convert the GSList itself to an array, while
40  * leaving the dumpspecs as C objects.
41  */
42 %types(dumpspec_t *);
43 %typemap(out) amglue_dumpspec_list * {
44     if ($1) {
45         GSList *iter;
46         EXTEND(SP, g_slist_length($1)); /* make room for return values */
47
48         iter = $1;
49         while (iter) {
50             /* Let SWIG take ownership of the object; we'll free the GSList momentarily */
51             $result = SWIG_NewPointerObj(iter->data, $descriptor(dumpspec_t*), SWIG_OWNER | SWIG_SHADOW);
52             argvi++;
53             iter = iter->next;
54         }
55
56         /* Now free the GSList, but *not* its contents (which are now "owned" by SWIG) */
57         g_slist_free($1);
58     }
59 }
60
61 /* Typemap to convert an arrayref of dumpspecs into a GSList of same.  This assumes
62  * that the dumpspecs are all C objects.  This borrows references to the underlying
63  * dumpspec objects, which remain owned by SWIG.
64  */
65 %typemap(in) amglue_dumpspec_list * {
66     AV *av;
67     int len;
68     int i;
69
70     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
71         SWIG_exception_fail(SWIG_TypeError, "Expected an arrayref of dumpspecs");
72     }
73     av = (AV *)SvRV($input);
74
75     len = av_len(av)+1;
76     $1 = NULL;
77     for (i = 0; i < len; i++) {
78         dumpspec_t *ds = NULL;
79         SV **elt = av_fetch(av, i, 0);
80         if (elt)
81             SWIG_ConvertPtr(*elt, (void **)&ds, $descriptor(dumpspec_t *), 0);
82         if (!ds)
83             SWIG_exception_fail(SWIG_TypeError, "Expected an arrayref of dumpspecs");
84         $1 = g_slist_append($1, ds);
85     }
86 }
87
88 %typemap(freearg) amglue_dumpspec_list * {
89     /* Free the GSList, but not its contents (which are still owned by SWIG) */
90     g_slist_free($1);
91 }