Changeset 3478:2abe354cd9d4 in roaraudio


Ignore:
Timestamp:
02/14/10 04:38:07 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

added iconv support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroarpulse/utf8.c

    r3476 r3478  
    3838 
    3939#include <libroarpulse/libroarpulse.h> 
     40#ifdef ROAR_HAVE_H_ICONV 
     41#include <iconv.h> 
     42#endif 
    4043 
    4144/** Test if the specified strings qualifies as valid UTF8. Return the string if so, otherwise NULL */ 
     
    4548char *pa_utf8_filter(const char *str); 
    4649 
     50static char * _roar_pa_iconv(const char * str, const char * from, const char * to) { 
     51#ifdef ROAR_HAVE_H_ICONV 
     52 iconv_t cd; 
     53 char   * out; 
     54 char   * ip, * op; 
     55 size_t   il,   ol; 
     56 size_t inlen; 
     57 size_t outlen; 
     58 size_t ret; 
     59 
     60 if ( str == NULL ) 
     61  return NULL; 
     62 
     63 if ( from == NULL ) 
     64  from = ""; 
     65 
     66 if ( to == NULL ) 
     67  to = ""; 
     68 
     69 inlen = strlen(str); 
     70 
     71 outlen = inlen * 1.2; 
     72 
     73 if ( (out = pa_xmalloc(outlen)) == NULL ) 
     74  return NULL; 
     75 
     76 if ( (cd = iconv_open(from, to)) == (iconv_t)(-1) ) 
     77  return NULL; 
     78 
     79 while (1) { 
     80  ip = (char*) str; 
     81  op = out; 
     82  il = inlen; 
     83  ol = outlen; 
     84 
     85  ret = iconv(cd, &ip, &il, &op, &ol); 
     86 
     87  if ( ret != (size_t)-1 ) 
     88   break; 
     89 
     90  if ( errno != E2BIG ) { 
     91   pa_xfree(out); 
     92   out = NULL; 
     93   break; 
     94  } 
     95 
     96  outlen += il * 1.2; 
     97  out = pa_xrealloc(out, outlen); 
     98 } 
     99 
     100 iconv_close(cd); 
     101 
     102 return out; 
     103#else 
     104 return NULL; 
     105#endif 
     106} 
     107 
    47108/** Convert a UTF-8 string to the current locale. Free the string using pa_xfree(). */ 
    48 char* pa_utf8_to_locale (const char *str); 
     109char* pa_utf8_to_locale (const char *str) { 
     110 return _roar_pa_iconv(str, "UTF-8", NULL); 
     111} 
    49112 
    50113/** Convert a string in the current locale to UTF-8. Free the string using pa_xfree(). */ 
    51 char* pa_locale_to_utf8 (const char *str); 
     114char* pa_locale_to_utf8 (const char *str) { 
     115 return _roar_pa_iconv(str, NULL, "UTF-8"); 
     116} 
    52117 
    53118//ll 
Note: See TracChangeset for help on using the changeset viewer.