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, 51 Franklin Street, Fifth Floor, |
---|
26 | * Boston, MA 02110-1301, USA. |
---|
27 | * |
---|
28 | * NOTE for everyone want's to change something and send patches: |
---|
29 | * read README and HACKING! There a addition information on |
---|
30 | * the license of this document you need to read before you send |
---|
31 | * any patches. |
---|
32 | * |
---|
33 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
34 | * or libpulse*: |
---|
35 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
36 | * and are therefore GPL. Because of this it may be illigal to use |
---|
37 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <libroarpulse/libroarpulse.h> |
---|
41 | #ifdef ROAR_HAVE_H_ICONV |
---|
42 | #include <iconv.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | /** Test if the specified strings qualifies as valid UTF8. Return the string if so, otherwise NULL */ |
---|
46 | ROAR_HAVE_TYPE_PA_UTF8_VALID pa_utf8_valid(const char *str); |
---|
47 | |
---|
48 | /** 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() */ |
---|
49 | char *pa_utf8_filter(const char *str); |
---|
50 | |
---|
51 | static char * _roar_pa_iconv(const char * str, const char * from, const char * to) { |
---|
52 | #ifdef ROAR_HAVE_H_ICONV |
---|
53 | iconv_t cd; |
---|
54 | char * out; |
---|
55 | char * ip, * op; |
---|
56 | size_t il, ol; |
---|
57 | size_t inlen; |
---|
58 | size_t outlen; |
---|
59 | size_t ret; |
---|
60 | |
---|
61 | if ( str == NULL ) |
---|
62 | return NULL; |
---|
63 | |
---|
64 | if ( from == NULL ) |
---|
65 | from = ""; |
---|
66 | |
---|
67 | if ( to == NULL ) |
---|
68 | to = ""; |
---|
69 | |
---|
70 | inlen = strlen(str); |
---|
71 | |
---|
72 | outlen = inlen * 1.2; |
---|
73 | |
---|
74 | if ( (out = pa_xmalloc(outlen)) == NULL ) |
---|
75 | return NULL; |
---|
76 | |
---|
77 | if ( (cd = iconv_open(from, to)) == (iconv_t)(-1) ) |
---|
78 | return NULL; |
---|
79 | |
---|
80 | while (1) { |
---|
81 | ip = (char*) str; |
---|
82 | op = out; |
---|
83 | il = inlen; |
---|
84 | ol = outlen; |
---|
85 | |
---|
86 | ret = iconv(cd, &ip, &il, &op, &ol); |
---|
87 | |
---|
88 | if ( ret != (size_t)-1 ) |
---|
89 | break; |
---|
90 | |
---|
91 | if ( errno != E2BIG ) { |
---|
92 | pa_xfree(out); |
---|
93 | out = NULL; |
---|
94 | break; |
---|
95 | } |
---|
96 | |
---|
97 | outlen += il * 1.2; |
---|
98 | out = pa_xrealloc(out, outlen); |
---|
99 | } |
---|
100 | |
---|
101 | iconv_close(cd); |
---|
102 | |
---|
103 | return out; |
---|
104 | #else |
---|
105 | return NULL; |
---|
106 | #endif |
---|
107 | } |
---|
108 | |
---|
109 | /** Convert a UTF-8 string to the current locale. Free the string using pa_xfree(). */ |
---|
110 | char* pa_utf8_to_locale (const char *str) { |
---|
111 | return _roar_pa_iconv(str, "UTF-8", NULL); |
---|
112 | } |
---|
113 | |
---|
114 | /** Convert a string in the current locale to UTF-8. Free the string using pa_xfree(). */ |
---|
115 | char* pa_locale_to_utf8 (const char *str) { |
---|
116 | return _roar_pa_iconv(str, NULL, "UTF-8"); |
---|
117 | } |
---|
118 | |
---|
119 | //ll |
---|