diff options
author | Michael Meeks <michael.meeks@collabora.com> | 2019-11-04 15:44:04 +0000 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2019-11-04 17:43:20 +0100 |
commit | 5dff78b49f489aaa552384e14225a798a8a093a6 (patch) | |
tree | 3ae0be9e9146075a95fcbb58363f90ced8893a00 /desktop | |
parent | ae1052613be96be1ec37695b094906edb41ac8cf (diff) |
lok: share conversion of OUString to C strings.
Change-Id: I0178f673dc1e59d9fba8f3daae532f19b46e4d36
Reviewed-on: https://gerrit.libreoffice.org/82036
Tested-by: Jenkins
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/source/lib/init.cxx | 85 |
1 files changed, 24 insertions, 61 deletions
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index b00638eaa127..ef201b32061e 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -260,6 +260,20 @@ static OUString getUString(const char* pString) return OStringToOUString(sString, RTL_TEXTENCODING_UTF8); } +// Tolerate embedded \0s etc. +static char *convertOString(const OString &rStr) +{ + char* pMemory = static_cast<char*>(malloc(rStr.getLength() + 1)); + assert(pMemory); // don't tolerate failed allocations. + memcpy(pMemory, rStr.getStr(), rStr.getLength() + 1); + return pMemory; +} + +static char *convertOUString(const OUString &aStr) +{ + return convertOString(OUStringToOString(aStr, RTL_TEXTENCODING_UTF8)); +} + /// Try to convert a relative URL to an absolute one, unless it already looks like a URL. static OUString getAbsoluteURL(const char* pURL) { @@ -2544,13 +2558,7 @@ static char* doc_getPartInfo(LibreOfficeKitDocument* pThis, int nPart) return nullptr; } - OUString aPartInfo = pDoc->getPartInfo( nPart ); - OString aString = OUStringToOString(aPartInfo, RTL_TEXTENCODING_UTF8); - - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; + return convertOUString(pDoc->getPartInfo(nPart)); } static void doc_selectPart(LibreOfficeKitDocument* pThis, int nPart, int nSelect) @@ -2599,13 +2607,7 @@ static char* doc_getPartPageRectangles(LibreOfficeKitDocument* pThis) return nullptr; } - OUString sRectangles = pDoc->getPartPageRectangles(); - OString aString = OUStringToOString(sRectangles, RTL_TEXTENCODING_UTF8); - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; - + return convertOUString(pDoc->getPartPageRectangles()); } static char* doc_getPartName(LibreOfficeKitDocument* pThis, int nPart) @@ -2622,13 +2624,7 @@ static char* doc_getPartName(LibreOfficeKitDocument* pThis, int nPart) return nullptr; } - OUString sName = pDoc->getPartName( nPart ); - OString aString = OUStringToOString(sName, RTL_TEXTENCODING_UTF8); - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; - + return convertOUString(pDoc->getPartName(nPart)); } static char* doc_getPartHash(LibreOfficeKitDocument* pThis, int nPart) @@ -2645,13 +2641,7 @@ static char* doc_getPartHash(LibreOfficeKitDocument* pThis, int nPart) return nullptr; } - OUString sHash = pDoc->getPartHash(nPart); - OString aString = OUStringToOString(sHash, RTL_TEXTENCODING_UTF8); - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; - + return convertOUString(pDoc->getPartHash(nPart)); } static void doc_setPartMode(LibreOfficeKitDocument* pThis, @@ -3752,14 +3742,6 @@ static bool getFromTransferrable( return true; } -// Tolerate embedded \0s etc. -static char *convertOString(const OString &rStr) -{ - char* pMemory = static_cast<char*>(malloc(rStr.getLength() + 1)); - memcpy(pMemory, rStr.getStr(), rStr.getLength() + 1); - return pMemory; -} - static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMimeType, char** pUsedMimeType) { comphelper::ProfileZone aZone("doc_getTextSelection"); @@ -3914,7 +3896,7 @@ static int doc_getClipboard(LibreOfficeKitDocument* pThis, else { (*pOutSizes)[i] = aRet.getLength(); - (*pOutStreams)[i] = convertOString(aRet); + (*pOutStreams)[i] = convertOString(aRet); } } @@ -4490,13 +4472,8 @@ static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCo OUString aHeaders = pDoc->getRowColumnHeaders(aRectangle); if (aHeaders.isEmpty()) return nullptr; - - OString aString = OUStringToOString(aHeaders, RTL_TEXTENCODING_UTF8); - - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; + else + return convertOUString(aHeaders); } else if (aCommand.startsWith(aCellCursor)) { @@ -4543,12 +4520,7 @@ static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCo while (nParamIndex >= 0); } - OString aString = pDoc->getCellCursor(nOutputWidth, nOutputHeight, nTileWidth, nTileHeight); - - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; + return convertOString(pDoc->getCellCursor(nOutputWidth, nOutputHeight, nTileWidth, nTileHeight)); } else if (aCommand.startsWith(aFontSubset)) { @@ -5110,11 +5082,7 @@ static char* lo_getError (LibreOfficeKit *pThis) SolarMutexGuard aGuard; LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis); - OString aString = OUStringToOString(pLib->maLastExceptionMsg, RTL_TEXTENCODING_UTF8); - char* pMemory = static_cast<char*>(malloc(aString.getLength() + 1)); - assert(pMemory); // Don't handle OOM conditions - strcpy(pMemory, aString.getStr()); - return pMemory; + return convertOUString(pLib->maLastExceptionMsg); } static void lo_freeError(char* pFree) @@ -5206,12 +5174,7 @@ static char* lo_getVersionInfo(SAL_UNUSED_PARAMETER LibreOfficeKit* /*pThis*/) "\"BuildId\": \"%BUILDID\" " "}" ); - const OString sVersionStr = OUStringToOString(ReplaceStringHookProc(sVersionStrTemplate), RTL_TEXTENCODING_UTF8); - - char* pVersion = static_cast<char*>(malloc(sVersionStr.getLength() + 1)); - assert(pVersion); // Don't handle OOM conditions - strcpy(pVersion, sVersionStr.getStr()); - return pVersion; + return convertOUString(ReplaceStringHookProc(sVersionStrTemplate)); } static void force_c_locale() |