Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csync: Use Qt for encoding conversion instead of iconv #5875

Merged
merged 4 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ add_definitions( -D__USE_MINGW_ANSI_STDIO=1 )
add_definitions( -DNOMINMAX )
endif( WIN32 )

include(QtVersionAbstraction)
setup_qt()
if (${Qt5Core_VERSION_MAJOR} EQUAL "5")
if (${Qt5Core_VERSION_MINOR} EQUAL "6" OR ${Qt5Core_VERSION_MINOR} GREATER 6)
else()
message(STATUS "If possible compile me with Qt 5.6 or higher.")
endif()
endif()

if (APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

# Handle Translations, pick all client_* files from trans directory.
file( GLOB TRANS_FILES ${CMAKE_SOURCE_DIR}/translations/client_*.ts)
set(TRANSLATIONS ${TRANS_FILES})
Expand Down
1 change: 1 addition & 0 deletions csync/src/std/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ include_directories(
add_library(${CSTDLIB_LIBRARY} STATIC ${cstdlib_SRCS})
if(NOT WIN32)
add_definitions( -fPIC )
qt5_use_modules(${CSTDLIB_LIBRARY} Core)
endif()
if(NOT HAVE_FNMATCH AND WIN32)
# needed for PathMatchSpec for our fnmatch replacement
Expand Down
49 changes: 26 additions & 23 deletions csync/src/std/c_utf8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@

#include "config_csync.h"

#include <assert.h>
#include <errno.h>
#ifdef _WIN32
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#include <limits.h>
#include <sys/types.h>
#include <wchar.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <QtCore/QTextCodec>
#include <QtCore/QFile>
#endif

extern "C" {
Expand All @@ -42,18 +41,15 @@ extern "C" {
/* Convert a locale String to UTF8 */
char* c_utf8_from_locale(const mbchar_t *wstr)
{
char *dst = NULL;
#ifdef _WIN32
char *mdst = NULL;
int size_needed;
size_t len;
#endif

if (wstr == NULL) {
return NULL;
}

#ifdef _WIN32
char *dst = NULL;
char *mdst = NULL;
int size_needed;
size_t len;
len = wcslen(wstr);
/* Call once to get the required size. */
size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL);
Expand All @@ -64,26 +60,33 @@ char* c_utf8_from_locale(const mbchar_t *wstr)
WideCharToMultiByte(CP_UTF8, 0, wstr, len, mdst, size_needed, NULL, NULL);
dst = mdst;
}
return dst;
#else
dst = c_strdup(wstr);
QTextDecoder dec(QTextCodec::codecForLocale());
QString s = dec.toUnicode(wstr, qstrlen(wstr));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const QString s = .... ?

Tested on Linux and works, but might be an issue on MacOSX

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

's' can't be const for two reason: First, on mac, we update it with s = s.normalized(QString::NormalizationForm_C);
Second, we std::move it before calling toUtf8. This saves an allocation as there is an overload of toUtf8 which does the conversion in-place. (but does not work if s was const)

if (s.isEmpty() || dec.hasFailure()) {
/* Conversion error: since we can't report error from this function, just return the original
string. We take care of invalid utf-8 in SyncEngine::treewalkFile */
return c_strdup(wstr);
}
#ifdef __APPLE__
s = s.normalized(QString::NormalizationForm_C);
#endif
return c_strdup(std::move(s).toUtf8().constData());
#endif
return dst;
}

/* Convert a an UTF8 string to locale */
mbchar_t* c_utf8_string_to_locale(const char *str)
{
mbchar_t *dst = NULL;
#ifdef _WIN32
size_t len;
int size_needed;
#endif

if (str == NULL ) {
return NULL;
}

#ifdef _WIN32
mbchar_t *dst = NULL;
size_t len;
int size_needed;

len = strlen(str);
size_needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
if (size_needed > 0) {
Expand All @@ -92,10 +95,10 @@ mbchar_t* c_utf8_string_to_locale(const char *str)
memset((void*)dst, 0, size_char);
MultiByteToWideChar(CP_UTF8, 0, str, -1, dst, size_needed);
}
return dst;
#else
dst = c_strdup(str);
return c_strdup(QFile::encodeName(QString::fromUtf8(str)));
#endif
return dst;
}

}
13 changes: 0 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ endif()

set(synclib_NAME ${APPLICATION_EXECUTABLE}sync)

include(QtVersionAbstraction)
setup_qt()
if (${Qt5Core_VERSION_MAJOR} EQUAL "5")
if (${Qt5Core_VERSION_MINOR} EQUAL "6" OR ${Qt5Core_VERSION_MINOR} GREATER 6)
else()
message(STATUS "If possible compile me with Qt 5.6 or higher.")
endif()
endif()

if (APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

if(NOT TOKEN_AUTH_ONLY)
find_package(Qt5Keychain REQUIRED)
endif()
Expand Down