Thread: Need help with iconv() usage

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    860

    Need help with iconv() usage

    I keep getting errors popup whenever my source input is not UTF-8 so I'm clearly using it wrong somehow, this is how I've been using it so far:
    Code:
    	U8.used = utflen(u8) + 1;
    	if ( (ret = vecsize( &CB, U8.size * 4 )) != EXIT_SUCCESS )
    		goto literalc_cleanup;
    	cb = CB.buff;
    	iconvSrcBuff = U8.buff;
    	iconvSrcSize = U8.used * sizeof(char8_t);
    	iconvDstBuff = CB.buff;
    	iconvDstSize = CB.size;
    	if ( encoding != std_encoding_utf ) {
    		while ( iconvSrcSize && iconvDstSize ) {
    			bytes = iconv( utf_to_iconv[encoding],
    				&iconvSrcBuff, &iconvSrcSize,
    				&iconvDstBuff, &iconvDstSize );
    			if ( !bytes ) break;
    			if ( errno != EXIT_SUCCESS ) break;
    		}
    		if ( errno != EXIT_SUCCESS ) {
    // Should not reach here in unit tests but does
    			ret = errno;
    			errno = 0;
    			FAIL( stderr, ret, "" );
    		}
    	}
    	else (void)utfcpy( cb, U8.used, u8, U8.used );
    And this is the section of unit tests that it fails at:
    Code:
    ...
    u8"abcd\u2ea2wxyz" =  64636261, 'abcd\u2ea2wxyz'
    u'cd' =  00FFFE63, '
    u'\u2ea2' =  0000A22E, '
    u'a\u2ea2' =  2E6100A2, '
    u'\u2ea2a' =  00A22E61, '
    u"\u2ea2" =  00002EA2, '
    Last character was '        '
    Character hex: 00 00 00 00 00 00
    rm libtsc.so libbase62.so char.elf libnext.so
    char.c:692:literalc(): Error: 0x00000054, 84, Invalid or incomplete multibyte or wide character, Info:
    char.c:692:literalc(): Error: 0x00000054, 84, Invalid or incomplete multibyte or wide character, Info:
    make: *** [makefile:36: char.run] Error 1
    Compilation failed.
    Edit 1: Just thought to clear errno before starting the loop so something else must be setting it instead, or it's setting it elsewhere, give me a few minutes
    Edit 2: Turned out printf was setting it, despite supporting %ls it doesn't actually support doing it, resolved it with a switch statement which was need anyway
    Code:
    switch ( encoding ) {
    	case std_encoding_wcs:
    		for ( i = 0; (c = ((wchar_t*)cb)[i]); ++i )
    			putwchar( c );
    		putchar('\'');
    		break;
    	case std_encoding_u16:
    		for ( i = 0; (c = ((char16_t*)cb)[i]); ++i )
    			putwchar( c );
    		putchar('\'');
    		break;
    	case std_encoding_u32:
    		for ( i = 0; (c = ((char32_t*)cb)[i]); ++i )
    			putwchar( c );
    		putchar('\'');
    		break;
    	default:
    		printf( "%s'", cb );
    	}
    Last edited by awsdert; 10-14-2019 at 07:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault via iconv
    By awsdert in forum C Programming
    Replies: 7
    Last Post: 10-11-2019, 09:31 PM
  2. Using iconv/iconv_open
    By awsdert in forum C Programming
    Replies: 0
    Last Post: 09-16-2019, 04:16 PM
  3. C++ using iconv
    By Chris87 in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2018, 10:11 PM
  4. iconv.dll
    By d1987 in forum Windows Programming
    Replies: 0
    Last Post: 08-16-2010, 09:28 AM
  5. Usage
    By whistlenm1 in forum C++ Programming
    Replies: 9
    Last Post: 07-10-2002, 11:06 AM

Tags for this Thread