GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-guile-utils.c
1 /********************************************************************\
2  * gnc-guile-utils.c -- basic guile extensions *
3  * Copyright (C) 2012 Geert Janssens *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
18 \********************************************************************/
19 
20 #include "config.h"
21 
22 #include <glib.h>
23 #include "swig-runtime.h"
24 #include <libguile.h>
25 
26 #include "qof.h"
27 #include "gnc-guile-utils.h"
28 #include "guile-mappings.h"
29 
30 /* This static indicates the debugging module this .o belongs to. */
31 static QofLogModule log_module = G_LOG_DOMAIN;
32 
33 
34 /********************************************************************\
35  * gnc_scm_to_utf8_string *
36  * returns the string representation of the scm string in *
37  * a newly allocated gchar * or NULL if it can't be retrieved. *
38  * *
39  * Args: symbol_value - the scm symbol *
40  * Returns: newly allocated gchar * or NULL, should be freed with *
41  * g_free by the caller *
42 \********************************************************************/
43 gchar *gnc_scm_to_utf8_string(SCM scm_string)
44 {
45  if (scm_is_string (scm_string))
46  {
47  gchar* s;
48  char * str;
49 
50  str = scm_to_utf8_string(scm_string);
51  s = g_strdup(str);
52  free (str);
53  return s;
54  }
55 
56  /* Unable to extract string from the symbol...*/
57  PERR("bad value\n");
58  return NULL;
59 }
60 
61 
62 /********************************************************************\
63  * gnc_scm_to_locale_string *
64  * returns the string representation of the scm string in *
65  * a newly allocated gchar * or NULL if it can't be retrieved. *
66  * The string will be encoded in the current locale's encoding. *
67  * Note: this function should only be use to convert filenames or *
68  * strings from the environment. Or other strings that are in the *
69  * system locale. *
70  * *
71  * Args: symbol_value - the scm symbol *
72  * Returns: newly allocated gchar * or NULL, should be freed with *
73  * g_free by the caller *
74 \********************************************************************/
75 gchar *gnc_scm_to_locale_string(SCM scm_string)
76 {
77  if (scm_is_string (scm_string))
78  {
79  gchar* s;
80  char * str;
81 
82  str = scm_to_locale_string(scm_string);
83  s = g_strdup(str);
84  free (str);
85  return s;
86  }
87 
88  /* Unable to extract string from the symbol...*/
89  PERR("bad value\n");
90  return NULL;
91 }
92 
93 
94 /********************************************************************\
95  * gnc_scm_symbol_to_locale_string *
96  * returns the string representation of the scm symbol in *
97  * a newly allocated gchar * or NULL if it can't be retrieved. *
98  * *
99  * Args: symbol_value - the scm symbol *
100  * Returns: newly allocated gchar * or NULL, should be freed with *
101  * g_free by the caller *
102 \********************************************************************/
103 gchar *
104 gnc_scm_symbol_to_locale_string(SCM symbol_value)
105 {
106 
107  if (scm_is_symbol(symbol_value))
108  {
109  SCM string_value = scm_symbol_to_string (symbol_value);
110  if (scm_is_string (string_value))
111  {
112  char *tmp = scm_to_utf8_string (string_value);
113  gchar *str = g_strdup (tmp);
114  free (tmp);
115  return str;
116  }
117  }
118 
119  /* Unable to extract string from the symbol...*/
120  PERR("bad value\n");
121  return NULL;
122 }
123 
124 
125 /********************************************************************\
126  * gnc_scm_call_1_to_string *
127  * returns the malloc'ed string returned by the guile function *
128  * or NULL if it can't be retrieved *
129  * *
130  * Args: func - the guile function to call *
131  * arg - the single function argument *
132  * Returns: g_malloc'ed char * or NULL must be freed with g_free *
133 \********************************************************************/
134 char *
135 gnc_scm_call_1_to_string(SCM func, SCM arg)
136 {
137  SCM value;
138 
139  if (scm_is_procedure(func))
140  {
141  value = scm_call_1(func, arg);
142 
143  if (scm_is_string(value))
144  {
145  return gnc_scm_to_utf8_string(value);
146  }
147  else
148  {
149  PERR("bad value\n");
150  }
151  }
152  else
153  {
154  PERR("not a procedure\n");
155  }
156 
157  return NULL;
158 }
159 
160 
161 /********************************************************************\
162  * gnc_scm_call_1_symbol_to_string *
163  * returns the malloc'ed string returned by the guile function *
164  * or NULL if it can't be retrieved. The return value of the *
165  * function should be a symbol. *
166  * *
167  * Args: func - the guile function to call *
168  * arg - the single function argument *
169  * Returns: malloc'ed char * or NULL *
170 \********************************************************************/
171 char *
172 gnc_scm_call_1_symbol_to_string(SCM func, SCM arg)
173 {
174  SCM symbol_value;
175 
176  if (scm_is_procedure(func))
177  {
178  symbol_value = scm_call_1(func, arg);
179  return gnc_scm_symbol_to_locale_string (symbol_value);
180  }
181  else
182  {
183  PERR("not a procedure\n");
184  }
185 
186  return NULL;
187 }
188 
189 
190 /********************************************************************\
191  * gnc_scm_call_1_to_procedure *
192  * returns the SCM handle to the procedure returned by the guile *
193  * function, or SCM_UNDEFINED if it couldn't be retrieved. *
194  * *
195  * Args: func - the guile function to call *
196  * arg - the single function argument *
197  * Returns: SCM function handle or SCM_UNDEFINED *
198 \********************************************************************/
199 SCM
200 gnc_scm_call_1_to_procedure(SCM func, SCM arg)
201 {
202  SCM value;
203 
204  if (scm_is_procedure(func))
205  {
206  value = scm_call_1(func, arg);
207 
208  if (scm_is_procedure(value))
209  return value;
210  else
211  {
212  PERR("bad value\n");
213  }
214  }
215  else
216  {
217  PERR("not a procedure\n");
218  }
219 
220  return SCM_UNDEFINED;
221 }
222 
223 
224 /********************************************************************\
225  * gnc_scm_call_1_to_list *
226  * returns the SCM handle to the list returned by the guile *
227  * function, or SCM_UNDEFINED if it couldn't be retrieved. *
228  * *
229  * Args: func - the guile function to call *
230  * arg - the single function argument *
231  * Returns: SCM list handle or SCM_UNDEFINED *
232 \********************************************************************/
233 SCM
234 gnc_scm_call_1_to_list(SCM func, SCM arg)
235 {
236  SCM value;
237 
238  if (scm_is_procedure(func))
239  {
240  value = scm_call_1(func, arg);
241 
242  if (scm_is_list(value))
243  return value;
244  else
245  {
246  PERR("bad value\n");
247  }
248  }
249  else
250  {
251  PERR("not a procedure\n");
252  }
253 
254  return SCM_UNDEFINED;
255 }
256 
257 
258 /********************************************************************\
259  * gnc_scm_call_1_to_vector *
260  * returns the SCM handle to the vector returned by the guile *
261  * function, or SCM_UNDEFINED if it couldn't be retrieved. *
262  * *
263  * Args: func - the guile function to call *
264  * arg - the single function argument *
265  * Returns: SCM vector handle or SCM_UNDEFINED *
266 \********************************************************************/
267 SCM
268 gnc_scm_call_1_to_vector(SCM func, SCM arg)
269 {
270  SCM value;
271 
272  if (scm_is_procedure(func))
273  {
274  value = scm_call_1(func, arg);
275 
276  if (scm_is_vector(value))
277  return value;
278  else
279  {
280  PERR("bad value\n");
281  }
282  }
283  else
284  {
285  PERR("not a procedure\n");
286  }
287 
288  return SCM_UNDEFINED;
289 }
290 
291 
292 /* Clean up a scheme options string for use in a key/value file.
293  * This function removes all full line comments, removes all blank
294  * lines, and removes all leading/trailing white space. */
295 gchar *gnc_scm_strip_comments (SCM scm_text)
296 {
297  gchar *raw_text, *text, **splits;
298  gint i, j;
299 
300  raw_text = gnc_scm_to_utf8_string (scm_text);
301  splits = g_strsplit(raw_text, "\n", -1);
302  for (i = j = 0; splits[i]; i++)
303  {
304  gchar *haystack, *needle;
305  if ((splits[i][0] == ';') || (splits[i][0] == '\0'))
306  {
307  g_free(splits[i]);
308  continue;
309  }
310 
311  /* Work around a bug in guile 1.8 that escapes spaces
312  * in a symbol printed on a string port. We don't
313  * want this, because this string can't be properly
314  * converted back into a symbol later on. */
315 
316  haystack = splits [i];
317  needle = g_strstr_len (haystack, -1, "\\ ");
318  while (needle)
319  {
320  gchar *new_haystack = NULL;
321  gsize prefix_size = needle - haystack;
322  gchar *prefix = g_strndup (haystack, prefix_size);
323  needle++;
324  new_haystack = g_strconcat (prefix, needle, NULL);
325  g_free (prefix);
326  g_free (haystack);
327  haystack = new_haystack;
328  needle = g_strstr_len (haystack, -1, "\\ ");
329  }
330  splits[j++] = haystack;
331  }
332  splits[j] = NULL;
333 
334  text = g_strjoinv(" ", splits);
335  g_free (raw_text);
336  g_strfreev(splits);
337  return text;
338 }
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PERR(format, args...)
Definition: qoflog.h:237
const gchar * QofLogModule
Definition: qofid.h:89