source: roaraudio/libroarpulse/utf8.c @ 3478:2abe354cd9d4

Last change on this file since 3478:2abe354cd9d4 was 3478:2abe354cd9d4, checked in by phi, 10 years ago

added iconv support

File size: 3.2 KB
Line 
1//utf8.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from libpulse*. They are mostly copyrighted by:
7 *  Lennart Poettering <poettering@users.sourceforge.net> and
8 *  Pierre Ossman <drzeus@drzeus.cx>
9 *
10 *  This file is part of libroarpulse a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *  NOTE for everyone want's to change something and send patches:
28 *  read README and HACKING! There a addition information on
29 *  the license of this document you need to read before you send
30 *  any patches.
31 *
32 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
33 *  or libpulse*:
34 *  The libs libroaresd, libroararts and libroarpulse link this libroar
35 *  and are therefore GPL. Because of this it may be illigal to use
36 *  them with any software that uses libesd, libartsc or libpulse*.
37 */
38
39#include <libroarpulse/libroarpulse.h>
40#ifdef ROAR_HAVE_H_ICONV
41#include <iconv.h>
42#endif
43
44/** Test if the specified strings qualifies as valid UTF8. Return the string if so, otherwise NULL */
45const char *pa_utf8_valid(const char *str);
46
47/** Filter all invalid UTF8 characters from the specified string, returning a new fully UTF8 valid string. Don't forget to free the returned string with pa_xfree() */
48char *pa_utf8_filter(const char *str);
49
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
108/** Convert a UTF-8 string to the current locale. Free the string using pa_xfree(). */
109char* pa_utf8_to_locale (const char *str) {
110 return _roar_pa_iconv(str, "UTF-8", NULL);
111}
112
113/** Convert a string in the current locale to UTF-8. Free the string using pa_xfree(). */
114char* pa_locale_to_utf8 (const char *str) {
115 return _roar_pa_iconv(str, NULL, "UTF-8");
116}
117
118//ll
Note: See TracBrowser for help on using the repository browser.