summaryrefslogtreecommitdiff
path: root/tools/iconv.h
blob: 872a7a8d613d16dd249b40c80648515a31c5b28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * iconv.h: iconv library wrapper
 *
 * See the main source file 'xineliboutput.c' for copyright information and
 * how to reach the author.
 *
 * $Id: iconv.h,v 1.1 2007-01-13 12:13:26 phintuka Exp $
 *
 */

#ifndef _XINELIBOUTPUT_ICONV_H_
#define _XINELIBOUTPUT_ICONV_H_

#if defined(USE_ICONV) && USE_ICONV == 0
#  undef USE_ICONV
#  warning iconv disabled
#endif

#ifdef USE_ICONV
#  include <iconv.h>
#endif

class cIConv
{
#ifdef USE_ICONV
  private:
    iconv_t  m_ic;
#endif

  public:
    cIConv(const char *SrcCharset = NULL, const char * DstCharset = NULL);
    virtual ~cIConv();

    cString Translate(const char *Text) const;
};

cIConv::cIConv(const char *SrcCharset, const char * DstCharset)
{
#ifdef USE_ICONV
  if(!SrcCharset)
    SrcCharset = "UTF-8";
  if(!DstCharset)
    DstCharset = I18nCharSets()[Setup.OSDLanguage];

  m_ic = iconv_open(DstCharset, SrcCharset);

  if(m_ic == (iconv_t)-1) 
    LOGERR("cIConv: iconv_open(\"%s\",\"%s\") failed",
	   SrcCharset, DstCharset);
# if 0
  else
    LOGDBG("cIConv: initialized conversion from \'%s\' to \'%s\'", 
	   SrcCharset, DstCharset);
# endif
#endif
}

cIConv::~cIConv()
{
#ifdef USE_ICONV
  if(m_ic != (iconv_t)-1) 
    iconv_close(m_ic);
#endif
}

cString cIConv::Translate(const char *Text) const
{
#ifdef USE_ICONV
  size_t  inc  = strlen(Text);
  size_t  outc = inc<2048 ? 2048 : inc+1;
  char   *in   = (char*)Text;
  char   *buf  = (char*)malloc(outc+1);
  char   *out  = buf;

  size_t n = iconv(m_ic, &in, &inc, &out, &outc);

  if(n != (size_t)-1) {
    *out = 0;
    return cString(buf, true);
  }

  LOGERR("cIConv: iconv(%s) failed", Text);
  free(buf);
#endif

  return cString(Text);
}


#endif // _XINELIBOUTPUT_ICONV_H_