Beispiel #1
0
/**
 * Convert a utf16 string to utf8
 * @param str the source utf8 string
 * @param u_len the length of utf16 str to process; set to utf8 len on exit
 * @return a utf8 string the caller must free
 */
char *utf16toutf8Len( UChar *u_str, int *u_len )
{
    int c_len = measure_to_encoding( u_str, *u_len, "UTF-8" );
    char *c_str = calloc(c_len+1,sizeof(char));
    if ( c_str != NULL )
    {
        int res = convert_to_encoding( u_str, *u_len, c_str, c_len+1, "UTF-8" );
        if ( res != c_len )
        {
            *u_len = 0;
            return NULL;
        }
    }
    *u_len = c_len;
    return c_str;
}
Beispiel #2
0
static char *print_utf8( UChar *ustr, int src_len )
{
    int dst_len = measure_to_encoding( ustr, src_len, "utf-8" );
    char *buf = malloc( dst_len+1 );
    if ( buf != NULL )
    {
        int n_bytes = convert_to_encoding( ustr, src_len, buf, 
            dst_len+1, "utf-8" );
        if ( n_bytes == dst_len )
        {
            buf[dst_len] = 0;
            printf("%s",buf);
        }
        free( buf );
    }
}
Beispiel #3
0
void test_encoding( int *passed, int *failed )
{
    size_t srclen;
    char *charset="utf-8";
    char *src = read_file( "tests/can_1316_01.txt", &srclen );
    if ( srclen > 0 )
    {
        size_t dstlen = measure_from_encoding( src, srclen, "utf-8" );
        if ( dstlen > 0 )
        {
            UChar *dst = (UChar*)calloc( dstlen+1, sizeof(UChar) );
            if ( dst != NULL )
            {
                int res = convert_from_encoding( src, srclen, 
                    dst, dstlen+1, charset );
                if ( res )
                {
                    (*passed)++;
                    size_t dst2len = measure_to_encoding( dst, dstlen, "utf-8" );
                    char *dst2 = calloc( dst2len+1, sizeof(char) );
                    if ( dst2 != NULL )
                    {
                        int res = convert_to_encoding( dst, dstlen, 
                            dst2, dst2len+1, "utf-8" );
                        if ( res ) 
                            (*passed)++;
                        else
                            (*failed)++;
                        if ( !data_is_same(dst2,dst2len,src,srclen) )
                            (*failed)++;
                        else
                            (*passed)++;
                        free( dst2 );
                    }
                }
                else
                {
                    printf("conversion failed\n");
                    (*failed)++;
                }
                free( dst );
            }
        }
        free( src );
    }
}
Beispiel #4
0
/**
 * Convert a UTF-16 string to UTF-8
 * @param ustr the original string
 * @param ulen the length in characters
 * @return an allocated utf8-string the user must free
 */
static char *to_utf8( UChar *ustr, int ulen )
{
    char *dst;
    if ( ulen == 0 )
    {
        dst = calloc( 1, sizeof(char) );
    }
    else
    {
        int len = measure_to_encoding( ustr, ulen, "utf-8" );
        dst = calloc( len+1, sizeof(char) );
        if ( dst != NULL )
        {
            convert_to_encoding( ustr, ulen, dst, len+1, "utf-8" );
        }
    }
    return dst;
}
Beispiel #5
0
/**
 * Convert a card to a utf-8 string
 * @param c the card to stringify
 * @return an allocated string - please dispose after use!
 */
char *card_tostring( card *c )
{
    char *str = NULL;
    char vbs[32];
    char *udata;
    int dlen = 0;
    pair *p = card_pair(c);
    bitset *bs = pair_versions(p);
    UChar *data = pair_data( p );
    int highest = bitset_top_bit( bs );
    bitset_tostring( bs, vbs, highest+2 );
    if ( pair_len(p) > 0 )
    {
        dlen = measure_to_encoding( data, pair_len(p), "utf-8" );
        udata = calloc( dlen+1, 1 );
        if ( udata != NULL )
            convert_to_encoding( data, pair_len(p), udata, dlen+1, "utf-8" );
    }
    else
        udata = calloc( 1, sizeof(UChar) );
    if ( udata != NULL )
    {
        char tid[16];
        if ( pair_is_parent(p) )
            snprintf(tid,16,"-p:%d",pair_id(p));
        else if ( pair_is_child(p) )
            snprintf(tid,16,"-c:%d",pair_id(p));
        else
            tid[0] = 0;
        int tlen = strlen(vbs)+dlen+strlen(tid)+3;
        str = calloc( tlen, 1 );
        if ( str != NULL )
            snprintf( str, tlen, "[%s%s]%s", vbs, tid, udata );
        free( udata );
    }
    return str;
}