2016-04-21 17:03:52 -05:00
|
|
|
// 21 april 2016
|
|
|
|
#include "uipriv_windows.hpp"
|
|
|
|
|
|
|
|
// see http://stackoverflow.com/a/29556509/3408572
|
|
|
|
|
|
|
|
WCHAR *toUTF16(const char *str)
|
|
|
|
{
|
|
|
|
WCHAR *wstr;
|
2016-12-03 10:31:11 -06:00
|
|
|
WCHAR *wp;
|
|
|
|
size_t n;
|
|
|
|
uint32_t rune;
|
2016-04-21 17:03:52 -05:00
|
|
|
|
|
|
|
if (*str == '\0') // empty string
|
|
|
|
return emptyUTF16();
|
2016-12-03 10:31:11 -06:00
|
|
|
n = utf8UTF16Count(str, 0);
|
|
|
|
wstr = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]");
|
|
|
|
wp = wstr;
|
|
|
|
while (*str) {
|
|
|
|
str = utf8DecodeRune(str, 0, &rune);
|
|
|
|
n = utf16EncodeRune(rune, wp);
|
|
|
|
wp += n;
|
2016-04-21 17:03:52 -05:00
|
|
|
}
|
|
|
|
return wstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *toUTF8(const WCHAR *wstr)
|
|
|
|
{
|
|
|
|
char *str;
|
2016-12-03 10:31:11 -06:00
|
|
|
char *sp;
|
|
|
|
size_t n;
|
|
|
|
uint32_t rune;
|
2016-04-21 17:03:52 -05:00
|
|
|
|
|
|
|
if (*wstr == L'\0') // empty string
|
|
|
|
return emptyUTF8();
|
2016-12-03 10:31:11 -06:00
|
|
|
n = utf16RuneCount(wstr, 0);
|
|
|
|
str = (char *) uiAlloc((n + 1) * sizeof (char), "char[]");
|
|
|
|
sp = str;
|
|
|
|
while (*wstr) {
|
|
|
|
wstr = utf16DecodeRune(wstr, &rune);
|
|
|
|
n = utf8EncodeRune(rune, sp);
|
|
|
|
sp += n;
|
2016-04-21 17:03:52 -05:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-04-22 12:01:55 -05:00
|
|
|
WCHAR *utf16dup(const WCHAR *orig)
|
|
|
|
{
|
|
|
|
WCHAR *out;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = wcslen(orig);
|
|
|
|
out = (WCHAR *) uiAlloc((len + 1) * sizeof (WCHAR), "WCHAR[]");
|
2016-05-06 15:06:55 -05:00
|
|
|
wcscpy_s(out, len + 1, orig);
|
2016-04-22 12:01:55 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-05-13 21:05:02 -05:00
|
|
|
WCHAR *strf(const WCHAR *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
str = vstrf(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR *vstrf(const WCHAR *format, va_list ap)
|
2016-04-21 17:03:52 -05:00
|
|
|
{
|
|
|
|
va_list ap2;
|
|
|
|
WCHAR *buf;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (*format == L'\0')
|
|
|
|
return emptyUTF16();
|
|
|
|
|
|
|
|
va_copy(ap2, ap);
|
2016-04-23 23:50:19 -05:00
|
|
|
n = _vscwprintf(format, ap2);
|
2016-04-21 17:03:52 -05:00
|
|
|
va_end(ap2);
|
2016-04-23 23:50:19 -05:00
|
|
|
n++; // terminating L'\0'
|
2016-04-21 17:03:52 -05:00
|
|
|
|
|
|
|
buf = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]");
|
2016-04-23 23:50:19 -05:00
|
|
|
// includes terminating L'\0' according to example in https://msdn.microsoft.com/en-us/library/xa1a1a6z.aspx
|
|
|
|
vswprintf_s(buf, n, format, ap);
|
2016-04-21 17:03:52 -05:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-12-03 10:31:11 -06:00
|
|
|
// TODO merge the following two with the toUTF*()s?
|
|
|
|
|
2016-04-21 17:10:30 -05:00
|
|
|
// Let's shove these utility routines here too.
|
2016-05-06 15:06:55 -05:00
|
|
|
// Prerequisite: lfonly is UTF-8.
|
2016-04-21 17:10:30 -05:00
|
|
|
char *LFtoCRLF(const char *lfonly)
|
|
|
|
{
|
|
|
|
char *crlf;
|
|
|
|
size_t i, len;
|
|
|
|
char *out;
|
|
|
|
|
|
|
|
len = strlen(lfonly);
|
2016-04-23 23:33:42 -05:00
|
|
|
crlf = (char *) uiAlloc((len * 2 + 1) * sizeof (char), "char[]");
|
2016-04-21 17:10:30 -05:00
|
|
|
out = crlf;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (*lfonly == '\n')
|
|
|
|
*crlf++ = '\r';
|
|
|
|
*crlf++ = *lfonly++;
|
|
|
|
}
|
|
|
|
*crlf = '\0';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-05-06 15:06:55 -05:00
|
|
|
// Prerequisite: s is UTF-8.
|
2016-04-21 17:10:30 -05:00
|
|
|
void CRLFtoLF(char *s)
|
|
|
|
{
|
|
|
|
char *t = s;
|
|
|
|
|
2016-05-06 15:06:55 -05:00
|
|
|
for (; *s != '\0'; s++) {
|
2016-04-21 17:10:30 -05:00
|
|
|
// be sure to preserve \rs that are genuinely there
|
|
|
|
if (*s == '\r' && *(s + 1) == '\n')
|
|
|
|
continue;
|
2016-04-23 23:33:42 -05:00
|
|
|
*t++ = *s;
|
2016-04-21 17:10:30 -05:00
|
|
|
}
|
|
|
|
*t = '\0';
|
2016-05-06 15:06:55 -05:00
|
|
|
// pad out the rest of t, just to be safe
|
|
|
|
while (t != s)
|
|
|
|
*t++ = '\0';
|
2016-04-21 17:10:30 -05:00
|
|
|
}
|
2016-05-17 11:35:44 -05:00
|
|
|
|
|
|
|
// std::to_string() always uses %f; we want %g
|
|
|
|
// fortunately std::iostream seems to use %g by default so
|
|
|
|
WCHAR *ftoutf16(double d)
|
|
|
|
{
|
|
|
|
std::wostringstream ss;
|
|
|
|
std::wstring s;
|
|
|
|
|
|
|
|
ss << d;
|
|
|
|
s = ss.str(); // to be safe
|
|
|
|
return utf16dup(s.c_str());
|
|
|
|
}
|
2016-05-17 11:44:43 -05:00
|
|
|
|
|
|
|
// to complement the above
|
2016-06-13 20:47:04 -05:00
|
|
|
WCHAR *itoutf16(int i)
|
2016-05-17 11:44:43 -05:00
|
|
|
{
|
|
|
|
std::wostringstream ss;
|
|
|
|
std::wstring s;
|
|
|
|
|
|
|
|
ss << i;
|
|
|
|
s = ss.str(); // to be safe
|
|
|
|
return utf16dup(s.c_str());
|
|
|
|
}
|