Skip to content

Commit 4e28c8d

Browse files
kwolekrest31
authored andcommittedJun 29, 2015
Fix *BSD build with GNU iconv
1 parent 0d65ee8 commit 4e28c8d

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed
 

Diff for: ‎src/CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,18 @@ else()
249249
else()
250250
set(PLATFORM_LIBS -lrt ${PLATFORM_LIBS})
251251
endif(APPLE)
252-
#set(CLIENT_PLATFORM_LIBS -lXxf86vm)
252+
253253
# This way Xxf86vm is found on OpenBSD too
254254
find_library(XXF86VM_LIBRARY Xxf86vm)
255255
mark_as_advanced(XXF86VM_LIBRARY)
256256
set(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${XXF86VM_LIBRARY})
257+
258+
# Prefer local iconv if installed
259+
find_library(ICONV_LIBRARY iconv)
260+
mark_as_advanced(ICONV_LIBRARY)
261+
if (ICONV_LIBRARY)
262+
set(PLATFORM_LIBS ${PLATFORM_LIBS} ${ICONV_LIBRARY})
263+
endif()
257264
endif()
258265

259266
check_include_files(endian.h HAVE_ENDIAN_H)

Diff for: ‎src/util/string.cpp

+28-13
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
#include <map>
3131

3232
#ifndef _WIN32
33-
#include <iconv.h>
33+
#include <iconv.h>
3434
#else
35-
#define _WIN32_WINNT 0x0501
36-
#include <windows.h>
35+
#define _WIN32_WINNT 0x0501
36+
#include <windows.h>
37+
#endif
38+
39+
#if defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__NetBSD__) || \
40+
defined(__OpenBSD__) || defined(__DragonFly__))
41+
#define BSD_ICONV_USED
3742
#endif
3843

3944
static bool parseHexColorString(const std::string &value, video::SColor &color);
4045
static bool parseNamedColorString(const std::string &value, video::SColor &color);
4146

4247
#ifndef _WIN32
48+
4349
bool convert(const char *to, const char *from, char *outbuf,
4450
size_t outbuf_size, char *inbuf, size_t inbuf_size)
4551
{
4652
iconv_t cd = iconv_open(to, from);
4753

48-
#if defined(__FreeBSD__) || defined(__FreeBSD)
54+
#ifdef BSD_ICONV_USED
4955
const char *inbuf_ptr = inbuf;
5056
#else
5157
char *inbuf_ptr = inbuf;
@@ -88,7 +94,7 @@ std::wstring utf8_to_wide(const std::string &input)
8894
delete[] outbuf;
8995
return L"<invalid UTF-8 string>";
9096
}
91-
std::wstring out((wchar_t*)outbuf);
97+
std::wstring out((wchar_t *)outbuf);
9298

9399
delete[] inbuf;
94100
delete[] outbuf;
@@ -97,12 +103,15 @@ std::wstring utf8_to_wide(const std::string &input)
97103
}
98104

99105
#ifdef __ANDROID__
106+
100107
// TODO: this is an ugly fix for wide_to_utf8 somehow not working on android
101108
std::string wide_to_utf8(const std::wstring &input)
102109
{
103110
return wide_to_narrow(input);
104111
}
105-
#else
112+
113+
#else // __ANDROID__
114+
106115
std::string wide_to_utf8(const std::wstring &input)
107116
{
108117
size_t inbuf_size = (input.length() + 1) * sizeof(wchar_t);
@@ -128,14 +137,18 @@ std::string wide_to_utf8(const std::wstring &input)
128137

129138
return out;
130139
}
131-
#endif
132-
#else
140+
141+
#endif // __ANDROID__
142+
143+
#else // _WIN32
144+
133145
std::wstring utf8_to_wide(const std::string &input)
134146
{
135147
size_t outbuf_size = input.size() + 1;
136148
wchar_t *outbuf = new wchar_t[outbuf_size];
137149
memset(outbuf, 0, outbuf_size * sizeof(wchar_t));
138-
MultiByteToWideChar(CP_UTF8, 0, input.c_str(), input.size(), outbuf, outbuf_size);
150+
MultiByteToWideChar(CP_UTF8, 0, input.c_str(), input.size(),
151+
outbuf, outbuf_size);
139152
std::wstring out(outbuf);
140153
delete[] outbuf;
141154
return out;
@@ -146,18 +159,20 @@ std::string wide_to_utf8(const std::wstring &input)
146159
size_t outbuf_size = (input.size() + 1) * 6;
147160
char *outbuf = new char[outbuf_size];
148161
memset(outbuf, 0, outbuf_size);
149-
WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(), outbuf, outbuf_size, NULL, NULL);
162+
WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(),
163+
outbuf, outbuf_size, NULL, NULL);
150164
std::string out(outbuf);
151165
delete[] outbuf;
152166
return out;
153167
}
154-
#endif
168+
169+
#endif // _WIN32
155170

156171
// You must free the returned string!
157172
// The returned string is allocated using new
158173
wchar_t *narrow_to_wide_c(const char *str)
159174
{
160-
wchar_t* nstr = 0;
175+
wchar_t *nstr = NULL;
161176
#if defined(_WIN32)
162177
int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
163178
if (nResult == 0) {
@@ -168,7 +183,7 @@ wchar_t *narrow_to_wide_c(const char *str)
168183
}
169184
#else
170185
size_t len = strlen(str);
171-
nstr = new wchar_t[len+1];
186+
nstr = new wchar_t[len + 1];
172187

173188
std::wstring intermediate = narrow_to_wide(str);
174189
memset(nstr, 0, (len + 1) * sizeof(wchar_t));

0 commit comments

Comments
 (0)
Please sign in to comment.