From ff2368e053b370bf65aff094c45c2697e13fced3 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Sun, 20 Sep 2015 22:58:56 +0200 Subject: [PATCH] support: add (broken) support of C++ standard streams --- src/gmio_support/stream_cpp.h | 121 ++++++++++++++++++++++++++++++++++ tests/fake_support/main.cpp | 7 ++ 2 files changed, 128 insertions(+) create mode 100644 src/gmio_support/stream_cpp.h diff --git a/src/gmio_support/stream_cpp.h b/src/gmio_support/stream_cpp.h new file mode 100644 index 0000000..83e8a07 --- /dev/null +++ b/src/gmio_support/stream_cpp.h @@ -0,0 +1,121 @@ +/**************************************************************************** +** gmio +** Copyright Fougue (2 Mar. 2015) +** contact@fougue.pro +** +** 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/licences/Licence_CeCILL-B_V1-en.html". +****************************************************************************/ + +/*! \file stream_cpp.h + * Support of standard C++ streams + * +* \addtogroup gmio_support + * @{ + */ + +#ifndef GMIO_SUPPORT_STREAM_CPP_H +#define GMIO_SUPPORT_STREAM_CPP_H + +#include "support_global.h" +#include "../gmio_core/stream.h" + +#include +#include + +template +gmio_stream_t gmio_stream_cpp(STREAM* s); + +// +// Implementation +// + +template +gmio_bool_t gmio_stream_cpp_at_end(void* cookie) +{ + STREAM* s = static_cast(cookie); + return s->eof(); +} + +template +int gmio_stream_cpp_error(void* cookie) +{ + STREAM* s = static_cast(cookie); + return s->rdstate(); +} + +template +size_t gmio_stream_cpp_read( + void* cookie, void* ptr, size_t item_size, size_t item_count) +{ + STREAM* s = static_cast(cookie); + s->read(static_cast(ptr), item_size * item_count); + return s->gcount() / item_size; +} + +template +size_t gmio_stream_cpp_write( + void* cookie, const void* ptr, size_t item_size, size_t item_count) +{ + STREAM* s = static_cast(cookie); + s->write(static_cast(ptr), item_size * item_count); + // TODO: return the number of bytes actually written + return item_size * item_count; +} + +template +size_t gmio_stream_cpp_size(void* cookie) +{ + STREAM* s = static_cast(cookie); + std::streampos pos = s->tellg(); + s->seekg(0, std::ios_base::beg); + std::streampos begin_pos = s->tellg(); + s->seekg(0, std::ios_base::end); + std::streampos end_pos = s->tellg(); + s->seekg(pos, std::ios_base::beg); + return end_pos - begin_pos; +} + +template +int gmio_stream_cpp_get_pos(void* cookie, gmio_stream_pos_t* pos) +{ + STREAM* s = static_cast(cookie); + std::streampos spos = s->tellg(); + std::memcpy(&pos->cookie[0], &spos, sizeof(std::streampos)); + return 0; +} + +template +static int gmio_stream_cpp_set_pos(void* cookie, const gmio_stream_pos_t* pos) +{ + STREAM* s = static_cast(cookie); + std::streampos spos; + std::memcpy(&spos, &pos->cookie[0], sizeof(std::streampos)); + s->seekg(spos); + s->seekp(spos); + return 0; // TODO: return error code +} + +template +gmio_stream_t gmio_stream_cpp(STREAM* s) +{ + gmio_stream_t stream = gmio_stream_null(); + stream.cookie = s; + stream.func_at_end = gmio_stream_cpp_at_end; + stream.func_error = gmio_stream_cpp_error; + stream.func_read = gmio_stream_cpp_read; + stream.func_write = gmio_stream_cpp_write; + stream.func_size = gmio_stream_cpp_size; + stream.func_get_pos = gmio_stream_cpp_get_pos; + stream.func_set_pos = gmio_stream_cpp_set_pos; + return stream; +} + +#endif /* GMIO_SUPPORT_STREAM_CPP_H */ +/*! @} */ diff --git a/tests/fake_support/main.cpp b/tests/fake_support/main.cpp index aa2a383..c5a1a7b 100644 --- a/tests/fake_support/main.cpp +++ b/tests/fake_support/main.cpp @@ -6,6 +6,9 @@ #include +#include "../../src/gmio_support/stream_cpp.h" +#include + int main() { // OpenCascade @@ -16,5 +19,9 @@ int main() QFile file; gmio_stream_qiodevice(&file); + // C++ + //std::ifstream s("test.txt"); + //gmio_stream_t stream = gmio_stream_cpp(&s); + return 0; }