Strings again :roll:

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Strings again :roll:

Post by dgrafix »

Hi,

I want to get 2 functions so i can interface my stuff with irrlicht's wide needs. I dont care about the need for foreign chars so only want to use normal ascii strings throughout my project.

Is this code 'safe' ? or should i be 'delete'ing wc_out?
///StrToWide
irr::core::stringw StrToWide(const std::string & str)
{
const char*st=str.c_str();
irr::core::stringw out=L"";
// Convert to a wchar_t*
size_t alloc_len = strlen(st) + 1;
wchar_t* wc_out = new wchar_t[alloc_len];
mbstowcs(wc_out, st, alloc_len);
out += wc_out;
return out;
}


///StrFromWide
std::string StrToWide(const irr::core::stringw & str)
{
//any help here will be appreciated also.
}
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You need to delete it. That said, if you only need to use ascii characters, you could just write this...

Code: Select all

irr::core::stringw make_wide (const core::stringc& s)
{
  return irr::core::stringw (s);
}
Travis
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Post by dgrafix »

ah they construct like that do they? does this work for irr classes to std & vice versa?
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The Irrlicht strings offer the functionality, so you have to rely on them to do the conversion.

Code: Select all

irr::core::stringw make_wide (const irr::core::string& s)
{
  return irr::core::stringw (s);
}

irr::core::stringc make_narrow (const irr::core::stringw& w)
{
  return irr::core::stringc (w);
}

std::stringw make_std (const irr::core::stringw& w)
{
  return std::wstring (w.c_str(), w.size());
}

std::string make_std (const irr::core::stringc& s)
{
  return std::string (s.c_str(), s.size());
}

irr::core::stringc make_irr (const std::string& s)
{
  return irr::core::stringc (s.c_str(), s.size());
}

irr::core::stringw make_irr (const std::wstring& w)
{
  return irr::core::stringw (w.c_str(), w.size());
}
Travis
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Post by dgrafix »

Thanks
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
Post Reply