Jump to content
  • Advertisement
Sign in to follow this  
michael879

long strings (C++)

This topic is 4498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

a lot of directX functions require long strings (L"blah"). I remember reading somewhere (maybe in the documentation) that these are wchar (_wchar maybe?) arrays, rather than char arrays. anyway, I could see no way to convert something of type string to one of these. the string class is the only way I know of manipulating strings in C++ so does anyone know how to do this or if there is another class I could use?

Share this post


Link to post
Share on other sites
Advertisement
std::wstring?

If that doesn't exist( I cannot remember ), then typedef std::basic_string<wchar_t> wstring;

Share this post


Link to post
Share on other sites
To convert between wide strings and narrow strings you can use the mbstowcs() and wcstombs() functions. Or if you're feeling adventurous you can try the narrow() and widen() members of the ctype locale facet.

Share this post


Link to post
Share on other sites
As mentioned above:
- There are standard C functions to perform char and wchar_t conversion
- There is a standard C++ class called wstring


The following illustrates their use.

wsts converts wide-string to string, stws converts string to wide-string:

string wsts(const wstring &src_string)
{
size_t src_len = src_string.length();

if(src_len == 0)
return "";

char *buf = new(std::nothrow) char[src_len + 1];

if(buf == 0)
return "";

wcstombs(buf, src_string.c_str(), src_len);
buf[src_len] = '\0';

string final_string = buf;

if(buf != 0)
delete [] buf;

return final_string;
}

wstring stws(const string &src_string)
{
size_t src_len = src_string.length();

if(src_len == 0)
return L"";

wchar_t *buf = new(std::nothrow) wchar_t[src_len + 1];

if(buf == 0)
return L"";

mbstowcs(buf, src_string.c_str(), src_len);
buf[src_len] = L'\0';

wstring final_string = buf;

if(buf != 0)
delete [] buf;

return final_string;
}






For trivia's sake; in Win32, MAX_PATH defines the maximum string length that Windows can return from the ASCII versions of various directory/file-related API calls.

Regardless of MAX_PATH's value, it is pretty much guaranteed to be less than the maximum path length for an NTFS file system object. Truncation of returned path strings becomes an unavoidable problem.

Once the Unicode versions of these API functions are used, the limit disappears, and the full path string can be returned.


wstring get_module_path_wide(void)
{
wchar_t wide_buf[65536];
wstring module_name;

if(!::GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))
return module_name;

wide_buf[65535] = L'\0';

module_name = wide_buf;

size_t pos = module_name.find_last_of(L'\\');

if(pos != string::npos)
module_name = module_name.substr(0, pos);

return module_name;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by michael879
is wstring defined in the string header file?


http://msdn.com/ is a good resource for C/C++ standard library information, including headers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!