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

Public Member Functions

 QTextCodecFromIOD (QIODevice *iod)
 
 ~QTextCodecFromIOD ()
 
bool ok () const
 
QTextDecodermakeDecoder () const
 
const char * name () const
 
int mibEnum () const
 
int heuristicContentMatch (const char *, int) const
 
int heuristicNameMatch (const char *hint) const
 
QString toUnicode (const char *chars, int len) const
 
QCString fromUnicode (const QString &uc, int &lenInOut) const
 
- Public Member Functions inherited from QTextCodec
virtual ~QTextCodec ()
 
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 Member Functions

bool stateless () const
 

Private Attributes

QCString n
 
char ** from_unicode_page
 
char *** from_unicode_page_multibyte
 
char unkn
 
ushortto_unicode
 
QMultiByteUnicodeTableto_unicode_multibyte
 
int max_bytes_per_char
 
QStrList aliases
 

Friends

class QTextCodecFromIODDecoder
 

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 953 of file qtextcodec.cpp.

Constructor & Destructor Documentation

QTextCodecFromIOD::QTextCodecFromIOD ( QIODevice iod)
inline

Definition at line 973 of file qtextcodec.cpp.

974  {
975  from_unicode_page = 0;
977  to_unicode = 0;
979  max_bytes_per_char = 1;
980 
981  const int maxlen=100;
982  char line[maxlen];
983  char esc='\\';
984  char comm='%';
985  bool incmap = FALSE;
986  while (iod->readLine(line,maxlen) > 0) {
987  if (0==qstrnicmp(line,"<code_set_name>",15))
988  n = line+15;
989  else if (0==qstrnicmp(line,"<escape_char> ",14))
990  esc = line[14];
991  else if (0==qstrnicmp(line,"<comment_char> ",15))
992  comm = line[15];
993  else if (line[0]==comm && 0==qstrnicmp(line+1," alias ",7)) {
994  aliases.append(line+8);
995  } else if (0==qstrnicmp(line,"CHARMAP",7)) {
996  if (!from_unicode_page) {
997  from_unicode_page = new char*[256];
998  for (int i=0; i<256; i++)
999  from_unicode_page[i]=0;
1000  }
1001  if (!to_unicode) {
1002  to_unicode = new ushort[256];
1003  }
1004  incmap = TRUE;
1005  } else if (0==qstrnicmp(line,"END CHARMAP",11))
1006  break;
1007  else if (incmap) {
1008  char* cursor = line;
1009  int byte=0,unicode=-1;
1010  ushort* mb_unicode=0;
1011  const int maxmb=8; // more -> we'll need to improve datastructures
1012  char mb[maxmb+1];
1013  int nmb=0;
1014 
1015  while (*cursor) {
1016  if (cursor[0]=='<' && cursor[1]=='U' &&
1017  cursor[2]>='0' && cursor[2]<='9' &&
1018  cursor[3]>='0' && cursor[3]<='9') {
1019 
1020  unicode = (int)strtol(cursor+2,&cursor,16);
1021 
1022  } else if (*cursor==esc) {
1023 
1024  byte = getByte(cursor);
1025 
1026  if ( *cursor == esc ) {
1027  if ( !to_unicode_multibyte ) {
1029  new QMultiByteUnicodeTable[256];
1030  for (int i=0; i<256; i++) {
1032  to_unicode[i];
1034  }
1035  delete [] to_unicode;
1036  to_unicode = 0;
1037  }
1038  QMultiByteUnicodeTable* mbut =
1040  mb[nmb++] = byte;
1041  while ( nmb < maxmb && *cursor == esc ) {
1042  // Always at least once
1043 
1044  mbut->unicode = CHAINED;
1045  byte = getByte(cursor);
1046  mb[nmb++] = byte;
1047  if (!mbut->multibyte) {
1048  mbut->multibyte =
1049  new QMultiByteUnicodeTable[256];
1050  }
1051  mbut = mbut->multibyte+byte;
1052  mb_unicode = & mbut->unicode;
1053  }
1054 
1055  if ( nmb > max_bytes_per_char )
1056  max_bytes_per_char = nmb;
1057  }
1058  } else {
1059  cursor++;
1060  }
1061  }
1062 
1063  if (unicode >= 0 && unicode <= 0xffff)
1064  {
1065  QChar ch((ushort)unicode);
1066  if (!from_unicode_page[ch.row()]) {
1067  from_unicode_page[ch.row()] = new char[256];
1068  for (int i=0; i<256; i++)
1069  from_unicode_page[ch.row()][i]=0;
1070  }
1071  if ( mb_unicode ) {
1072  from_unicode_page[ch.row()][ch.cell()] = 0;
1074  from_unicode_page_multibyte = new char**[256];
1075  for (int i=0; i<256; i++)
1077  }
1078  if (!from_unicode_page_multibyte[ch.row()]) {
1079  from_unicode_page_multibyte[ch.row()] = new char*[256];
1080  for (int i=0; i<256; i++)
1081  from_unicode_page_multibyte[ch.row()][i] = 0;
1082  }
1083  mb[nmb++] = 0;
1084  from_unicode_page_multibyte[ch.row()][ch.cell()]
1085  = qstrdup(mb);
1086  *mb_unicode = unicode;
1087  } else {
1088  from_unicode_page[ch.row()][ch.cell()] = (char)byte;
1089  if ( to_unicode )
1090  to_unicode[byte] = unicode;
1091  else
1092  to_unicode_multibyte[byte].unicode = unicode;
1093  }
1094  } else {
1095  }
1096  }
1097  }
1098  n = n.stripWhiteSpace();
1099 
1100  unkn = '?'; // ##### Might be a bad choice.
1101  }
QMultiByteUnicodeTable * multibyte
Definition: qtextcodec.cpp:925
QCString stripWhiteSpace() const
Definition: qcstring.cpp:295
QMultiByteUnicodeTable * to_unicode_multibyte
Definition: qtextcodec.cpp:966
static const int maxlen
Definition: qregexp.cpp:904
static int getByte(char *&cursor)
Definition: qtextcodec.cpp:929
const bool FALSE
Definition: qglobal.h:370
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
char *** from_unicode_page_multibyte
Definition: qtextcodec.cpp:961
static constexpr double mb
Definition: Units.h:79
void append(const type *d)
Definition: qinternallist.h:61
virtual int readLine(char *data, uint maxlen)
Definition: qiodevice.cpp:581
char ** from_unicode_page
Definition: qtextcodec.cpp:960
#define CHAINED
Definition: qtextcodec.cpp:911
unsigned short ushort
Definition: qglobal.h:350
byte_as<> byte
Type of data size stored in bytes, in long long precision.
Definition: datasize.h:98
void line(double t, double *p, double &x, double &y, double &z)
char * qstrdup(const char *str)
Definition: qcstring.cpp:548
int qstrnicmp(const char *str1, const char *str2, uint len)
Definition: qcstring.cpp:581
const bool TRUE
Definition: qglobal.h:371
QTextCodecFromIOD::~QTextCodecFromIOD ( )
inline

Definition at line 1103 of file qtextcodec.cpp.

1104  {
1105  if ( from_unicode_page ) {
1106  for (int i=0; i<256; i++)
1107  if (from_unicode_page[i])
1108  delete [] from_unicode_page[i];
1109  }
1111  for (int i=0; i<256; i++)
1113  for (int j=0; j<256; j++)
1114  if (from_unicode_page_multibyte[i][j])
1115  delete [] from_unicode_page_multibyte[i][j];
1116  }
1117  if ( to_unicode )
1118  delete [] to_unicode;
1119  if ( to_unicode_multibyte )
1120  delete [] to_unicode_multibyte;
1121  }
QMultiByteUnicodeTable * to_unicode_multibyte
Definition: qtextcodec.cpp:966
char *** from_unicode_page_multibyte
Definition: qtextcodec.cpp:961
char ** from_unicode_page
Definition: qtextcodec.cpp:960

Member Function Documentation

QCString QTextCodecFromIOD::fromUnicode ( const QString uc,
int &  lenInOut 
) const
inlinevirtual

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 1189 of file qtextcodec.cpp.

1190  {
1191  if (lenInOut > (int)uc.length())
1192  lenInOut = uc.length();
1193  int rlen = lenInOut*max_bytes_per_char;
1194  QCString rstr(rlen);
1195  char* cursor = rstr.rawData();
1196  char* s=0;
1197  int l = lenInOut;
1198  int lout = 0;
1199  for (int i=0; i<l; i++) {
1200  QChar ch = uc[i];
1201  if ( ch == QChar::null ) {
1202  // special
1203  *cursor++ = 0;
1204  } else if ( from_unicode_page[ch.row()] &&
1205  from_unicode_page[ch.row()][ch.cell()] )
1206  {
1207  *cursor++ = from_unicode_page[ch.row()][ch.cell()];
1208  lout++;
1209  } else if ( from_unicode_page_multibyte &&
1211  (s=from_unicode_page_multibyte[ch.row()][ch.cell()]) )
1212  {
1213  while (*s) {
1214  *cursor++ = *s++;
1215  lout++;
1216  }
1217  } else {
1218  *cursor++ = unkn;
1219  lout++;
1220  }
1221  }
1222  *cursor = 0;
1223  lenInOut = lout;
1224  return rstr;
1225  }
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
static QStrList * l
Definition: config.cpp:1044
char *** from_unicode_page_multibyte
Definition: qtextcodec.cpp:961
uchar & cell()
Definition: qstring.h:167
uint length() const
Definition: qstring.h:679
char ** from_unicode_page
Definition: qtextcodec.cpp:960
QT_STATIC_CONST QChar null
Definition: qstring.h:68
uchar & row()
Definition: qstring.h:168
static QCString * s
Definition: config.cpp:1042
int QTextCodecFromIOD::heuristicContentMatch ( const char *  chars,
int  len 
) const
inlinevirtual

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 1146 of file qtextcodec.cpp.

1147  {
1148  return 0;
1149  }
int QTextCodecFromIOD::heuristicNameMatch ( const char *  hint) const
inlinevirtual

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 1151 of file qtextcodec.cpp.

1152  {
1153  int bestr = QTextCodec::heuristicNameMatch(hint);
1155  char* a;
1156  while ((a=it.current())) {
1157  ++it;
1158  int r = simpleHeuristicNameMatch(a,hint);
1159  if (r > bestr)
1160  bestr = r;
1161  }
1162  return bestr;
1163  }
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 double a
QTextDecoder* QTextCodecFromIOD::makeDecoder ( ) const
inlinevirtual

Creates a QTextDecoder 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 sufficient for only the simplest encodings where each byte corresponds to exactly one Unicode character.

The caller is responsible for deleting the returned object.

Reimplemented from QTextCodec.

Definition at line 1128 of file qtextcodec.cpp.

1129  {
1130  if ( stateless() )
1131  return QTextCodec::makeDecoder();
1132  else
1133  return new QTextCodecFromIODDecoder(this);
1134  }
virtual QTextDecoder * makeDecoder() const
Definition: qtextcodec.cpp:726
bool stateless() const
Definition: qtextcodec.cpp:970
friend class QTextCodecFromIODDecoder
Definition: qtextcodec.cpp:954
int QTextCodecFromIOD::mibEnum ( ) const
inlinevirtual

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 1141 of file qtextcodec.cpp.

1142  {
1143  return 0; // #### Unknown.
1144  }
const char* QTextCodecFromIOD::name ( ) const
inlinevirtual

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 1136 of file qtextcodec.cpp.

1137  {
1138  return n;
1139  }
bool QTextCodecFromIOD::ok ( ) const
inline

Definition at line 1123 of file qtextcodec.cpp.

1124  {
1125  return !!from_unicode_page;
1126  }
char ** from_unicode_page
Definition: qtextcodec.cpp:960
bool QTextCodecFromIOD::stateless ( ) const
inlineprivate

Definition at line 970 of file qtextcodec.cpp.

970 { return !to_unicode_multibyte; }
QMultiByteUnicodeTable * to_unicode_multibyte
Definition: qtextcodec.cpp:966
QString QTextCodecFromIOD::toUnicode ( const char *  chars,
int  len 
) const
inlinevirtual

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 1165 of file qtextcodec.cpp.

1166  {
1167  const uchar* uchars = (const uchar*)chars;
1168  QString result;
1170  if ( multibyte ) {
1171  while (len--) {
1172  QMultiByteUnicodeTable& mb = multibyte[*uchars];
1173  if ( mb.multibyte ) {
1174  // Chained multi-byte
1175  multibyte = mb.multibyte;
1176  } else {
1177  result += QChar(mb.unicode);
1178  multibyte=to_unicode_multibyte;
1179  }
1180  uchars++;
1181  }
1182  } else {
1183  while (len--)
1184  result += QChar(to_unicode[*uchars++]);
1185  }
1186  return result;
1187  }
QMultiByteUnicodeTable * multibyte
Definition: qtextcodec.cpp:925
static QCString result
QMultiByteUnicodeTable * to_unicode_multibyte
Definition: qtextcodec.cpp:966
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
unsigned char uchar
Definition: nybbler.cc:11
static constexpr double mb
Definition: Units.h:79

Friends And Related Function Documentation

friend class QTextCodecFromIODDecoder
friend

Definition at line 954 of file qtextcodec.cpp.

Member Data Documentation

QStrList QTextCodecFromIOD::aliases
private

Definition at line 968 of file qtextcodec.cpp.

char** QTextCodecFromIOD::from_unicode_page
private

Definition at line 960 of file qtextcodec.cpp.

char*** QTextCodecFromIOD::from_unicode_page_multibyte
private

Definition at line 961 of file qtextcodec.cpp.

int QTextCodecFromIOD::max_bytes_per_char
private

Definition at line 967 of file qtextcodec.cpp.

QCString QTextCodecFromIOD::n
private

Definition at line 956 of file qtextcodec.cpp.

ushort* QTextCodecFromIOD::to_unicode
private

Definition at line 965 of file qtextcodec.cpp.

QMultiByteUnicodeTable* QTextCodecFromIOD::to_unicode_multibyte
private

Definition at line 966 of file qtextcodec.cpp.

char QTextCodecFromIOD::unkn
private

Definition at line 962 of file qtextcodec.cpp.


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