コード例 #1
0
void test_block(const QString &fileName)
{
    QIODevice *dev = KFilterDev::deviceForFile(fileName);
    if(!dev)
    {
        kdWarning() << "dev=0" << endl;
        return;
    }
    if(!dev->open(IO_ReadOnly))
    {
        kdWarning() << "open failed " << endl;
        return;
    }

    // This is what KGzipDev::readAll could do, if QIODevice::readAll was virtual....

    QByteArray array(1024);
    int n;
    while((n = dev->readBlock(array.data(), array.size())))
    {
        kdDebug() << "readBlock returned " << n << endl << endl;
        // QCString s(array,n+1); // Terminate with 0 before printing
        // printf("%s", s.data());

        kdDebug() << "dev.at = " << dev->at() << endl;
        // kdDebug() << "f.at = " << f.at() << endl;
    }
    dev->close();
    delete dev;
}
コード例 #2
0
ファイル: qpngio.cpp プロジェクト: AliYousuf/univ-aca-mips
static
void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
    QImageIO* iio = (QImageIO*)png_get_io_ptr(png_ptr);
    QIODevice* in = iio->ioDevice();

    while (length) {
	int nr = in->readBlock((char*)data, length);
	if (nr <= 0) {
	    png_error(png_ptr, "Read Error");
	    return;
	}
	length -= nr;
    }
}
コード例 #3
0
    static
    boolean qt_fill_input_buffer(j_decompress_ptr cinfo)
    {
        int num_read;
        my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src;
        QIODevice* dev = src->iio->ioDevice();
        src->next_input_byte = src->buffer;
        num_read = dev->readBlock((char*)src->buffer, max_buf);
        if ( num_read <= 0 ) {
            // Insert a fake EOI marker - as per jpeglib recommendation
            src->buffer[0] = (JOCTET) 0xFF;
            src->buffer[1] = (JOCTET) JPEG_EOI;
            src->bytes_in_buffer = 2;
        } else {
            src->bytes_in_buffer = num_read;
        }
#if defined(_OS_UNIXWARE7_)
        return B_TRUE;
#else
        return TRUE;
#endif
    }
コード例 #4
0
ファイル: pak.cpp プロジェクト: Uiomae/kio_pak
void PakProtocol::get(const KURL &url) {
	kdDebug(PAK_DEBUG_ID) << "ArchiveProtocol::get " << url << endl;

	QString path;
	KIO::Error errorNum;
	if ( !checkNewFile( url, path, errorNum ) )
	{
		if ( errorNum == KIO::ERR_CANNOT_OPEN_FOR_READING )
		{
			// If we cannot open, it might be a problem with the archive header (e.g. unsupported format)
			// Therefore give a more specific error message
			error( KIO::ERR_SLAVE_DEFINED,
					i18n( "Could not open the file, probably due to an unsupported file format.\n%1")
					.arg( url.prettyURL() ) );
			return;
		}
		else
		{
			// We have any other error
			error( errorNum, url.prettyURL() );
			return;
		}
	}
	kdDebug(PAK_DEBUG_ID) << "Continue getting" << endl;

	path = QString::fromLocal8Bit(remoteEncoding()->encode(path));

	kdDebug(PAK_DEBUG_ID) << "Path > " << path << endl;
	const KArchiveDirectory* root = _pakFile->directory();
	const KArchiveEntry* archiveEntry = root->entry( path );

	kdDebug(PAK_DEBUG_ID) << "Check if no archiveEntry > " << archiveEntry << endl;
	if ( !archiveEntry )
	{
		error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
		return;
	}
	kdDebug(PAK_DEBUG_ID) << "archiveEntry::name > " << archiveEntry->name() << endl;
	if ( archiveEntry->isDirectory() )
	{
		error( KIO::ERR_IS_DIRECTORY, url.prettyURL() );
		return;
	}
	const KArchiveFile* archiveFileEntry = static_cast<const KArchiveFile *>(archiveEntry);
	if ( !archiveEntry->symlink().isEmpty() )
	{
		kdDebug(7109) << "Redirection to " << archiveEntry->symlink() << endl;
		KURL realURL;
		if (archiveEntry->symlink().startsWith("/")) { // absolute path
			realURL.setPath(archiveEntry->symlink() ); // goes out of tar:/, back into file:
		} else {
			realURL = KURL( url, archiveEntry->symlink() );
		}
		kdDebug(7109) << "realURL= " << realURL << endl;
		redirection( realURL );
		finished();
		return;
	}

	//kdDebug(7109) << "Preparing to get the archive data" << endl;

	/*
	 * The easy way would be to get the data by calling archiveFileEntry->data()
	 * However this has drawbacks:
	 * - the complete file must be read into the memory
	 * - errors are skipped, resulting in an empty file
	 */

	QIODevice* io = 0;
	// Getting the device is hard, as archiveFileEntry->device() is not virtual!
	if ( url.protocol() == "pak" )
	{
		io = archiveFileEntry->device();
	}
	else
	{
		// Wrong protocol? Why was this not catched by checkNewFile?
		kdWarning(7109) << "Protocol " << url.protocol() << " not supported by this IOSlave; " << k_funcinfo << endl;
		error( KIO::ERR_UNSUPPORTED_PROTOCOL, url.protocol() );
		return;
	}

	if (!io)
	{
		error( KIO::ERR_SLAVE_DEFINED,
				i18n( "The archive file could not be opened, perhaps because the format is unsupported.\n%1" )
				.arg( url.prettyURL() ) );
		return;
	}

	if ( !io->open( IO_ReadOnly ) )
	{
		error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.prettyURL() );
		return;
	}

	totalSize( archiveFileEntry->size() );

	// Size of a QIODevice read. It must be large enough so that the mime type check will not fail
	const int maxSize = 0x100000; // 1MB

	int bufferSize = kMin( maxSize, archiveFileEntry->size() );
	QByteArray buffer ( bufferSize );
	if ( buffer.isEmpty() && bufferSize > 0 )
	{
		// Something went wrong
		error( KIO::ERR_OUT_OF_MEMORY, url.prettyURL() );
		return;
	}

	bool firstRead = true;

	// How much file do we still have to process?
	int fileSize = archiveFileEntry->size();
	KIO::filesize_t processed = 0;

	while ( !io->atEnd() && fileSize > 0 )
	{
		if ( !firstRead )
		{
			bufferSize = kMin( maxSize, fileSize );
			buffer.resize( bufferSize, QGArray::SpeedOptim );
		}
		const Q_LONG read = io->readBlock( buffer.data(), buffer.size() ); // Avoid to use bufferSize here, in case something went wrong.
		if ( read != bufferSize )
		{
			kdWarning(7109) << "Read " << read << " bytes but expected " << bufferSize << endl;
			error( KIO::ERR_COULD_NOT_READ, url.prettyURL() );
			return;
		}
		if ( firstRead )
		{
			// We use the magic one the first data read
			// (As magic detection is about fixed positions, we can be sure that it is enough data.)
			KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( buffer, path );
			kdDebug(7109) << "Emitting mimetype " << result->mimeType() << endl;
			mimeType( result->mimeType() );
			firstRead = false;
		}
		data( buffer );
		processed += read;
		processedSize( processed );
		fileSize -= bufferSize;
	}
	io->close();
	delete io;

	data( QByteArray() );

	finished();

	/*kdDebug(PAK_DEBUG_ID) << "Entering get()" << endl;
	mimetype("text/plain");
	QCString str("Hello Pak World!!");
	data(str);
	finished();
	kdDebug(PAK_DEBUG_ID) << "Exiting get()" << endl;*/
}