2012-12-15 11:58:40 -06:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc-guile-utils.c -- basic guile extensions *
|
|
|
|
* Copyright (C) 2012 Geert Janssens *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2012-12-15 11:58:40 -06:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include "swig-runtime.h"
|
|
|
|
#include <libguile.h>
|
|
|
|
|
|
|
|
#include "gnc-guile-utils.h"
|
|
|
|
#include "guile-mappings.h"
|
|
|
|
|
|
|
|
|
2013-12-13 14:21:41 -06:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc_scm_to_utf8_string *
|
|
|
|
* returns the string representation of the scm string in *
|
|
|
|
* a newly allocated gchar * or NULL if it can't be retrieved. *
|
|
|
|
* *
|
|
|
|
* Args: symbol_value - the scm symbol *
|
|
|
|
* Returns: newly allocated gchar * or NULL, should be freed with *
|
|
|
|
* g_free by the caller *
|
|
|
|
\********************************************************************/
|
|
|
|
gchar *gnc_scm_to_utf8_string(SCM scm_string)
|
|
|
|
{
|
|
|
|
if (scm_is_string (scm_string))
|
2021-10-14 09:09:18 -05:00
|
|
|
return scm_to_utf8_stringn(scm_string, NULL);
|
2013-12-13 14:21:41 -06:00
|
|
|
|
|
|
|
/* Unable to extract string from the symbol...*/
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2013-12-13 14:21:41 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-15 11:58:40 -06:00
|
|
|
/********************************************************************\
|
2012-12-22 12:20:29 -06:00
|
|
|
* gnc_scm_to_locale_string *
|
|
|
|
* returns the string representation of the scm string in *
|
|
|
|
* a newly allocated gchar * or NULL if it can't be retrieved. *
|
2013-12-13 14:21:41 -06:00
|
|
|
* The string will be encoded in the current locale's encoding. *
|
|
|
|
* Note: this function should only be use to convert filenames or *
|
|
|
|
* strings from the environment. Or other strings that are in the *
|
|
|
|
* system locale. *
|
2012-12-22 12:20:29 -06:00
|
|
|
* *
|
|
|
|
* Args: symbol_value - the scm symbol *
|
|
|
|
* Returns: newly allocated gchar * or NULL, should be freed with *
|
|
|
|
* g_free by the caller *
|
|
|
|
\********************************************************************/
|
|
|
|
gchar *gnc_scm_to_locale_string(SCM scm_string)
|
|
|
|
{
|
|
|
|
if (scm_is_string (scm_string))
|
2021-10-14 09:09:18 -05:00
|
|
|
return scm_to_locale_string(scm_string);
|
2012-12-22 12:20:29 -06:00
|
|
|
|
|
|
|
/* Unable to extract string from the symbol...*/
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2012-12-22 12:20:29 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************\
|
|
|
|
* gnc_scm_symbol_to_locale_string *
|
2012-12-15 11:58:40 -06:00
|
|
|
* returns the string representation of the scm symbol in *
|
|
|
|
* a newly allocated gchar * or NULL if it can't be retrieved. *
|
|
|
|
* *
|
|
|
|
* Args: symbol_value - the scm symbol *
|
|
|
|
* Returns: newly allocated gchar * or NULL, should be freed with *
|
|
|
|
* g_free by the caller *
|
|
|
|
\********************************************************************/
|
|
|
|
gchar *
|
|
|
|
gnc_scm_symbol_to_locale_string(SCM symbol_value)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (scm_is_symbol(symbol_value))
|
|
|
|
{
|
|
|
|
SCM string_value = scm_symbol_to_string (symbol_value);
|
|
|
|
if (scm_is_string (string_value))
|
2021-10-14 09:09:18 -05:00
|
|
|
return scm_to_utf8_string (string_value);
|
2012-12-15 11:58:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unable to extract string from the symbol...*/
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2012-12-15 11:58:40 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-12-22 12:20:05 -06:00
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************\
|
2012-12-22 12:20:53 -06:00
|
|
|
* gnc_scm_call_1_to_string *
|
2012-12-22 12:20:05 -06:00
|
|
|
* returns the malloc'ed string returned by the guile function *
|
|
|
|
* or NULL if it can't be retrieved *
|
|
|
|
* *
|
|
|
|
* Args: func - the guile function to call *
|
|
|
|
* arg - the single function argument *
|
|
|
|
* Returns: g_malloc'ed char * or NULL must be freed with g_free *
|
|
|
|
\********************************************************************/
|
|
|
|
char *
|
2012-12-22 12:20:53 -06:00
|
|
|
gnc_scm_call_1_to_string(SCM func, SCM arg)
|
2012-12-22 12:20:05 -06:00
|
|
|
{
|
|
|
|
SCM value;
|
|
|
|
|
|
|
|
if (scm_is_procedure(func))
|
|
|
|
{
|
|
|
|
value = scm_call_1(func, arg);
|
|
|
|
|
|
|
|
if (scm_is_string(value))
|
|
|
|
{
|
2013-12-13 14:21:41 -06:00
|
|
|
return gnc_scm_to_utf8_string(value);
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("not a procedure\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************\
|
2012-12-22 12:20:53 -06:00
|
|
|
* gnc_scm_call_1_symbol_to_string *
|
2012-12-22 12:20:05 -06:00
|
|
|
* returns the malloc'ed string returned by the guile function *
|
|
|
|
* or NULL if it can't be retrieved. The return value of the *
|
|
|
|
* function should be a symbol. *
|
|
|
|
* *
|
|
|
|
* Args: func - the guile function to call *
|
|
|
|
* arg - the single function argument *
|
|
|
|
* Returns: malloc'ed char * or NULL *
|
|
|
|
\********************************************************************/
|
|
|
|
char *
|
2012-12-22 12:20:53 -06:00
|
|
|
gnc_scm_call_1_symbol_to_string(SCM func, SCM arg)
|
2012-12-22 12:20:05 -06:00
|
|
|
{
|
|
|
|
SCM symbol_value;
|
|
|
|
|
|
|
|
if (scm_is_procedure(func))
|
|
|
|
{
|
|
|
|
symbol_value = scm_call_1(func, arg);
|
|
|
|
return gnc_scm_symbol_to_locale_string (symbol_value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("not a procedure\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************\
|
2012-12-22 12:20:53 -06:00
|
|
|
* gnc_scm_call_1_to_procedure *
|
2012-12-22 12:20:05 -06:00
|
|
|
* returns the SCM handle to the procedure returned by the guile *
|
|
|
|
* function, or SCM_UNDEFINED if it couldn't be retrieved. *
|
|
|
|
* *
|
|
|
|
* Args: func - the guile function to call *
|
|
|
|
* arg - the single function argument *
|
|
|
|
* Returns: SCM function handle or SCM_UNDEFINED *
|
|
|
|
\********************************************************************/
|
|
|
|
SCM
|
2012-12-22 12:20:53 -06:00
|
|
|
gnc_scm_call_1_to_procedure(SCM func, SCM arg)
|
2012-12-22 12:20:05 -06:00
|
|
|
{
|
|
|
|
SCM value;
|
|
|
|
|
|
|
|
if (scm_is_procedure(func))
|
|
|
|
{
|
|
|
|
value = scm_call_1(func, arg);
|
|
|
|
|
|
|
|
if (scm_is_procedure(value))
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("not a procedure\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return SCM_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************\
|
2012-12-22 12:20:53 -06:00
|
|
|
* gnc_scm_call_1_to_list *
|
2012-12-22 12:20:05 -06:00
|
|
|
* returns the SCM handle to the list returned by the guile *
|
|
|
|
* function, or SCM_UNDEFINED if it couldn't be retrieved. *
|
|
|
|
* *
|
|
|
|
* Args: func - the guile function to call *
|
|
|
|
* arg - the single function argument *
|
|
|
|
* Returns: SCM list handle or SCM_UNDEFINED *
|
|
|
|
\********************************************************************/
|
|
|
|
SCM
|
2012-12-22 12:20:53 -06:00
|
|
|
gnc_scm_call_1_to_list(SCM func, SCM arg)
|
2012-12-22 12:20:05 -06:00
|
|
|
{
|
|
|
|
SCM value;
|
|
|
|
|
|
|
|
if (scm_is_procedure(func))
|
|
|
|
{
|
|
|
|
value = scm_call_1(func, arg);
|
|
|
|
|
|
|
|
if (scm_is_list(value))
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("not a procedure\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return SCM_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************\
|
2012-12-22 12:20:53 -06:00
|
|
|
* gnc_scm_call_1_to_vector *
|
2012-12-22 12:20:05 -06:00
|
|
|
* returns the SCM handle to the vector returned by the guile *
|
|
|
|
* function, or SCM_UNDEFINED if it couldn't be retrieved. *
|
|
|
|
* *
|
|
|
|
* Args: func - the guile function to call *
|
|
|
|
* arg - the single function argument *
|
|
|
|
* Returns: SCM vector handle or SCM_UNDEFINED *
|
|
|
|
\********************************************************************/
|
|
|
|
SCM
|
2012-12-22 12:20:53 -06:00
|
|
|
gnc_scm_call_1_to_vector(SCM func, SCM arg)
|
2012-12-22 12:20:05 -06:00
|
|
|
{
|
|
|
|
SCM value;
|
|
|
|
|
|
|
|
if (scm_is_procedure(func))
|
|
|
|
{
|
|
|
|
value = scm_call_1(func, arg);
|
|
|
|
|
|
|
|
if (scm_is_vector(value))
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("bad value\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 10:12:16 -05:00
|
|
|
g_error ("not a procedure\n");
|
2012-12-22 12:20:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return SCM_UNDEFINED;
|
|
|
|
}
|
2012-12-22 12:21:03 -06:00
|
|
|
|
|
|
|
|
|
|
|
/* Clean up a scheme options string for use in a key/value file.
|
|
|
|
* This function removes all full line comments, removes all blank
|
|
|
|
* lines, and removes all leading/trailing white space. */
|
|
|
|
gchar *gnc_scm_strip_comments (SCM scm_text)
|
|
|
|
{
|
|
|
|
gchar *raw_text, *text, **splits;
|
|
|
|
gint i, j;
|
|
|
|
|
2013-12-13 14:21:41 -06:00
|
|
|
raw_text = gnc_scm_to_utf8_string (scm_text);
|
2012-12-22 12:21:03 -06:00
|
|
|
splits = g_strsplit(raw_text, "\n", -1);
|
|
|
|
for (i = j = 0; splits[i]; i++)
|
|
|
|
{
|
|
|
|
if ((splits[i][0] == ';') || (splits[i][0] == '\0'))
|
|
|
|
{
|
|
|
|
g_free(splits[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-19 16:13:01 -06:00
|
|
|
splits[j++] = splits [i];
|
2012-12-22 12:21:03 -06:00
|
|
|
}
|
|
|
|
splits[j] = NULL;
|
|
|
|
|
|
|
|
text = g_strjoinv(" ", splits);
|
|
|
|
g_free (raw_text);
|
|
|
|
g_strfreev(splits);
|
|
|
|
return text;
|
|
|
|
}
|