gchar *html_entities_encode_latin9( const gchar *pcIn )
{
guint u ;
gchar *pcName ;
gunichar *pwIn ;
GString *pString ;
if( !g_utf8_validate( pcIn, -1, NULL ) )
{
g_printerr( "%s() - Invalid UTF-8 : %s\n", __FUNCTION__, pcIn );
return NULL ;
}
pwIn = g_utf8_to_ucs4_fast( pcIn, -1, NULL );
if( pwIn == NULL )
{
g_printerr( "%s() - Unable to convert from UTF-8 to UCS4\n", __FUNCTION__ );
return NULL ;
}
pString = g_string_sized_new( 1024 );
while( *pwIn )
{
/* On élimine les caractères de contrôle et ceux non-assigné de Latin 9 : */
if(( *pwIn < (gunichar)0x20 )||(( *pwIn > (gunichar)0x7E )&&( *pwIn < (gunichar)0xA0 )))
{
pwIn++ ;
continue ;
}
/* On laisse les caractères Latin 9 restants tels quels : */
if( *pwIn < (gunichar) 256 )
{
g_string_append_c( pString, (gchar) *pwIn );
pwIn++ ;
continue ;
}
/* On convertit les caractères UTF-8 existants en leur équivalent Latin 9 :
*
* - 8364 (euro) en UTF-8 == 0xA4 en Latin 9
* - 352 (Scaron) en UTF-8 == 0xA6 en Latin 9
* - 353 (scaron) en UTF-8 == 0xA8 en Latin 9
* - 338 (OElig) en UTF-8 == 0xBC en Latin 9
* - 339 (oelig) en UTF-8 == 0xBD en Latin 9
* - 376 (Yuml) en UTF-8 == 0xBE en Latin 9
* - 381 (Zcaron) en UTF-8 == 0xB4 en Latin 9
* - 382 (zcaron) en UTF-8 == 0xB8 en Latin 9
*/
if( *pwIn == (gunichar) 8364 ) { g_string_append_c( pString, 0xA4 ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 352 ) { g_string_append_c( pString, 0xA6 ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 353 ) { g_string_append_c( pString, 0xA8 ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 338 ) { g_string_append_c( pString, 0xBC ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 339 ) { g_string_append_c( pString, 0xBD ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 376 ) { g_string_append_c( pString, 0xBE ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 381 ) { g_string_append_c( pString, 0xB4 ); pwIn++ ; continue ; }
if( *pwIn == (gunichar) 382 ) { g_string_append_c( pString, 0xB8 ); pwIn++ ; continue ; }
/* Si on arrive ici, c'est que le caractère n'est pas Latin 9 :
* on utilise d'abord son entité symbolique puis, le cas
* échéant, on utilise son entité numérique décimale. */
u = 0 ;
pcName = NULL ;
while( aHtmlEntities[u].uValue && aHtmlEntities[u].pcName )
{
if( ( (gunichar)aHtmlEntities[u].uValue ) == *pwIn )
{
pcName = aHtmlEntities[u].pcName ;
break ;
}
u++ ;
}
if( pcName )
{
g_string_append_printf( pString, "&%s;", pcName );
}
else
{
g_string_append_printf( pString, "%d;", (guint) *pwIn );
}
pwIn ++ ;
}
return g_string_free( pString, FALSE );
}