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
     1 /*
     2  * MP3/MPlayer plugin to VDR (C++)
     3  *
     4  * (C) 2001-2009 Stefan Huelswitt <s.huelswitt@gmx.de>
     5  *
     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.
    10  *
    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.
    15  *
    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
    20  */
    21 
    22 #include <stdlib.h>
    23 #include <string.h>
    24 
    25 #if APIVERSNUM < 10503
    26 
    27 #include <errno.h>
    28 #include <vdr/tools.h>
    29 #include "common.h"
    30 
    31 // --- cCharSetConv ----------------------------------------------------------
    32 
    33 char *cCharSetConv::systemCharacterTable = NULL;
    34 
    35 cCharSetConv::cCharSetConv(const char *FromCode, const char *ToCode)
    36 {
    37   if (!FromCode)
    38      FromCode = systemCharacterTable;
    39   if (!ToCode)
    40      ToCode = "UTF-8";
    41   cd = (FromCode && ToCode) ? iconv_open(ToCode, FromCode) : (iconv_t)-1;
    42   result = NULL;
    43   length = 0;
    44 }
    45 
    46 cCharSetConv::~cCharSetConv()
    47 {
    48   free(result);
    49   iconv_close(cd);
    50 }
    51 
    52 void cCharSetConv::SetSystemCharacterTableX(const char *CharacterTable)
    53 {
    54   free(systemCharacterTable);
    55   systemCharacterTable = NULL;
    56   if (!strcasestr(CharacterTable, "UTF-8")) {
    57      systemCharacterTable = strdup(CharacterTable);
    58      }
    59 }
    60 
    61 const char *cCharSetConv::Convert(const char *From, char *To, size_t ToLength)
    62 {
    63   if (cd != (iconv_t)-1 && From && *From) {
    64      char *FromPtr = (char *)From;
    65      size_t FromLength = strlen(From);
    66      char *ToPtr = To;
    67      if (!ToPtr) {
    68         length = max(length, FromLength * 2); // some reserve to avoid later reallocations
    69         result = (char *)realloc(result, length);
    70         ToPtr = result;
    71         ToLength = length;
    72         }
    73      else if (!ToLength)
    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) {
    80                  if (To)
    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;
    85                  length += r;
    86                  Converted = result = (char *)realloc(result, length);
    87                  ToLength += r;
    88                  ToPtr = result + d;
    89                  }
    90               if (errno == EILSEQ) {
    91                  // A character can't be converted, so mark it with '?' and proceed:
    92                  FromPtr++;
    93                  FromLength--;
    94                  *ToPtr++ = '?';
    95                  ToLength--;
    96                  }
    97               else if (errno != E2BIG)
    98                  return From; // unknown error, return original string
    99               }
   100            }
   101      *ToPtr = 0;
   102      return Converted;
   103      }
   104   return From;
   105 }
   106 
   107 #endif