]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/strconv.h
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     interface of wxMBConvUTF7 
   4 // Author:      wxWidgets team 
   6 // Licence:     wxWindows license 
   7 ///////////////////////////////////////////////////////////////////////////// 
  12     This class is the base class of a hierarchy of classes capable of 
  13     converting text strings between multibyte (SBCS or DBCS) encodings and 
  16     This is an abstract base class which defines the operations implemented by 
  17     all different conversion classes. The derived classes don't add any new 
  18     operations of their own (except, possibly, some non-default constructors) 
  19     and so you should simply use this class ToWChar() and FromWChar() (or 
  20     cMB2WC() and cWC2MB()) methods with the objects of the derived class. 
  22     In the documentation for this and related classes please notice that 
  23     length of the string refers to the number of characters in the string 
  24     not counting the terminating @c NUL, if any. While the size of the string 
  25     is the total number of bytes in the string, including any trailing @c NUL. 
  26     Thus, length of wide character string @c L"foo" is 3 while its size can 
  27     be either 8 or 16 depending on whether @c wchar_t is 2 bytes (as 
  28     under Windows) or 4 (Unix). 
  33     @see wxCSConv, wxEncodingConverter, @ref overview_mbconv 
  39         Trivial default constructor. 
  44         This pure virtual function is overridden in each of the derived classes 
  45         to return a new copy of the object it is called on. 
  47         It is used for copying the conversion objects while preserving their 
  50     virtual wxMBConv
* Clone() const = 0; 
  53         This function returns 1 for most of the multibyte encodings in which the 
  54         string is terminated by a single @c NUL, 2 for UTF-16 and 4 for UTF-32 for 
  55         which the string is terminated with 2 and 4 @c NUL characters respectively. 
  56         The other cases are not currently supported and @c wxCONV_FAILED 
  57         (defined as -1) is returned for them. 
  59     virtual size_t GetMBNulLen() const; 
  62         Returns the maximal value which can be returned by GetMBNulLen() for 
  63         any conversion object. 
  65         Currently this value is 4. 
  67         This method can be used to allocate the buffer with enough space for the 
  68         trailing @c NUL characters for any encoding. 
  70     static size_t GetMaxMBNulLen(); 
  73         Convert multibyte string to a wide character one. 
  75         This is the most general function for converting a multibyte string to 
  76         a wide string, cMB2WC() may be often more convenient, however this 
  77         function is the most efficient one as it allows to avoid any 
  80         The main case is when @a dst is not @NULL and @a srcLen is not 
  81         @c wxNO_LEN (which is defined as @c (size_t)-1): then the function 
  82         converts exactly @a srcLen bytes starting at @a src into wide string 
  83         which it output to @e dst. If the length of the resulting wide 
  84         string is greater than @e dstLen, an error is returned. Note that if 
  85         @a srcLen bytes don't include @c NUL characters, the resulting wide 
  86         string is not @c NUL-terminated neither. 
  88         If @a srcLen is @c wxNO_LEN, the function supposes that the string is 
  89         properly (i.e. as necessary for the encoding handled by this 
  90         conversion) @c NUL-terminated and converts the entire string, including 
  91         any trailing @c NUL bytes. In this case the wide string is also @c 
  94         Finally, if @a dst is @NULL, the function returns the length of the 
  97         Example of use of this function: 
  99         size_t dstLen = conv.ToWChar(NULL, 0, src); 
 100         if ( dstLen == wxCONV_FAILED ) 
 102         wchar_t *dst = new wchar_t[dstLen]; 
 103         if ( conv.ToWChar(dst, dstLen, src) == wxCONV_FAILED ) 
 107         Notice that when passing the explicit source length the output will 
 108         @e not be @c NUL terminated if you pass @c strlen(str) as parameter. 
 109         Either leave @a srcLen as default @c wxNO_LEN or add one to @c strlen 
 110         result if you want the output to be @c NUL terminated. 
 113             Pointer to output buffer of the size of at least @a dstLen or @NULL. 
 115             Maximal number of characters to be written to the output buffer if 
 116             @a dst is non-@NULL, unused otherwise. 
 118             Point to the source string, must not be @NULL. 
 120             The number of characters of the source string to convert or 
 121             @c wxNO_LEN (default parameter) to convert everything up to and 
 122             including the terminating @c NUL character(s). 
 125             The number of character written (or which would have been written 
 126             if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error. 
 128     virtual size_t ToWChar(wchar_t* dst
, size_t dstLen
, const char* src
, 
 129                            size_t srcLen 
= wxNO_LEN
) const; 
 132         Converts wide character string to multibyte. 
 134         This function has the same semantics as ToWChar() except that it 
 135         converts a wide string to multibyte one. As with ToWChar(), it may be 
 136         more convenient to use cWC2MB() when working with @c NUL terminated 
 140             Pointer to output buffer of the size of at least @a dstLen or @NULL. 
 142             Maximal number of characters to be written to the output buffer if 
 143             @a dst is non-@NULL, unused otherwise. 
 145             Point to the source string, must not be @NULL. 
 147             The number of characters of the source string to convert or 
 148             @c wxNO_LEN (default parameter) to convert everything up to and 
 149             including the terminating @c NUL character. 
 152             The number of character written (or which would have been written 
 153             if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error. 
 155     virtual size_t FromWChar(char* dst
, size_t dstLen
, const wchar_t* src
, 
 156                              size_t srcLen 
= wxNO_LEN
) const; 
 159         Converts from multibyte encoding to Unicode by calling ToWChar() and 
 160         allocating a temporary wxWCharBuffer to hold the result. 
 162         This function is a convenient wrapper around ToWChar() as it takes care 
 163         of allocating the buffer of the necessary size itself. Its parameters 
 164         have the same meaning as for ToWChar(), in particular @a inLen can be 
 165         specified explicitly in which case exactly that many characters are 
 166         converted and @a outLen receives (if non-@NULL) exactly the 
 167         corresponding number of wide characters, whether the last one of them 
 168         is @c NUL or not. However if @c inLen is @c wxNO_LEN, then @c outLen 
 169         doesn't count the trailing @c NUL even if it is always present in this 
 172         Finally notice that if the conversion fails, the returned buffer is 
 173         invalid and @a outLen is set to 0 (and not @c wxCONV_FAILED for 
 174         compatibility concerns). 
 176     const wxWCharBuffer 
cMB2WC(const char* in
, 
 178                                size_t *outLen
) const; 
 182         Converts from multibyte encoding to the current wxChar type (which 
 183         depends on whether wxUSE_UNICODE is set to 1). 
 185         If wxChar is char, it returns the parameter unaltered. If wxChar is 
 186         wchar_t, it returns the result in a wxWCharBuffer. The macro wxMB2WXbuf 
 187         is defined as the correct return type (without const). 
 189     const char* cMB2WX(const char* psz
) const; 
 190     const wxWCharBuffer 
cMB2WX(const char* psz
) const; 
 194         Converts from Unicode to multibyte encoding by calling FromWChar() and 
 195         allocating a temporary wxCharBuffer to hold the result. 
 197         This function is a convenient wrapper around FromWChar() as it takes 
 198         care of allocating the buffer of necessary size itself. 
 200         Its parameters have the same meaning as the corresponding parameters of 
 201         FromWChar(), please see the description of cMB2WC() for more details. 
 203     const wxCharBuffer 
cWC2MB(const wchar_t* in
, 
 205                               size_t *outLen
) const; 
 209         Converts from Unicode to the current wxChar type. 
 211         If wxChar is wchar_t, it returns the parameter unaltered. If wxChar is 
 212         char, it returns the result in a wxCharBuffer. The macro wxWC2WXbuf is 
 213         defined as the correct return type (without const). 
 215     const wchar_t* cWC2WX(const wchar_t* psz
) const; 
 216     const wxCharBuffer 
cWC2WX(const wchar_t* psz
) const; 
 221         Converts from the current wxChar type to multibyte encoding. 
 223         If wxChar is char, it returns the parameter unaltered. If wxChar is 
 224         wchar_t, it returns the result in a wxCharBuffer. The macro wxWX2MBbuf 
 225         is defined as the correct return type (without const). 
 227     const char* cWX2MB(const wxChar
* psz
) const; 
 228     const wxCharBuffer 
cWX2MB(const wxChar
* psz
) const; 
 233         Converts from the current wxChar type to Unicode. 
 235         If wxChar is wchar_t, it returns the parameter unaltered. If wxChar is 
 236         char, it returns the result in a wxWCharBuffer. The macro wxWX2WCbuf is 
 237         defined as the correct return type (without const). 
 239     const wchar_t* cWX2WC(const wxChar
* psz
) const; 
 240     const wxWCharBuffer 
cWX2WC(const wxChar
* psz
) const; 
 244         @deprecated This function is deprecated, please use ToWChar() instead. 
 246         Converts from a string @a in in multibyte encoding to Unicode putting up to 
 247         @a outLen characters into the buffer @e out. 
 249         If @a out is @NULL, only the length of the string which would result 
 250         from the conversion is calculated and returned. Note that this is the 
 251         length and not size, i.e. the returned value does not include the 
 252         trailing @c NUL. But when the function is called with a non-@NULL @a 
 253         out buffer, the @a outLen parameter should be one more to allow to 
 254         properly @c NUL-terminate the string. 
 256         So to properly use this function you need to write: 
 258             size_t lenConv = conv.MB2WC(NULL, in, 0); 
 259             if ( lenConv == wxCONV_FAILED ) 
 261             // allocate 1 more character for the trailing NUL and also pass 
 262             // the size of the buffer to the function now 
 263             wchar_t *out = new wchar_t[lenConv + 1]; 
 264             if ( conv.MB2WC(out, in, lenConv + 1) == wxCONV_FAILED ) 
 267         For this and other reasons, ToWChar() is strongly recommended as a 
 271             The output buffer, may be @NULL if the caller is only 
 272             interested in the length of the resulting string 
 274             The NUL-terminated input string, cannot be @NULL 
 276             The length of the output buffer but including 
 277             NUL, ignored if out is @NULL 
 279         @return The length of the converted string excluding the trailing NUL. 
 281     virtual size_t MB2WC(wchar_t* out
, const char* in
, size_t outLen
) const; 
 284         @deprecated This function is deprecated, please use FromWChar() instead. 
 286         Converts from Unicode to multibyte encoding. 
 287         The semantics of this function (including the return value meaning) is 
 288         the same as for wxMBConv::MB2WC. Notice that when the function is 
 289         called with a non-@NULL buffer, the @a n parameter should be the size 
 290         of the buffer and so it should take into account the trailing @c NUL, 
 291         which might take two or four bytes for some encodings (UTF-16 and 
 292         UTF-32) and not one, i.e. GetMBNulLen(). 
 294     virtual size_t WC2MB(char* buf
, const wchar_t* psz
, size_t n
) const; 
 301     This class converts between the UTF-7 encoding and Unicode. 
 302     It has one predefined instance, @b wxConvUTF7. 
 304     Notice that, unlike all the other conversion objects, this converter is 
 305     stateful, i.e. it remembers its state from the last call to its ToWChar() 
 306     or FromWChar() and assumes it is called on the continuation of the same 
 307     string when the same method is called again. This assumption is only made 
 308     if an explicit length is specified as parameter to these functions as if an 
 309     entire @c NUL terminated string is processed the state doesn't need to be 
 312     This also means that, unlike the other predefined conversion objects, 
 313     @b wxConvUTF7 is @em not thread-safe. 
 318     @see wxMBConvUTF8, @ref overview_mbconv 
 320 class wxMBConvUTF7 
: public wxMBConv
 
 329     This class converts between the UTF-8 encoding and Unicode. 
 330     It has one predefined instance, @b wxConvUTF8. 
 335     @see wxMBConvUTF7, @ref overview_mbconv 
 337 class wxMBConvUTF8 
: public wxMBConv
 
 346     This class is used to convert between multibyte encodings and UTF-16 Unicode 
 347     encoding (also known as UCS-2). 
 349     Unlike UTF-8 encoding, UTF-16 uses words and not bytes and hence depends 
 350     on the byte ordering: big or little endian. Hence this class is provided in 
 351     two versions: wxMBConvUTF16LE and wxMBConvUTF16BE and wxMBConvUTF16 itself 
 352     is just a typedef for one of them (native for the given platform, e.g. LE 
 353     under Windows and BE under Mac). 
 358     @see wxMBConvUTF8, wxMBConvUTF32, @ref overview_mbconv 
 360 class wxMBConvUTF16 
: public wxMBConv
 
 368     This class is used to convert between multibyte encodings and UTF-32 
 369     Unicode encoding (also known as UCS-4). 
 370     Unlike UTF-8 encoding, UTF-32 uses (double) words and not bytes and hence 
 371     depends on the byte ordering: big or little endian. Hence this class is 
 372     provided in two versions: wxMBConvUTF32LE and wxMBConvUTF32BE and 
 373     wxMBConvUTF32 itself is just a typedef for one of them (native for the 
 374     given platform, e.g. LE under Windows and BE under Mac). 
 379     @see wxMBConvUTF8, wxMBConvUTF16, @ref overview_mbconv 
 381 class wxMBConvUTF32 
: public wxMBConv
 
 391     This class converts between any character set supported by the system and 
 394     Please notice that this class uses system-provided conversion functions, 
 395     e.g. @c MultiByteToWideChar() and @c WideCharToMultiByte() under MSW and @c 
 396     iconv(3) under Unix systems and as such may support different encodings and 
 397     different encoding names on different platforms (although all relatively 
 398     common encodings are supported should be supported everywhere). 
 400     It has one predefined instance, @b wxConvLocal, for the default user 
 406     @see wxMBConv, wxEncodingConverter, @ref overview_mbconv 
 408 class wxCSConv 
: public wxMBConv
 
 414         You can specify the name of the character set you want to convert 
 415         from/to. If the character set name is not recognized, ISO 8859-1 is 
 416         used as fall back, use IsOk() to test for this. 
 418         @param charset The name of the encoding, shouldn't be empty. 
 420     wxCSConv(const wxString
& charset
); 
 425         You can specify an encoding constant for the character set you want to 
 426         convert from/to. Use IsOk() after construction to check whether the 
 427         encoding is supported by the current system. 
 429         @param encoding Any valid (i.e. not wxFONTENCODING_MAX) font encoding. 
 431     wxCSConv(wxFontEncoding encoding
); 
 434         Returns @true if the charset (or the encoding) given at constructor is 
 435         really available to use. 
 437         Returns @false if ISO 8859-1 will be used instead. 
 439         Note this does not mean that a given string will be correctly 
 440         converted. A malformed string may still make conversion functions 
 441         return @c wxCONV_FAILED. 
 453     This class used to define the class instance @b wxConvFileName, but 
 454     nowadays @b wxConvFileName is either of type wxConvLibc (on most platforms) 
 455     or wxConvUTF8 (on MacOS X). 
 457     @b wxConvFileName converts filenames between filesystem multibyte encoding 
 458     and Unicode. @b wxConvFileName can also be set to a something else at 
 459     run-time which is used e.g. by wxGTK to use a class which checks the 
 460     environment variable @b G_FILESYSTEM_ENCODING indicating that filenames 
 461     should not be interpreted as UTF8 and also for converting invalid UTF8 
 462     characters (e.g. if there is a filename in iso8859_1) to strings with octal 
 465     Since some platforms (such as Win32) use Unicode in the filenames, 
 466     and others (such as Unix) use multibyte encodings, this class should only 
 467     be used directly if wxMBFILES is defined to 1. A convenience macro, 
 468     @c wxFNCONV, is defined to @c wxConvFileName->cWX2MB in this case. You 
 469     could use it like this: 
 472     wxChar *name = wxT("rawfile.doc"); 
 473     FILE *fil = fopen(wxFNCONV(name), "r"); 
 476     (although it would be better to just use wxFopen(name, "r") in this 
 477     particular case, you only need to use this class for functions taking file 
 478     names not wrapped by wxWidgets.) 
 483     @see @ref overview_mbconv 
 485 class wxMBConvFile 
: public wxMBConv