diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2007-12-10 18:07:20 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2007-12-10 18:07:20 +0100 |
commit | da95d22587b4d1b117625b8a69e07db27ca06dce (patch) | |
tree | d72902c98439f3050eca5277159947094564575d | |
parent | 256557f900d82149b2ada5619bd52b5e4e3fc78b (diff) | |
download | weechat-da95d22587b4d1b117625b8a69e07db27ca06dce.zip |
Added UTF-8 functions to plugins API
-rw-r--r-- | src/core/wee-utf8.c | 42 | ||||
-rw-r--r-- | src/core/wee-utf8.h | 1 | ||||
-rw-r--r-- | src/plugins/irc/irc-protocol.c | 54 | ||||
-rw-r--r-- | src/plugins/plugin-api.c | 199 | ||||
-rw-r--r-- | src/plugins/plugin-api.h | 16 | ||||
-rw-r--r-- | src/plugins/plugin.c | 15 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 50 |
7 files changed, 331 insertions, 46 deletions
diff --git a/src/core/wee-utf8.c b/src/core/wee-utf8.c index 085e6e6dc..9e52dd6b3 100644 --- a/src/core/wee-utf8.c +++ b/src/core/wee-utf8.c @@ -226,7 +226,7 @@ utf8_next_char (char *string) } /* - * utf8_char_size: return UTF-8 char size + * utf8_char_size: return UTF-8 char size (in bytes) */ int @@ -455,43 +455,3 @@ utf8_pos (char *string, int real_pos) } return count; } - -/* - * utf8_get_wide_char: get wide char from string (first char) - */ - -wint_t -utf8_get_wide_char (char *string) -{ - int char_size; - wint_t result; - - if (!string || !string[0]) - return WEOF; - - char_size = utf8_char_size (string); - switch (char_size) - { - case 1: - result = (wint_t)string[0]; - break; - case 2: - result = ((wint_t)((unsigned char)string[0])) << 8 - | ((wint_t)((unsigned char)string[1])); - break; - case 3: - result = ((wint_t)((unsigned char)string[0])) << 16 - | ((wint_t)((unsigned char)string[1])) << 8 - | ((wint_t)((unsigned char)string[2])); - break; - case 4: - result = ((wint_t)((unsigned char)string[0])) << 24 - | ((wint_t)((unsigned char)string[1])) << 16 - | ((wint_t)((unsigned char)string[2])) << 8 - | ((wint_t)((unsigned char)string[3])); - break; - default: - result = WEOF; - } - return result; -} diff --git a/src/core/wee-utf8.h b/src/core/wee-utf8.h index b66996864..f16733535 100644 --- a/src/core/wee-utf8.h +++ b/src/core/wee-utf8.h @@ -47,6 +47,5 @@ extern int utf8_char_size_screen (char *); extern char *utf8_add_offset (char *, int); extern int utf8_real_pos (char *, int); extern int utf8_pos (char *, int); -extern wint_t utf8_get_wide_char (char *); #endif /* wee-utf8.h */ diff --git a/src/plugins/irc/irc-protocol.c b/src/plugins/irc/irc-protocol.c index a9efb3245..f69a6c0b1 100644 --- a/src/plugins/irc/irc-protocol.c +++ b/src/plugins/irc/irc-protocol.c @@ -24,6 +24,16 @@ #include "config.h" #endif +#ifndef __USE_XOPEN +#define __USE_XOPEN +#endif + +#if defined(__OpenBSD__) +#include <utf8/wchar.h> +#else +#include <wchar.h> +#endif + #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -161,13 +171,53 @@ char *irc_message = NULL; /* + * irc_protocol_get_wide_char: get wide char from string (first char) + */ + +wint_t +irc_protocol_get_wide_char (char *string) +{ + int char_size; + wint_t result; + + if (!string || !string[0]) + return WEOF; + + char_size = weechat_utf8_char_size (string); + switch (char_size) + { + case 1: + result = (wint_t)string[0]; + break; + case 2: + result = ((wint_t)((unsigned char)string[0])) << 8 + | ((wint_t)((unsigned char)string[1])); + break; + case 3: + result = ((wint_t)((unsigned char)string[0])) << 16 + | ((wint_t)((unsigned char)string[1])) << 8 + | ((wint_t)((unsigned char)string[2])); + break; + case 4: + result = ((wint_t)((unsigned char)string[0])) << 24 + | ((wint_t)((unsigned char)string[1])) << 16 + | ((wint_t)((unsigned char)string[2])) << 8 + | ((wint_t)((unsigned char)string[3])); + break; + default: + result = WEOF; + } + return result; +} + +/* * irc_protocol_is_word_char: return 1 if given character is a "word character" */ int irc_protocol_is_word_char (char *str) { - wint_t c = utf8_get_wide_char (str); + wint_t c = irc_protocol_get_wide_char (str); if (c == WEOF) return 0; @@ -222,7 +272,7 @@ irc_protocol_is_highlight (char *message, char *nick) match = strstr (message, nick); if (match) { - match_pre = utf8_prev_char (message, match); + match_pre = weechat_utf8_prev_char (message, match); if (!match_pre) match_pre = match - 1; match_post = match + strlen(nick); diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c index 5b14f0efe..85d87b952 100644 --- a/src/plugins/plugin-api.c +++ b/src/plugins/plugin-api.c @@ -251,6 +251,205 @@ plugin_api_string_free_splitted_command (struct t_weechat_plugin *plugin, } /* + * plugin_api_utf8_has_8bits: return 1 if string has 8-bits chars, 0 if only + * 7-bits chars + */ + +int +plugin_api_utf8_has_8bits (struct t_weechat_plugin *plugin, char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_has_8bits (string); +} + +/* + * plugin_api_utf8_is_valid: return 1 if UTF-8 string is valid, 0 otherwise + * if error is not NULL, it's set with first non + * valid UTF-8 char in string, if any + */ + +int +plugin_api_utf8_is_valid (struct t_weechat_plugin *plugin, char *string, + char **error) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_is_valid (string, error); +} + +/* + * plugin_api_utf8_normalize: normalize UTF-8 string: remove non UTF-8 chars + * and replace them by a char + */ + +void +plugin_api_utf8_normalize (struct t_weechat_plugin *plugin, char *string, + char replacement) +{ + /* make C compiler happy */ + (void) plugin; + + utf8_normalize (string, replacement); +} + +/* + * plugin_api_utf8_prev_char: return previous UTF-8 char in a string + */ + +char * +plugin_api_utf8_prev_char (struct t_weechat_plugin *plugin, char *string_start, + char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_prev_char (string_start, string); +} + +/* + * plugin_api_utf8_next_char: return next UTF-8 char in a string + */ + +char * +plugin_api_utf8_next_char (struct t_weechat_plugin *plugin, char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_next_char (string); +} + +/* + * plugin_api_utf8_char_size: return UTF-8 char size (in bytes) + */ + +int +plugin_api_utf8_char_size (struct t_weechat_plugin *plugin, char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_char_size (string); +} + +/* + * plugin_api_utf8_strlen: return length of an UTF-8 string (<= strlen(string)) + */ + +int +plugin_api_utf8_strlen (struct t_weechat_plugin *plugin, char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_strlen (string); +} + +/* + * plugin_api_utf8_strnlen: return length of an UTF-8 string, for N bytes max + * in string + */ + +int +plugin_api_utf8_strnlen (struct t_weechat_plugin *plugin, char *string, + int bytes) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_strnlen (string, bytes); +} + +/* + * plugin_api_utf8_strlen_screen: return number of chars needed on screen to + * display UTF-8 string + */ + +int +plugin_api_utf8_strlen_screen (struct t_weechat_plugin *plugin, char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_strlen_screen (string); +} + +/* + * plugin_api_utf8_charcasecmp: compare two utf8 chars (case is ignored) + */ + +int +plugin_api_utf8_charcasecmp (struct t_weechat_plugin *plugin, char *string1, + char *string2) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_charcasecmp (string1, string2); +} + +/* + * plugin_api_utf8_char_size_screen: return number of chars needed on screen + * to display UTF-8 char + */ + +int +plugin_api_utf8_char_size_screen (struct t_weechat_plugin *plugin, char *string) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_char_size_screen (string); +} + +/* + * plugin_api_utf8_add_offset: moves forward N chars in an UTF-8 string + */ + +char * +plugin_api_utf8_add_offset (struct t_weechat_plugin *plugin, char *string, + int offset) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_add_offset (string, offset); +} + +/* + * plugin_api_utf8_real_pos: get real position in UTF-8 + * for example: ("aébc", 2) returns 3 + */ + +int +plugin_api_utf8_real_pos (struct t_weechat_plugin *plugin, char *string, + int pos) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_real_pos (string, pos); +} + +/* + * plugin_api_utf8_pos: get position in UTF-8 + * for example: ("aébc", 3) returns 2 + */ + +int +plugin_api_utf8_pos (struct t_weechat_plugin *plugin, char *string, + int real_pos) +{ + /* make C compiler happy */ + (void) plugin; + + return utf8_real_pos (string, real_pos); +} + +/* * plugin_api_mkdir_home: create a directory in WeeChat home */ diff --git a/src/plugins/plugin-api.h b/src/plugins/plugin-api.h index 2f7505e55..1f7a227eb 100644 --- a/src/plugins/plugin-api.h +++ b/src/plugins/plugin-api.h @@ -44,6 +44,22 @@ extern char **plugin_api_string_split_command (struct t_weechat_plugin *, extern void plugin_api_string_free_splitted_command (struct t_weechat_plugin *, char **); +/* UTF-8 strings */ +extern int plugin_api_utf8_has_8bits (struct t_weechat_plugin *, char *); +extern int plugin_api_utf8_is_valid (struct t_weechat_plugin *, char *, char **); +extern void plugin_api_utf8_normalize (struct t_weechat_plugin *, char *, char); +extern char *plugin_api_utf8_prev_char (struct t_weechat_plugin *, char *, char *); +extern char *plugin_api_utf8_next_char (struct t_weechat_plugin *, char *); +extern int plugin_api_utf8_char_size (struct t_weechat_plugin *, char *); +extern int plugin_api_utf8_strlen (struct t_weechat_plugin *, char *); +extern int plugin_api_utf8_strnlen (struct t_weechat_plugin *, char *, int); +extern int plugin_api_utf8_strlen_screen (struct t_weechat_plugin *, char *); +extern int plugin_api_utf8_charcasecmp (struct t_weechat_plugin *, char *, char *); +extern int plugin_api_utf8_char_size_screen (struct t_weechat_plugin *, char *); +extern char *plugin_api_utf8_add_offset (struct t_weechat_plugin *, char *, int); +extern int plugin_api_utf8_real_pos (struct t_weechat_plugin *, char *, int); +extern int plugin_api_utf8_pos (struct t_weechat_plugin *, char *, int); + /* directories */ extern int plugin_api_mkdir_home (struct t_weechat_plugin *, char *, int); extern int plugin_api_mkdir (struct t_weechat_plugin *, char *, int); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 034d18e78..b085195e5 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -236,6 +236,21 @@ plugin_load (char *filename) new_plugin->string_free_exploded = &plugin_api_string_free_exploded; new_plugin->string_split_command = &plugin_api_string_split_command; new_plugin->string_free_splitted_command = &plugin_api_string_free_splitted_command; + + new_plugin->utf8_has_8bits = &plugin_api_utf8_has_8bits; + new_plugin->utf8_is_valid = &plugin_api_utf8_is_valid; + new_plugin->utf8_normalize = &plugin_api_utf8_normalize; + new_plugin->utf8_prev_char = &plugin_api_utf8_prev_char; + new_plugin->utf8_next_char = &plugin_api_utf8_next_char; + new_plugin->utf8_char_size = &plugin_api_utf8_char_size; + new_plugin->utf8_strlen = &plugin_api_utf8_strlen; + new_plugin->utf8_strnlen = &plugin_api_utf8_strnlen; + new_plugin->utf8_strlen_screen = &plugin_api_utf8_strlen_screen; + new_plugin->utf8_charcasecmp = &plugin_api_utf8_charcasecmp; + new_plugin->utf8_char_size_screen = &plugin_api_utf8_char_size_screen; + new_plugin->utf8_add_offset = &plugin_api_utf8_add_offset; + new_plugin->utf8_real_pos = &plugin_api_utf8_real_pos; + new_plugin->utf8_pos = &plugin_api_utf8_pos; new_plugin->mkdir_home = &plugin_api_mkdir_home; new_plugin->mkdir = &plugin_api_mkdir; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index bed73a4f6..ae43acfde 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -71,15 +71,31 @@ struct t_weechat_plugin char **(*string_split_command) (struct t_weechat_plugin *, char *, char); void (*string_free_splitted_command) (struct t_weechat_plugin *, char **); + /* UTF-8 strings */ + int (*utf8_has_8bits) (struct t_weechat_plugin *, char *); + int (*utf8_is_valid) (struct t_weechat_plugin *, char *, char **); + void (*utf8_normalize) (struct t_weechat_plugin *, char *, char); + char *(*utf8_prev_char) (struct t_weechat_plugin *, char *, char *); + char *(*utf8_next_char) (struct t_weechat_plugin *, char *); + int (*utf8_char_size) (struct t_weechat_plugin *, char *); + int (*utf8_strlen) (struct t_weechat_plugin *, char *); + int (*utf8_strnlen) (struct t_weechat_plugin *, char *, int); + int (*utf8_strlen_screen) (struct t_weechat_plugin *, char *); + int (*utf8_charcasecmp) (struct t_weechat_plugin *, char *, char *); + int (*utf8_char_size_screen) (struct t_weechat_plugin *, char *); + char *(*utf8_add_offset) (struct t_weechat_plugin *, char *, int); + int (*utf8_real_pos) (struct t_weechat_plugin *, char *, int); + int (*utf8_pos) (struct t_weechat_plugin *, char *, int); + /* directories */ int (*mkdir_home) (struct t_weechat_plugin *, char *, int); int (*mkdir) (struct t_weechat_plugin *, char *, int); void (*exec_on_files) (struct t_weechat_plugin *, char *, int (*)(char *)); - + /* util */ long (*timeval_diff) (struct t_weechat_plugin *, void *, void *); - + /* sorted list */ struct t_weelist *(*list_new) (struct t_weechat_plugin *); char *(*list_add) (struct t_weechat_plugin *, void *, char *, @@ -240,6 +256,36 @@ struct t_weechat_plugin weechat_plugin->string_free_splitted_command(weechat_plugin, \ __array_str) +/* UTF-8 strings */ +#define weechat_utf8_has_8bits(__string) \ + weechat_plugin->utf8_has_8bits(weechat_plugin, __string) +#define weechat_utf8_is_valid(__string, __error) \ + weechat_plugin->utf8_is_valid(weechat_plugin, __string, __error) +#define weechat_utf8_normalise(__string, __char) \ + weechat_plugin->utf8_normalize(weechat_plugin, __string, __char) +#define weechat_utf8_prev_char(__start, __string) \ + weechat_plugin->utf8_has_8bits(weechat_plugin, __start, __string) +#define weechat_utf8_next_char(__string) \ + weechat_plugin->utf8_next_char(weechat_plugin, __string) +#define weechat_utf8_char_size(__string) \ + weechat_plugin->utf8_char_size(weechat_plugin, __string) +#define weechat_utf8_strlen(__string) \ + weechat_plugin->utf8_strlen(weechat_plugin, __string) +#define weechat_utf8_strnlen(__string, __bytes) \ + weechat_plugin->utf8_strnlen(weechat_plugin, __string, __bytes) +#define weechat_utf8_strlen_screen(__string) \ + weechat_plugin->utf8_strlen_screen(weechat_plugin, __string) +#define weechat_utf8_charcasecmp(__string) \ + weechat_plugin->utf8_charcasecmp(weechat_plugin, __string) +#define weechat_utf8_char_size_screen(__string) \ + weechat_plugin->utf8_char_size_screen(weechat_plugin, __string) +#define weechat_utf8_add_offset(__string, __offset) \ + weechat_plugin->utf8_add_offset(weechat_plugin, __string, __offset) +#define weechat_utf8_real_pos(__string, __pos) \ + weechat_plugin->utf8_real_pos(weechat_plugin, __string, __pos) +#define weechat_utf8_pos(__string, __real_pos) \ + weechat_plugin->utf8_pos(weechat_plugin, __string, __real_pos) + /* directories */ #define weechat_mkdir_home(__directory, __mode) \ weechat_plugin->mkdir_home(weechat_plugin, __directory, __mode) |