diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -38,6 +38,7 @@ #include <ctype.h> #include <glib.h> #include <time.h> +#include <iconv.h> void strip_linefeed(gchar *text) { @@ -444,3 +445,51 @@ char *ipv6_unwrap( char *src ) return ( src + 7 ); } #endif + +/* Convert from one charset to another. + + from_cs, to_cs: Source and destination charsets + src, dst: Source and destination strings + size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. + maxbuf: Maximum number of bytes to write to dst + + Returns the number of bytes written to maxbuf or -1 on an error. +*/ +signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ) +{ + iconv_t cd; + size_t res; + size_t inbytesleft, outbytesleft; + char *inbuf = src; + char *outbuf = dst; + + cd = iconv_open( to_cs, from_cs ); + if( cd == (iconv_t) -1 ) + return( -1 ); + + inbytesleft = size ? size : strlen( src ); + outbytesleft = maxbuf - 1; + res = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); + *outbuf = '\0'; + iconv_close( cd ); + + if( res == (size_t) -1 ) + return( -1 ); + else + return( outbuf - dst ); +} + +char *set_eval_charset( irc_t *irc, set_t *set, char *value ) +{ + iconv_t cd; + + if ( g_strncasecmp( value, "none", 4 ) == 0 ) + return( value ); + + cd = iconv_open( "UTF-8", value ); + if( cd == (iconv_t) -1 ) + return( NULL ); + + iconv_close( cd ); + return( value ); +} |