src/corelib/codecs/qtextcodec.cpp
changeset 30 5dc02b23752f
parent 22 79de32ba3296
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   103 // enabling this is not exception safe!
   103 // enabling this is not exception safe!
   104 // #define Q_DEBUG_TEXTCODEC
   104 // #define Q_DEBUG_TEXTCODEC
   105 
   105 
   106 QT_BEGIN_NAMESPACE
   106 QT_BEGIN_NAMESPACE
   107 
   107 
   108 #ifndef QT_NO_TEXTCODECPLUGIN
   108 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
   109 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
   109 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
   110     (QTextCodecFactoryInterface_iid, QLatin1String("/codecs")))
   110     (QTextCodecFactoryInterface_iid, QLatin1String("/codecs")))
   111 #endif
   111 #endif
   112 
   112 
   113 
   113 
   147 }
   147 }
   148 
   148 
   149 
   149 
   150 static QTextCodec *createForName(const QByteArray &name)
   150 static QTextCodec *createForName(const QByteArray &name)
   151 {
   151 {
   152 #ifndef QT_NO_TEXTCODECPLUGIN
   152 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
   153     QFactoryLoader *l = loader();
   153     QFactoryLoader *l = loader();
   154     QStringList keys = l->keys();
   154     QStringList keys = l->keys();
   155     for (int i = 0; i < keys.size(); ++i) {
   155     for (int i = 0; i < keys.size(); ++i) {
   156         if (nameMatch(name, keys.at(i).toLatin1())) {
   156         if (nameMatch(name, keys.at(i).toLatin1())) {
   157             QString realName = keys.at(i);
   157             QString realName = keys.at(i);
   411 #endif
   411 #endif
   412     delete mbcs;
   412     delete mbcs;
   413     return s;
   413     return s;
   414 }
   414 }
   415 
   415 
   416 QByteArray QWindowsLocalCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *) const
   416 QByteArray QWindowsLocalCodec::convertFromUnicode(const QChar *ch, int uclen, ConverterState *) const
   417 {
   417 {
   418     return qt_winQString2MB(uc, len);
   418     if (!ch)
       
   419         return QByteArray();
       
   420     if (uclen == 0)
       
   421         return QByteArray("");
       
   422     BOOL used_def;
       
   423     QByteArray mb(4096, 0);
       
   424     int len;
       
   425     while (!(len=WideCharToMultiByte(CP_ACP, 0, (const wchar_t*)ch, uclen,
       
   426                 mb.data(), mb.size()-1, 0, &used_def)))
       
   427     {
       
   428         int r = GetLastError();
       
   429         if (r == ERROR_INSUFFICIENT_BUFFER) {
       
   430             mb.resize(1+WideCharToMultiByte(CP_ACP, 0,
       
   431                                 (const wchar_t*)ch, uclen,
       
   432                                 0, 0, 0, &used_def));
       
   433                 // and try again...
       
   434         } else {
       
   435 #ifndef QT_NO_DEBUG
       
   436             // Fail.
       
   437             qWarning("WideCharToMultiByte: Cannot convert multibyte text (error %d): %s (UTF-8)",
       
   438                 r, QString(ch, uclen).toLocal8Bit().data());
       
   439 #endif
       
   440             break;
       
   441         }
       
   442     }
       
   443     mb.resize(len);
       
   444     return mb;
   419 }
   445 }
   420 
   446 
   421 
   447 
   422 QByteArray QWindowsLocalCodec::name() const
   448 QByteArray QWindowsLocalCodec::name() const
   423 {
   449 {
  1113 
  1139 
  1114 #ifndef QT_NO_THREAD
  1140 #ifndef QT_NO_THREAD
  1115     locker.unlock();
  1141     locker.unlock();
  1116 #endif
  1142 #endif
  1117 
  1143 
  1118 #ifndef QT_NO_TEXTCODECPLUGIN
  1144 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
  1119     QFactoryLoader *l = loader();
  1145     QFactoryLoader *l = loader();
  1120     QStringList keys = l->keys();
  1146     QStringList keys = l->keys();
  1121     for (int i = 0; i < keys.size(); ++i) {
  1147     for (int i = 0; i < keys.size(); ++i) {
  1122         if (!keys.at(i).startsWith(QLatin1String("MIB: "))) {
  1148         if (!keys.at(i).startsWith(QLatin1String("MIB: "))) {
  1123             QByteArray name = keys.at(i).toLatin1();
  1149             QByteArray name = keys.at(i).toLatin1();
  1153 
  1179 
  1154 #ifndef QT_NO_THREAD
  1180 #ifndef QT_NO_THREAD
  1155     locker.unlock();
  1181     locker.unlock();
  1156 #endif
  1182 #endif
  1157 
  1183 
  1158 #ifndef QT_NO_TEXTCODECPLUGIN
  1184 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
  1159     QFactoryLoader *l = loader();
  1185     QFactoryLoader *l = loader();
  1160     QStringList keys = l->keys();
  1186     QStringList keys = l->keys();
  1161     for (int i = 0; i < keys.size(); ++i) {
  1187     for (int i = 0; i < keys.size(); ++i) {
  1162         if (keys.at(i).startsWith(QLatin1String("MIB: "))) {
  1188         if (keys.at(i).startsWith(QLatin1String("MIB: "))) {
  1163             int mib = keys.at(i).mid(5).toInt();
  1189             int mib = keys.at(i).mid(5).toInt();
  1289 QTextDecoder* QTextCodec::makeDecoder() const
  1315 QTextDecoder* QTextCodec::makeDecoder() const
  1290 {
  1316 {
  1291     return new QTextDecoder(this);
  1317     return new QTextDecoder(this);
  1292 }
  1318 }
  1293 
  1319 
       
  1320 /*!
       
  1321     Creates a QTextDecoder with a specified \a flags to decode chunks
       
  1322     of \c{char *} data to create chunks of Unicode data.
       
  1323 
       
  1324     The caller is responsible for deleting the returned object.
       
  1325 
       
  1326     \since 4.7
       
  1327 */
       
  1328 QTextDecoder* QTextCodec::makeDecoder(QTextCodec::ConversionFlags flags) const
       
  1329 {
       
  1330     return new QTextDecoder(this, flags);
       
  1331 }
       
  1332 
  1294 
  1333 
  1295 /*!
  1334 /*!
  1296     Creates a QTextEncoder which stores enough state to encode chunks
  1335     Creates a QTextEncoder which stores enough state to encode chunks
  1297     of Unicode data as \c{char *} data.
  1336     of Unicode data as \c{char *} data.
  1298 
  1337 
  1299     The caller is responsible for deleting the returned object.
  1338     The caller is responsible for deleting the returned object.
  1300 */
  1339 */
  1301 QTextEncoder* QTextCodec::makeEncoder() const
  1340 QTextEncoder* QTextCodec::makeEncoder() const
  1302 {
  1341 {
  1303     return new QTextEncoder(this);
  1342     return new QTextEncoder(this);
       
  1343 }
       
  1344 
       
  1345 /*!
       
  1346     Creates a QTextEncoder with a specified \a flags to encode chunks
       
  1347     of Unicode data as \c{char *} data.
       
  1348 
       
  1349     The caller is responsible for deleting the returned object.
       
  1350 
       
  1351     \since 4.7
       
  1352 */
       
  1353 QTextEncoder* QTextCodec::makeEncoder(QTextCodec::ConversionFlags flags) const
       
  1354 {
       
  1355     return new QTextEncoder(this, flags);
  1304 }
  1356 }
  1305 
  1357 
  1306 /*!
  1358 /*!
  1307     \fn QByteArray QTextCodec::fromUnicode(const QChar *input, int number,
  1359     \fn QByteArray QTextCodec::fromUnicode(const QChar *input, int number,
  1308                                            ConverterState *state) const
  1360                                            ConverterState *state) const
  1440 /*!
  1492 /*!
  1441     \fn QTextEncoder::QTextEncoder(const QTextCodec *codec)
  1493     \fn QTextEncoder::QTextEncoder(const QTextCodec *codec)
  1442 
  1494 
  1443     Constructs a text encoder for the given \a codec.
  1495     Constructs a text encoder for the given \a codec.
  1444 */
  1496 */
       
  1497 
       
  1498 /*!
       
  1499     Constructs a text encoder for the given \a codec and conversion \a flags.
       
  1500 
       
  1501     \since 4.7
       
  1502 */
       
  1503 QTextEncoder::QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags)
       
  1504     : c(codec), state()
       
  1505 {
       
  1506     state.flags = flags;
       
  1507 }
  1445 
  1508 
  1446 /*!
  1509 /*!
  1447     Destroys the encoder.
  1510     Destroys the encoder.
  1448 */
  1511 */
  1449 QTextEncoder::~QTextEncoder()
  1512 QTextEncoder::~QTextEncoder()
  1516 /*!
  1579 /*!
  1517     \fn QTextDecoder::QTextDecoder(const QTextCodec *codec)
  1580     \fn QTextDecoder::QTextDecoder(const QTextCodec *codec)
  1518 
  1581 
  1519     Constructs a text decoder for the given \a codec.
  1582     Constructs a text decoder for the given \a codec.
  1520 */
  1583 */
       
  1584 
       
  1585 /*!
       
  1586     Constructs a text decoder for the given \a codec and conversion \a flags.
       
  1587 
       
  1588     \since 4.7
       
  1589 */
       
  1590 
       
  1591 QTextDecoder::QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags)
       
  1592     : c(codec), state()
       
  1593 {
       
  1594     state.flags = flags;
       
  1595 }
  1521 
  1596 
  1522 /*!
  1597 /*!
  1523     Destroys the decoder.
  1598     Destroys the decoder.
  1524 */
  1599 */
  1525 QTextDecoder::~QTextDecoder()
  1600 QTextDecoder::~QTextDecoder()