From: Marcin 'Malcom' Malich on
On 9 Kwi, 11:00, "Declan McMullen" <declan.mcmul...(a)gmail.com> wrote:

> string Utility::wx2std(wxString s)
> {
>     char * newString = NULL;
>     int length = s.Length();
>     newString = new char[length];
>     return strcpy( newString, (const char*)s.mb_str(wxConvUTF8) );
>
> }
>

memory leak

std::string Utility::wx2std(wxString s)
{
return std::string(s.mb_str(wxConvUTF8), s.Length());
}

--
malcom
http://malcom.pl
From: Milan Babuskov on
Declan McMullen wrote:
> Any tips on how I should do it ?

We are using the following in FlameRobin:

//-----------------------------------------------------------------------------
std::string wx2std(const wxString& input, wxMBConv* conv = 0)
{
if (input.empty())
return "";
if (!conv)
conv = wxConvCurrent;
const wxWX2MBbuf buf(input.mb_str(*conv));
// conversion may fail and return 0,
// which isn't a safe value to pass
// to std:string constructor
if (!buf)
return "";
return std::string(buf);
}
//-----------------------------------------------------------------------------
wxString std2wx(const std::string& input, wxMBConv* conv = 0)
{
if (input.empty())
return wxEmptyString;
if (!conv)
conv = wxConvCurrent;
return wxString(input.c_str(), *conv);
}
//-----------------------------------------------------------------------------

Any comments are appreciated.

--
Milan Babuskov
http://www.flamerobin.org
From: Robert Roebling on

Marcin 'Malcom' Malich wrote:
>
> Declan McMullen wrote:
>
> > string Utility::wx2std(wxString s)
> > {
> > char * newString = NULL;
> > int length = s.Length();
> > newString = new char[length];
> > return strcpy( newString, (const char*)s.mb_str(wxConvUTF8) );
> >
> > }
> >
>
> memory leak
>
> std::string Utility::wx2std(wxString s)
> {
> return std::string(s.mb_str(wxConvUTF8), s.Length());
> }

wxString::Length() returns the number characters in the
unconverted string, not the number of bytes so this will
only work for ASCII characters. Why don't you simply use:

wxString s1 = "test";
std::string s2 = s1.utf8_str();
// or
std::wstring s2 = s1.wc_str();

or is there something wrong with this?

Robert


From: Milan Babuskov on
Vadim Zeitlin wrote:
> MB> //-----------------------------------------------------------------------------
> MB> std::string wx2std(const wxString& input, wxMBConv* conv = 0)
> MB> {
> MB> if (input.empty())
> MB> return "";
> MB> if (!conv)
> MB> conv = wxConvCurrent;
> MB> const wxWX2MBbuf buf(input.mb_str(*conv));
> MB> // conversion may fail and return 0,
> MB> // which isn't a safe value to pass
> MB> // to std:string constructor
> MB> if (!buf)
> MB> return "";
> MB> return std::string(buf);
> MB> }
> MB> //-----------------------------------------------------------------------------
> MB> wxString std2wx(const std::string& input, wxMBConv* conv = 0)
> MB> {
> MB> if (input.empty())
> MB> return wxEmptyString;
> MB> if (!conv)
> MB> conv = wxConvCurrent;
> MB> return wxString(input.c_str(), *conv);
> MB> }
> MB> //-----------------------------------------------------------------------------
> MB>
> MB> Any comments are appreciated.
>
> This code is correct although I'm not sure why do you think it's necessary
> to test for empty string explicitly, normally you should be able to remove
> the checks for input.empty() without any ill effects.

We used to have some more complex code that allocated buffers and such,
so if string is empty it seemed efficient to not do all that. I guess it
could be removed now.

Thanks,

--
Milan Babuskov
http://www.flamerobin.org
From: Declan McMullen on
Cheers for that.

Could you tell me what the second parameter is doing? And should I be
passing a value to it or leave it defaulting to 0 ?

On Wed, Apr 9, 2008 at 11:12 AM, Milan Babuskov <milanb(a)panonnet.net> wrote:

> Declan McMullen wrote:
>
> > Any tips on how I should do it ?
> >
>
> We are using the following in FlameRobin:
>
>
> //-----------------------------------------------------------------------------
> std::string wx2std(const wxString& input, wxMBConv* conv = 0)
> {
> if (input.empty())
> return "";
> if (!conv)
> conv = wxConvCurrent;
> const wxWX2MBbuf buf(input.mb_str(*conv));
> // conversion may fail and return 0,
> // which isn't a safe value to pass
> // to std:string constructor
> if (!buf)
> return "";
> return std::string(buf);
> }
>
> //-----------------------------------------------------------------------------
> wxString std2wx(const std::string& input, wxMBConv* conv = 0)
> {
> if (input.empty())
> return wxEmptyString;
> if (!conv)
> conv = wxConvCurrent;
> return wxString(input.c_str(), *conv);
> }
>
> //-----------------------------------------------------------------------------
>
> Any comments are appreciated.
>
> --
> Milan Babuskov
> http://www.flamerobin.org
>
> _______________________________________________
> wx-users mailing list
> wx-users(a)lists.wxwidgets.org
> http://lists.wxwidgets.org/mailman/listinfo/wx-users
>



--
http://www.computing.dcu.ie/~dmcmullen
declan.mcmullen(a)computing.dcu.ie
School of Computing
Postgrad Bay A