2 * MP3/MPlayer plugin to VDR (C++)
4 * (C) 2001-2009 Stefan Huelswitt <s.huelswitt@gmx.de>
6 * This code is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This code is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
25 #if APIVERSNUM < 10503
28 #include <vdr/tools.h>
31 // --- cCharSetConv ----------------------------------------------------------
33 char *cCharSetConv::systemCharacterTable = NULL;
35 cCharSetConv::cCharSetConv(const char *FromCode, const char *ToCode)
38 FromCode = systemCharacterTable;
41 cd = (FromCode && ToCode) ? iconv_open(ToCode, FromCode) : (iconv_t)-1;
46 cCharSetConv::~cCharSetConv()
52 void cCharSetConv::SetSystemCharacterTableX(const char *CharacterTable)
54 free(systemCharacterTable);
55 systemCharacterTable = NULL;
56 if (!strcasestr(CharacterTable, "UTF-8")) {
57 systemCharacterTable = strdup(CharacterTable);
61 const char *cCharSetConv::Convert(const char *From, char *To, size_t ToLength)
63 if (cd != (iconv_t)-1 && From && *From) {
64 char *FromPtr = (char *)From;
65 size_t FromLength = strlen(From);
68 length = max(length, FromLength * 2); // some reserve to avoid later reallocations
69 result = (char *)realloc(result, length);
74 return From; // can't convert into a zero sized buffer
75 ToLength--; // save space for terminating 0
76 char *Converted = ToPtr;
77 while (FromLength > 0) {
78 if (iconv(cd, &FromPtr, &FromLength, &ToPtr, &ToLength) == size_t(-1)) {
79 if (errno == E2BIG || errno == EILSEQ && ToLength < 1) {
81 break; // caller provided a fixed size buffer, but it was too small
82 // The result buffer is too small, so increase it:
83 size_t d = ToPtr - result;
84 size_t r = length / 2;
86 Converted = result = (char *)realloc(result, length);
90 if (errno == EILSEQ) {
91 // A character can't be converted, so mark it with '?' and proceed:
97 else if (errno != E2BIG)
98 return From; // unknown error, return original string