GnuCash  5.6-150-g038405b370+
glib-guile.c
1 /********************************************************************\
2  * glib-guile.c -- glib helper functions for guile *
3  * Copyright (C) 2000 Linas Vepstas *
4  * Copyright (C) 2006 Chris Shoemaker <c.shoemaker@cox.net> *
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 gnu@gnu.org *
22  * *
23 \********************************************************************/
24 
25 #include <config.h>
26 
27 #include <errno.h>
28 #include <string.h>
29 #include <glib.h>
30 
31 #ifdef __MINGW32__
32 #define _GL_UNISTD_H //Deflect poisonous define of close in Guile's GnuLib
33 #endif
34 #include <libguile.h>
35 #ifdef HAVE_UNISTD_H
36 # ifdef close
37 # undef close
38 # endif
39 # include <unistd.h>
40 #else
41 # include <io.h>
42 # define close _close
43 #endif
44 
45 #include <libguile.h>
46 #include "swig-runtime.h"
47 #include "guile-mappings.h"
48 #include "gnc-glib-utils.h"
49 #include "gnc-guile-utils.h"
50 #include "glib-guile.h"
51 
52 #include <platform.h>
53 #if PLATFORM(WINDOWS)
54 #include <winsock.h>
55 #include <windows.h>
56 #endif
57 
58 #include "qof.h"
59 #include "gnc-engine-guile.h"
60 
61 
62 #define UNUSED_VAR __attribute__ ((unused))
63 
64 /* This static indicates the debugging module this .o belongs to. */
65 static QofLogModule UNUSED_VAR log_module = GNC_MOD_GUILE;
66 
67 static SCM
68 glist_to_scm_list_helper(GList *glist, swig_type_info *wct)
69 {
70  SCM list = SCM_EOL;
71  GList *node;
72 
73  for (node = glist; node; node = node->next)
74  list = scm_cons(SWIG_NewPointerObj(node->data, wct, 0), list);
75 
76  return scm_reverse (list);
77 }
78 
79 SCM
80 gnc_glist_to_scm_list(GList *glist, const gchar *wct)
81 {
82  swig_type_info *stype = SWIG_TypeQuery(wct);
83  g_return_val_if_fail(stype, SCM_UNDEFINED);
84  return glist_to_scm_list_helper(glist, stype);
85 }
86 
87 GList *
88 gnc_scm_list_to_glist(SCM rest)
89 {
90  GList *result = NULL;
91  SCM scm_item;
92 
93  SWIG_GetModule(NULL); /* Work-around for SWIG bug. */
94  SCM_ASSERT(scm_is_list(rest), rest, SCM_ARG1, "gnc_scm_list_to_glist");
95 
96  while (!scm_is_null(rest))
97  {
98  void *item;
99 
100  scm_item = SCM_CAR(rest);
101  rest = SCM_CDR(rest);
102 
103  if (scm_item == SCM_BOOL_F)
104  {
105  result = g_list_prepend(result, NULL);
106  }
107  else
108  {
109  if (!SWIG_IsPointer(scm_item))
110  scm_misc_error("gnc_scm_list_to_glist",
111  "Item in list not a wcp.", scm_item);
112 
113  item = (void *)SWIG_PointerAddress(scm_item);
114  result = g_list_prepend(result, item);
115  }
116  }
117 
118  return g_list_reverse(result);
119 }
120 
121 /********************************************************************
122  * gnc_glist_string_to_scm
123  * i.e. (glist-of (<gw:mchars> calee-owned) callee-owned)
124  * or equivalently
125  * i.e. (glist-of (<gw:gchars> calee-owned) callee-owned)
126  ********************************************************************/
127 SCM
128 gnc_glist_string_to_scm(GList *glist)
129 {
130  SCM list = SCM_EOL;
131  GList *node;
132 
133  for (node = glist; node; node = node->next)
134  {
135  if (node->data)
136  list = scm_cons (scm_from_utf8_string(node->data), list);
137  else
138  list = scm_cons (SCM_BOOL_F, list);
139  }
140 
141  return scm_reverse (list);
142 }
143 
144 
145 
146 
147 /********************************************************************
148  * gnc_scm_to_glist_string
149  * i.e. (glist-of (<gw:mchars> callee-owned) callee-owned)
150  * or equivalently
151  * i.e. (glist-of (<gw:gchars> callee-owned) callee-owned)
152  ********************************************************************/
153 
154 GList *
155 gnc_scm_to_glist_string(SCM list)
156 {
157  GList *glist = NULL;
158 
159  while (!scm_is_null (list))
160  {
161  if (scm_is_string(SCM_CAR(list)))
162  {
163  gchar * str;
164 
165  str = gnc_scm_to_utf8_string (SCM_CAR(list));
166  if (str)
167  glist = g_list_prepend (glist, str);
168  }
169  list = SCM_CDR (list);
170  }
171 
172  return g_list_reverse (glist);
173 }
174 
175 GSList *
176 gnc_scm_to_gslist_string(SCM list)
177 {
178  GSList *gslist = NULL;
179 
180  while (!scm_is_null (list))
181  {
182  if (scm_is_string(SCM_CAR(list)))
183  {
184  gchar * str;
185 
186  str = gnc_scm_to_utf8_string (SCM_CAR(list));
187  if (str)
188  gslist = g_slist_prepend (gslist, str);
189  }
190  list = SCM_CDR (list);
191  }
192 
193  return g_slist_reverse (gslist);
194 }
195 
196 /********************************************************************
197  * gnc_glist_string_p
198  ********************************************************************/
199 
200 int
201 gnc_glist_string_p(SCM list)
202 {
203  return scm_is_list(list);
204 }
GLib helper routines.