nsEmbedString is to be used when you need to pass a string to a XPCOM method expecting a |const nsAString&|. you can used nsEmbedString like this:
{
const PRUnichar data[] = {'h','e','l','l','o','\0'};
nsEmbedString s(data);nsCOMPtr<nsIFoo> foo = do_CreateInstance(NS_FOO_CONTRACTID); foo->SetData(s); ... }
Assuming, nsIFoo is some XPCOM interface with a SetData(const nsAString&) method.
I think the tricky thing is generating the PRUnichar array under Linux. wchar_t is typically not compatible with PRUnichar under Linux. In fact, wchar_t is commonly UTF-32, while PRUnichar is UTF-16. You therefore need to write your own code to convert from UTF-32 to UTF-16.
I think the code bsmedberg showed you would work if you only care about supporting the UCS-2 subset of UTF-16. Under Linux, you can use the iconv function to convert strings from one charset to another.
Attached is some sample code that you can use to convert between wchar_t arrays and nsAString objects. I've not tested this code extensively, so I don't make any promises that it is correct. But, it seems to work for me.
NOTE: This sample code requires a recent Mozilla trunk. It won't work with any existing Mozilla releases. However, it should not be too hard to make it work with an older release.
-Darin
banli wrote:
Under linux, is there some c++ API which can output string of type nsEmbedString to file? So that I don't need the conversion when I use "fputws"?
In the cxc book, nsEmbedString and nsEmbedCString are mentioned. In the string-guide document, it mentions that "nsString or nsSharableString for member variables". But in the mozilla source code, nsString is under "/xpcom/string/obsolete" directory. So if I need some string variable to feed into some interface method to get return value of type of nsAString, which string type I should choose to declare string variable? thanks!
"Benjamin D. Smedberg" <[EMAIL PROTECTED]> wrote news:[EMAIL PROTECTED]
Unfortunately, there is not a way to to UTF16->UTF8 conversions without linking to non-frozen stuff (yet). Darin and I are trying to figure out the minimum necessary linkage to accomplish these kinds of tasks.
You can't cast PRUnichar* to wchar_t* because on most *ixes, wchar_t is a four-byte type. However, you can safely do a funky copy-conversion to a wchar_t type:
PRUnichar *start = nsStr.get(); PRUnichar *end nsStr.get() + nsStr.Length(); wchar_t *wstart = new wchar_t[nsStr.Length()]; wchar_t *wend = start + nsStr.Length();
for(; start < end; ++start) { *wstart = (wchar_t) *start; ++wstart; } *wstart = 0;
There are also some interfaces in intl (charset conversion) that might do the trick for you.
--BDS
test_iconv.tar.gz
Description: Unix tar archive
