diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2022-12-03 16:56:01 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2022-12-10 16:05:14 +0100 |
commit | 71ae8f1907592a25d8fa6b10e02020f19c16b215 (patch) | |
tree | 42d2bccfc3f77ecca605e5441211cb827f69d8da /src | |
parent | e5cbbd781d814e321845598775f594f0f808e18e (diff) | |
download | weechat-71ae8f1907592a25d8fa6b10e02020f19c16b215.zip |
api: add function utf8_strncpy
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-utf8.c | 27 | ||||
-rw-r--r-- | src/core/wee-utf8.h | 1 | ||||
-rw-r--r-- | src/plugins/plugin.c | 1 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 5 |
4 files changed, 33 insertions, 1 deletions
diff --git a/src/core/wee-utf8.c b/src/core/wee-utf8.c index 78f717817..6f6526319 100644 --- a/src/core/wee-utf8.c +++ b/src/core/wee-utf8.c @@ -738,3 +738,30 @@ utf8_strndup (const char *string, int length) return string_strndup (string, end - string); } + +/* + * Copies max N chars from a string to another and adds null byte at the end. + * + * Note: the target string "dest" must be long enough. + */ + +void +utf8_strncpy (char *dest, const char *string, int length) +{ + const char *end; + + if (!dest) + return; + + dest[0] = '\0'; + + if (!string || (length <= 0)) + return; + + end = utf8_add_offset (string, length); + if (!end || (end == string)) + return; + + memcpy (dest, string, end - string); + dest[end - string] = '\0'; +} diff --git a/src/core/wee-utf8.h b/src/core/wee-utf8.h index 5a228cc0f..85259ef82 100644 --- a/src/core/wee-utf8.h +++ b/src/core/wee-utf8.h @@ -51,5 +51,6 @@ extern const char *utf8_add_offset (const char *string, int offset); extern int utf8_real_pos (const char *string, int pos); extern int utf8_pos (const char *string, int real_pos); extern char *utf8_strndup (const char *string, int length); +extern void utf8_strncpy (char *dest, const char *string, int length); #endif /* WEECHAT_UTF8_H */ diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 6f2e7a5cc..b9b7584fb 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -665,6 +665,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->utf8_real_pos = &utf8_real_pos; new_plugin->utf8_pos = &utf8_pos; new_plugin->utf8_strndup = &utf8_strndup; + new_plugin->utf8_strncpy = &utf8_strncpy; new_plugin->crypto_hash = &plugin_api_crypto_hash; new_plugin->crypto_hash_file = &plugin_api_crypto_hash_file; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 19c09ba37..551f14008 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -68,7 +68,7 @@ struct timeval; * 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 "20221003-01" +#define WEECHAT_PLUGIN_API_VERSION "20221203-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -379,6 +379,7 @@ struct t_weechat_plugin int (*utf8_real_pos) (const char *string, int pos); int (*utf8_pos) (const char *string, int real_pos); char *(*utf8_strndup) (const char *string, int length); + void (*utf8_strncpy) (char *dest, const char *string, int length); /* crypto */ int (*crypto_hash) (const void *data, int data_size, @@ -1378,6 +1379,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); (weechat_plugin->utf8_pos)(__string, __real_pos) #define weechat_utf8_strndup(__string, __length) \ (weechat_plugin->utf8_strndup)(__string, __length) +#define weechat_utf8_strncpy(__dest, __string, __length) \ + (weechat_plugin->utf8_strncpy)(__dest, __string, __length) /* crypto */ #define weechat_crypto_hash(__data, __data_size, __hash_algo, \ |