src/corelib/codecs/qtextcodec.cpp
changeset 22 79de32ba3296
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
19:fcece45ef507 22:79de32ba3296
   108 #ifndef QT_NO_TEXTCODECPLUGIN
   108 #ifndef 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 static char qtolower(register char c)
   114 static char qtolower(register char c)
   114 { if (c >= 'A' && c <= 'Z') return c + 0x20; return c; }
   115 { if (c >= 'A' && c <= 'Z') return c + 0x20; return c; }
   115 static bool qisalnum(register char c)
   116 static bool qisalnum(register char c)
   116 { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
   117 { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
   117 
   118 
   221     destroying_is_ok = false;
   222     destroying_is_ok = false;
   222 #endif
   223 #endif
   223 }
   224 }
   224 
   225 
   225 Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup)
   226 Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup)
       
   227 
       
   228 bool QTextCodec::validCodecs()
       
   229 {
       
   230 #ifdef Q_OS_SYMBIAN
       
   231     // If we don't have a trap handler, we're outside of the main() function,
       
   232     // ie. in global constructors or destructors. Don't use codecs in this
       
   233     // case as it would lead to crashes because we don't have a cleanup stack on Symbian
       
   234     return (User::TrapHandler() != NULL);
       
   235 #else
       
   236     return true;
       
   237 #endif
       
   238 }
       
   239 
   226 
   240 
   227 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
   241 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
   228 class QWindowsLocalCodec: public QTextCodec
   242 class QWindowsLocalCodec: public QTextCodec
   229 {
   243 {
   230 public:
   244 public:
   681 // textCodecsMutex need to be locked to enter this function
   695 // textCodecsMutex need to be locked to enter this function
   682 static void setup()
   696 static void setup()
   683 {
   697 {
   684     if (all)
   698     if (all)
   685         return;
   699         return;
       
   700 
       
   701 #ifdef Q_OS_SYMBIAN
       
   702     // If we don't have a trap handler, we're outside of the main() function,
       
   703     // ie. in global constructors or destructors. Don't create codecs in this
       
   704     // case as it would lead to crashes because of a missing cleanup stack on Symbian
       
   705     if (User::TrapHandler() == NULL)
       
   706         return;
       
   707 #endif
   686 
   708 
   687 #ifdef Q_DEBUG_TEXTCODEC
   709 #ifdef Q_DEBUG_TEXTCODEC
   688     if (destroying_is_ok)
   710     if (destroying_is_ok)
   689         qWarning("QTextCodec: Creating new codec during codec cleanup");
   711         qWarning("QTextCodec: Creating new codec during codec cleanup");
   690 #endif
   712 #endif
   984 #ifndef QT_NO_THREAD
  1006 #ifndef QT_NO_THREAD
   985     QMutexLocker locker(textCodecsMutex());
  1007     QMutexLocker locker(textCodecsMutex());
   986 #endif
  1008 #endif
   987     setup();
  1009     setup();
   988 
  1010 
       
  1011     if (!validCodecs())
       
  1012         return 0;
       
  1013 
   989     static QHash <QByteArray, QTextCodec *> cache;
  1014     static QHash <QByteArray, QTextCodec *> cache;
   990     if (clearCaches & 0x1) {
  1015     if (clearCaches & 0x1) {
   991         cache.clear();
  1016         cache.clear();
   992         clearCaches &= ~0x1;
  1017         clearCaches &= ~0x1;
   993     }
  1018     }
  1025 #ifndef QT_NO_THREAD
  1050 #ifndef QT_NO_THREAD
  1026     QMutexLocker locker(textCodecsMutex());
  1051     QMutexLocker locker(textCodecsMutex());
  1027 #endif
  1052 #endif
  1028     setup();
  1053     setup();
  1029 
  1054 
       
  1055     if (!validCodecs())
       
  1056         return 0;
       
  1057 
  1030     static QHash <int, QTextCodec *> cache;
  1058     static QHash <int, QTextCodec *> cache;
  1031     if (clearCaches & 0x2) {
  1059     if (clearCaches & 0x2) {
  1032         cache.clear();
  1060         cache.clear();
  1033         clearCaches &= ~0x2;
  1061         clearCaches &= ~0x2;
  1034     }
  1062     }
  1072     QMutexLocker locker(textCodecsMutex());
  1100     QMutexLocker locker(textCodecsMutex());
  1073 #endif
  1101 #endif
  1074     setup();
  1102     setup();
  1075 
  1103 
  1076     QList<QByteArray> codecs;
  1104     QList<QByteArray> codecs;
       
  1105 
       
  1106     if (!validCodecs())
       
  1107         return codecs;
       
  1108 
  1077     for (int i = 0; i < all->size(); ++i) {
  1109     for (int i = 0; i < all->size(); ++i) {
  1078         codecs += all->at(i)->name();
  1110         codecs += all->at(i)->name();
  1079         codecs += all->at(i)->aliases();
  1111         codecs += all->at(i)->aliases();
  1080     }
  1112     }
  1081 
  1113 
  1110     QMutexLocker locker(textCodecsMutex());
  1142     QMutexLocker locker(textCodecsMutex());
  1111 #endif
  1143 #endif
  1112     setup();
  1144     setup();
  1113 
  1145 
  1114     QList<int> codecs;
  1146     QList<int> codecs;
       
  1147 
       
  1148     if (!validCodecs())
       
  1149         return codecs;
       
  1150 
  1115     for (int i = 0; i < all->size(); ++i)
  1151     for (int i = 0; i < all->size(); ++i)
  1116         codecs += all->at(i)->mibEnum();
  1152         codecs += all->at(i)->mibEnum();
  1117 
  1153 
  1118 #ifndef QT_NO_THREAD
  1154 #ifndef QT_NO_THREAD
  1119     locker.unlock();
  1155     locker.unlock();
  1163     "System".
  1199     "System".
  1164 */
  1200 */
  1165 
  1201 
  1166 QTextCodec* QTextCodec::codecForLocale()
  1202 QTextCodec* QTextCodec::codecForLocale()
  1167 {
  1203 {
       
  1204     if (!validCodecs())
       
  1205         return 0;
       
  1206 
  1168     if (localeMapper)
  1207     if (localeMapper)
  1169         return localeMapper;
  1208         return localeMapper;
  1170 
  1209 
  1171 #ifndef QT_NO_THREAD
  1210 #ifndef QT_NO_THREAD
  1172     QMutexLocker locker(textCodecsMutex());
  1211     QMutexLocker locker(textCodecsMutex());