Imported Upstream version 3.3.3
[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
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 %{
23 #include "cmdline.h"
24 %}
25
26 /*
27  * Typemaps for lists of dumpspecs
28  *
29  * To use:
30  *   %include "amglue/dumpspecs.swg"
31  *   %import "Amanda/Logfile.swg"
32  * and declare functions as either taking or returning "amglue_dumpspec_list *".
33  */
34
35 %inline %{
36 typedef GSList amglue_dumpspec_list;
37 %}
38
39 /* Typemap to convert a GSList of dumpspec_t's into an array of same. This uses
40  * some SWIG trickery to manage to convert the GSList itself to an array, while
41  * leaving the dumpspecs as C objects.
42  */
43 %types(dumpspec_t *);
44 %typemap(out) amglue_dumpspec_list * {
45     if ($1) {
46         GSList *iter;
47         EXTEND(SP, g_slist_length($1)); /* make room for return values */
48
49         iter = $1;
50         while (iter) {
51             /* Let SWIG take ownership of the object; we'll free the GSList momentarily */
52             $result = SWIG_NewPointerObj(iter->data, $descriptor(dumpspec_t*), SWIG_OWNER | SWIG_SHADOW);
53             argvi++;
54             iter = iter->next;
55         }
56
57         /* Now free the GSList, but *not* its contents (which are now "owned" by SWIG) */
58         g_slist_free($1);
59     }
60 }
61
62 /* Typemap to convert an arrayref of dumpspecs into a GSList of same.  This assumes
63  * that the dumpspecs are all C objects.  This borrows references to the underlying
64  * dumpspec objects, which remain owned by SWIG.
65  */
66 %typemap(in) amglue_dumpspec_list * {
67     AV *av;
68     int len;
69     int i;
70
71     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
72         SWIG_exception_fail(SWIG_TypeError, "Expected an arrayref of dumpspecs");
73     }
74     av = (AV *)SvRV($input);
75
76     len = av_len(av)+1;
77     $1 = NULL;
78     for (i = 0; i < len; i++) {
79         dumpspec_t *ds = NULL;
80         SV **elt = av_fetch(av, i, 0);
81         if (elt)
82             SWIG_ConvertPtr(*elt, (void **)&ds, $descriptor(dumpspec_t *), 0);
83         if (!ds)
84             SWIG_exception_fail(SWIG_TypeError, "Expected an arrayref of dumpspecs");
85         $1 = g_slist_append($1, ds);
86     }
87 }
88
89 %typemap(freearg) amglue_dumpspec_list * {
90     /* Free the GSList, but not its contents (which are still owned by SWIG) */
91     g_slist_free($1);
92 }