GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
glib-helpers.c
1 /********************************************************************\
2  * gnc-helpers.c -- gnucash glib helper functions *
3  * Copyright (C) 2000 Linas Vepstas *
4  * Copyright (C) 2006 Chris Shoemaker <[email protected]> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA [email protected] *
22  * *
23 \********************************************************************/
24 
25 #include "config.h"
26 
27 #include <string.h>
28 #include <glib.h>
29 #include <libguile.h>
30 #include "guile-mappings.h"
31 #include "gnc-guile-utils.h"
32 #include "swig-runtime.h"
33 #include "glib-helpers.h"
34 
35 
36 static SCM
37 glist_to_scm_list_helper(GList *glist, swig_type_info *wct)
38 {
39  SCM list = SCM_EOL;
40  GList *node;
41 
42  for (node = glist; node; node = node->next)
43  list = scm_cons(SWIG_NewPointerObj(node->data, wct, 0), list);
44 
45  return scm_reverse (list);
46 }
47 
48 SCM
49 gnc_glist_to_scm_list(GList *glist, gchar *wct)
50 {
51  swig_type_info *stype = SWIG_TypeQuery(wct);
52  g_return_val_if_fail(stype, SCM_UNDEFINED);
53  return glist_to_scm_list_helper(glist, stype);
54 }
55 
56 GList *
57 gnc_scm_list_to_glist(SCM rest)
58 {
59  GList *result = NULL;
60  SCM scm_item;
61 
62  SWIG_GetModule(NULL); /* Work-around for SWIG bug. */
63  SCM_ASSERT(scm_is_list(rest), rest, SCM_ARG1, "gnc_scm_list_to_glist");
64 
65  while (!scm_is_null(rest))
66  {
67  void *item;
68 
69  scm_item = SCM_CAR(rest);
70  rest = SCM_CDR(rest);
71 
72  if (scm_item == SCM_BOOL_F)
73  {
74  result = g_list_prepend(result, NULL);
75  }
76  else
77  {
78  if (!SWIG_IsPointer(scm_item))
79  scm_misc_error("gnc_scm_list_to_glist",
80  "Item in list not a wcp.", scm_item);
81 
82  item = (void *)SWIG_PointerAddress(scm_item);
83  result = g_list_prepend(result, item);
84  }
85  }
86 
87  return g_list_reverse(result);
88 }
89 
90 /********************************************************************
91  * gnc_glist_string_to_scm
92  * i.e. (glist-of (<gw:mchars> calee-owned) callee-owned)
93  * or equivalently
94  * i.e. (glist-of (<gw:gchars> calee-owned) callee-owned)
95  ********************************************************************/
96 SCM
97 gnc_glist_string_to_scm(GList *glist)
98 {
99  SCM list = SCM_EOL;
100  GList *node;
101 
102  for (node = glist; node; node = node->next)
103  {
104  if (node->data)
105  list = scm_cons (scm_from_utf8_string(node->data), list);
106  else
107  list = scm_cons (SCM_BOOL_F, list);
108  }
109 
110  return scm_reverse (list);
111 }
112 
113 
114 
115 
116 /********************************************************************
117  * gnc_scm_to_glist_string
118  * i.e. (glist-of (<gw:mchars> callee-owned) callee-owned)
119  * or equivalently
120  * i.e. (glist-of (<gw:gchars> callee-owned) callee-owned)
121  ********************************************************************/
122 
123 GList *
124 gnc_scm_to_glist_string(SCM list)
125 {
126  GList *glist = NULL;
127 
128  while (!scm_is_null (list))
129  {
130  if (scm_is_string(SCM_CAR(list)))
131  {
132  gchar * str;
133 
134  str = gnc_scm_to_utf8_string (SCM_CAR(list));
135  if (str)
136  glist = g_list_prepend (glist, g_strdup (str));
137  g_free (str);
138  }
139  list = SCM_CDR (list);
140  }
141 
142  return g_list_reverse (glist);
143 }
144 
145 GSList *
146 gnc_scm_to_gslist_string(SCM list)
147 {
148  GSList *gslist = NULL;
149 
150  while (!scm_is_null (list))
151  {
152  if (scm_is_string(SCM_CAR(list)))
153  {
154  gchar * str;
155 
156  str = gnc_scm_to_utf8_string (SCM_CAR(list));
157  if (str)
158  gslist = g_slist_prepend (gslist, g_strdup (str));
159  g_free (str);
160  }
161  list = SCM_CDR (list);
162  }
163 
164  return g_slist_reverse (gslist);
165 }
166 
167 /********************************************************************
168  * gnc_glist_string_p
169  ********************************************************************/
170 
171 int
172 gnc_glist_string_p(SCM list)
173 {
174  return scm_is_list(list);
175 }