X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4701dc09838c3da46a8bc2836265a7dffee541ee..d3fa4bc22e84e3ca4d88cc1772f2d414140a1017:/interface/wx/strconv.h diff --git a/interface/wx/strconv.h b/interface/wx/strconv.h index a234e7e06d..fd0169589c 100644 --- a/interface/wx/strconv.h +++ b/interface/wx/strconv.h @@ -3,7 +3,7 @@ // Purpose: interface of wxMBConvUTF7 // Author: wxWidgets team // RCS-ID: $Id$ -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// /** @@ -67,7 +67,7 @@ public: This method can be used to allocate the buffer with enough space for the trailing @c NUL characters for any encoding. */ - const size_t GetMaxMBNulLen(); + static size_t GetMaxMBNulLen(); /** Convert multibyte string to a wide character one. @@ -125,8 +125,7 @@ public: The number of character written (or which would have been written if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error. */ - virtual size_t ToWChar(wchar_t* dst, size_t dstLen, - const char* src, + virtual size_t ToWChar(wchar_t* dst, size_t dstLen, const char* src, size_t srcLen = wxNO_LEN) const; /** @@ -153,8 +152,7 @@ public: The number of character written (or which would have been written if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error. */ - virtual size_t FromWChar(char* dst, size_t dstLen, - const wchar_t* src, + virtual size_t FromWChar(char* dst, size_t dstLen, const wchar_t* src, size_t srcLen = wxNO_LEN) const; /** @@ -176,8 +174,25 @@ public: compatibility concerns). */ const wxWCharBuffer cMB2WC(const char* in, - size_t inLen = wxNO_LEN, - size_t *outLen = NULL) const; + size_t inLen, + size_t *outLen) const; + + /** + Converts a char buffer to wide char one. + + This is the most convenient and safest conversion function as you + don't have to deal with the buffer lengths directly. Use it if the + input buffer is known not to be empty or if you are sure that the + conversion is going to succeed -- otherwise, use the overload above to + be able to distinguish between empty input and conversion failure. + + @return + The buffer containing the converted text, empty if the input was + empty or if the conversion failed. + + @since 2.9.1 + */ + const wxWCharBuffer cMB2WC(const wxCharBuffer& buf) const; //@{ /** @@ -203,8 +218,25 @@ public: FromWChar(), please see the description of cMB2WC() for more details. */ const wxCharBuffer cWC2MB(const wchar_t* in, - size_t inLen = wxNO_LEN, - size_t *outLen = NULL) const; + size_t inLen, + size_t *outLen) const; + + /** + Converts a wide char buffer to char one. + + This is the most convenient and safest conversion function as you + don't have to deal with the buffer lengths directly. Use it if the + input buffer is known not to be empty or if you are sure that the + conversion is going to succeed -- otherwise, use the overload above to + be able to distinguish between empty input and conversion failure. + + @return + The buffer containing the converted text, empty if the input was + empty or if the conversion failed. + + @since 2.9.1 + */ + const wxCharBuffer cWC2MB(const wxWCharBuffer& buf) const; //@{ /** @@ -245,7 +277,7 @@ public: /** @deprecated This function is deprecated, please use ToWChar() instead. - Converts from a string @a in in multibyte encoding to Unicode putting up to + Converts from a string @a in multibyte encoding to Unicode putting up to @a outLen characters into the buffer @e out. If @a out is @NULL, only the length of the string which would result @@ -255,6 +287,20 @@ public: out buffer, the @a outLen parameter should be one more to allow to properly @c NUL-terminate the string. + So to properly use this function you need to write: + @code + size_t lenConv = conv.MB2WC(NULL, in, 0); + if ( lenConv == wxCONV_FAILED ) + ... handle error ... + // allocate 1 more character for the trailing NUL and also pass + // the size of the buffer to the function now + wchar_t *out = new wchar_t[lenConv + 1]; + if ( conv.MB2WC(out, in, lenConv + 1) == wxCONV_FAILED ) + ... handle error ... + @endcode + For this and other reasons, ToWChar() is strongly recommended as a + replacement. + @param out The output buffer, may be @NULL if the caller is only interested in the length of the resulting string @@ -277,7 +323,7 @@ public: called with a non-@NULL buffer, the @a n parameter should be the size of the buffer and so it should take into account the trailing @c NUL, which might take two or four bytes for some encodings (UTF-16 and - UTF-32) and not one. + UTF-32) and not one, i.e. GetMBNulLen(). */ virtual size_t WC2MB(char* buf, const wchar_t* psz, size_t n) const; }; @@ -436,33 +482,30 @@ public: /** - @class wxMBConvFile - - This class used to define the class instance @b wxConvFileName, but - nowadays @b wxConvFileName is either of type wxConvLibc (on most platforms) - or wxConvUTF8 (on MacOS X). + Conversion object used for converting file names from their external + representation to the one used inside the program. @b wxConvFileName converts filenames between filesystem multibyte encoding and Unicode. @b wxConvFileName can also be set to a something else at - run-time which is used e.g. by wxGTK to use a class which checks the + run-time which is used e.g. by wxGTK to use an object which checks the environment variable @b G_FILESYSTEM_ENCODING indicating that filenames should not be interpreted as UTF8 and also for converting invalid UTF8 characters (e.g. if there is a filename in iso8859_1) to strings with octal values. Since some platforms (such as Win32) use Unicode in the filenames, - and others (such as Unix) use multibyte encodings, this class should only + and others (such as Unix) use multibyte encodings, this object should only be used directly if wxMBFILES is defined to 1. A convenience macro, @c wxFNCONV, is defined to @c wxConvFileName->cWX2MB in this case. You could use it like this: @code - wxChar *name = wxT("rawfile.doc"); + wxChar *name = "rawfile.doc"; FILE *fil = fopen(wxFNCONV(name), "r"); @endcode (although it would be better to just use wxFopen(name, "r") in this - particular case, you only need to use this class for functions taking file + particular case, you only need to use this object for functions taking file names not wrapped by wxWidgets.) @library{wxbase} @@ -470,7 +513,4 @@ public: @see @ref overview_mbconv */ -class wxMBConvFile : public wxMBConv -{ -public: -}; +extern wxMBConv* wxConvFileName;