File: utf8util.cpp

package info (click to toggle)
libtunepimp 0.5.3-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,668 kB
  • ctags: 3,470
  • sloc: ansic: 15,652; cpp: 12,752; sh: 11,929; perl: 1,179; python: 720; makefile: 310; pascal: 33
file content (131 lines) | stat: -rw-r--r-- 3,277 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*----------------------------------------------------------------------------

   libtunepimp -- The MusicBrainz tagging library.  
                  Let a thousand taggers bloom!
   
   Copyright (C) Robert Kaye 2003
   
   This file is part of libtunepimp.

   libtunepimp is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   libtunepimp is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libtunepimp; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   $Id: utf8util.cpp 1477 2006-01-31 21:56:19Z fuchs $

----------------------------------------------------------------------------*/

#include <stdio.h>
#include "utf8util.h"
#include "utf8.h"
#ifdef WIN32
#include "../../config_win32.h"
#endif

string utf8Encode(const string &from)
{
    int   ret;
    char *dest;
    string to;

    to.clear();
    ret = utf8_encode(from.c_str(), &dest);
    if (ret >= 0)
    {
        to = string(dest);
        free(dest);
    }
    return to;
}

string utf8Decode(const string &from)
{
    int   ret;
    char *dest;
    string to;

    to.clear();
    ret = utf8_decode(from.c_str(), &dest);
    if (ret >= 0)
    {
        to = string(dest);
        free(dest);
    }
    return to;
}

#if !defined(WIN32) && defined(HAVE_ICONV)
extern "C"
{
int iconvert(const char *fromcode, const char *tocode,
	     const char *from, size_t fromlen,
	     char **to, size_t *tolen);
}
#endif

string utf8ToEncoding(const string &from, const string &encoding)
{
#if !defined(WIN32) && defined(HAVE_ICONV)
    string to;

    if (strcasecmp(encoding.c_str(), "utf-8") == 0)
    {
        to = from;
        return to;
    }

    int   ret;
    char *dest;

	ret = iconvert("UTF-8", encoding.c_str(), from.c_str(), from.length(), &dest, 0);
    if (ret >= 0)
    {
        to = string(dest);
        free(dest);
    }
    return to;
#else
    /* 
    It's perfectly safe to do this, because on Windows we have internally all file names in UTF-8, and for WINAPI functions we only can use file names in
    ANSI CPs (windows-1250, windows-1251, ...) or UNICODE16. File names in these encodings are then automaticaly (inside WINAPI) converted to right
    encoding. So if we will always use WINAPI functions with W suffix, we don't need any other encoding than UNICODE.
    */
    return from;
#endif
}

string utf8FromEncoding(const string &from, const string &encoding)
{
#if !defined(WIN32) && defined(HAVE_ICONV)
    string to;

    if (strcasecmp(encoding.c_str(), "utf-8") == 0)
    {
        to = from;
        return to;
    }

    int   ret;
    char *dest;
    ret = iconvert(encoding.c_str(), "UTF-8", from.c_str(), from.length(), &dest, 0);
    if (ret >= 0)
    {
        to = string(dest);
        free(dest);
    }
    
    return to;
#else
    return from;
#endif
}