Public Member Functions | Private Attributes | List of all members
QSimpleTextCodec Class Reference
Inheritance diagram for QSimpleTextCodec:
QTextCodec

Public Member Functions

 QSimpleTextCodec (int)
 
 ~QSimpleTextCodec ()
 
QString toUnicode (const char *chars, int len) const
 
QCString fromUnicode (const QString &uc, int &lenInOut) const
 
const char * name () const
 
int mibEnum () const
 
int heuristicContentMatch (const char *chars, int len) const
 
int heuristicNameMatch (const char *hint) const
 
- Public Member Functions inherited from QTextCodec
virtual ~QTextCodec ()
 
virtual QTextDecodermakeDecoder () const
 
virtual QTextEncodermakeEncoder () const
 
QCString fromUnicode (const QString &uc) const
 
QString toUnicode (const QByteArray &, int len) const
 
QString toUnicode (const QByteArray &) const
 
QString toUnicode (const char *chars) const
 
virtual bool canEncode (QChar) const
 
virtual bool canEncode (const QString &) const
 

Private Attributes

int forwardIndex
 

Additional Inherited Members

- Static Public Member Functions inherited from QTextCodec
static QTextCodecloadCharmap (QIODevice *)
 
static QTextCodecloadCharmapFile (QString filename)
 
static QTextCodeccodecForMib (int mib)
 
static QTextCodeccodecForName (const char *hint, int accuracy=0)
 
static QTextCodeccodecForContent (const char *chars, int len)
 
static QTextCodeccodecForIndex (int i)
 
static QTextCodeccodecForLocale ()
 
static void deleteAllCodecs ()
 
static const char * locale ()
 
- Protected Member Functions inherited from QTextCodec
 QTextCodec ()
 
- Static Protected Member Functions inherited from QTextCodec
static int simpleHeuristicNameMatch (const char *name, const char *hint)
 

Detailed Description

Definition at line 1326 of file qtextcodec.cpp.

Constructor & Destructor Documentation

QSimpleTextCodec::QSimpleTextCodec ( int  i)

Definition at line 1813 of file qtextcodec.cpp.

1814  : QTextCodec(), forwardIndex( i )
1815 {
1816 }
QSimpleTextCodec::~QSimpleTextCodec ( )

Definition at line 1819 of file qtextcodec.cpp.

1820 {
1821  if ( reverseOwner == this ) {
1822  delete reverseMap;
1823  reverseMap = 0;
1824  reverseOwner = 0;
1825  }
1826 }
static const QSimpleTextCodec * reverseOwner
static QArray< char > * reverseMap

Member Function Documentation

QCString QSimpleTextCodec::fromUnicode ( const QString uc,
int &  lenInOut 
) const
virtual

Subclasses of QTextCodec must reimplement either this function or makeEncoder(). It converts the first lenInOut characters of uc from Unicode to the encoding of the subclass. If lenInOut is negative or too large, the length of uc is used instead.

The value returned is the property of the caller, which is responsible for deleting it with "delete []". The length of the resulting Unicode character sequence is returned in lenInOut.

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 must reimplement one function or the other to avoid infinite recursion.

Reimplemented from QTextCodec.

Definition at line 1851 of file qtextcodec.cpp.

1852 {
1853  if ( reverseOwner != this ) {
1854  int m = 0;
1855  int i = 0;
1856  while( i < 128 ) {
1857  if ( unicodevalues[forwardIndex].values[i] > m &&
1858  unicodevalues[forwardIndex].values[i] < 0xfffd )
1859  m = unicodevalues[forwardIndex].values[i];
1860  i++;
1861  }
1862  m++;
1863  if ( !reverseMap )
1864  reverseMap = new QArray<char>( m );
1865  if ( m > (int)(reverseMap->size()) )
1866  reverseMap->resize( m );
1867  for( i = 0; i < 128 && i < m; i++ )
1868  (*reverseMap)[i] = (char)i;
1869  for( ;i < m; i++ )
1870  (*reverseMap)[i] = '?';
1871  for( i=128; i<256; i++ ) {
1872  int u = unicodevalues[forwardIndex].values[i-128];
1873  if ( u < m )
1874  (*reverseMap)[u] = (char)(unsigned char)(i);
1875  }
1876  reverseOwner = this;
1877  }
1878  if ( len <0 || len > (int)uc.length() )
1879  len = uc.length();
1880  QCString r( len+1 );
1881  int i = len;
1882  int u;
1883  const QChar* ucp = uc.unicode();
1884  char* rp = r.rawData();
1885  char* rmp = reverseMap->data();
1886  int rmsize = (int) reverseMap->size();
1887  while( i-- )
1888  {
1889  u = ucp->unicode();
1890  *rp++ = u < 128 ? u : (( u < rmsize ) ? (*(rmp+u)) : '?' );
1891  ucp++;
1892  }
1893  r[len] = 0;
1894  return r;
1895 }
const QChar * unicode() const
Definition: qstring.h:508
bool resize(uint size)
Definition: qarray.h:69
ushort unicode() const
Definition: qstring.h:151
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
static struct @4 unicodevalues[]
type * data() const
Definition: qarray.h:63
uint length() const
Definition: qstring.h:679
Q_UINT16 values[128]
static const QSimpleTextCodec * reverseOwner
static QArray< char > * reverseMap
uint size() const
Definition: qarray.h:65
int QSimpleTextCodec::heuristicContentMatch ( const char *  chars,
int  len 
) const
virtual

Subclasses of QTextCodec must reimplement this function. It examines the first len bytes of chars and returns a value indicating how likely it is that the string is a prefix of text encoded in the encoding of the subclass. Any negative return value indicates that the text is detectably not in the encoding (eg. it contains undefined characters). 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 len. Thus, most decoders will return -1, 0, or -len.

The characters are not null terminated.

See also
codecForContent().

Implements QTextCodec.

Definition at line 1927 of file qtextcodec.cpp.

1928 {
1929  if ( len<1 || !chars )
1930  return -1;
1931  int i = 0;
1932  const uchar * c = (const unsigned char *)chars;
1933  int r = 0;
1934  while( i<len && c && *c ) {
1935  if ( *c >= 128 ) {
1936  if ( unicodevalues[forwardIndex].values[(*c)-128] == 0xfffd )
1937  return -1;
1938  }
1939  if ( (*c >= ' ' && *c < 127) ||
1940  *c == '\n' || *c == '\t' || *c == '\r' )
1941  r++;
1942  i++;
1943  c++;
1944  }
1945  if ( mibEnum()==4 )
1946  r+=1;
1947  return r;
1948 }
int mibEnum() const
unsigned char uchar
Definition: nybbler.cc:11
static struct @4 unicodevalues[]
Q_UINT16 values[128]
int QSimpleTextCodec::heuristicNameMatch ( const char *  hint) const
virtual

Returns a value indicating how likely this decoder is for decoding some format that has the given name.

A good match returns a positive number around the length of the string. A bad match is negative.

The default implementation calls simpleHeuristicNameMatch() with the name of the codec.

Reimplemented from QTextCodec.

Definition at line 1909 of file qtextcodec.cpp.

1910 {
1911  if ( hint[0]=='k' ) {
1912  // Help people with messy fonts
1913  if ( QCString(hint) == "koi8-1" )
1914  return QTextCodec::heuristicNameMatch("koi8-r")-1;
1915  if ( QCString(hint) == "koi8-ru" )
1916  return QTextCodec::heuristicNameMatch("koi8-r")-1;
1917  } else if ( hint[0] == 't' && QCString(name()) == "ISO 8859-11" ) {
1918  // 8859-11 and tis620 are byte by bute equivalent
1919  int i = simpleHeuristicNameMatch("tis620-0", hint);
1920  if( !i )
1921  i = simpleHeuristicNameMatch("tis-620", hint);
1922  if( i ) return i;
1923  }
1924  return QTextCodec::heuristicNameMatch(hint);
1925 }
static int simpleHeuristicNameMatch(const char *name, const char *hint)
Definition: qtextcodec.cpp:316
virtual int heuristicNameMatch(const char *hint) const
Definition: qtextcodec.cpp:277
const char * name() const
int QSimpleTextCodec::mibEnum ( ) const
virtual

Subclasses of QTextCodec must reimplement this function. It returns the MIBenum (see the IANA character-sets encoding file for more information). It is important that each QTextCodec subclass return the correct unique value for this function.

Implements QTextCodec.

Definition at line 1904 of file qtextcodec.cpp.

1905 {
1906  return unicodevalues[forwardIndex].mib;
1907 }
static struct @4 unicodevalues[]
const char * QSimpleTextCodec::name ( ) const
virtual

Subclasses of QTextCodec 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 QTextCodec can convert between Unicode and the encoding of a font with encoding hint, such as "iso8859-1" for Latin-1 fonts, "koi8-r" for Russian KOI8 fonts. The default algorithm of heuristicNameMatch() uses name().
  • Some applications may use this function to present encodings to the end user.

Implements QTextCodec.

Definition at line 1898 of file qtextcodec.cpp.

1899 {
1900  return unicodevalues[forwardIndex].cs;
1901 }
static struct @4 unicodevalues[]
QString QSimpleTextCodec::toUnicode ( const char *  chars,
int  len 
) const
virtual

Subclasses of QTextCodec must reimplement this function or makeDecoder(). It converts the first len characters of chars to Unicode.

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 must reimplement one function or the other to avoid infinite recursion.

Reimplemented from QTextCodec.

Definition at line 1829 of file qtextcodec.cpp.

1830 {
1831  if(len <= 0)
1832  return QString::null;
1833 
1834  int clen = qstrlen(chars);
1835  len = QMIN(len, clen); // Note: NUL ends string
1836 
1837  QString r;
1838  r.setUnicode(0, len);
1839  QChar* uc = (QChar*)r.unicode(); // const_cast
1840  const unsigned char * c = (const unsigned char *)chars;
1841  for( int i=0; i<len; i++ ) {
1842  if ( c[i] > 127 )
1843  uc[i] = unicodevalues[forwardIndex].values[c[i]-128];
1844  else
1845  uc[i] = c[i];
1846  }
1847  return r;
1848 }
const QChar * unicode() const
Definition: qstring.h:508
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
static struct @4 unicodevalues[]
#define QMIN(a, b)
Definition: qglobal.h:391
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
static const Null null
Definition: qstring.h:376
QString & setUnicode(const QChar *unicode, uint len)
Definition: qstring.cpp:14736

Member Data Documentation

int QSimpleTextCodec::forwardIndex
private

Definition at line 1343 of file qtextcodec.cpp.


The documentation for this class was generated from the following file: