comparison 3rdparty/vmime/src/vmime/charsetConverter_win.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library. Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23
24 #include "vmime/config.hpp"
25
26
27 #if VMIME_CHARSETCONV_LIB_IS_WIN
28
29
30 #include "vmime/charsetConverter_win.hpp"
31
32 #include "vmime/exception.hpp"
33 #include "vmime/utility/stringUtils.hpp"
34 #include "vmime/utility/inputStreamStringAdapter.hpp"
35 #include "vmime/utility/outputStreamStringAdapter.hpp"
36
37 #include <string.h>
38 #include <stdlib.h>
39
40
41 #if (_WIN32 || _WIN64 || WIN32 || WIN64)
42 #include <windows.h>
43 #include "vmime/platforms/windows/windowsCodepages.hpp"
44 #else
45 #error Please use VMIME_CHARSETCONV_LIB_IS_WIN only on Windows!
46 #endif
47
48
49 #define CP_UNICODE 1200
50
51
52 namespace vmime
53 {
54
55
56 // static
57 shared_ptr <charsetConverter> charsetConverter::createGenericConverter
58 (const charset& source, const charset& dest,
59 const charsetConverterOptions& opts)
60 {
61 return make_shared <charsetConverter_win>(source, dest, opts);
62 }
63
64
65 charsetConverter_win::charsetConverter_win
66 (const charset& source, const charset& dest, const charsetConverterOptions& opts)
67 : m_source(source), m_dest(dest), m_options(opts)
68 {
69 }
70
71
72 void charsetConverter_win::convert(utility::inputStream& in, utility::outputStream& out)
73 {
74 byte_t buffer[32768];
75 string inStr, outStr;
76
77 while (!in.eof())
78 {
79 const size_t len = in.read(buffer, sizeof(buffer));
80 utility::stringUtils::appendBytesToString(inStr, buffer, len);
81 }
82
83 convert(inStr, outStr);
84
85 out.write(outStr.data(), outStr.length());
86 }
87
88
89 void charsetConverter_win::convert(const string& in, string& out)
90 {
91 if (m_source == m_dest)
92 {
93 // No conversion needed
94 out = in;
95 return;
96 }
97
98 const int sourceCodePage = getCodePage(m_source.getName().c_str());
99 const int destCodePage = getCodePage(m_dest.getName().c_str());
100
101 // Convert from source charset to Unicode
102 std::vector <char> unicodeBuffer;
103 const WCHAR* unicodePtr = NULL;
104 size_t unicodeLen = 0;
105
106 if (sourceCodePage == CP_UNICODE)
107 {
108 unicodePtr = reinterpret_cast <const WCHAR*>(in.c_str());
109 unicodeLen = in.length() / 2;
110 }
111 else
112 {
113 const size_t bufferSize = in.length() * 2; // in wide characters
114 unicodeBuffer.resize(bufferSize);
115
116 unicodePtr = reinterpret_cast <const WCHAR*>(&unicodeBuffer[0]);
117 unicodeLen = MultiByteToWideChar
118 (sourceCodePage, 0, in.c_str(), static_cast <int>(in.length()),
119 reinterpret_cast <WCHAR*>(&unicodeBuffer[0]), static_cast <int>(bufferSize));
120 }
121
122 // Convert from Unicode to destination charset
123 if (destCodePage == CP_UNICODE)
124 {
125 out.assign(reinterpret_cast <const char*>(unicodePtr), unicodeLen * 2);
126 }
127 else
128 {
129 const size_t bufferSize = unicodeLen * 6; // in multibyte characters
130
131 std::vector <char> buffer;
132 buffer.resize(bufferSize);
133
134 const size_t len = WideCharToMultiByte
135 (destCodePage, 0, unicodePtr, static_cast <int>(unicodeLen),
136 &buffer[0], static_cast <int>(bufferSize), 0, NULL);
137
138 out.assign(&buffer[0], len);
139 }
140 }
141
142
143 // static
144 int charsetConverter_win::getCodePage(const char* name)
145 {
146 if (_stricmp(name, charsets::UTF_16) == 0) // wchar_t is UTF-16 on Windows
147 return CP_UNICODE;
148
149 // "cp1252" --> return 1252
150 if ((name[0] == 'c' || name[0] == 'C') &&
151 (name[1] == 'p' || name[1] == 'P'))
152 {
153 return atoi(name + 2);
154 }
155
156 return vmime::platforms::windows::windowsCodepages::getByName(name); // throws
157 }
158
159
160 shared_ptr <utility::charsetFilteredOutputStream>
161 charsetConverter_win::getFilteredOutputStream(utility::outputStream& /* os */)
162 {
163 // TODO: implement me!
164 return null;
165 }
166
167
168 } // vmime
169
170
171 #endif // VMIME_CHARSETCONV_LIB_IS_WIN