2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2011 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/IArchString.h"
|
|
|
|
|
#include "arch/Arch.h"
|
|
|
|
|
#include "common/common.h"
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <climits>
|
|
|
|
|
#include <cstdlib>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <cstring>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
static ArchMutex s_mutex = NULL;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// use C library non-reentrant multibyte conversion with mutex
|
|
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IArchString::~IArchString()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (s_mutex != NULL) {
|
|
|
|
|
ARCH->closeMutex(s_mutex);
|
|
|
|
|
s_mutex = NULL;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
int IArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool *errors)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ptrdiff_t len = 0;
|
|
|
|
|
|
|
|
|
|
bool dummyErrors;
|
|
|
|
|
if (errors == NULL) {
|
|
|
|
|
errors = &dummyErrors;
|
|
|
|
|
}
|
|
|
|
|
*errors = false;
|
|
|
|
|
|
|
|
|
|
if (s_mutex == NULL) {
|
|
|
|
|
s_mutex = ARCH->newMutex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARCH->lockMutex(s_mutex);
|
|
|
|
|
|
|
|
|
|
if (dst == NULL) {
|
|
|
|
|
char dummy[MB_LEN_MAX];
|
|
|
|
|
const wchar_t *scan = src;
|
|
|
|
|
for (; n > 0; --n) {
|
|
|
|
|
ptrdiff_t mblen = wctomb(dummy, *scan);
|
|
|
|
|
if (mblen == -1) {
|
|
|
|
|
*errors = true;
|
|
|
|
|
mblen = 1;
|
|
|
|
|
}
|
|
|
|
|
len += mblen;
|
|
|
|
|
++scan;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
ptrdiff_t mblen = wctomb(dummy, L'\0');
|
|
|
|
|
if (mblen != -1) {
|
|
|
|
|
len += mblen - 1;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
|
|
|
|
char *dst0 = dst;
|
|
|
|
|
const wchar_t *scan = src;
|
|
|
|
|
for (; n > 0; --n) {
|
|
|
|
|
ptrdiff_t mblen = wctomb(dst, *scan);
|
|
|
|
|
if (mblen == -1) {
|
|
|
|
|
*errors = true;
|
|
|
|
|
*dst++ = '?';
|
|
|
|
|
} else {
|
|
|
|
|
dst += mblen;
|
|
|
|
|
}
|
|
|
|
|
++scan;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
ptrdiff_t mblen = wctomb(dst, L'\0');
|
|
|
|
|
if (mblen != -1) {
|
|
|
|
|
// don't include nul terminator
|
|
|
|
|
dst += mblen - 1;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
len = dst - dst0;
|
|
|
|
|
}
|
|
|
|
|
ARCH->unlockMutex(s_mutex);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return static_cast<int>(len);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
int IArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool *errors)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ptrdiff_t len = 0;
|
|
|
|
|
wchar_t dummy;
|
|
|
|
|
|
|
|
|
|
bool dummyErrors;
|
|
|
|
|
if (errors == NULL) {
|
|
|
|
|
errors = &dummyErrors;
|
|
|
|
|
}
|
|
|
|
|
*errors = false;
|
|
|
|
|
|
|
|
|
|
if (s_mutex == NULL) {
|
|
|
|
|
s_mutex = ARCH->newMutex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARCH->lockMutex(s_mutex);
|
|
|
|
|
|
|
|
|
|
if (dst == NULL) {
|
|
|
|
|
const char *scan = src;
|
|
|
|
|
while (n > 0) {
|
|
|
|
|
ptrdiff_t mblen = mbtowc(&dummy, scan, n);
|
|
|
|
|
switch (mblen) {
|
|
|
|
|
case -2:
|
|
|
|
|
// incomplete last character. convert to unknown character.
|
|
|
|
|
*errors = true;
|
|
|
|
|
len += 1;
|
|
|
|
|
n = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
|
// invalid character. count one unknown character and
|
|
|
|
|
// start at the next byte.
|
|
|
|
|
*errors = true;
|
|
|
|
|
len += 1;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
len += 1;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// normal character
|
|
|
|
|
len += 1;
|
|
|
|
|
scan += static_cast<int>(mblen);
|
|
|
|
|
n -= static_cast<int>(mblen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
|
|
|
|
wchar_t *dst0 = dst;
|
|
|
|
|
const char *scan = src;
|
|
|
|
|
while (n > 0) {
|
|
|
|
|
ptrdiff_t mblen = mbtowc(dst, scan, n);
|
|
|
|
|
switch (mblen) {
|
|
|
|
|
case -2:
|
|
|
|
|
// incomplete character. convert to unknown character.
|
|
|
|
|
*errors = true;
|
|
|
|
|
*dst = (wchar_t)0xfffd;
|
|
|
|
|
n = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
|
// invalid character. count one unknown character and
|
|
|
|
|
// start at the next byte.
|
|
|
|
|
*errors = true;
|
|
|
|
|
*dst = (wchar_t)0xfffd;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
*dst = (wchar_t)0x0000;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// normal character
|
|
|
|
|
scan += static_cast<int>(mblen);
|
|
|
|
|
n -= static_cast<int>(mblen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++dst;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
len = dst - dst0;
|
|
|
|
|
}
|
|
|
|
|
ARCH->unlockMutex(s_mutex);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return static_cast<int>(len);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|