U32 getNumChars() const { if( mNumChars == U32_MAX ) mNumChars = dStrlen( utf16() ); return mNumChars; }
void WINAPI winServiceMain(DWORD, LPTSTR *) { auto serviceName = getServiceName(GetCurrentProcessId()); statusHandle = RegisterServiceCtrlHandler((const wchar_t*)serviceName.utf16(), serviceHandler); if (! statusHandle) { return; } // Service status serviceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(statusHandle, &serviceStatus); // Main function int ret = 1; QString pathstr = getServiceFilePath(serviceName); if (!pathstr.isEmpty()) { auto argList = parseArguments(pathstr); auto *args = new char*[argList.count()]; for (int i = 0; i < argList.count(); i++) { args[i] = argList[i].data(); } ret = managerMain(argList.count(), args); delete[] args; } // Cleanup code must be executed before setting status to SERVICE_STOPPED tSystemInfo("Windows service stopped"); Tf::releaseSystemLogger(); // Service status serviceStatus.dwCurrentState = SERVICE_STOPPED; serviceStatus.dwWin32ExitCode = ret; SetServiceStatus(statusHandle, &serviceStatus); }
void tst_QVersitWriter::testWritingDocument() { QFETCH(QVersitDocument, document); QFETCH(QByteArray, expected); mOutputDevice->open(QBuffer::ReadWrite); mWriter->setDevice(mOutputDevice); QVERIFY2(mWriter->startWriting(document), QString::number(mWriter->error()).toLatin1().data()); QVERIFY2(mWriter->waitForFinished(), QString::number(mWriter->error()).toLatin1().data()); mOutputDevice->seek(0); QByteArray result(mOutputDevice->readAll()); if (result!=expected) qDebug() << result << expected; QCOMPARE(result, expected); // try it again in another codec QTextCodec* utf16(QTextCodec::codecForName("UTF-16")); mWriter->setDefaultCodec(utf16); mOutputDevice->buffer().clear(); mOutputDevice->seek(0); QVERIFY2(mWriter->startWriting(document), QString::number(mWriter->error()).toLatin1().data()); QVERIFY2(mWriter->waitForFinished(), QString::number(mWriter->error()).toLatin1().data()); mOutputDevice->seek(0); result = mOutputDevice->readAll(); expected = utf16->fromUnicode(QString::fromLatin1(expected)); if (result!=expected) qDebug() << result << expected; QCOMPARE(result, expected); }
template<> jsval ScriptInterface::ToJSVal<std::wstring>(JSContext* cx, const std::wstring& val) { utf16string utf16(val.begin(), val.end()); JSString* str = JS_NewUCStringCopyN(cx, reinterpret_cast<const jschar*> (utf16.c_str()), utf16.length()); if (str) return STRING_TO_JSVAL(str); return JSVAL_VOID; }
static jstring getString_native(JNIEnv* env, jobject object, jint row, jint column) { int32_t err; CursorWindow * window = GET_WINDOW(env, object); LOG_WINDOW("Getting string for %d,%d from %p", row, column, window); field_slot_t field; err = window->read_field_slot(row, column, &field); if (err != 0) { throwExceptionWithRowCol(env, row, column); return NULL; } uint8_t type = field.type; if (type == FIELD_TYPE_STRING) { uint32_t size = field.data.buffer.size; if (size > 0) { #if WINDOW_STORAGE_UTF8 // Pass size - 1 since the UTF8 is null terminated and we don't want a null terminator on the UTF16 string String16 utf16((char const *)window->offsetToPtr(field.data.buffer.offset), size - 1); return env->NewString((jchar const *)utf16.string(), utf16.size()); #else return env->NewString((jchar const *)window->offsetToPtr(field.data.buffer.offset), size / 2); #endif } else { return env->NewStringUTF(""); } } else if (type == FIELD_TYPE_INTEGER) { int64_t value; if (window->getLong(row, column, &value)) { char buf[32]; snprintf(buf, sizeof(buf), "%lld", value); return env->NewStringUTF(buf); } return NULL; } else if (type == FIELD_TYPE_FLOAT) { double value; if (window->getDouble(row, column, &value)) { char buf[32]; //selete the print way by code to impove the precision if (((value > 0.0001) && (value < 1000000)) || ((value < -0.0001) && (value > -1000000))) snprintf(buf, sizeof(buf), "%lf", value); else snprintf(buf, sizeof(buf), "%e", value); return env->NewStringUTF(buf); } return NULL; } else if (type == FIELD_TYPE_NULL) { return NULL; } else if (type == FIELD_TYPE_BLOB) { throw_sqlite3_exception(env, "Unable to convert BLOB to string"); return NULL; } else { throwUnknowTypeException(env, type); return NULL; } }
static std::wstring to_utf16(const std::string& utf8) { int size = MultiByteToWideChar(CP_UTF8,0,utf8.c_str(),-1,0,0); boost::scoped_array<wchar_t> utf16(new wchar_t[size]); MultiByteToWideChar(CP_UTF8,0,utf8.c_str(),-1,utf16.get(),size); return std::wstring(utf16.get()); }
template<> void ScriptInterface::ToJSVal<std::wstring>(JSContext* cx, JS::MutableHandleValue ret, const std::wstring& val) { JSAutoRequest rq(cx); utf16string utf16(val.begin(), val.end()); JS::RootedString str(cx, JS_NewUCStringCopyN(cx, reinterpret_cast<const char16_t*> (utf16.c_str()), utf16.length())); if (str) ret.setString(str); else ret.setUndefined(); }
// Test the cases where we should be the same as WebKit's old KURL. TEST(KURLTest, SameGetters) { struct GetterCase { const char* url; const char* protocol; const char* host; int port; const char* user; const char* pass; const char* lastPathComponent; const char* query; const char* ref; bool hasRef; } cases[] = { {"http://www.google.com/foo/blah?bar=baz#ref", "http", "www.google.com", 0, "", 0, "blah", "bar=baz", "ref", true}, {"http://foo.com:1234/foo/bar/", "http", "foo.com", 1234, "", 0, "bar", 0, 0, false}, {"http://www.google.com?#", "http", "www.google.com", 0, "", 0, 0, "", "", true}, {"https://*****:*****@google.com:23#foo", "https", "google.com", 23, "me", "pass", 0, 0, "foo", true}, {"javascript:hello!//world", "javascript", "", 0, "", 0, "world", 0, 0, false}, }; for (size_t i = 0; i < arraysize(cases); i++) { // UTF-8 KURL kurl(ParsedURLString, cases[i].url); EXPECT_EQ(cases[i].protocol, kurl.protocol()); EXPECT_EQ(cases[i].host, kurl.host()); EXPECT_EQ(cases[i].port, kurl.port()); EXPECT_EQ(cases[i].user, kurl.user()); EXPECT_EQ(cases[i].pass, kurl.pass()); EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent()); EXPECT_EQ(cases[i].query, kurl.query()); EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier()); EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier()); // UTF-16 String utf16(cases[i].url); kurl = KURL(ParsedURLString, utf16); EXPECT_EQ(cases[i].protocol, kurl.protocol()); EXPECT_EQ(cases[i].host, kurl.host()); EXPECT_EQ(cases[i].port, kurl.port()); EXPECT_EQ(cases[i].user, kurl.user()); EXPECT_EQ(cases[i].pass, kurl.pass()); EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent()); EXPECT_EQ(cases[i].query, kurl.query()); EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier()); EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier()); } }
size_t Platform_AnsiToUnicode(const char * src, size_t src_len, utf16 * dest, size_t dest_len) { if (!dest) return src_len; // What is commonly refered to ANSI is close enough to Latin 1 codepage to // allow just copy the 8-bit values into the low 8-bits of the UTF values. for (size_t n_chars = std::min(src_len, dest_len); n_chars; --n_chars) *dest++ = utf16(*src++); if (dest_len > src_len) *dest = '\0'; return dest_len; }
JSize JXFontManager::GetStringWidth16 ( const JFontID fontID, const JSize size, const JFontStyle& style, const JCharacter16* str, const JSize charCount ) const { #ifdef _J_USE_XFT XftFont* xftfont = GetXftFont(fontID); XGlyphInfo extents; unsigned short test = 0x00FF; bool bigE = ((const char*)&test)[0] == 0x00; XftTextExtentsUtf16(*itsDisplay, xftfont, (XftChar8*)str, bigE ? FcEndianBig : FcEndianLittle, charCount * sizeof(JCharacter16), &extents); return extents.xOff; #else XFontStruct* xfont = GetXFontInfo(fontID); if (IsMonospace(*xfont)) { return charCount * xfont->min_bounds.width; } else { JString16 utf16(str, charCount); JString str_ascii = utf16.ToASCII(); const JCharacter* _str = str_ascii.GetCString(); JSize actualCount = str_ascii.GetLength(); const JSize maxStringLength = itsDisplay->GetMaxStringLength(); JSize width = 0; JSize offset = 0; while (offset < actualCount) { const JSize count = JMin(actualCount - offset, maxStringLength); width += XTextWidth(xfont, _str + offset, count); offset += count; } return width; } #endif }
/** * Copies UTF-8 characters into the buffer. Returns the number of Java chars * which were buffered. * * @returns number of UTF-16 characters which were copied */ static size_t fillBuffer(ParsingContext* parsingContext, const char* utf8, int byteCount) { JNIEnv* env = parsingContext->env; // Grow buffer if necessary (the length in bytes is always >= the length in chars). jcharArray javaChars = parsingContext->ensureCapacity(byteCount); if (javaChars == NULL) { return -1; } // Decode UTF-8 characters into our char[]. ScopedCharArrayRW chars(env, javaChars); if (chars.get() == NULL) { return -1; } UErrorCode status = U_ZERO_ERROR; UnicodeString utf16(UnicodeString::fromUTF8(StringPiece(utf8, byteCount))); return utf16.extract(chars.get(), byteCount, status); }
static jstring nativeGetString(JNIEnv* env, jclass clazz, jlong windowPtr, jint row, jint column) { CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); LOG_WINDOW("Getting string for %d,%d from %p", row, column, window); CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); if (!fieldSlot) { throwExceptionWithRowCol(env, row, column); return NULL; } int32_t type = window->getFieldSlotType(fieldSlot); if (type == CursorWindow::FIELD_TYPE_STRING) { size_t sizeIncludingNull; const char* value = window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull); if (sizeIncludingNull <= 1) { return gEmptyString; } // Convert to UTF-16 here instead of calling NewStringUTF. NewStringUTF // doesn't like UTF-8 strings with high codepoints. It actually expects // Modified UTF-8 with encoded surrogate pairs. String16 utf16(value, sizeIncludingNull - 1); return env->NewString(reinterpret_cast<const jchar*>(utf16.string()), utf16.size()); } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { int64_t value = window->getFieldSlotValueLong(fieldSlot); char buf[32]; snprintf(buf, sizeof(buf), "%" PRId64, value); return env->NewStringUTF(buf); } else if (type == CursorWindow::FIELD_TYPE_FLOAT) { double value = window->getFieldSlotValueDouble(fieldSlot); char buf[32]; snprintf(buf, sizeof(buf), "%g", value); return env->NewStringUTF(buf); } else if (type == CursorWindow::FIELD_TYPE_NULL) { return NULL; } else if (type == CursorWindow::FIELD_TYPE_BLOB) { throw_sqlite3_exception(env, "Unable to convert BLOB to string"); return NULL; } else { throwUnknownTypeException(env, type); return NULL; } }
/** * Converts an UTF16 encoded string into an UTF8 encoded string. * \param[in] unicode The UTF16 encoded string. * \param[in] size The size to convert. * \return A newly created string. */ xlui::string xlui::string::fromUTF16(wchar_t *unicode, int32_t size) { std::wstring utf16(unicode, size); xlui::string utf8; utf8::utf16to8(utf16.begin(), utf16.end(), back_inserter(utf8)); return utf8; }
static jcharArray copyStringToBuffer_native(JNIEnv* env, jobject object, jint row, jint column, jint bufferSize, jobject buf) { int32_t err; CursorWindow * window = GET_WINDOW(env, object); LOG_WINDOW("Copying string for %d,%d from %p", row, column, window); field_slot_t field; err = window->read_field_slot(row, column, &field); if (err != 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to get field slot"); return NULL; } jcharArray buffer = (jcharArray)env->GetObjectField(buf, gBufferField); if (buffer == NULL) { jniThrowException(env, "java/lang/IllegalStateException", "buf should not be null"); return NULL; } jchar* dst = env->GetCharArrayElements(buffer, NULL); uint8_t type = field.type; uint32_t sizeCopied = 0; jcharArray newArray = NULL; if (type == FIELD_TYPE_STRING) { uint32_t size = field.data.buffer.size; if (size > 0) { #if WINDOW_STORAGE_UTF8 // Pass size - 1 since the UTF8 is null terminated and we don't want a null terminator on the UTF16 string String16 utf16((char const *)window->offsetToPtr(field.data.buffer.offset), size - 1); int32_t strSize = utf16.size(); if (strSize > bufferSize || dst == NULL) { newArray = env->NewCharArray(strSize); env->SetCharArrayRegion(newArray, 0, strSize, (jchar const *)utf16.string()); } else { memcpy(dst, (jchar const *)utf16.string(), strSize * 2); } sizeCopied = strSize; #else sizeCopied = size/2 + size % 2; if (size > bufferSize * 2 || dst == NULL) { newArray = env->NewCharArray(sizeCopied); memcpy(newArray, (jchar const *)window->offsetToPtr(field.data.buffer.offset), size); } else { memcpy(dst, (jchar const *)window->offsetToPtr(field.data.buffer.offset), size); } #endif } } else if (type == FIELD_TYPE_INTEGER) { int64_t value; if (window->getLong(row, column, &value)) { char buf[32]; int len; snprintf(buf, sizeof(buf), "%lld", value); jchar* dst = env->GetCharArrayElements(buffer, NULL); sizeCopied = charToJchar(buf, dst, bufferSize); } } else if (type == FIELD_TYPE_FLOAT) { double value; if (window->getDouble(row, column, &value)) { char tempbuf[32]; snprintf(tempbuf, sizeof(tempbuf), "%g", value); jchar* dst = env->GetCharArrayElements(buffer, NULL); sizeCopied = charToJchar(tempbuf, dst, bufferSize); } } else if (type == FIELD_TYPE_NULL) { } else if (type == FIELD_TYPE_BLOB) { throw_sqlite3_exception(env, "Unable to convert BLOB to string"); } else { LOGE("Unknown field type %d", type); throw_sqlite3_exception(env, "UNKNOWN type in copyStringToBuffer_native()"); } SET_SIZE_COPIED(env, buf, sizeCopied); env->ReleaseCharArrayElements(buffer, dst, JNI_OK); return newArray; }
const wchar_t *Unicode::path() { return utf16(); }