1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 05:46:38 +02:00

Added UTF-8 functions to plugins API

This commit is contained in:
Sebastien Helleu
2007-12-10 18:07:20 +01:00
parent 256557f900
commit da95d22587
7 changed files with 331 additions and 46 deletions
+52 -2
View File
@@ -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>
@@ -160,6 +170,46 @@ struct t_irc_protocol_msg irc_protocol_messages[] =
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"
*/
@@ -167,7 +217,7 @@ char *irc_message = NULL;
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);
+199
View File
@@ -250,6 +250,205 @@ plugin_api_string_free_splitted_command (struct t_weechat_plugin *plugin,
string_free_splitted_command (splitted_command);
}
/*
* 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
*/
+16
View File
@@ -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);
+15
View File
@@ -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;
+48 -2
View File
@@ -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)