Almost all the functions in this class are reentrant when TQt is built with thread support. The exceptions are \fB~TQTextCodec\fR(), \fBsetCodecForTr\fR(), \fBsetCodecForCStrings\fR(), and \fBTQTextCodec\fR(). </p>
Qt uses Unicode to store, draw and manipulate strings. In many situations you may wish to deal with data that uses a different encoding. For example, most Japanese documents are still stored in Shift-JIS or ISO2022, while Russian users often have their documents in KOI8-R or CP1251.
Qt provides a set of TQTextCodec classes to help with converting non-Unicode formats to and from Unicode. You can also create your own codec classes (see later).
TQTextCodecs can be used as follows to convert some locally encoded string to Unicode. Suppose you have some string encoded in Russian KOI8-R encoding, and want to convert it to Unicode. The simple way to do this is:
Some care must be taken when trying to convert the data in chunks, for example, when receiving it over a network. In such cases it is possible that a multi-byte character will be split over two chunks. At best this might result in the loss of a character and at worst cause the entire conversion to fail.
The approach to use in these situations is to create a TQTextDecoder object for the codec and use this TQTextDecoder for the whole decoding process, as shown below:
The abstract virtual functions describe the encoder to the system and the coder is used as required in the different text file formats supported by TQTextStream, and under X11, for the locale-specific character input and output.
Return the MIB enum for the encoding if it is listed in the IANA character-sets encoding file.
.PP
If the encoding is multi-byte then it will have "state"; that is, the interpretation of some bytes will be dependent on some preceding bytes. For such encodings, you must implement:
The base TQTextCodec class has default implementations of the above two functions, \fIbut they are mutually recursive\fR, so you must re-implement at least one of them, or both for improved efficiency.
For conversion from Unicode to 8-bit encodings, it is rarely necessary to maintain state. However, two functions similar to the two above are used for encoding:
Converts \fIlenInOut\fR characters (of type TQChar) from the start of the string \fIuc\fR, returning a TQCString result, and also returning the length of the result in \fIlenInOut\fR.
Constructs a TQTextCodec, and gives it the highest precedence. The TQTextCodec should always be constructed on the heap (i.e. with \fCnew\fR). TQt takes ownership and will delete it when the application terminates.
Returns TRUE if the Unicode character \fIch\fR can be fully encoded with this codec; otherwise returns FALSE. The default implementation tests if the result of toUnicode(fromUnicode(ch)) is the original \fIch\fR. Subclasses may be able to improve the efficiency.
Returns the codec used by TQString to convert to and from const char* and TQCStrings. If this function returns 0 (the default), TQString assumes Latin-1.
Note that this is often a poor choice, since character encodings often use most of the available character sequences, and so only by linguistic analysis could a true match be made.
.PP
\fIchars\fR contains the string to check, and \fIlen\fR contains the number of characters in the string to use.
.SH "TQTextCodec * TQTextCodec::codecForIndex ( int i )\fC [static]\fR"
Returns the TQTextCodec \fIi\fR positions from the most recently inserted codec, or 0 if there is no such TQTextCodec. Thus, codecForIndex(0) returns the most recently created TQTextCodec.
Searches all installed TQTextCodec objects and returns the one which best matches \fIname\fR; the match is case-insensitive. Returns 0 if no codec's heuristicNameMatch() reports a match better than \fIaccuracy\fR, or if \fIname\fR is a null string.
QApplication calls this function just before exiting to delete any TQTextCodec objects that may be lying around. Since various other classes hold pointers to TQTextCodec objects, it is not safe to call this function earlier.
If you are using the utility classes (like TQString) but not using QApplication, calling this function at the very end of your application may be helpful for chasing down memory leaks by eliminating any TQTextCodec objects.
TQTextCodec subclasses must reimplement either this function or makeEncoder(). It converts the first \fIlenInOut\fR characters of \fIuc\fR from Unicode to the encoding of the subclass. If \fIlenInOut\fR is negative or too large, the length of \fIuc\fR is used instead.
Converts \fIlenInOut\fR characters (not bytes) from \fIuc\fR, producing a TQCString. \fIlenInOut\fR will be set to the length of the result (in bytes).
The default implementation makes an encoder with makeEncoder() and converts the input with that. Note that the default makeEncoder() implementation makes an encoder that simply calls this function, hence subclasses \fImust\fR reimplement one function or the other to avoid infinite recursion.
.SH "int TQTextCodec::heuristicContentMatch ( const char * chars, int len ) const\fC [pure virtual]\fR"
TQTextCodec subclasses must reimplement this function. It examines the first \fIlen\fR bytes of \fIchars\fR and returns a value indicating how likely it is that the string is a prefix of text encoded in the encoding of the subclass. A negative return value indicates that the text is detectably not in the encoding (e.g. it contains characters undefined in the encoding). A return value of 0 indicates that the text should be decoded with this codec rather than as ASCII, but there is no particular evidence. The value should range up to \fIlen\fR. Thus, most decoders will return -1, 0, or -\fIlen\fR.
Returns a value indicating how likely it is that this decoder is appropriate for decoding some format that has the given name. The name is compared with the \fIhint\fR.
.PP
A good match returns a positive number around the length of the string. A bad match is negative.
.PP
The default implementation calls simpleHeuristicNameMatch() with the name of the codec.
Note that a codec constructed in this way uses much more memory and is slower than a hand-written TQTextCodec subclass, since tables in code are kept in memory shared by all TQt applications.
Creates a TQTextDecoder which stores enough state to decode chunks of char* data to create chunks of Unicode data. The default implementation creates a stateless decoder, which is only sufficient for the simplest encodings where each byte corresponds to exactly one Unicode character.
Creates a TQTextEncoder which stores enough state to encode chunks of Unicode data as char* data. The default implementation creates a stateless encoder, which is only sufficient for the simplest encodings where each Unicode character corresponds to exactly one character.
Subclasses of TQTextCodec must reimplement this function. It returns the MIBenum (see the IANA character-sets encoding file for more information). It is important that each TQTextCodec subclass returns the correct unique value for this function.
TQTextCodec subclasses must reimplement this function. It returns the name of the encoding supported by the subclass. When choosing a name for an encoding, consider these points:
On X11, heuristicNameMatch( const char * hint ) is used to test if a the TQTextCodec can convert between Unicode and the encoding of a font with encoding \fIhint\fR, such as "iso8859-1" for Latin-1 fonts," koi8-r" for Russian KOI8 fonts. The default algorithm of heuristicNameMatch() uses name().
\fBWarning:\fR Some codecs do not preserve the characters in the ascii range (0x00 to 0x7f). For example, the Japanese Shift-JIS encoding maps the backslash character (0x5a) to the Yen character. This leads to unexpected results when using the backslash character to escape characters in strings used in e.g. regular expressions. Use TQString::fromLatin1() to preserve characters in the ascii range when needed.
Set the codec to \fIc\fR; this will be returned by codecForLocale(). This might be needed for some applications that want to use their own mechanism for setting the locale.
If the literal quoted text in the program is not in the Latin-1 encoding, this function can be used to set the appropriate encoding. For example, software developed by Korean programmers might use eucKR for all the text in the program, in which case the main() function might look like this:
Note that this is not the way to select the encoding that the \fIuser\fR has chosen. For example, to convert an application containing literal English strings to Korean, all that is needed is for the English strings to be passed through tr() and for translation files to be loaded. For details of internationalization, see the TQt internationalization documentation.
A simple utility function for heuristicNameMatch(): it does some very minor character-skipping so that almost-exact matches score high. \fIname\fR is the text we're matching and \fIhint\fR is used for the comparison.
The default implementation makes a decoder with makeDecoder() and converts the input with that. Note that the default makeDecoder() implementation makes a decoder that simply calls this function, hence subclasses \fImust\fR reimplement one function or the other to avoid infinite recursion.