convert int to nsEmbedString

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

convert int to nsEmbedString

martin.koppmann
Can anybody tell me how to convert an int value to a nsEmbedString?
I need to use this to set the value of a DOM progress bar:
mProgressBar->SetAttribute("value", "45%");

// the way I did this in earlier
TCHAR szValue[5];
wsprintf(szValue, TEXT("%li%%"), *Value);
mProgressBar->SetAttribute(NS_LITERAL_STRING("value"),
NS_ConvertUTF8toUCS2(szValue));

But now I need to use fronzen string API with nsEmbedString, which does
not offer NS_ConvertUTF8toUCS2.

How can I construct an nsEmbedString with an int plus "%".

Thank you very much!

Martin

_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

Benjamin Smedberg
[hidden email] wrote:
> Can anybody tell me how to convert an int value to a nsEmbedString?
> I need to use this to set the value of a DOM progress bar:
> mProgressBar->SetAttribute("value", "45%");
>
> // the way I did this in earlier
> TCHAR szValue[5];
> wsprintf(szValue, TEXT("%li%%"), *Value);
> mProgressBar->SetAttribute(NS_LITERAL_STRING("value"),
> NS_ConvertUTF8toUCS2(szValue));

Are you compiling with _UNICODE defined or not? I can't see how this would
compile properly, since you have a wide-char buffer, but you're treating it
as UTF8 (which is a narrow-char buffer).

> But now I need to use fronzen string API with nsEmbedString, which does
> not offer NS_ConvertUTF8toUCS2.
>
> How can I construct an nsEmbedString with an int plus "%".

If your code is windows-only:

wchar wValue[10];
wsprintf(wValue, L"%li%%"), myint32);
mProgressBar->SetAttribute(NS_LITERAL_STRING("value"),
nsDependentString(wValue));

If your code needs to be cross-platform (wsprintf not available):

char cValue[10];
sprintf(cValue, "%i%%", myint32);
nsEmbedString wide;
NS_CStringToUTF16(nsDependentCString(cValue), NS_CSTRING_ENCODING_ASCII, wide);
mProgressBar->SetAttribute(NS_LITERAL_STRING("value"), wide);

--BDS
_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

Christian Biesinger
In reply to this post by martin.koppmann
[hidden email] wrote:
> // the way I did this in earlier
> TCHAR szValue[5];
> wsprintf(szValue, TEXT("%li%%"), *Value);
> mProgressBar->SetAttribute(NS_LITERAL_STRING("value"),
> NS_ConvertUTF8toUCS2(szValue));
>
> But now I need to use fronzen string API with nsEmbedString, which does
> not offer NS_ConvertUTF8toUCS2.

It does offer NS_CStringToUTF16...
http://lxr.mozilla.org/seamonkey/source/xpcom/string/public/nsStringAPI.h#774


If you use Windows-specific APIs anyway, you could just use wsprintfW
(unless you need compatibility with win9x/ME, which I don't think offer
this function):
   WCHAR szValue[12];
   wsprintfW(szValue, L"%li%%", *Value);
   nsDependentString value(szValue);

-biesi
_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

Benjamin Smedberg
In reply to this post by Benjamin Smedberg
Benjamin Smedberg wrote:

> If your code is windows-only:
>
> wchar wValue[10];
> wsprintf(wValue, L"%li%%"), myint32);

oops, I got confused between "wsprintf" and "swprintf", I meant the latter.

--BDS
_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

martin.koppmann
In reply to this post by Benjamin Smedberg
Hi Benjamin,

Thanks, your cross-platform version compiled fine.

Now only the linker gives me one last error: Unresolved external for
nsCreateInstanceByContractID::nsCreateInstanceByContractID(char const
*,class nsISupports *,unsigned int *)

I am linking against: nspr4.lib embed_base_s.lib xpcom.lib
xpcomglue_s.lib js3250.lib

According to http://developer.mozilla.org/en/docs/XPCOM_Glue
I want to use frozen linkage (dependent on xpcom.dll).

Can you give me a tipp?

Thanks again,
Martin

_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

Benjamin Smedberg
[hidden email] wrote:

> Hi Benjamin,
>
> Thanks, your cross-platform version compiled fine.
>
> Now only the linker gives me one last error: Unresolved external for
> nsCreateInstanceByContractID::nsCreateInstanceByContractID(char const
> *,class nsISupports *,unsigned int *)
>
> I am linking against: nspr4.lib embed_base_s.lib xpcom.lib
> xpcomglue_s.lib js3250.lib

Interesting... I'm pretty sure that symbol *should* be available in
xpcomglue_s.lib... actually reading the code now, that form uses the wrong
macro to declare linking :-(

I'll try to get this fixed for final, but if you want to fix it locally,
look for nsComponentManagerUtils.h and replace all instances of NS_COM with
NS_COM_GLUE.

--BDS
_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

martin.koppmann
Yipppiieeeh! If have got working XPCOM dll now. Works in FF 1.5b2.
THANK YOU

As you suggested I only replaced in nsComponentManagerUtils.h:
class NS_COM nsCreateInstanceByContractID : public nsCOMPtr_helper
to
class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper

One last question:
Against which gecko-sdk version should I compile to achieve most
compatibilty.
I want to use frozen API plus XPCOM_STRICT_API. Since which version
have they been frozen?

Martin

_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom
Reply | Threaded
Open this post in threaded view
|

Re: convert int to nsEmbedString

Benjamin Smedberg
[hidden email] wrote:

> One last question:
> Against which gecko-sdk version should I compile to achieve most
> compatibilty.
> I want to use frozen API plus XPCOM_STRICT_API. Since which version
> have they been frozen?

You should use the gecko SDK from the earliest release of Mozilla/Firefox
that you want to support. For example, if you are building an extension that
you want to work in Firefox 1.0 and 1.5, you should use the SDK from gecko
1.7. If you just want things to work in Firefox 1.5 and later, use the SDK
from gecko 1.8. We add new features to the SDK in each release, so the 1.8
sdk is going to be much more powerful than the 1.7 SDK, and the 1.4 sdk is
going to be almost useless ;-)

--BDS
_______________________________________________
Mozilla-xpcom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-xpcom