* mu-guile, mu-guile-message: cleanups, fix some small leaks

This commit is contained in:
djcb
2012-01-09 08:21:35 +02:00
parent ba3448fe30
commit e650e6e212
3 changed files with 52 additions and 44 deletions

View File

@ -179,7 +179,7 @@ check_flag (MuFlags flag, FlagData *fdata)
return; return;
flagsym = g_strconcat ("mu:", mu_flag_name(flag), NULL); flagsym = g_strconcat ("mu:", mu_flag_name(flag), NULL);
item = scm_list_1 (scm_from_locale_symbol(flagsym)); item = scm_list_1 (scm_from_utf8_symbol(flagsym));
g_free (flagsym); g_free (flagsym);
@ -207,11 +207,11 @@ get_prio_scm (MuMsg *msg)
switch (mu_msg_get_prio (msg)) { switch (mu_msg_get_prio (msg)) {
case MU_MSG_PRIO_LOW: case MU_MSG_PRIO_LOW:
return scm_from_locale_symbol("mu:low"); return scm_from_utf8_symbol("mu:low");
case MU_MSG_PRIO_NORMAL: case MU_MSG_PRIO_NORMAL:
return scm_from_locale_symbol("mu:normal"); return scm_from_utf8_symbol("mu:normal");
case MU_MSG_PRIO_HIGH: case MU_MSG_PRIO_HIGH:
return scm_from_locale_symbol("mu:high"); return scm_from_utf8_symbol("mu:high");
default: default:
g_return_val_if_reached (SCM_UNDEFINED); g_return_val_if_reached (SCM_UNDEFINED);
} }
@ -229,7 +229,7 @@ msg_string_list_field (MuMsg *msg, MuMsgFieldId mfid)
lst = g_slist_next(lst)) { lst = g_slist_next(lst)) {
SCM item; SCM item;
item = scm_list_1 item = scm_list_1
(scm_from_string_or_null((const char*)lst->data)); (scm_from_str_or_null((const char*)lst->data));
scmlst = scm_append_x (scm_list_2(scmlst, item)); scmlst = scm_append_x (scm_list_2(scmlst, item));
} }
@ -260,7 +260,7 @@ SCM_DEFINE_PUBLIC(get_field, "mu:get-field", 2, 0, 0,
switch (mu_msg_field_type (mfid)) { switch (mu_msg_field_type (mfid)) {
case MU_MSG_FIELD_TYPE_STRING: case MU_MSG_FIELD_TYPE_STRING:
return scm_from_string_or_null return scm_from_str_or_null
(mu_msg_get_field_string(msgwrap->_msg, mfid)); (mu_msg_get_field_string(msgwrap->_msg, mfid));
case MU_MSG_FIELD_TYPE_BYTESIZE: case MU_MSG_FIELD_TYPE_BYTESIZE:
case MU_MSG_FIELD_TYPE_TIME_T: case MU_MSG_FIELD_TYPE_TIME_T:
@ -288,22 +288,18 @@ typedef struct _EachContactData EachContactData;
static void static void
contacts_to_list (MuMsgContact *contact, EachContactData *ecdata) contacts_to_list (MuMsgContact *contact, EachContactData *ecdata)
{ {
if (ecdata->ctype == MU_MSG_CONTACT_TYPE_ALL || SCM item;
mu_msg_contact_type (contact) == ecdata->ctype) {
SCM item; if (ecdata->ctype != MU_MSG_CONTACT_TYPE_ALL &&
const char *addr, *name; mu_msg_contact_type (contact) != ecdata->ctype)
return;
addr = mu_msg_contact_address (contact); item = scm_list_1
name = mu_msg_contact_name (contact); (scm_cons
(scm_from_str_or_null(mu_msg_contact_name (contact)),
scm_from_str_or_null(mu_msg_contact_address (contact))));
item = scm_list_1 ecdata->lst = scm_append_x (scm_list_2(ecdata->lst, item));
(scm_cons (
scm_from_string_or_null(name),
scm_from_string_or_null(addr)));
ecdata->lst = scm_append_x (scm_list_2(ecdata->lst, item));
}
} }
@ -353,7 +349,7 @@ SCM_DEFINE_PUBLIC (get_header, "mu:get-header", 2, 0, 0,
#define FUNC_NAME s_get_header #define FUNC_NAME s_get_header
{ {
MuMsgWrapper *msgwrap; MuMsgWrapper *msgwrap;
const char *header; char *header;
const char *val; const char *val;
SCM_ASSERT (mu_guile_scm_is_msg(MSG), MSG, SCM_ARG1, FUNC_NAME); SCM_ASSERT (mu_guile_scm_is_msg(MSG), MSG, SCM_ARG1, FUNC_NAME);
@ -363,8 +359,9 @@ SCM_DEFINE_PUBLIC (get_header, "mu:get-header", 2, 0, 0,
msgwrap = (MuMsgWrapper*) SCM_CDR(MSG); msgwrap = (MuMsgWrapper*) SCM_CDR(MSG);
header = scm_to_utf8_string (HEADER); header = scm_to_utf8_string (HEADER);
val = mu_msg_get_header(msgwrap->_msg, header); val = mu_msg_get_header(msgwrap->_msg, header);
free (header);
return val ? scm_from_string_or_null(val) : SCM_BOOL_F; return val ? scm_from_str_or_null(val) : SCM_BOOL_F;
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -492,12 +489,11 @@ SCM_DEFINE_PUBLIC (for_each_msg_internal, "mu:for-each-msg-internal", 2, 0, 0,
#define FUNC_NAME s_for_each_msg_internal #define FUNC_NAME s_for_each_msg_internal
{ {
MuMsgIter *iter; MuMsgIter *iter;
const char* expr; char* expr;
SCM_ASSERT (scm_procedure_p (FUNC), FUNC, SCM_ARG1, FUNC_NAME); SCM_ASSERT (scm_procedure_p (FUNC), FUNC, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_bool(EXPR) || scm_is_string (EXPR), SCM_ASSERT (scm_is_bool(EXPR) || scm_is_string (EXPR),
EXPR, SCM_ARG2, FUNC_NAME); EXPR, SCM_ARG2, FUNC_NAME);
if (!mu_guile_initialized()) if (!mu_guile_initialized())
return mu_guile_error (FUNC_NAME, 0, "mu not initialized", return mu_guile_error (FUNC_NAME, 0, "mu not initialized",
SCM_UNSPECIFIED); SCM_UNSPECIFIED);
@ -506,11 +502,13 @@ SCM_DEFINE_PUBLIC (for_each_msg_internal, "mu:for-each-msg-internal", 2, 0, 0,
return SCM_UNSPECIFIED; /* nothing to do */ return SCM_UNSPECIFIED; /* nothing to do */
if (EXPR == SCM_BOOL_T) if (EXPR == SCM_BOOL_T)
expr = ""; /* note, "" matches *all* messages */ expr = strdup (""); /* note, "" matches *all* messages */
else else
expr = scm_to_utf8_string(EXPR); expr = scm_to_utf8_string(EXPR);
iter = get_query_iter (mu_guile_instance()->query, expr); iter = get_query_iter (mu_guile_instance()->query, expr);
free (expr);
if (!iter) if (!iter)
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;

View File

@ -21,6 +21,8 @@
#include <config.h> #include <config.h>
#endif /*HAVE_CONFIG_H*/ #endif /*HAVE_CONFIG_H*/
#include <locale.h>
#include <glib-object.h> #include <glib-object.h>
#include <libguile.h> #include <libguile.h>
@ -34,11 +36,14 @@
#include "mu-guile.h" #include "mu-guile.h"
SCM SCM
scm_from_string_or_null (const char *str) scm_from_str_or_null (const char *str)
{ {
return str ? scm_from_utf8_string (str) : SCM_BOOL_F; if (!str)
return SCM_BOOL_F;
return scm_from_stringn (str, strlen(str), "UTF-8",
SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
} }
@ -51,7 +56,7 @@ mu_guile_error (const char *func_name, int status,
scm_from_utf8_string (fmt), args, scm_from_utf8_string (fmt), args,
scm_list_1 (scm_from_int (status))); scm_list_1 (scm_from_int (status)));
return SCM_UNSPECIFIED; return SCM_BOOL_F;
} }
SCM SCM
@ -62,7 +67,7 @@ mu_guile_g_error (const char *func_name, GError *err)
scm_from_utf8_string (err ? err->message : "error"), scm_from_utf8_string (err ? err->message : "error"),
SCM_UNDEFINED, SCM_UNDEFINED); SCM_UNDEFINED, SCM_UNDEFINED);
return SCM_UNSPECIFIED; return SCM_BOOL_F;
} }
@ -77,6 +82,8 @@ mu_guile_init_instance (const char *muhome)
MuQuery *query; MuQuery *query;
GError *err; GError *err;
setlocale (LC_ALL, "");
if (!mu_runtime_init (muhome, "guile")) if (!mu_runtime_init (muhome, "guile"))
return FALSE; return FALSE;
@ -131,34 +138,36 @@ mu_guile_initialized (void)
SCM_DEFINE_PUBLIC (mu_initialize, "mu:initialize", 0, 2, 0, SCM_DEFINE_PUBLIC (mu_initialize, "mu:initialize", 0, 1, 0,
(SCM PARAM, SCM MUHOME), (SCM MUHOME),
"Initialize mu - needed before you call any of the other " "Initialize mu - needed before you call any of the other "
"functions. Optionally, you can provide PARAM (must be #t for now if " "functions. Optionally, you can provide MUHOME which should be an "
"provided, for future use) and MUHOME which should be an absolute path " "absolute path to your mu home directory "
"to your mu home directory " "-- typically, the default, ~/.mu, should be just fine\n.")
"-- typically, the default, ~/.mu, should be just fine\n.")
#define FUNC_NAME s_mu_initialize #define FUNC_NAME s_mu_initialize
{ {
const char *muhome; char *muhome;
gboolean rv;
SCM_ASSERT (PARAM == SCM_BOOL_T || SCM_UNBNDP(PARAM),
PARAM, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_string (MUHOME) || MUHOME == SCM_BOOL_F || SCM_UNBNDP(MUHOME), SCM_ASSERT (scm_is_string (MUHOME) || MUHOME == SCM_BOOL_F || SCM_UNBNDP(MUHOME),
MUHOME, SCM_ARG2, FUNC_NAME); MUHOME, SCM_ARG1, FUNC_NAME);
if (mu_guile_initialized()) if (mu_guile_initialized())
return mu_guile_error (FUNC_NAME, 0, "Already initialized", return mu_guile_error (FUNC_NAME, 0, "Already initialized",
SCM_UNSPECIFIED); SCM_UNSPECIFIED);
if (SCM_UNBNDP(MUHOME) || MUHOME == SCM_BOOL_F) if (SCM_UNBNDP(MUHOME) || MUHOME == SCM_BOOL_F)
muhome = NULL; muhome = NULL;
else else
muhome = scm_to_utf8_string (MUHOME); muhome = scm_to_utf8_string (MUHOME);
if (!mu_guile_init_instance(muhome)) rv = mu_guile_init_instance(muhome);
free (muhome);
if (!rv)
return mu_guile_error (FUNC_NAME, 0, "Failed to initialize mu", return mu_guile_error (FUNC_NAME, 0, "Failed to initialize mu",
SCM_UNSPECIFIED); SCM_UNSPECIFIED);
/* cleanup when we're exiting */ /* cleanup when we're exiting */
g_atexit (mu_guile_uninit_instance); g_atexit (mu_guile_uninit_instance);
@ -193,6 +202,7 @@ write_log (GLogLevelFlags level, SCM FRM, SCM ARGS)
gchar *output; gchar *output;
output = scm_to_utf8_string (str); output = scm_to_utf8_string (str);
g_log (G_LOG_DOMAIN, level, "%s", output); g_log (G_LOG_DOMAIN, level, "%s", output);
free (output);
} }
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;

View File

@ -80,7 +80,7 @@ SCM mu_guile_error (const char *func_name, int status,
* *
* @return a guile string or #f * @return a guile string or #f
*/ */
SCM scm_from_string_or_null (const char *str); SCM scm_from_str_or_null (const char *str);
/** /**