Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: jmccay on October 03, 2006, 04:31:30 am

Title: wide charatcers
Post by: jmccay on October 03, 2006, 04:31:30 am
Does anyone know of any good tutorials on using wide characters (& Unicode) with mingw (& in C++ general)?

jmccay
Title: Re: wide charatcers
Post by: David Perfors on October 03, 2006, 09:29:16 am
I don't know a good tutorial, but in C++ you can use wchar_t for characters and std::wstring for strings... I think that is the official way for using wide characters in C++
Title: Re: wide charatcers
Post by: jmccay on October 04, 2006, 02:50:42 am
What about all the char functions?  Like the wide char equivilant of sprintf (swprintf)?  That's what I was reallylooking for info about.

jmccay
Title: Re: wide charatcers
Post by: stahta01 on October 04, 2006, 03:49:38 am
Did you look in wchar.h and see if the function exists? Did you include it?

Tim S
Title: Re: wide charatcers
Post by: thomas on October 04, 2006, 09:00:46 am
Challenging Google with "standard library wide character functions" leads to about as many helpful sites as searching for "Amendment 1 to ISO C90". Several of them, for example the Debian one, have code examples.

The GNU C Libarary's documentation and MSDN (Visual C++ section) have chapters on it, too.
Title: Re: wide charatcers
Post by: krisz on October 04, 2006, 11:40:52 am
Hi,
I am not really sure about this (excuse me MinGW guys if I am wrong), but I think for MinGW the wide char support is fully ported.  :?:
However I has not tried yet, but I would recommend to link against STLPort library (should contain all the standard wide char support.)  :idea:
Bye, K
Title: Re: wide charatcers
Post by: severach on October 08, 2006, 06:18:24 pm
The compiler doesn't matter. Once they allow L before 'c' and "String" they support UNICODE. My favorite tutorial is in Programming Windows by Charles Petzold. Just for fun I Googled "UNICODE _UNICODE Tutorial" and I found a decent tutorial.

http://www.flipcode.com/articles/article_advstrings01.shtml

A few comments

>#define _UNICODE
Generally you place -D_UNICODE on the compile line so it's there for all source files and easy to remove or make multiple compiles.

>#include <tchar.h>   // Include Unicode support functions
These are the macros. The wide functions are defined elsewhere but #include <windows.h> typcially gets most of them.

>wchar_t mystring[] = _TEXT("flipcode");

Bad code! WCHAR is preferred to match existing Windows types. wchar_t is always wide and _TEXT() is wide only when -DUNICODE. Either of the following are acceptable.
WCHAR mystring[] = L"flipcode";
TCHAR mystring[] = _TEXT("flipcode");

My little UNICODE tutorial:

Compiler support:If the compiler accepts L before strings, such as L'C' or L"String", UNICODE is supported.

Library support: Libraries now provide wide versions of most common functions such as snwprintf, wcslen, and wmemcpy. Library standards are trying to push out the old unsafe functions so they are often prefixed with an _ depending on what state of flux your compiler is in. Search those include files.

TCHAR is a system of macros that allow your programs to compile as UNICODE (WCHAR) or Ansi (CHAR) as desired just by adding or removing /DUNICODE and /D_UNICODE to your compile lines. Unless your app is never going to be compiled in Ansi it is best to develop entirely in TCHAR. Each time you forget to use TCHAR, WCHAR, or the _T() macro the compiler will beat you with a stick and after enough beatings you'll get to where you do it right almost every time. You'll want to compile Ansi occasionally to ensure that you haven't inadvertantly used a TCHAR where a CHAR or WCHAR belongs and when debugging code that depends on exact pointer behavior. With a few dirty compiler tricks, well written TCHAR code can be made to simultaneously generate UNICODE and Ansi functions without code duplication.

Plan on using the cb and cch Hungarian prefixes. You will need them.

Many <tchar.h> files are incomplete. If you think there's a WCHAR or TCHAR version of a library function, there probably is. Search the header files and create your own defines when necessary.

#if defined(__MINGW32__) || defined(_MSC_VER) && !defined(__POCC__)
#ifdef _UNICODE
#define _tmemmove wmemmove
#else
#define _tmemmove memmove
#endif
#endif

Title: Re: wide charatcers
Post by: jmccay on October 10, 2006, 03:03:44 am
Sorry it has taken me so long to reply, but I was sick last weekend when I was going to get back to this.  I should have been more clear.  I want to do Unicode programming on multiple platforms (windows XP & Linux) using mingw 3.4.5 for the moment, but I realise I would have to support other compilers.  I am also looking for the gotchas from those who have already done it programmed on both environments (or just one).

   My plan is to try and convert UML Pad to Unicode as a test / trial by fire learning experience.  I did some Google-ing and I have put together the following list:


   I have not had a chance to review the stuff mentioned above yet, and I will review them.  It seems I need to download & compile stlport to get wide character stream support.  Is that true?  Thank you for the help.

jmccay
Title: Re: wide charatcers
Post by: jmccay on October 12, 2006, 02:59:50 am
Is there a linux equivilant to tchar.h?  I could probably use the mingw version since it is public domain.  Has anyone tried this?
jmccay