summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/core-string.c55
-rw-r--r--src/core/core-string.h1
-rw-r--r--src/plugins/plugin.c1
-rw-r--r--src/plugins/weechat-plugin.h7
4 files changed, 62 insertions, 2 deletions
diff --git a/src/core/core-string.c b/src/core/core-string.c
index 572e6d1cd..4b8f3c957 100644
--- a/src/core/core-string.c
+++ b/src/core/core-string.c
@@ -71,6 +71,61 @@ char **string_concat_buffer[STRING_NUM_CONCAT_BUFFERS];
/*
+ * Formats a message in a string allocated by the function.
+ *
+ * This function is defined for systems where the GNU function `asprintf()`
+ * is not available.
+ * The behavior is almost the same except that `*result` is set to NULL on error.
+ *
+ * Returns the number of bytes in the resulting string, negative value in case
+ * of error.
+ *
+ * Value of `*result` is allocated with the result string (NULL if error),
+ * it must be freed after use.
+ */
+
+int
+string_asprintf (char **result, const char *fmt, ...)
+{
+ va_list argptr;
+ int num_bytes;
+ size_t size;
+
+ if (!result)
+ return -1;
+
+ *result = NULL;
+
+ if (!fmt)
+ return -1;
+
+ /* determine required size */
+ va_start (argptr, fmt);
+ num_bytes = vsnprintf (NULL, 0, fmt, argptr);
+ va_end (argptr);
+
+ if (num_bytes < 0)
+ return num_bytes;
+
+ size = (size_t)num_bytes + 1;
+ *result = malloc (size);
+ if (!*result)
+ return -1;
+
+ va_start (argptr, fmt);
+ num_bytes = vsnprintf (*result, size, fmt, argptr);
+ va_end (argptr);
+
+ if (num_bytes < 0)
+ {
+ free (*result);
+ *result = NULL;
+ }
+
+ return num_bytes;
+}
+
+/*
* Defines a "strndup" function for systems where this function does not exist
* (FreeBSD and maybe others).
*
diff --git a/src/core/core-string.h b/src/core/core-string.h
index e79ce6279..6408114f2 100644
--- a/src/core/core-string.h
+++ b/src/core/core-string.h
@@ -39,6 +39,7 @@ struct t_string_dyn
struct t_hashtable;
+extern int string_asprintf (char **result, const char *fmt, ...);
extern char *string_strndup (const char *string, int bytes);
extern char *string_cut (const char *string, int length, int count_suffix,
int screen, const char *cut_suffix);
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index 90227fd25..d885774b8 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -608,6 +608,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->iconv_from_internal = &string_iconv_from_internal;
new_plugin->gettext = &plugin_api_gettext;
new_plugin->ngettext = &plugin_api_ngettext;
+ new_plugin->asprintf = &string_asprintf;
new_plugin->strndup = &string_strndup;
new_plugin->string_cut = &string_cut;
new_plugin->string_tolower = &string_tolower;
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index c71313545..052ccb90c 100644
--- a/src/plugins/weechat-plugin.h
+++ b/src/plugins/weechat-plugin.h
@@ -73,7 +73,7 @@ struct t_weelist_item;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
-#define WEECHAT_PLUGIN_API_VERSION "20240307-01"
+#define WEECHAT_PLUGIN_API_VERSION "20240402-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -316,6 +316,7 @@ struct t_weechat_plugin
char *(*iconv_from_internal) (const char *charset, const char *string);
const char *(*gettext) (const char *string);
const char *(*ngettext) (const char *single, const char *plural, int count);
+ int (*asprintf) (char **result, const char *fmt, ...);
char *(*strndup) (const char *string, int bytes);
char *(*string_cut) (const char *string, int length, int count_suffix,
int screen, const char *cut_suffix);
@@ -1290,7 +1291,9 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
#define weechat_gettext(string) (weechat_plugin->gettext)(string)
#define weechat_ngettext(single,plural,number) \
(weechat_plugin->ngettext)(single, plural, number)
-#define weechat_strndup(__string, __bytes) \
+#define weechat_asprintf(__result, __fmt, __argz...) \
+ (weechat_plugin->asprintf)(__result, __fmt, ##__argz)
+#define weechat_strndup(__string, __bytes) \
(weechat_plugin->strndup)(__string, __bytes)
#define weechat_string_cut(__string, __length, __count_suffix, \
__screen, __cut_suffix) \