Changeset 601e813 for util.c


Ignore:
Timestamp:
2006-05-24T23:04:18Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
80c1e4d
Parents:
46ad029 (diff), fc630f9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

[merge] Wilmer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r46ad029 r601e813  
    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.