void DataBlockFile::prepareToWrite() { // std::cerr << "DataBlockFile[" << m_fileName << "]: prepareToWrite" << std::endl; if (!m_file.isWritable()) { m_file.close(); m_file.open(QIODevice::WriteOnly | QIODevice::Append); Q_ASSERT(m_file.isWritable()); } }
/** \fn copy(QFile&,QFile&,uint) * \brief Copies src file to dst file. * * If the dst file is open, it must be open for writing. * If the src file is open, if must be open for reading. * * The files will be in the same open or close state after * this function runs as they were prior to this function being called. * * This function does not care if the files are actual files. * For compatibility with pipes and socket streams the file location * will not be reset to 0 at the end of this function. If the function * is successful the file pointers will be at the end of the copied * data. * * \param dst Destination QFile * \param src Source QFile * \param block_size Optional block size in bytes, must be at least 1024, * otherwise the default of 16 KB will be used. * \return bytes copied on success, -1 on failure. */ long long copy(QFile &dst, QFile &src, uint block_size) { uint buflen = (block_size < 1024) ? (16 * 1024) : block_size; char *buf = new char[buflen]; bool odst = false, osrc = false; if (!buf) return -1LL; if (!dst.isWritable() && !dst.isOpen()) odst = dst.open(QIODevice::Unbuffered|QIODevice::WriteOnly|QIODevice::Truncate); if (!src.isReadable() && !src.isOpen()) osrc = src.open(QIODevice::Unbuffered|QIODevice::ReadOnly); bool ok = dst.isWritable() && src.isReadable(); long long total_bytes = 0LL; while (ok) { long long rlen, wlen, off = 0; rlen = src.read(buf, buflen); if (rlen<0) { VERBOSE(VB_IMPORTANT, "util.cpp:copy: read error"); ok = false; break; } if (rlen==0) break; total_bytes += (long long) rlen; while ((rlen-off>0) && ok) { wlen = dst.write(buf + off, rlen - off); if (wlen>=0) off+= wlen; if (wlen<0) { VERBOSE(VB_IMPORTANT, "util.cpp:copy: write error"); ok = false; } } } delete[] buf; if (odst) dst.close(); if (osrc) src.close(); return (ok) ? total_bytes : -1LL; }
static __forceinline void doWriteOutput(QFile &outFile, const QResource *const resource) { for(int i = 0; i < 64; i++) { if(outFile.open(QIODevice::WriteOnly)) { break; } if(i == 0) { qWarning("Failed to open file on first attemp, retrying..."); } Sleep(25); } //Write data to file if(outFile.isOpen() && outFile.isWritable()) { if(outFile.write(reinterpret_cast<const char*>(resource->data()), resource->size()) != resource->size()) { QFile::remove(QFileInfo(outFile).canonicalFilePath()); MUTILS_THROW_FMT("File '%s' could not be written!", MUTILS_UTF8(QFileInfo(outFile).fileName())); } } else { MUTILS_THROW_FMT("File '%s' could not be created!", MUTILS_UTF8(QFileInfo(outFile).fileName())); } //Close file after it has been written outFile.close(); }
void XmlSerializable :: saveAsXml(const QFileInfo& fileinfo, const char* element_tag) const { QString s = getAsXml(element_tag); if (s.isNull()) ntk_throw_exception("Empty xml document."); QFile file (fileinfo.absoluteFilePath()); qDebug() << fileinfo.absoluteFilePath(); file.open(QFile::WriteOnly); ntk_throw_exception_if(!file.isWritable(), "Cannot write into file " + fileinfo.absoluteFilePath().toStdString()); file.write(s.toUtf8()); file.close(); }
bool File::writable(const String& file) { QFileInfo fi(file.toQString()); bool tmp = false; if (fi.exists()) { tmp = fi.isWritable(); } else { QFile f; f.setFileName(file.toQString()); f.open(QIODevice::WriteOnly); tmp = f.isWritable(); f.remove(); } return tmp; }
void RxDev::openSpecFile(){ const QModelIndex index = ui->treeView_availableNodes->selectionModel()->currentIndex(); QModelIndex seekRoot = index; while(seekRoot.parent() != QModelIndex()) { seekRoot = seekRoot.parent(); } QString filePath = seekRoot.child(0,0).child(0,0).data(Qt::DisplayRole).toString(); //qDebug()<<filePath; SpecFileParser *specParser = new SpecFileParser; specParser->nodeParser(filePath); SpecFileEdit specFile(&specParser->node); //qDebug()<<QString::fromStdString(specParser->node.type); specFile.setWindowTitle("Specfile: "+seekRoot.data(Qt::DisplayRole).toString()); bool accept = specFile.exec(); if ((accept)){ QFile file; file.setFileName(filePath); file.open(QIODevice::WriteOnly | QIODevice::Text); if (file.isWritable()){ QString tempContens = specParser->writeSpecFile(&specParser->node); QTextStream text(&file); text<<tempContens; //qDebug()<<"subs "<<specFile.getSubscriptions(); file.close(); QMessageBox::information( this, "File written!", "The file "+filePath+" was updated\n", QMessageBox::Ok, 0 ); on_pushButton_refreshNodesOrComps_clicked(); } else QMessageBox::critical(this, "File is write protected!", "The file "+filePath+" could not get updated\n", QMessageBox::Ok, 0 ); // setType(nodeEdit.getType()); // setName(nodeEdit.getName()); // setArgs(nodeEdit.getArgs()); } // QDesktopServices::openUrl(QUrl::fromLocalFile( filePath)); }
bool FormattedTouchstone::Write(FormattedNetworkData &network, QString filename) { QFile file; CreateFile(file, filename, network); if (file.isWritable() == false) { if (file.isOpen()) file.close(); return(false); } //else QTextStream snpFile(&file); WriteComments(network, snpFile); WriteOptions(network, snpFile); if (network.numberOfPorts() != 2) WriteData(network, snpFile); else { FormattedNetworkData copyNetwork = network; Flip2Ports(copyNetwork); WriteData(copyNetwork, snpFile); } snpFile.flush(); file.close(); return(true); }
ClientSocketOpt::ClientSocketOpt(QObject *parent) : QObject(parent) { timerid = -1; recvCounts = 0; socket = nullptr; socketThread = new QThread; socketThread->setObjectName("client socket thread"); this->moveToThread(socketThread); connect(socketThread, &QThread::finished, this, [=] () { this->deleteLater(); socketThread->deleteLater(); }); connect(this, &ClientSocketOpt::connectHost, this, [=] (QString ip, quint16 port) { socket = new QTcpSocket(this); connect(socket, &QTcpSocket::connected, this, [=] () { // connected emit connected(); }); socket->connectToHost(ip, port); if (!socket->waitForConnected()) { emit recvInformation(cn("接收终止")); emit disconnected(); socketThread->quit(); return; } QFile *file = new QFile(this); TcpDataDeal *deal = new TcpDataDeal(this); MessageDataDeal *msg = new MessageDataDeal(this); connect(deal, &TcpDataDeal::fullFrameData, msg, &MessageDataDeal::ParserFrameData); connect(msg, &MessageDataDeal::recvMessageData, this, [=] (MessageData data) { if (data.datatype() != FileSendType && data.datatype() != commonStr) return; if (data.datatype() == commonStr) { MessageCharData *charconent = dynamic_cast<MessageCharData*>(data.content()); if (charconent && charconent->type() == FileInfo) { QString strinfo = QString::fromLocal8Bit(charconent->content()); emit recvInformation(strinfo); } return; } // write file FileDataContent *content = dynamic_cast<FileDataContent*>(data.content()); if (!content) return; FileDataContent::FileHead *head = content->sendInfo(); FileDataContent::FileNameHead *namehead = content->fileName(); emit recvProgress(head->totalProgress >= 100 ? 99 : head->totalProgress, head->currentProgress / 100.0); QString filename = namehead->name.replace(namehead->path, savePath + "/"); QFileInfo info(filename); emit recvInformation(cn("正在接收 %1").arg(info.fileName())); QDir dir(info.absolutePath()); if (!dir.exists()) dir.mkpath(info.absolutePath()); if (file->fileName() != filename) { file->close(); if (info.exists()) { QString dt = QDateTime::currentDateTime().toString("yyyyMMddhhmmss"); filename = info.absolutePath() + "/" + info.bundleName() + dt + "." + info.suffix(); } file->setFileName(filename); } if (!file->isOpen()) { file->open(QIODevice::WriteOnly | QIODevice::Append); } if (file->isWritable()) { file->write(content->data(), head->fileCurrentSize); if (head->currentProgress == 100) file->close(); } if (socketThread->isInterruptionRequested()) { emit recvInformation(cn("接收终止")); file->close(); socket->disconnectFromHost(); socket->abort(); socketThread->quit(); } if (head->currentProgress == 100 && head->totalProgress == 100) { file->close(); emit recvProgress(100, 1); emit recvInformation(cn("接收完成")); emit disconnected(); socket->disconnectFromHost(); socket->abort(); socketThread->quit(); } }); connect(socket, &QTcpSocket::disconnected, this, [=] () { // disconnected }); connect(socket, static_cast<void(QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error), this, [=] () { emit recvInformation(cn("接收终止")); emit disconnected(); socketThread->quit(); }); connect(this, &ClientSocketOpt::senddata, this, [=] (QByteArray ba) { if (socket->state() != QAbstractSocket::ConnectedState) return; socket->write(ba); socket->waitForBytesWritten(); socket->flush(); }); connect(socket, &QTcpSocket::readyRead, this, [=] () { // read data QByteArray ba = socket->readAll(); recvCounts += ba.length(); NetworkData nd(ba.data(), ba.length()); deal->slotPosData(nd); }); timerid = startTimer(1000); }); socketThread->start(); }