本文整理汇总了C++中nsCOMPtr::GetMaxLength方法的典型用法代码示例。如果您正苦于以下问题:C++ nsCOMPtr::GetMaxLength方法的具体用法?C++ nsCOMPtr::GetMaxLength怎么用?C++ nsCOMPtr::GetMaxLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nsCOMPtr
的用法示例。
在下文中一共展示了nsCOMPtr::GetMaxLength方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ConvertPlatformPlainTextToUnicode
//
// ConvertPlatformPlainTextToUnicode
//
// Given a char buffer (flavor text/plaikn), this converts it to unicode using
// the appropriate platform charset encoding. |outUnicode| is null terminated,
// but its length parameter, |outUnicodeLen|, does not reflect that. |outUnicodeLen| is
// the length of the string in characters, not bytes.
//
nsresult
nsPrimitiveHelpers :: ConvertPlatformPlainTextToUnicode ( const char* inText, int32_t inTextLen,
PRUnichar** outUnicode, int32_t* outUnicodeLen )
{
if ( !outUnicode || !outUnicodeLen )
return NS_ERROR_INVALID_ARG;
// Get the appropriate unicode decoder. We're guaranteed that this won't change
// through the life of the app so we can cache it.
nsresult rv = NS_OK;
static nsCOMPtr<nsIUnicodeDecoder> decoder;
static bool hasConverter = false;
if ( !hasConverter ) {
// get the charset
nsAutoCString platformCharset;
nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
if (NS_FAILED(rv))
platformCharset.AssignLiteral("ISO-8859-1");
// get the decoder
nsCOMPtr<nsICharsetConverterManager> ccm =
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
rv = ccm->GetUnicodeDecoderRaw(platformCharset.get(),
getter_AddRefs(decoder));
NS_ASSERTION(NS_SUCCEEDED(rv), "GetUnicodeEncoderRaw failed.");
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;
hasConverter = true;
}
// Estimate out length and allocate the buffer based on a worst-case estimate, then do
// the conversion.
decoder->GetMaxLength(inText, inTextLen, outUnicodeLen); // |outUnicodeLen| is number of chars
if ( *outUnicodeLen ) {
*outUnicode = reinterpret_cast<PRUnichar*>(nsMemory::Alloc((*outUnicodeLen + 1) * sizeof(PRUnichar)));
if ( *outUnicode ) {
rv = decoder->Convert(inText, &inTextLen, *outUnicode, outUnicodeLen);
(*outUnicode)[*outUnicodeLen] = '\0'; // null terminate. Convert() doesn't do it for us
}
} // if valid length
NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting plain text to unicode" );
return rv;
} // ConvertPlatformPlainTextToUnicode
示例2: ConvertPlatformPlainTextToUnicode
//
// ConvertPlatformPlainTextToUnicode
//
// Given a char buffer (flavor text/plaikn), this converts it to unicode using
// the appropriate platform charset encoding. |outUnicode| is null terminated,
// but its length parameter, |outUnicodeLen|, does not reflect that. |outUnicodeLen| is
// the length of the string in characters, not bytes.
//
nsresult
nsPrimitiveHelpers :: ConvertPlatformPlainTextToUnicode ( const char* inText, int32_t inTextLen,
char16_t** outUnicode, int32_t* outUnicodeLen )
{
if ( !outUnicode || !outUnicodeLen )
return NS_ERROR_INVALID_ARG;
// Get the appropriate unicode decoder. We're guaranteed that this won't change
// through the life of the app so we can cache it.
nsresult rv = NS_OK;
static nsCOMPtr<nsIUnicodeDecoder> decoder;
static bool hasConverter = false;
if ( !hasConverter ) {
// get the charset
nsAutoCString platformCharset;
nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
if (NS_FAILED(rv))
platformCharset.AssignLiteral("windows-1252");
decoder = EncodingUtils::DecoderForEncoding(platformCharset);
hasConverter = true;
}
// Estimate out length and allocate the buffer based on a worst-case estimate, then do
// the conversion.
rv = decoder->GetMaxLength(inText, inTextLen, outUnicodeLen); // |outUnicodeLen| is number of chars
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if ( *outUnicodeLen ) {
*outUnicode = reinterpret_cast<char16_t*>(moz_xmalloc((*outUnicodeLen + 1) * sizeof(char16_t)));
if ( *outUnicode ) {
rv = decoder->Convert(inText, &inTextLen, *outUnicode, outUnicodeLen);
(*outUnicode)[*outUnicodeLen] = '\0'; // null terminate. Convert() doesn't do it for us
}
} // if valid length
NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting plain text to unicode" );
return rv;
} // ConvertPlatformPlainTextToUnicode