From 6173bb0e6c3bf51cd463f7bc4f725429d9087b2b Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Tue, 5 Oct 2010 19:14:46 +0000 Subject: Merge treeview-redux to trunk svn path=/trunk/netsurf/; revision=10865 --- utils/messages.c | 31 +++++++++++++++++++++++++++++++ utils/messages.h | 12 ++++++++++++ 2 files changed, 43 insertions(+) (limited to 'utils') diff --git a/utils/messages.c b/utils/messages.c index 2f3bc173e..6ad17c3dd 100644 --- a/utils/messages.c +++ b/utils/messages.c @@ -30,6 +30,8 @@ #include #include #include +#include + #include "utils/log.h" #include "utils/messages.h" #include "utils/utils.h" @@ -158,6 +160,35 @@ const char *messages_get_ctx(const char *key, struct hash_table *ctx) return r ? r : key; } +/* exported interface documented in messages.h */ +char *messages_get_buff(const char *key, ...) +{ + const char *msg_fmt; + char *buff = NULL; /* formatted buffer to return */ + int buff_len = 0; + va_list ap; + + msg_fmt = messages_get_ctx(key, messages_hash); + + va_start(ap, key); + buff_len = vsnprintf(buff, buff_len, msg_fmt, ap); + va_end(ap); + + buff = malloc(buff_len + 1); + + if (buff == NULL) { + LOG(("malloc failed")); + warn_user("NoMemory", 0); + } else { + va_start(ap, key); + vsnprintf(buff, buff_len + 1, msg_fmt, ap); + va_end(ap); + } + + return buff; +} + + /** * Fast lookup of a message by key from the standard Messages hash. * diff --git a/utils/messages.h b/utils/messages.h index 1c72db2f8..2b78d07ee 100644 --- a/utils/messages.h +++ b/utils/messages.h @@ -40,4 +40,16 @@ struct hash_table *messages_load_ctx(const char *path, struct hash_table *ctx); const char *messages_get_ctx(const char *key, struct hash_table *ctx); const char *messages_get(const char *key); +/** + * Formatted message from a key in the global message hash. + * + * \param key key of message + * \param ... message parameters + * \return buffer containing formatted message text or NULL if memory + * is unavailable. The caller owns the returned buffer and is + * responsible for freeing it. + */ + +char *messages_get_buff(const char *key, ...); + #endif -- cgit v1.2.3