#include "Xpclucene.h" #include "nsString.h" #include "nsLuceneStringConverter.h" /* * On linux and macosx, clucene is compiled with 4-byte wide characters. Since * mozilla uses -fshort-wchar, the strings are not compatible. I have been * unable to get clucene to work when compiled with -fshort-wchar, so this * class handles the conversion to and from 2-byte to 4 byte character strings. * For other platforms, this just copies the memory around directly * * See the definition of utf32_t in Xpclucene.h */ nsLuceneStringConverter::nsLuceneStringConverter(const nsAString& s) { const nsPromiseFlatString &tString = PromiseFlatString(s); PRInt32 length = tString.Length(); #if defined(LINUX) || defined(XP_MACOSX) mBuffer = NS_STATIC_CAST(utf32_t*, nsMemory::Alloc((length + 1) * sizeof(PRUnichar) * 2)); #else mBuffer = NS_STATIC_CAST(utf32_t*, nsMemory::Alloc((length + 1) * sizeof(PRUnichar))); #endif if (mBuffer) { const PRUnichar* source = tString.get(); utf32_t* dest = mBuffer; PRInt32 i = 0; while(source[i] != '\0') { dest[i] = source[i]; i++; } dest[i] = '\0'; } } nsLuceneStringConverter::~nsLuceneStringConverter() { if(mBuffer) { nsMemory::Free(mBuffer); } } utf32_t* nsLuceneStringConverter::get() { return mBuffer; } void nsLuceneStringConverter::makeNsString(utf32_t* data, nsAString& string) { int length = 0; utf32_t* p = data; while(*p) { length++; *p++; } PRUnichar* buffer = NS_STATIC_CAST(PRUnichar*, nsMemory::Alloc((length + 1) * sizeof(PRUnichar))); // TODO: check for null... possibly return an NS error? utf32_t* source = data; PRUnichar* dest = buffer; for(PRInt32 i = 0; i < length; i++) { dest[i] = source[i]; } dest[length] = '\0'; string.Assign(buffer); nsMemory::Free(buffer); }