473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Implementing CString with std::string and std::wstring

bajajv
152 100+
Hi, I was trying to implement CString with std::string and std::wstring.
class CString
{
public:
std::string str;
CString() {str = "ABCD";} //this works
}:

class CString
{
public:
std::wstring wstr;
CString() {wstr = "ABCD";} //this doesn't works
}:
The wstring is giving error for =operator. What do I need to add here for wstring?
I am using MS visual studio 2005, but I want to make it compiler independent.
Dec 8 '09 #1
10 8204
Banfa
9,065 Expert Mod 8TB
What error is it giving?

Have you tried L"ABCD"?
Dec 8 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
Your Visual Studio project has a charactert set property. This is where you specify char or Unicode. In your code there should never be explicit char or wide char syntax.

You write your code to use the Microsoft-specific amppings between char and Unicode. Do reasearch on the TCHAR mappings.

Note that Windoes does all processing using Unicode. Unless your program specifically needs the char character set, there should be no use of char in your program.
Dec 8 '09 #3
bajajv
152 100+
I want to write a CString which can be OS independent. Is this possible with wide char? I am trying for UNICODE only.
Dec 10 '09 #4
bajajv
152 100+
Thanks Banfa. I missed that.
Dec 10 '09 #5
Banfa
9,065 Expert Mod 8TB
I think you may have a little trouble creating an OS independent multi-byte/wide character string because I believe Windows uses wide characters, that is characters with 16 or more bits per character like UNICODE where as Linux uses multi-byte characters that is the bytes are all 8 bits but sometimes a characters uses more than 1 of them like UTF8.

With wide characters all the characters are the same width (16 bits I think for UNICODE), with multi-byte characters not all characters are the same width, in UTF8 characters can be 1,2,3 or 4 bytes. The first few bits of the byte tell you whether there are more bytes to follow. Additionally in UTF8 there is a 1 - 1 mapping of the first 127 ASCII codes and the first 127 UTF8 codes.

Anyway in Linux a std::string can be used to hold either an ASCII string or a UTF8 string since the basic data unit of both is 8 bit octets. This is not true for Windows if the program is compiled to use UNICODE then it needs to use wide characters wchar_t (although as weaknessforcats says you shouldn't use them directly but through the TCHAR type) which actually have more bits per character (like I siad I think 16).

Because of this difference I think you would find it hard to make a OS independent wide/multi-byte character type.
Dec 10 '09 #6
bajajv
152 100+
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <stdarg.h>
  4.  
  5. using namespace std;
  6.  
  7. class MyString
  8. {
  9. public:
  10.    wstring wStr;
  11.    MyString ()  {wStr = L"";}
  12.    MyString (wstring str)  {wStr = str;}
  13.    void Format(const wchar_t*lpszFormat, ...);   
  14. };
  15.  
  16. void MyString::Format(const wchar_t* lpszFormat, ...)
  17. {
  18.     wchar_t szBuffer[256];
  19.     va_list args;
  20.     va_start(args, lpszFormat);
  21.     vswprintf(szBuffer, 256, lpszFormat, args);
  22.     va_end (args);
  23.     // Got the string in szBuffer, Now convert to a wchar_t*
  24.     wcscpy(const_cast<wchar_t*>(this->wStr.c_str()), szBuffer);
  25. }
  26.  
  27. int main()
  28. {
  29.     wstring firstName = L"Vipul";
  30.     wstring lastName = L"Bajaj";
  31.     MyString myStr (firstName); 
  32.     wprintf(L"%s\n", myStr.wStr.c_str());
  33.     cout<<myStr.wStr.c_str()<<endl;
  34.     myStr.Format(L"%s%s", firstName.c_str(), lastName.c_str());
  35.     wprintf(L"%s\n", myStr.wStr.c_str());
  36.  
  37. return 0;
  38. }
The above code works fine, but doesnt works when I change the format control string.

e.g. if I change the format string to "%s %s", the code below will not work -
wcscpy(const_cast<wchar_t*>(this->wStr.c_str()), szBuffer);
How can I improve this?
is there any way to accomodate that space, or a slash '\' or anything in between those two '%s'?
Dec 12 '09 #7
bajajv
152 100+
One reason could be that the space between - "%s %s" - is in ASCII... but it should also be wide character, as I am providing it using an L - L"%s %s"... Please clear this doubt.. Is this L converting the space also to wide character?
Dec 12 '09 #8
weaknessforcats
9,208 Expert Mod 8TB
Why aren't you using:

Expand|Select|Wrap|Line Numbers
  1. wcout << myStr << endl;
?

Avoid the printf family of functions. They are from C and they do not allow print of user defined types so there's no way to print a Date or a Person object.

A wcscpy does not convert a char string to a wchar_t string. You need to call MultiByteToWideString.

Again, all of this is done for you already if you use TCHAR.
Dec 12 '09 #9
bajajv
152 100+
But I cant use windows specific things. I need to make it work on both windows and unix.
Dec 14 '09 #10
Banfa
9,065 Expert Mod 8TB
Since Windows and UNIX handle multi-byte, wide characters rather differently you are unlikely to be able to to create a class that will do both.

As weaknessforcats says you should be using TCHAR for Windows which wont work for *nix, as I have said you need to be use std:string for *nix which wont work for Windows. Windows and *nix have very different approaches to implementing wide/multi-byte characters.

The only way to implement your class as a normal class would be to make it very complex internally effectively implement 2 different methods to choose between depending on the operating system detected.

An easier method might be to use the adaptor or façade design patterns. These patterns effectively allow you to wrap one class in another or to wrap some API in a class. You would create a standard declaration of a string class that your code can call but you would provide different implementations depending on the target OS during compile time.

That way you could provide one implementation that worked on Windows and another that worked on *nix.
Dec 14 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: red floyd | last post by:
I have a an app that I'm writing which uses char and std::string. I'm using a library which expects wchar_t arrays. Is there a standard way to convert between std::string and std::wstring, or do...
12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
3
by: Lars Nielsen | last post by:
Hey there I have a win32 application written i c++. I have a std::vector of std::string's i will fill with filenames. typedef vector<std::string> strvector; strvector vFiles; ...
9
by: vsgdp | last post by:
Hi, Is there a unicode equivalent to std::string?
5
by: Karthik | last post by:
Hello, How can I convert a BSTR data type to std::string??? Thanks a much! Karthik
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
10
by: Jeffrey Walton | last post by:
Hi All, I've done a little homework (I've read responses to similar from P.J. Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below is what I am performing (Stroustrup's...
3
by: Angus | last post by:
I can see how to get a char* but is it possible to get a wide char - eg wchar_t?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.