xref: /haiku/src/add-ons/media/plugins/ape_reader/MAClib/CharacterHelper.cpp (revision be878f60874196f746f336f235797c8efa25004e)
1 #include "All.h"
2 #include "CharacterHelper.h"
3 
GetANSIFromUTF8(const str_utf8 * pUTF8)4 str_ansi * GetANSIFromUTF8(const str_utf8 * pUTF8)
5 {
6     str_utf16 * pUTF16 = GetUTF16FromUTF8(pUTF8);
7     str_ansi * pANSI = GetANSIFromUTF16(pUTF16);
8     delete [] pUTF16;
9     return pANSI;
10 }
11 
GetANSIFromUTF16(const str_utf16 * pUTF16)12 str_ansi * GetANSIFromUTF16(const str_utf16 * pUTF16)
13 {
14     const int nCharacters = pUTF16 ? wcslen(pUTF16) : 0;
15     #ifdef _WIN32
16         int nANSICharacters = (2 * nCharacters);
17         str_ansi * pANSI = new str_ansi [nANSICharacters + 1];
18         memset(pANSI, 0, (nANSICharacters + 1) * sizeof(str_ansi));
19         if (pUTF16)
20             WideCharToMultiByte(CP_ACP, 0, pUTF16, -1, pANSI, nANSICharacters, NULL, NULL);
21     #else
22         str_utf8 * pANSI = new str_utf8 [nCharacters + 1];
23 //        for (int z = 0; z < nCharacters; z++)
24 //            pANSI[z] = (pUTF16[z] >= 256) ? '?' : (str_utf8) pUTF16[z];
25 //        pANSI[nCharacters] = 0;
26 		memcpy(pANSI, pUTF16, nCharacters + 1);
27     #endif
28 
29     return (str_ansi *) pANSI;
30 }
31 
GetUTF16FromANSI(const str_ansi * pANSI)32 str_utf16 * GetUTF16FromANSI(const str_ansi * pANSI)
33 {
34     const int nCharacters = pANSI ? strlen(pANSI) : 0;
35     str_utf16 * pUTF16 = new str_utf16 [nCharacters + 1];
36 
37     #ifdef _WIN32
38         memset(pUTF16, 0, sizeof(str_utf16) * (nCharacters + 1));
39         if (pANSI)
40             MultiByteToWideChar(CP_ACP, 0, pANSI, -1, pUTF16, nCharacters);
41     #else
42 //        for (int z = 0; z < nCharacters; z++)
43 //            pUTF16[z] = (str_utf16) ((str_utf8) pANSI[z]);
44 //        pUTF16[nCharacters] = 0;
45 			strcpy(pUTF16, pANSI);
46     #endif
47 
48     return pUTF16;
49 }
50 
GetUTF16FromUTF8(const str_utf8 * pUTF8)51 str_utf16 * GetUTF16FromUTF8(const str_utf8 * pUTF8)
52 {
53 #if 0
54     // get the length
55     int nCharacters = 0; int nIndex = 0;
56     while (pUTF8[nIndex] != 0)
57     {
58         if ((pUTF8[nIndex] & 0x80) == 0)
59             nIndex += 1;
60         else if ((pUTF8[nIndex] & 0xE0) == 0xE0)
61             nIndex += 3;
62         else
63             nIndex += 2;
64 
65         nCharacters += 1;
66     }
67 
68     // make a UTF-16 string
69     str_utf16 * pUTF16 = new str_utf16 [nCharacters + 1];
70     nIndex = 0; nCharacters = 0;
71     while (pUTF8[nIndex] != 0)
72     {
73         if ((pUTF8[nIndex] & 0x80) == 0)
74         {
75             pUTF16[nCharacters] = pUTF8[nIndex];
76             nIndex += 1;
77         }
78         else if ((pUTF8[nIndex] & 0xE0) == 0xE0)
79         {
80             pUTF16[nCharacters] = ((pUTF8[nIndex] & 0x1F) << 12) | ((pUTF8[nIndex + 1] & 0x3F) << 6) | (pUTF8[nIndex + 2] & 0x3F);
81             nIndex += 3;
82         }
83         else
84         {
85             pUTF16[nCharacters] = ((pUTF8[nIndex] & 0x3F) << 6) | (pUTF8[nIndex + 1] & 0x3F);
86             nIndex += 2;
87         }
88 
89         nCharacters += 1;
90     }
91     pUTF16[nCharacters] = 0;
92 #endif
93 	int nCharacters = strlen(reinterpret_cast<const char*>(pUTF8));
94 	str_utf16 * pUTF16 = new str_utf16 [nCharacters + 1];
95 	memcpy(pUTF16, pUTF8, nCharacters + 1);
96     return pUTF16;
97 }
98 
GetUTF8FromANSI(const str_ansi * pANSI)99 str_utf8 * GetUTF8FromANSI(const str_ansi * pANSI)
100 {
101     str_utf16 * pUTF16 = GetUTF16FromANSI(pANSI);
102     str_utf8 * pUTF8 = GetUTF8FromUTF16(pUTF16);
103     delete [] pUTF16;
104     return pUTF8;
105 }
106 
GetUTF8FromUTF16(const str_utf16 * pUTF16)107 str_utf8 * GetUTF8FromUTF16(const str_utf16 * pUTF16)
108 {
109 #if 0
110     // get the size(s)
111     int nCharacters = wcslen(pUTF16);
112     int nUTF8Bytes = 0;
113     for (int z = 0; z < nCharacters; z++)
114     {
115         if (pUTF16[z] < 0x0080)
116             nUTF8Bytes += 1;
117         else if (pUTF16[z] < 0x0800)
118             nUTF8Bytes += 2;
119         else
120             nUTF8Bytes += 3;
121     }
122 
123     // allocate a UTF-8 string
124     str_utf8 * pUTF8 = new str_utf8 [nUTF8Bytes + 1];
125 
126     // create the UTF-8 string
127     int nUTF8Index = 0;
128     for (int z = 0; z < nCharacters; z++)	// SHINTA
129     {
130         if (pUTF16[z] < 0x0080)
131         {
132             pUTF8[nUTF8Index++] = (str_utf8) pUTF16[z];
133         }
134         else if (pUTF16[z] < 0x0800)
135         {
136             pUTF8[nUTF8Index++] = 0xC0 | (pUTF16[z] >> 6);
137             pUTF8[nUTF8Index++] = 0x80 | (pUTF16[z] & 0x3F);
138         }
139         else
140         {
141             pUTF8[nUTF8Index++] = 0xE0 | (pUTF16[z] >> 12);
142             pUTF8[nUTF8Index++] = 0x80 | ((pUTF16[z] >> 6) & 0x3F);
143             pUTF8[nUTF8Index++] = 0x80 | (pUTF16[z] & 0x3F);
144         }
145     }
146     pUTF8[nUTF8Index++] = 0;
147 #endif
148 	int nCharacters = strlen(pUTF16);
149     str_utf8 * pUTF8 = new str_utf8 [nCharacters + 1];
150 	memcpy(pUTF8, pUTF16, nCharacters + 1);
151     // return the UTF-8 string
152     return pUTF8;
153 }
154