diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2020-11-27 13:13:54 +0300 |
---|---|---|
committer | Kevin Ottens <kevin.ottens@nextcloud.com> | 2020-12-15 12:59:15 +0300 |
commit | 47dc7e6c492f0b2241916c1f97a63c2efb677e79 (patch) | |
tree | 705b80fee933568f8560f22f83717bf3e49d3a3d /src/csync | |
parent | 29c6f4412483a61e7e683226d410c4f060c26853 (diff) |
Remove custome string functions
Diffstat (limited to 'src/csync')
-rw-r--r-- | src/csync/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/csync/csync_exclude.cpp | 1 | ||||
-rw-r--r-- | src/csync/std/c_lib.h | 1 | ||||
-rw-r--r-- | src/csync/std/c_time.cpp (renamed from src/csync/std/c_time.c) | 17 | ||||
-rw-r--r-- | src/csync/std/c_time.h | 11 | ||||
-rw-r--r-- | src/csync/std/c_utf8.cpp | 128 | ||||
-rw-r--r-- | src/csync/std/c_utf8.h | 136 | ||||
-rw-r--r-- | src/csync/vio/csync_vio_local.h | 4 | ||||
-rw-r--r-- | src/csync/vio/csync_vio_local_unix.cpp | 11 | ||||
-rw-r--r-- | src/csync/vio/csync_vio_local_win.cpp | 14 |
10 files changed, 23 insertions, 303 deletions
diff --git a/src/csync/CMakeLists.txt b/src/csync/CMakeLists.txt index 2d50c2988..7f0740a72 100644 --- a/src/csync/CMakeLists.txt +++ b/src/csync/CMakeLists.txt @@ -36,8 +36,7 @@ set(csync_SRCS std/c_alloc.c std/c_string.c - std/c_time.c - std/c_utf8.cpp + std/c_time.cpp ) diff --git a/src/csync/csync_exclude.cpp b/src/csync/csync_exclude.cpp index 902dffc76..2b9c549c2 100644 --- a/src/csync/csync_exclude.cpp +++ b/src/csync/csync_exclude.cpp @@ -26,7 +26,6 @@ #include "c_lib.h" #include "c_private.h" -#include "c_utf8.h" #include "csync_exclude.h" diff --git a/src/csync/std/c_lib.h b/src/csync/std/c_lib.h index e277f4e95..f6092a953 100644 --- a/src/csync/std/c_lib.h +++ b/src/csync/std/c_lib.h @@ -24,5 +24,4 @@ #include "c_macro.h" #include "c_alloc.h" #include "c_string.h" -#include "c_time.h" #include "c_private.h" diff --git a/src/csync/std/c_time.c b/src/csync/std/c_time.cpp index 28528fdc6..616255313 100644 --- a/src/csync/std/c_time.c +++ b/src/csync/std/c_time.cpp @@ -23,13 +23,12 @@ #include "c_string.h" #include "c_time.h" -#include "c_utf8.h" + +#include <QFile> #ifdef HAVE_UTIMES -int c_utimes(const char *uri, const struct timeval *times) { - mbchar_t *wuri = c_utf8_path_to_locale(uri); - int ret = utimes(wuri, times); - c_free_locale_string(wuri); +int c_utimes(const QString &uri, const struct timeval *times) { + int ret = utimes(QFile::encodeName(uri).constData(), times); return ret; } #else // HAVE_UTIMES @@ -50,12 +49,12 @@ static void UnixTimevalToFileTime(struct timeval t, LPFILETIME pft) pft->dwHighDateTime = ll >> 32; } -int c_utimes(const char *uri, const struct timeval *times) { +int c_utimes(const QString &uri, const struct timeval *times) { FILETIME LastAccessTime; FILETIME LastModificationTime; HANDLE hFile; - mbchar_t *wuri = c_utf8_path_to_locale( uri ); + auto wuri = uri.toStdWString(); if(times) { UnixTimevalToFileTime(times[0], &LastAccessTime); @@ -66,7 +65,7 @@ int c_utimes(const char *uri, const struct timeval *times) { GetSystemTimeAsFileTime(&LastModificationTime); } - hFile=CreateFileW(wuri, FILE_WRITE_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, + hFile=CreateFileW(wuri.data(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL+FILE_FLAG_BACKUP_SEMANTICS, NULL); if(hFile==INVALID_HANDLE_VALUE) { switch(GetLastError()) { @@ -94,12 +93,10 @@ int c_utimes(const char *uri, const struct timeval *times) { //can this happen? errno=ENOENT; CloseHandle(hFile); - c_free_locale_string(wuri); return -1; } CloseHandle(hFile); - c_free_locale_string(wuri); return 0; } diff --git a/src/csync/std/c_time.h b/src/csync/std/c_time.h index 3792198f8..55a6aa6bc 100644 --- a/src/csync/std/c_time.h +++ b/src/csync/std/c_time.h @@ -21,11 +21,9 @@ #ifndef _C_TIME_H #define _C_TIME_H -#include "ocsynclib.h" +#include <QString> -#ifdef __cplusplus -extern "C" { -#endif +#include "ocsynclib.h" #ifdef _WIN32 #include <time.h> @@ -33,10 +31,7 @@ extern "C" { #include <sys/time.h> #endif -OCSYNC_EXPORT int c_utimes(const char *uri, const struct timeval *times); +OCSYNC_EXPORT int c_utimes(const QString &uri, const struct timeval *times); -#ifdef __cplusplus -} -#endif #endif /* _C_TIME_H */ diff --git a/src/csync/std/c_utf8.cpp b/src/csync/std/c_utf8.cpp deleted file mode 100644 index 93e678304..000000000 --- a/src/csync/std/c_utf8.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - * cynapses libc functions - * - * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org> - * Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "config_csync.h" -#include "c_utf8.h" - -#ifdef _WIN32 -#include <ctype.h> -#include <string.h> -#include <stdlib.h> -#include <limits.h> -#include <sys/types.h> -#include <wchar.h> -#include <windows.h> -#else -#include <QtCore/QTextCodec> -#include <QtCore/QFile> -#endif - -#include "c_alloc.h" -#include "c_string.h" -#include "common/filesystembase.h" -#include "common/utility.h" - -/* Convert a locale String to UTF8 */ -QByteArray c_utf8_from_locale(const mbchar_t *wstr) -{ - if (!wstr) { - return QByteArray(); - } - -#ifdef _WIN32 - QByteArray dst; - int size_needed; - size_t len; - len = wcslen(wstr); - /* Call once to get the required size. */ - size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, OCC::Utility::convertSizeToInt(len), nullptr, 0, nullptr, nullptr); - if (size_needed > 0) { - dst.resize(size_needed); - WideCharToMultiByte(CP_UTF8, 0, wstr, OCC::Utility::convertSizeToInt(len), dst.data(), size_needed, nullptr, nullptr); - } - return dst; -#else - auto codec = QTextCodec::codecForLocale(); -#ifndef __APPLE__ - if (codec->mibEnum() == 106) { // UTF-8 - // Optimisation for UTF-8: no need to convert to QString. - // We still need to do it for mac because of normalization - return QByteArray(wstr); - } -#endif - QTextDecoder dec(codec); - QString s = dec.toUnicode(wstr, qstrlen(wstr)); - 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 QByteArray(wstr); - } -#ifdef __APPLE__ - s = s.normalized(QString::NormalizationForm_C); -#endif - return std::move(s).toUtf8(); -#endif -} - -extern "C" { - -/* Convert a an UTF8 string to locale */ -mbchar_t* c_utf8_string_to_locale(const char *str) -{ - if (!str) { - return nullptr; - } -#ifdef _WIN32 - mbchar_t *dst = nullptr; - size_t len; - int size_needed; - - len = strlen(str); - size_needed = MultiByteToWideChar(CP_UTF8, 0, str, OCC::Utility::convertSizeToInt(len), nullptr, 0); - if (size_needed > 0) { - int size_char = (size_needed + 1) * sizeof(mbchar_t); - dst = (mbchar_t*)c_malloc(size_char); - memset((void*)dst, 0, size_char); - MultiByteToWideChar(CP_UTF8, 0, str, -1, dst, size_needed); - } - return dst; -#else - return c_strdup(QFile::encodeName(QString::fromUtf8(str))); -#endif -} - - mbchar_t* c_utf8_path_to_locale(const char *str) - { - if(!str) { - return nullptr; - } else { - #ifdef _WIN32 - size_t strLength = strlen(str); - QByteArray unc_str = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(str, OCC::Utility::convertSizeToInt(strLength))); - mbchar_t *dst = c_utf8_string_to_locale(unc_str); - return dst; - #else - return c_utf8_string_to_locale(str); - #endif - } - } - -} diff --git a/src/csync/std/c_utf8.h b/src/csync/std/c_utf8.h deleted file mode 100644 index 2bbe1d4bd..000000000 --- a/src/csync/std/c_utf8.h +++ /dev/null @@ -1,136 +0,0 @@ -/* - * cynapses libc functions - * - * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org> - * Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file c_string.h - * - * @brief Interface of the cynapses string implementations - * - * @defgroup cynStringInternals cynapses libc string functions - * @ingroup cynLibraryAPI - * - * @{ - */ -#ifndef _C_UTF8_H -#define _C_UTF8_H - -#include "c_private.h" -#include "c_macro.h" - -#ifdef __cplusplus -#include <QByteArray> - -/** - * @brief Convert a platform locale string to utf8. - * - * This function is part of the multi platform abstraction of basic file - * operations to handle various platform encoding correctly. - * - * Instead of using the standard file operations the multi platform aliases - * defined in c_private.h have to be used instead. - * - * To convert path names returned by these functions to the internally used - * utf8 format this function has to be used. - * - * @param str The multibyte encoded string to convert - * - * @return The converted string or a null QByteArray on error. - * - * @see c_free_locale_string() - * @see c_utf8_to_locale() - * - */ - QByteArray c_utf8_from_locale(const mbchar_t *str); - -extern "C" { - -#endif // __cplusplus - -/** - * @brief Convert a utf8 encoded string to platform specific locale. - * - * This function is part of the multi platform abstraction of basic file - * operations to handle various platform encoding correctly. - * - * Instead of using the standard file operations the multi platform aliases - * defined in c_private.h have to be used instead. - * - * To convert strings as input for the cross platform functions from the - * internally used utf8 format, this function has to be used. - * The returned string has to be freed by c_free_locale_string(). On some - * platforms this method allocates memory and on others not but it has never - * sto be cared about. - * - * If the string to convert is a path, consider using c_utf8_path_to_locale(). - * - * @param str The utf8 string to convert. - * - * @return The malloced converted multibyte string or \c nullptr on error. - * - * @see c_free_locale_string() - * @see c_utf8_from_locale() - * - */ -mbchar_t* c_utf8_string_to_locale(const char *wstr); - -/** - * @brief c_utf8_path_to_locale converts a unixoid path to the locale aware format - * - * On windows, it converts to UNC and multibyte. - * On Mac, it converts to the correct utf8 using iconv. - * On Linux, it returns utf8 - * - * @param str The path to convert - * - * @return a pointer to the converted string. Caller has to free it using the - * function c_free_locale_string. - */ -mbchar_t* c_utf8_path_to_locale(const char *str); - -/** - * @brief Free buffer malloced by c_utf8_to_locale(). - * - * This function is part of the multi platform abstraction of basic file - * operations to handle various platform encoding correctly. - * - * Instead of using the standard file operations the multi platform aliases - * defined in c_private.h have to be used instead. - * - * This function frees the memory that was allocated by a previous call to - * c_utf8_to_locale(). - * - * @param buf The buffer to free. - * - * @see c_utf8_from_locale(), c_utf8_to_locale() - * - */ -#define c_free_locale_string(x) SAFE_FREE(x) - - -/** - * }@ - */ - -#ifdef __cplusplus -} -#endif - -#endif /* _C_UTF8_H */ diff --git a/src/csync/vio/csync_vio_local.h b/src/csync/vio/csync_vio_local.h index 769d1d020..04bd057ea 100644 --- a/src/csync/vio/csync_vio_local.h +++ b/src/csync/vio/csync_vio_local.h @@ -21,6 +21,8 @@ #ifndef _CSYNC_VIO_LOCAL_H #define _CSYNC_VIO_LOCAL_H +#include <QString> + struct csync_vio_handle_t; namespace OCC { class Vfs; @@ -30,6 +32,6 @@ csync_vio_handle_t OCSYNC_EXPORT *csync_vio_local_opendir(const QString &name); int OCSYNC_EXPORT csync_vio_local_closedir(csync_vio_handle_t *dhandle); std::unique_ptr<csync_file_stat_t> OCSYNC_EXPORT csync_vio_local_readdir(csync_vio_handle_t *dhandle, OCC::Vfs *vfs); -int OCSYNC_EXPORT csync_vio_local_stat(const char *uri, csync_file_stat_t *buf); +int OCSYNC_EXPORT csync_vio_local_stat(const QString &uri, csync_file_stat_t *buf); #endif /* _CSYNC_VIO_LOCAL_H */ diff --git a/src/csync/vio/csync_vio_local_unix.cpp b/src/csync/vio/csync_vio_local_unix.cpp index 5b58b01cb..fae033b9d 100644 --- a/src/csync/vio/csync_vio_local_unix.cpp +++ b/src/csync/vio/csync_vio_local_unix.cpp @@ -31,7 +31,6 @@ #include "c_private.h" #include "c_lib.h" #include "c_string.h" -#include "c_utf8.h" #include "csync_util.h" #include "vio/csync_vio_local.h" @@ -86,7 +85,7 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h } while (qstrcmp(dirent->d_name, ".") == 0 || qstrcmp(dirent->d_name, "..") == 0); file_stat = std::make_unique<csync_file_stat_t>(); - file_stat->path = c_utf8_from_locale(dirent->d_name); + file_stat->path = QFile::decodeName(dirent->d_name).toUtf8(); QByteArray fullPath = handle->path % '/' % QByteArray() % const_cast<const char *>(dirent->d_name); if (file_stat->path.isNull()) { file_stat->original_path = fullPath; @@ -133,13 +132,9 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h } -int csync_vio_local_stat(const char *uri, csync_file_stat_t *buf) +int csync_vio_local_stat(const QString &uri, csync_file_stat_t *buf) { - mbchar_t *wuri = c_utf8_path_to_locale(uri); - *buf = csync_file_stat_t(); - int rc = _csync_vio_local_stat_mb(wuri, buf); - c_free_locale_string(wuri); - return rc; + return _csync_vio_local_stat_mb(QFile::encodeName(uri).constData(), buf); } static int _csync_vio_local_stat_mb(const mbchar_t *wuri, csync_file_stat_t *buf) diff --git a/src/csync/vio/csync_vio_local_win.cpp b/src/csync/vio/csync_vio_local_win.cpp index 8f0114767..354aea0f3 100644 --- a/src/csync/vio/csync_vio_local_win.cpp +++ b/src/csync/vio/csync_vio_local_win.cpp @@ -31,7 +31,6 @@ #include "c_private.h" #include "c_lib.h" -#include "c_utf8.h" #include "csync_util.h" #include "vio/csync_vio_local.h" #include "common/filesystembase.h" @@ -137,12 +136,12 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h return nullptr; } } - auto path = c_utf8_from_locale(handle->ffd.cFileName); + auto path = QString::fromWCharArray(handle->ffd.cFileName); if (path == "." || path == "..") return csync_vio_local_readdir(handle, vfs); file_stat = std::make_unique<csync_file_stat_t>(); - file_stat->path = path; + file_stat->path = path.toUtf8(); if (vfs && vfs->statTypeVirtualFile(file_stat.get(), &handle->ffd)) { // all good @@ -177,7 +176,7 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h std::wstring fullPath; fullPath.reserve(handle->path.size() + std::wcslen(handle->ffd.cFileName)); - fullPath += reinterpret_cast<const wchar_t *>(handle->path.utf16()); // path always ends with '\', by construction + fullPath += handle->path.toStdWString(); // path always ends with '\', by construction fullPath += handle->ffd.cFileName; if (_csync_vio_local_stat_mb(fullPath.data(), file_stat.get()) < 0) { @@ -189,11 +188,10 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h } -int csync_vio_local_stat(const char *uri, csync_file_stat_t *buf) +int csync_vio_local_stat(const QString &uri, csync_file_stat_t *buf) { - mbchar_t *wuri = c_utf8_path_to_locale(uri); - int rc = _csync_vio_local_stat_mb(wuri, buf); - c_free_locale_string(wuri); + const std::wstring wuri = uri.toStdWString(); + int rc = _csync_vio_local_stat_mb(wuri.data(), buf); return rc; } |