1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 12:56:37 +02:00

api: add function "asprintf"

This commit is contained in:
Sébastien Helleu
2024-04-02 19:32:13 +02:00
parent 08bc6404eb
commit 2cf66de423
10 changed files with 360 additions and 2 deletions
+55
View File
@@ -70,6 +70,61 @@ int string_concat_index = 0;
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).
+1
View File
@@ -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);
+1
View File
@@ -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;
+5 -2
View File
@@ -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) \