compat.c
author nathan
Sun, 12 Dec 2010 11:31:54 +0100
branchtrunk
changeset 38 79b272a68eb4
parent 25 887faebaba0a
permissions -rw-r--r--
fix compile without OGG library
nathan@25
     1
/*
nathan@25
     2
 * MP3/MPlayer plugin to VDR (C++)
nathan@25
     3
 *
nathan@25
     4
 * (C) 2001-2009 Stefan Huelswitt <s.huelswitt@gmx.de>
nathan@25
     5
 *
nathan@25
     6
 * This code is free software; you can redistribute it and/or
nathan@25
     7
 * modify it under the terms of the GNU General Public License
nathan@25
     8
 * as published by the Free Software Foundation; either version 2
nathan@25
     9
 * of the License, or (at your option) any later version.
nathan@25
    10
 *
nathan@25
    11
 * This code is distributed in the hope that it will be useful,
nathan@25
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nathan@25
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nathan@25
    14
 * GNU General Public License for more details.
nathan@25
    15
 *
nathan@25
    16
 * You should have received a copy of the GNU General Public License
nathan@25
    17
 * along with this program; if not, write to the Free Software
nathan@25
    18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
nathan@25
    19
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
nathan@25
    20
 */
nathan@25
    21
nathan@25
    22
#include <stdlib.h>
nathan@25
    23
#include <string.h>
nathan@25
    24
nathan@25
    25
#if APIVERSNUM < 10503
nathan@25
    26
nathan@25
    27
#include <errno.h>
nathan@25
    28
#include <vdr/tools.h>
nathan@25
    29
#include "common.h"
nathan@25
    30
nathan@25
    31
// --- cCharSetConv ----------------------------------------------------------
nathan@25
    32
nathan@25
    33
char *cCharSetConv::systemCharacterTable = NULL;
nathan@25
    34
nathan@25
    35
cCharSetConv::cCharSetConv(const char *FromCode, const char *ToCode)
nathan@25
    36
{
nathan@25
    37
  if (!FromCode)
nathan@25
    38
     FromCode = systemCharacterTable;
nathan@25
    39
  if (!ToCode)
nathan@25
    40
     ToCode = "UTF-8";
nathan@25
    41
  cd = (FromCode && ToCode) ? iconv_open(ToCode, FromCode) : (iconv_t)-1;
nathan@25
    42
  result = NULL;
nathan@25
    43
  length = 0;
nathan@25
    44
}
nathan@25
    45
nathan@25
    46
cCharSetConv::~cCharSetConv()
nathan@25
    47
{
nathan@25
    48
  free(result);
nathan@25
    49
  iconv_close(cd);
nathan@25
    50
}
nathan@25
    51
nathan@25
    52
void cCharSetConv::SetSystemCharacterTableX(const char *CharacterTable)
nathan@25
    53
{
nathan@25
    54
  free(systemCharacterTable);
nathan@25
    55
  systemCharacterTable = NULL;
nathan@25
    56
  if (!strcasestr(CharacterTable, "UTF-8")) {
nathan@25
    57
     systemCharacterTable = strdup(CharacterTable);
nathan@25
    58
     }
nathan@25
    59
}
nathan@25
    60
nathan@25
    61
const char *cCharSetConv::Convert(const char *From, char *To, size_t ToLength)
nathan@25
    62
{
nathan@25
    63
  if (cd != (iconv_t)-1 && From && *From) {
nathan@25
    64
     char *FromPtr = (char *)From;
nathan@25
    65
     size_t FromLength = strlen(From);
nathan@25
    66
     char *ToPtr = To;
nathan@25
    67
     if (!ToPtr) {
nathan@25
    68
        length = max(length, FromLength * 2); // some reserve to avoid later reallocations
nathan@25
    69
        result = (char *)realloc(result, length);
nathan@25
    70
        ToPtr = result;
nathan@25
    71
        ToLength = length;
nathan@25
    72
        }
nathan@25
    73
     else if (!ToLength)
nathan@25
    74
        return From; // can't convert into a zero sized buffer
nathan@25
    75
     ToLength--; // save space for terminating 0
nathan@25
    76
     char *Converted = ToPtr;
nathan@25
    77
     while (FromLength > 0) {
nathan@25
    78
           if (iconv(cd, &FromPtr, &FromLength, &ToPtr, &ToLength) == size_t(-1)) {
nathan@25
    79
              if (errno == E2BIG || errno == EILSEQ && ToLength < 1) {
nathan@25
    80
                 if (To)
nathan@25
    81
                    break; // caller provided a fixed size buffer, but it was too small
nathan@25
    82
                 // The result buffer is too small, so increase it:
nathan@25
    83
                 size_t d = ToPtr - result;
nathan@25
    84
                 size_t r = length / 2;
nathan@25
    85
                 length += r;
nathan@25
    86
                 Converted = result = (char *)realloc(result, length);
nathan@25
    87
                 ToLength += r;
nathan@25
    88
                 ToPtr = result + d;
nathan@25
    89
                 }
nathan@25
    90
              if (errno == EILSEQ) {
nathan@25
    91
                 // A character can't be converted, so mark it with '?' and proceed:
nathan@25
    92
                 FromPtr++;
nathan@25
    93
                 FromLength--;
nathan@25
    94
                 *ToPtr++ = '?';
nathan@25
    95
                 ToLength--;
nathan@25
    96
                 }
nathan@25
    97
              else if (errno != E2BIG)
nathan@25
    98
                 return From; // unknown error, return original string
nathan@25
    99
              }
nathan@25
   100
           }
nathan@25
   101
     *ToPtr = 0;
nathan@25
   102
     return Converted;
nathan@25
   103
     }
nathan@25
   104
  return From;
nathan@25
   105
}
nathan@25
   106
nathan@25
   107
#endif