GP32 Wstring


pea

developer
Joined
Oct 3, 2004
Messages
1,089
Age
45
Location
New Zealand
Website
www.projectitis.com
Hi all,

Just run up against wstring. I take it this isn't just a unsigned char* equivalent (e.g. unsigned short*)?
How do I emulate a wstring in c?
 
ok, I now have the definition of a wstring:

A wstring is a string where the elements are wchar_t instead of char.

Does this mean that I can conclude:

typedef wchar_t* wstring;
(so wchar_t* == wstring)

??
 
That would be fairly close I think.

The standard definition of wstring is as a specialization of basic_string, ie.
typedef basic_string <wchar_t> wstring

In ANSI C, you could pretend that it is a pointer to an array of wchar_t or unsigned short.

The basic_string template class provides methods for manipulation of the string. You would have to provide any equivalent functions that use wchar_t *.

Here's a page describing basic_string
http://www.tacc.utexas.edu/services/usergu...cr/bas_0007.htm
 
wstring, wchar_t, etc, just means "wide string" etc. It means it supports both unicode and non-unicode, usually by compiler option (wchar_t is defined as unsigned char or unsigned short depending on compiler option, and the appropriate function calls mapped to wide versions of printf etc)
 
Remember.. as soon as you go to unicode, Life Is Hell. ie: You have to worry about how you're storing the unicode (UTF8, UTf16, etc) and how you're presenting it (mapping it to local iso-8859-1, etc); when reading from local sources.. are you receiving Unicode, or form some specific 8bit format or codepage, and how are you emitting it? Be careful to compare the same encodings only, etc :)

But this is what lead to wcslen and strlen in win32 (why didn't they just call it wstrlen?) and other silliness.. bleh :)

jeff
 
Well, I'm attempting a port that absolutely needs unicode support, so no getting around it. All the functions are there and already written, but the compiler does not undertand 'wstring' so I need to typedef it myself (apparently it was removed from newer versions of GCC?).

The problem is I don't know what to typedef it as! Sometimes it seems to be used as an integer!
Code:
wstring utf8Towstring(const char *s,int len) {
	int len0 = len;
	const char *s0 = s;
	if (len ==0) len = strlen(s);
	wstring res;
	while (len >0) {
  wchar_t w;
  int j = mbrtowc(&w,s,len);
  if (j > 0) {
  	s += j;
  	len -= j;
  	res += w; // <- RIGHT HERE!
  } else {
  	printf("utf8Towstring: decoding error! (error:%d)\n",j);  puts((char*)s0);
  	// It's not UTF-8, it's extended ASCII.
  	return extentedAsciiTowstring(s0,len0);
  }
	}
	return res;
}

Also, there is a function 'wstring' also which I presume converts a wchar_t* to a wstring (if they aren't the same thing??) and I don't know what this function does. Maybe a simple typecast would suffice? Would be ok if I knew the typedef :)
Code:
wstring stringTowstring(const char *s) {
	int len = strlen(s);
	wchar_t *tmp = (wchar_t*)malloc((len+1)*sizeof(wchar_t));
	for (int i=0; i< len; i++)  tmp[i] = (wchar_t) (unsigned char)s[i];
	tmp[len]=0;
	wstring res = wstring(tmp); // <- RIGHT HERE. could it be wstring res = (wstring)tmp; ?
	free(tmp);
	return res;
}
 
res += w;

means add the wchar_t w to the end of wstring res.

"hell" + 'o' = "hello"

The += operator is overloaded by basic_string to mean 'concatenate'.

The statement

wstring res = wstring(tmp);

creates a new wstring object and copies the contents of tmp into it.
 
PRobably never part of gcc.. ie: The compiler doesn't know or care, likely.. its all library supported, just like stdio does FILE and string.h does strlen.

wstring.. you'll have to look it up; likely a UInt16*, just as C-sting is UInt8*. A wchar_t is usually UInt16.

Remember that in C 'char' and 'unsigned char' (Int8 and UInt8 in my brain) are characters, and characters are really just numbers (small ones). "char" can be used as 'C' for instance, but the ' operator in C just changes it to a number. 'A' == 65.

ie:

char x = 'A';

if ( x == 65 ) // true

So its entirely possible they're using wstring to mean a string of wchar_t ("unsigned short int" or "UInt16" in my brain), and using it as both Unicode character, and the number representing that Unicode char.

Hope that helps :)

jeff

ie: Likely you can..

typedef unsigned short int wchar_t;
typedef unsigned short int *wstring;

*shrug*
 
Last edited by a moderator:
Thanks guys. Not sure if I can use basic_string in this environment, but I'll see.

The weird thing is that wchar_t is already defined...
 
Back
Top