bool QTranslatorPrivate::do_load(const QString &realname) { QTranslatorPrivate *d = this; bool ok = false; const bool isResourceFile = realname.startsWith(QLatin1Char(':')); if (isResourceFile) { // If the translation is in a non-compressed resource file, the data is already in // memory, so no need to use QFile to copy it again. Q_ASSERT(!d->resource); d->resource = new QResource(realname); if (d->resource->isValid() && !d->resource->isCompressed()) { d->dataLength = d->resource->size(); d->dataPointer = const_cast<uchar *>(d->resource->data()); ok = true; } else { delete d->resource; d->resource = 0; } } if (!ok) { QFile file(realname); d->dataLength = file.size(); if (!d->dataLength) return false; d->dataPointer = new uchar[d->dataLength]; if (file.open(QIODevice::ReadOnly)) { if (d->dataLength != (uint)file.read(reinterpret_cast<char*>(d->dataPointer), d->dataLength)) { delete [] d->dataPointer; d->dataPointer = 0; d->dataLength = 0; return false; } } } return d->do_load(d->dataPointer, d->dataLength); }
bool QTranslatorPrivate::do_load(const QString &realname, const QString &directory) { QTranslatorPrivate *d = this; bool ok = false; QFile file(realname); if (!file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) return false; qint64 fileSize = file.size(); if (fileSize <= MagicLength || quint32(-1) <= fileSize) return false; { char magicBuffer[MagicLength]; if (MagicLength != file.read(magicBuffer, MagicLength) || memcmp(magicBuffer, magic, MagicLength)) return false; } d->unmapLength = quint32(fileSize); #ifdef QT_USE_MMAP #ifndef MAP_FILE #define MAP_FILE 0 #endif #ifndef MAP_FAILED #define MAP_FAILED -1 #endif int fd = file.handle(); if (fd >= 0) { char *ptr; ptr = reinterpret_cast<char *>( mmap(0, d->unmapLength, // any address, whole file PROT_READ, // read-only memory MAP_FILE | MAP_PRIVATE, // swap-backed map from file fd, 0)); // from offset 0 of fd if (ptr && ptr != reinterpret_cast<char *>(MAP_FAILED)) { file.close(); d->used_mmap = true; d->unmapPointer = ptr; ok = true; } } #endif // QT_USE_MMAP if (!ok) { d->unmapPointer = new char[d->unmapLength]; if (d->unmapPointer) { file.seek(0); qint64 readResult = file.read(d->unmapPointer, d->unmapLength); if (readResult == qint64(unmapLength)) ok = true; } } if (ok && d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength, directory)) return true; #if defined(QT_USE_MMAP) if (used_mmap) { used_mmap = false; munmap(unmapPointer, unmapLength); } else #endif delete [] unmapPointer; d->unmapPointer = 0; d->unmapLength = 0; return false; }
bool QTranslatorPrivate::do_load(const QString &realname, const QString &directory) { QTranslatorPrivate *d = this; bool ok = false; if (realname.startsWith(':')) { // If the translation is in a non-compressed resource file, the data is already in // memory, so no need to use QFile to copy it again. Q_ASSERT(!d->resource); d->resource = new QResource(realname); if (resource->isValid() && !resource->isCompressed() && resource->size() > MagicLength && !memcmp(resource->data(), magic, MagicLength)) { d->unmapLength = resource->size(); d->unmapPointer = reinterpret_cast<char *>(const_cast<uchar *>(resource->data())); #if defined(QT_USE_MMAP) d->used_mmap = false; #endif ok = true; } else { delete resource; resource = 0; } } if (!ok) { QFile file(realname); if (!file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) return false; qint64 fileSize = file.size(); if (fileSize <= MagicLength || quint32(-1) <= fileSize) return false; { char magicBuffer[MagicLength]; if (MagicLength != file.read(magicBuffer, MagicLength) || memcmp(magicBuffer, magic, MagicLength)) return false; } d->unmapLength = quint32(fileSize); #ifdef QT_USE_MMAP #ifndef MAP_FILE #define MAP_FILE 0 #endif #ifndef MAP_FAILED #define MAP_FAILED -1 #endif int fd = file.handle(); if (fd >= 0) { char *ptr; ptr = reinterpret_cast<char *>( mmap(0, d->unmapLength, // any address, whole file PROT_READ, // read-only memory MAP_FILE | MAP_PRIVATE, // swap-backed map from file fd, 0)); // from offset 0 of fd if (ptr && ptr != reinterpret_cast<char *>(MAP_FAILED)) { file.close(); d->used_mmap = true; d->unmapPointer = ptr; ok = true; } } #endif // QT_USE_MMAP if (!ok) { d->unmapPointer = new char[d->unmapLength]; if (d->unmapPointer) { file.seek(0); qint64 readResult = file.read(d->unmapPointer, d->unmapLength); if (readResult == qint64(unmapLength)) ok = true; } } } if (ok && d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength, directory)) return true; #if defined(QT_USE_MMAP) if (used_mmap) { used_mmap = false; munmap(unmapPointer, unmapLength); } else #endif if (!d->resource) delete [] unmapPointer; delete d->resource; d->resource = 0; d->unmapPointer = 0; d->unmapLength = 0; return false; }
bool QTranslatorPrivate::do_load(const QString &realname) { QTranslatorPrivate *d = this; bool ok = false; const bool isResourceFile = realname.startsWith(QLatin1Char(':')); if (isResourceFile) { // If the translation is in a non-compressed resource file, the data is already in // memory, so no need to use QFile to copy it again. Q_ASSERT(!d->resource); d->resource = new QResource(realname); if (d->resource->isValid() && !d->resource->isCompressed()) { d->unmapLength = d->resource->size(); d->unmapPointer = reinterpret_cast<char *>(const_cast<uchar *>(d->resource->data())); d->used_mmap = false; ok = true; } else { delete d->resource; d->resource = 0; } } #ifdef QT_USE_MMAP #ifndef MAP_FILE #define MAP_FILE 0 #endif #ifndef MAP_FAILED #define MAP_FAILED -1 #endif else { int fd = QT_OPEN(QFile::encodeName(realname), O_RDONLY, #if defined(Q_OS_WIN) _S_IREAD | _S_IWRITE #else 0666 #endif ); if (fd >= 0) { QT_STATBUF st; if (!QT_FSTAT(fd, &st)) { char *ptr; ptr = reinterpret_cast<char *>( mmap(0, st.st_size, // any address, whole file PROT_READ, // read-only memory MAP_FILE | MAP_PRIVATE, // swap-backed map from file fd, 0)); // from offset 0 of fd if (ptr && ptr != reinterpret_cast<char *>(MAP_FAILED)) { d->used_mmap = true; d->unmapPointer = ptr; d->unmapLength = st.st_size; ok = true; } } ::close(fd); } } #endif // QT_USE_MMAP if (!ok) { QFile file(realname); d->unmapLength = file.size(); if (!d->unmapLength) return false; d->unmapPointer = new char[d->unmapLength]; if (file.open(QIODevice::ReadOnly)) ok = (d->unmapLength == (uint)file.read(d->unmapPointer, d->unmapLength)); if (!ok) { delete [] d->unmapPointer; d->unmapPointer = 0; d->unmapLength = 0; return false; } } return d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength); }