2015-03-05 00:41:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
** GeomIO Library
|
|
|
|
** Copyright FougSys (2 Mar. 2015)
|
|
|
|
** contact@fougsys.fr
|
|
|
|
**
|
|
|
|
** This software is a reusable library whose purpose is to provide complete
|
|
|
|
** I/O support for various CAD file formats (eg. STL)
|
|
|
|
**
|
|
|
|
** This software is governed by the CeCILL-B license under French law and
|
|
|
|
** abiding by the rules of distribution of free software. You can use,
|
|
|
|
** modify and/ or redistribute the software under the terms of the CeCILL-B
|
|
|
|
** license as circulated by CEA, CNRS and INRIA at the following URL
|
|
|
|
** "http://www.cecill.info".
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
#include <gmio_support/qt_stream.h>
|
2014-03-14 00:49:39 +08:00
|
|
|
|
2013-03-07 00:02:19 +08:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
#include <QtCore/QIODevice>
|
|
|
|
|
2015-03-18 23:28:25 +08:00
|
|
|
QT_USE_NAMESPACE
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
static gmio_bool_t gmio_stream_qiodevice_at_end(void* cookie)
|
2013-03-07 00:02:19 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
return static_cast<QIODevice*>(cookie)->atEnd();
|
2013-03-07 00:02:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
static int gmio_stream_qiodevice_error(void* cookie)
|
2013-03-07 00:02:19 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
const QIODevice* device = static_cast<QIODevice*>(cookie);
|
|
|
|
const QFile* file = dynamic_cast<const QFile*>(device);
|
|
|
|
if (file != NULL) {
|
|
|
|
return file->error();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const QString err_str = static_cast<QIODevice*>(cookie)->errorString();
|
|
|
|
return !err_str.isEmpty() ? 1 : 0;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-03-07 00:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 00:31:08 +08:00
|
|
|
static size_t gmio_stream_qiodevice_read(
|
|
|
|
void* cookie, void* ptr, size_t item_size, size_t item_count)
|
2013-03-07 00:02:19 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
QIODevice* device = static_cast<QIODevice*>(cookie);
|
|
|
|
const qint64 c = device->read(static_cast<char*>(ptr), item_size * item_count);
|
|
|
|
return c / item_size;
|
2013-03-07 00:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 00:31:08 +08:00
|
|
|
static size_t gmio_stream_qiodevice_write(
|
|
|
|
void* cookie, const void* ptr, size_t item_size, size_t item_count)
|
2013-03-07 00:02:19 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
QIODevice* device = static_cast<QIODevice*>(cookie);
|
|
|
|
const qint64 c = device->write(
|
|
|
|
static_cast<const char*>(ptr), item_size * item_count);
|
|
|
|
return c / item_size;
|
2013-03-07 00:02:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
void gmio_stream_set_qiodevice(gmio_stream_t* stream, QIODevice* device)
|
2013-03-07 00:02:19 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
stream->cookie = device;
|
|
|
|
stream->at_end_func = gmio_stream_qiodevice_at_end;
|
|
|
|
stream->error_func = gmio_stream_qiodevice_error;
|
|
|
|
stream->read_func = gmio_stream_qiodevice_read;
|
|
|
|
stream->write_func = gmio_stream_qiodevice_write;
|
2013-03-07 00:02:19 +08:00
|
|
|
}
|
2015-03-19 23:34:53 +08:00
|
|
|
|
|
|
|
gmio_stream_t gmio_stream_qiodevice(QIODevice* device)
|
|
|
|
{
|
|
|
|
gmio_stream_t qtstream = { 0 };
|
|
|
|
gmio_stream_set_qiodevice(&qtstream, device);
|
|
|
|
return qtstream;
|
|
|
|
}
|