14 #include "gnc-guile-utils.h" 19 # define strdup _strdup 25 gfec_string_from_utf8(
void *data)
27 char *str = (
char*)data;
28 return scm_from_utf8_string(str);
32 gfec_string_from_locale(
void *data)
34 char *str = (
char*)data;
35 return scm_from_locale_string(str);
39 gfec_string_inner_handler(
void *data, SCM key, SCM args)
41 char *str = (
char*)data;
42 SCM scm_string = scm_internal_catch(SCM_BOOL_T,
43 gfec_string_from_locale, (
void*)str,
44 gfec_string_inner_handler,
50 gfec_eval_string(
const char *str, gfec_error_handler error_handler)
52 SCM result = SCM_UNDEFINED;
53 SCM func = scm_c_eval_string(
"gnc:eval-string-with-error-handling");
54 if (scm_is_procedure(func))
57 SCM call_result, error = SCM_UNDEFINED;
62 SCM scm_string = scm_internal_catch(SCM_BOOL_T,
63 gfec_string_from_utf8, (
void*)str,
64 gfec_string_inner_handler,
68 error_handler(
"Contents could not be interpreted as UTF-8 or the current locale/codepage.");
71 call_result = scm_call_1 (func, scm_string);
73 error = scm_list_ref (call_result, scm_from_uint (1));
74 if (scm_is_true (error))
75 err_msg = gnc_scm_to_utf8_string (error);
77 result = scm_list_ref (call_result, scm_from_uint (0));
82 error_handler (err_msg);
92 gfec_eval_file(
const char *file, gfec_error_handler error_handler)
94 gchar *contents = NULL;
95 GError *save_error = NULL;
98 if (!g_file_get_contents (file, &contents, NULL, &save_error))
100 gchar *full_msg = g_strdup_printf (
"Couldn't read contents of %s.\nReason: %s", file, save_error->message);
101 error_handler(full_msg);
103 g_error_free (save_error);
106 return SCM_UNDEFINED;
109 result = gfec_eval_string (contents, error_handler);
114 gchar *full_msg = g_strdup_printf (
"Couldn't read contents of %s", file);
115 error_handler(full_msg);
124 gfec_apply(SCM proc, SCM arglist, gfec_error_handler error_handler)
126 SCM result = SCM_UNDEFINED;
127 SCM func = scm_c_eval_string(
"gnc:apply-with-error-handling");
128 if (scm_is_procedure(func))
130 char *err_msg = NULL;
131 SCM call_result, error;
132 call_result = scm_call_2 (func, proc, arglist);
134 error = scm_list_ref (call_result, scm_from_uint (1));
135 if (scm_is_true (error))
136 err_msg = gnc_scm_to_utf8_string (error);
138 result = scm_list_ref (call_result, scm_from_uint (0));
143 error_handler (err_msg);
152 static int error_in_scm_eval = FALSE;
155 error_handler(
const char *msg)
157 g_warning(
"%s", msg);
158 error_in_scm_eval = TRUE;
162 gfec_try_load(
const gchar *fn)
164 DEBUG(
"looking for %s", fn);
165 if (g_file_test(fn, G_FILE_TEST_EXISTS))
167 DEBUG(
"trying to load %s", fn);
168 error_in_scm_eval = FALSE;
169 gfec_eval_file(fn, error_handler);
170 return !error_in_scm_eval;
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define DEBUG(format, args...)
Print a debugging message.