Changed to cache unicode decoder, bug 68826, r=yokoyama, sr=erik.

This commit is contained in:
nhotta%netscape.com 2001-02-22 23:27:53 +00:00
parent dac2cb33da
commit cee5e4461c
4 changed files with 56 additions and 50 deletions

View File

@ -22,7 +22,6 @@
*/
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsDateTimeFormatMac.h"
#include <Resources.h>
#include <IntlResources.h>
@ -35,7 +34,6 @@
#include "nsIPlatformCharset.h"
#include "nsIMacLocale.h"
#include "nsCRT.h"
#include "nsCOMPtr.h"
#include "plstr.h"
#include "prmem.h"
@ -301,6 +299,18 @@ nsresult nsDateTimeFormatMac::Initialize(nsILocale* locale)
res = platformCharset->GetCharset(kPlatformCharsetSel_FileName, mSystemCharset);
}
}
// Initialize unicode decoder
nsCOMPtr <nsIAtom> charsetAtom;
nsCOMPtr <nsICharsetConverterManager2> charsetConverterManager;
charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
res = charsetConverterManager->GetCharsetAtom(mUseDefaultLocale ? mSystemCharset.GetUnicode() : mCharset.GetUnicode(),
getter_AddRefs(charsetAtom));
if (NS_SUCCEEDED(res)) {
res = charsetConverterManager->GetUnicodeDecoder(charsetAtom, getter_AddRefs(mDecoder));
}
}
return res;
}
@ -403,40 +413,29 @@ nsresult nsDateTimeFormatMac::FormatTMTime(nsILocale* locale,
}
// construct a C string
char *aBuffer;
char *localBuffer;
if (dateFormatSelector != kDateFormatNone && timeFormatSelector != kTimeFormatNone) {
aBuffer = p2cstr(dateString);
strcat(aBuffer, " ");
strcat(aBuffer, p2cstr(timeString));
localBuffer = p2cstr(dateString);
strcat(localBuffer, " ");
strcat(localBuffer, p2cstr(timeString));
}
else if (dateFormatSelector != kDateFormatNone) {
aBuffer = p2cstr(dateString);
localBuffer = p2cstr(dateString);
}
else if (timeFormatSelector != kTimeFormatNone) {
aBuffer = p2cstr(timeString);
localBuffer = p2cstr(timeString);
}
// convert result to unicode
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &res);
if(NS_SUCCEEDED(res) && ccm) {
nsCOMPtr <nsIUnicodeDecoder> decoder;
res = ccm->GetUnicodeDecoder(mUseDefaultLocale ? &mSystemCharset : &mCharset, getter_AddRefs(decoder));
if(NS_SUCCEEDED(res) && decoder) {
PRInt32 unicharLength = 0;
PRInt32 srcLength = (PRInt32) PL_strlen(aBuffer);
res = decoder->GetMaxLength(aBuffer, srcLength, &unicharLength);
PRUnichar *unichars = new PRUnichar [ unicharLength ];
if (nsnull != unichars) {
res = decoder->Convert(aBuffer, &srcLength,
unichars, &unicharLength);
if (NS_SUCCEEDED(res)) {
stringOut.Assign(unichars, unicharLength);
}
}
delete [] unichars;
}
if (mDecoder) {
PRInt32 srcLength = (PRInt32) PL_strlen(localBuffer);
PRInt32 unicharLength = sizeof(Str255)*2;
PRUnichar unichars[sizeof(Str255)*2]; // buffer for combined date and time
res = mDecoder->Convert(localBuffer, &srcLength, unichars, &unicharLength);
if (NS_SUCCEEDED(res)) {
stringOut.Assign(unichars, unicharLength);
}
}
return res;

View File

@ -24,6 +24,9 @@
#define nsDateTimeFormatMac_h__
#include "nsICharsetConverterManager.h"
#include "nsICharsetConverterManager2.h"
#include "nsCOMPtr.h"
#include "nsIDateTimeFormat.h"
@ -75,6 +78,7 @@ private:
short mLangcode;
short mRegioncode;
bool mUseDefaultLocale;
nsCOMPtr <nsIUnicodeDecoder> mDecoder;
};
#endif /* nsDateTimeFormatMac_h__ */

View File

@ -24,14 +24,12 @@
#include <locale.h>
#include "plstr.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsDateTimeFormatUnix.h"
#include "nsIComponentManager.h"
#include "nsLocaleCID.h"
#include "nsILocaleService.h"
#include "nsIPlatformCharset.h"
#include "nsIPosixLocale.h"
#include "nsCOMPtr.h"
#include "nsCRT.h"
static NS_DEFINE_IID(kIDateTimeFormatIID, NS_IDATETIMEFORMAT_IID);
@ -110,6 +108,17 @@ nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale)
}
}
// Initialize unicode decoder
nsCOMPtr <nsIAtom> charsetAtom;
nsCOMPtr <nsICharsetConverterManager2> charsetConverterManager;
charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
res = charsetConverterManager->GetCharsetAtom(mCharset.GetUnicode(), getter_AddRefs(charsetAtom));
if (NS_SUCCEEDED(res)) {
res = charsetConverterManager->GetUnicodeDecoder(charsetAtom, getter_AddRefs(mDecoder));
}
}
mLocalePreferred24hour = LocalePreferred24hour();
return res;
@ -158,7 +167,7 @@ nsresult nsDateTimeFormatUnix::FormatTMTime(nsILocale* locale,
nsString& stringOut)
{
#define NSDATETIME_FORMAT_BUFFER_LEN 80
char strOut[NSDATETIME_FORMAT_BUFFER_LEN];
char strOut[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time
char fmtD[NSDATETIME_FORMAT_BUFFER_LEN], fmtT[NSDATETIME_FORMAT_BUFFER_LEN];
nsresult res;
@ -232,25 +241,15 @@ nsresult nsDateTimeFormatUnix::FormatTMTime(nsILocale* locale,
(void) setlocale(LC_TIME, old_locale);
// convert result to unicode
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &res);
if(NS_SUCCEEDED(res) && ccm) {
nsCOMPtr<nsIUnicodeDecoder> decoder;
res = ccm->GetUnicodeDecoder(&mCharset, getter_AddRefs(decoder));
if (NS_SUCCEEDED(res) && decoder) {
PRInt32 unicharLength = 0;
PRInt32 srcLength = (PRInt32) PL_strlen(strOut);
res = decoder->GetMaxLength(strOut, srcLength, &unicharLength);
PRUnichar *unichars = new PRUnichar [ unicharLength ];
if (nsnull != unichars) {
res = decoder->Convert(strOut, &srcLength,
unichars, &unicharLength);
if (NS_SUCCEEDED(res)) {
stringOut.Assign(unichars, unicharLength);
}
}
delete [] unichars;
}
if (mDecoder) {
PRInt32 srcLength = (PRInt32) PL_strlen(strOut);
PRInt32 unicharLength = NSDATETIME_FORMAT_BUFFER_LEN*2;
PRUnichar unichars[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time
res = mDecoder->Convert(strOut, &srcLength, unichars, &unicharLength);
if (NS_SUCCEEDED(res)) {
stringOut.Assign(unichars, unicharLength);
}
}
return res;

View File

@ -24,6 +24,9 @@
#define nsDateTimeFormatUnix_h__
#include "nsICharsetConverterManager.h"
#include "nsICharsetConverterManager2.h"
#include "nsCOMPtr.h"
#include "nsIDateTimeFormat.h"
#define kPlatformLocaleLength 64
@ -78,6 +81,7 @@ private:
nsString mCharset; // in order to convert API result to unicode
char mPlatformLocale[kPlatformLocaleLength+1]; // for setlocale
PRBool mLocalePreferred24hour; // true if 24 hour format is preferred by current locale
nsCOMPtr <nsIUnicodeDecoder> mDecoder;
};
#endif /* nsDateTimeFormatUnix_h__ */