// Code taken from QCString::insert, but trailing nul removed void KMail::Util::insert(QByteArray &that, uint index, const char *s) { int len = qstrlen(s); if(len == 0) return; uint olen = that.size(); int nlen = olen + len; if(index >= olen) // insert after end of string { that.detach(); if(that.resize(nlen + index - olen, QByteArray::SpeedOptim)) { memset(that.data() + olen, ' ', index - olen); memcpy(that.data() + index, s, len); } } else { that.detach(); if(that.resize(nlen, QByteArray::SpeedOptim)) // normal insert { memmove(that.data() + index + len, that.data() + index, olen - index); memcpy(that.data() + index, s, len); } } }
void DynamicPropertyData::setProperty(const char *name, const QVariant &value, const QList<QByteArray> &gNames, const QList<Setter> &gSetters) { QByteArray prop = QByteArray::fromRawData(name, strlen(name)); int id = gNames.indexOf(prop); if (id < 0) { id = names.indexOf(prop); if (!value.isValid()) { if(id < 0) return; names.removeAt(id); values.removeAt(id); } else { if (id < 0) { prop.detach(); names.append(prop); values.append(value); } else { values[id] = value; } } } else { (this->*gSetters.at(id))(value); } }
void KMail::Util::append(QByteArray &that, const QCString &str) { that.detach(); uint len1 = that.size(); uint len2 = str.size() - 1; if(that.resize(len1 + len2, QByteArray::SpeedOptim)) memcpy(that.data() + len1, str.data(), len2); }
TextSymbol* OgrFileImport::getSymbolForLabel(OGRStyleToolH tool, const QByteArray&) { Q_ASSERT(OGR_ST_GetType(tool) == OGRSTCLabel); int is_null; auto label_string = OGR_ST_GetParamStr(tool, OGRSTLabelTextString, &is_null); if (is_null) return nullptr; auto color_string = OGR_ST_GetParamStr(tool, OGRSTLabelFColor, &is_null); auto font_size_string = OGR_ST_GetParamStr(tool, OGRSTLabelSize, &is_null); // Don't use the style string as a key: The style contains the label. QByteArray key; key.reserve(qstrlen(color_string) + qstrlen(font_size_string) + 1); key.append(color_string); key.append(font_size_string); auto text_symbol = static_cast<TextSymbol*>(text_symbols.value(key)); if (!text_symbol) { text_symbol = static_cast<TextSymbol*>(default_text_symbol->duplicate()); auto color = makeColor(tool, color_string); if (color) text_symbol->setColor(color); else text_symbol->setHidden(true); auto font_size = OGR_ST_GetParamDbl(tool, OGRSTLabelSize, &is_null); if (!is_null && font_size > 0.0) text_symbol->scale(font_size / text_symbol->getFontSize()); key.detach(); text_symbols.insert(key, text_symbol); map->addSymbol(text_symbol, map->getNumSymbols()); } auto anchor = qBound(1, OGR_ST_GetParamNum(tool, OGRSTLabelAnchor, &is_null), 12); if (is_null) anchor = 1; auto angle = OGR_ST_GetParamDbl(tool, OGRSTLabelAngle, &is_null); if (is_null) angle = 0.0; QString description; description.reserve(qstrlen(label_string) + 100); description.append(QString::number(100 + anchor)); description.append(QString::number(angle, 'g', 1)); description.append(QLatin1Char(' ')); description.append(label_string); text_symbol->setDescription(description); return text_symbol; }
void KMail::Util::append(QByteArray &that, const char *str) { if(!str) return; // nothing to append that.detach(); uint len1 = that.size(); uint len2 = qstrlen(str); if(that.resize(len1 + len2, QByteArray::SpeedOptim)) memcpy(that.data() + len1, str, len2); }
void _replyFinished() { Q_Q( NetworkAccessManagerProxy ); QNetworkReply *reply = static_cast<QNetworkReply*>( q->sender() ); KUrl url = reply->request().url(); QList<CallBackData*> callbacks = urlMap.values( url ); urlMap.remove( url ); QByteArray data = reply->readAll(); data.detach(); // detach so the bytes are not deleted before methods are invoked foreach( const CallBackData *cb, callbacks ) { // There may have been a redirect. KUrl redirectUrl = q->getRedirectUrl( reply ); // Check if there's no redirect. if( redirectUrl.isEmpty() ) { QByteArray sig = QMetaObject::normalizedSignature( cb->method ); sig.remove( 0, 1 ); // remove first char, which is the member code (see qobjectdefs.h) // and let Qt's meta object system handle the rest. if( cb->receiver ) { bool success( false ); const QMetaObject *mo = cb->receiver.data()->metaObject(); int methodIndex = mo->indexOfSlot( sig ); if( methodIndex != -1 ) { Error err = { reply->error(), reply->errorString() }; QMetaMethod method = mo->method( methodIndex ); success = method.invoke( cb->receiver.data(), cb->type, Q_ARG( KUrl, reply->request().url() ), Q_ARG( QByteArray, data ), Q_ARG( NetworkAccessManagerProxy::Error, err ) ); } if( !success ) { debug() << QString( "Failed to invoke method %1 of %2" ) .arg( QString(sig) ).arg( mo->className() ); } } } else { debug() << "the server is redirecting the request to: " << redirectUrl; // Let's try to fetch the data again, but this time from the new url. QNetworkReply *newReply = q->getData( redirectUrl, cb->receiver.data(), cb->method, cb->type ); emit q->requestRedirected( url, redirectUrl ); emit q->requestRedirected( reply, newReply ); } }
void FileRetriever::slotResult(KIO::Job *job) { QByteArray data = d->buffer->buffer(); data.detach(); delete d->buffer; d->buffer = NULL; d->lastError = job->error(); emit dataRetrieved(data, d->lastError == 0); }
QByteArray FrameHelper::wireRepresentation() { quint8 byte = 0x00; QByteArray wireRep; quint64 payloadLength = m_payload.length(); //FIN, opcode byte = static_cast<quint8>((m_opCode & 0x0F) | (m_isFinalFrame ? 0x80 : 0x00)); //FIN, opcode //RSV1-3 byte |= static_cast<quint8>(((m_rsv1 & 0x01) << 6) | ((m_rsv2 & 0x01) << 5) | ((m_rsv3 & 0x01) << 4)); wireRep.append(static_cast<char>(byte)); byte = 0x00; if (m_mask != 0) { byte |= 0x80; } if (payloadLength <= 125) { byte |= static_cast<quint8>(payloadLength); wireRep.append(static_cast<char>(byte)); } else if (payloadLength <= 0xFFFFU) { byte |= 126; wireRep.append(static_cast<char>(byte)); quint16 swapped = qToBigEndian<quint16>(static_cast<quint16>(payloadLength)); wireRep.append(static_cast<const char *>(static_cast<const void *>(&swapped)), 2); } else { byte |= 127; wireRep.append(static_cast<char>(byte)); quint64 swapped = qToBigEndian<quint64>(payloadLength); wireRep.append(static_cast<const char *>(static_cast<const void *>(&swapped)), 8); } //Write mask if (m_mask != 0) { wireRep.append(static_cast<const char *>(static_cast<const void *>(&m_mask)), sizeof(quint32)); } QByteArray tmpData = m_payload; if (m_mask) { tmpData.detach(); QWebSocketProtocol::mask(&tmpData, m_mask); } wireRep.append(tmpData); return wireRep; }
static ExtensionNode *ensureNode(const QMetaObject *meta) { QByteArray id = QByteArray::fromRawData(meta->className(), qstrlen(meta->className())); ExtensionNodeHash::Iterator it = d->nodes.find(id); if (it != d->nodes.end()) return it.value(); ExtensionNode *parent = meta->superClass() ? ensureNode(meta->superClass()) : 0; id.detach(); ExtensionNode *node = d->nodes.insert(id, new ExtensionNode).value(); if (parent) parent->children << node; return node; }
bool NoSqlDatabase::fetch(const QString &key, QByteArray &data) { qDebug() << "Fetching data" << key; ::std::string value; if( ! m_db->Get(leveldb::ReadOptions(), key.toStdString(), &value).ok() ) { qDebug() << "Data not found" << key; return false; } data.setRawData(value.c_str(), value.size()); data.detach(); return true; }
/** Load a QByteArray from a stream with only one leading byte instead of 4. */ void ShortLoad (QDataStream &s, QByteArray &str) { str.detach(); quint8 len; s >> len; // read size of string if ( len == 0 || s.atEnd() ) { // end of file reached str.resize( 0 ); return; } str.resize( len ); s.readRawData( str.data(), len ); return; }
void OutputRetriever::slotExited(KProcess *p) { if (!p->normalExit()) d->lastError = p->exitStatus(); QByteArray data = d->buffer->buffer(); data.detach(); delete d->buffer; d->buffer = NULL; delete d->process; d->process = NULL; emit dataRetrieved(data, p->normalExit() && p->exitStatus() == 0); }
void tst_QByteArray::constByteArray() { const char *ptr = "abc"; QByteArray cba = QByteArray::fromRawData(ptr, 3); QVERIFY(cba.constData() == ptr); cba.squeeze(); QVERIFY(cba.constData() == ptr); cba.detach(); QVERIFY(cba.size() == 3); QVERIFY(cba.capacity() == 3); QVERIFY(cba.constData() != ptr); QVERIFY(cba.constData()[0] == 'a'); QVERIFY(cba.constData()[1] == 'b'); QVERIFY(cba.constData()[2] == 'c'); QVERIFY(cba.constData()[3] == '\0'); }
static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectangle &extent, int res ) { QElapsedTimer t; t.start(); // TODO: use feedback object? (but GDAL currently does not support cancelation anyway) QgsRasterBlock *block = provider->block( 1, extent, res, res ); QByteArray data; if ( block ) { block->convert( Qgis::Float32 ); // currently we expect just floats data = block->data(); data.detach(); // this should make a deep copy delete block; } return data; }
QByteArray QgsDemHeightMapGenerator::renderSynchronously( int x, int y, int z ) { // extend the rect by half-pixel on each side? to get the values in "corners" QgsRectangle extent = mTilingScheme.tileToExtent( x, y, z ); float mapUnitsPerPixel = extent.width() / mResolution; extent.grow( mapUnitsPerPixel / 2 ); // but make sure not to go beyond the full extent (returns invalid values) QgsRectangle fullExtent = mTilingScheme.tileToExtent( 0, 0, 0 ); extent = extent.intersect( &fullExtent ); QgsRasterBlock *block = mDtm->dataProvider()->block( 1, extent, mResolution, mResolution ); QByteArray data; if ( block ) { block->convert( Qgis::Float32 ); // currently we expect just floats data = block->data(); data.detach(); // this should make a deep copy delete block; } return data; }
void ProtocolDecoder::parseBuffer(QByteArray buffer) { for (int i=0;i<buffer.size();i++) { if (static_cast<unsigned char>(buffer.at(i)) == START_BYTE) { if (m_isInPacket) { //Bad start QLOG_DEBUG() << "Bad Start"; } m_isInPacket = true; m_isInEscape = false; m_currMsg.clear(); } else if (static_cast<unsigned char>(buffer.at(i)) == STOP_BYTE) { if (!m_isInPacket) { //Bad stop QLOG_DEBUG() << "Bad Stop"; continue; } m_isInPacket = false; QByteArray toemit = m_currMsg; toemit.detach(); emit newPacket(toemit); m_currMsg.clear(); } else if (m_isInPacket) { if (m_isInEscape) { if (static_cast<unsigned char>(buffer.at(i)) == ESCAPE_START) { m_currMsg.append(START_BYTE); } else if (static_cast<unsigned char>(buffer.at(i)) == ESCAPE_ESCAPE) { m_currMsg.append(ESCAPE_BYTE); } else if (static_cast<unsigned char>(buffer.at(i)) == ESCAPE_STOP) { m_currMsg.append(STOP_BYTE); } else { //Bad escape character QLOG_DEBUG() << "Bad Escape char"; } m_isInEscape = false; } else if (static_cast<unsigned char>(buffer.at(i)) == ESCAPE_BYTE) { m_isInEscape = true; } else { m_currMsg.append(buffer.at(i)); } } else { //Out of packet bytes. QLOG_DEBUG() << "Out of packet bytes" << QString::number(buffer.at(i),16); } } }