Changeset e27661d for util.c


Ignore:
Timestamp:
2006-03-31T17:55:47Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
7d31002
Parents:
d783e48
Message:

Finished the iconv() fix. Instead of doing it every time something goes from
or to the IM-modules, it's now just done with everything that goes between
BitlBee and the user. Incomparably more efficient/reliable. Plus some more
cleanups. It compiles, can't test it for real yet. ;-)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    rd783e48 re27661d  
    3939#include <glib.h>
    4040#include <time.h>
     41#include <iconv.h>
    4142
    4243void strip_linefeed(gchar *text)
     
    445446}
    446447#endif
     448
     449/* Convert from one charset to another.
     450   
     451   from_cs, to_cs: Source and destination charsets
     452   src, dst: Source and destination strings
     453   size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though.
     454   maxbuf: Maximum number of bytes to write to dst
     455   
     456   Returns the number of bytes written to maxbuf or -1 on an error.
     457*/
     458signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf )
     459{
     460        iconv_t cd;
     461        size_t res;
     462        size_t inbytesleft, outbytesleft;
     463        char *inbuf = src;
     464        char *outbuf = dst;
     465       
     466        cd = iconv_open( to_cs, from_cs );
     467        if( cd == (iconv_t) -1 )
     468                return( -1 );
     469       
     470        inbytesleft = size ? size : strlen( src );
     471        outbytesleft = maxbuf - 1;
     472        res = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
     473        *outbuf = '\0';
     474        iconv_close( cd );
     475       
     476        if( res == (size_t) -1 )
     477                return( -1 );
     478        else
     479                return( outbuf - dst );
     480}
     481
     482char *set_eval_charset( irc_t *irc, set_t *set, char *value )
     483{
     484        iconv_t cd;
     485
     486        if ( g_strncasecmp( value, "none", 4 ) == 0 )
     487                return( value );
     488
     489        cd = iconv_open( "UTF-8", value );
     490        if( cd == (iconv_t) -1 )
     491                return( NULL );
     492
     493        iconv_close( cd );
     494        return( value );
     495}
Note: See TracChangeset for help on using the changeset viewer.