gmio_support: add gmio_[i/o]stream_cpp() functions
This commit is contained in:
parent
6488c03667
commit
53002389e1
@ -29,29 +29,41 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
template<typename STREAM>
|
/*! Returns a gmio_stream for C++ input stream (cookie will hold \p s ) */
|
||||||
gmio_stream_t gmio_stream_cpp(STREAM* s);
|
template<typename CHAR, typename TRAITS>
|
||||||
|
gmio_stream_t gmio_istream_cpp(std::basic_istream<CHAR, TRAITS>* s);
|
||||||
|
|
||||||
|
/*! Returns a gmio_stream for C++ output stream (cookie will hold \p s ) */
|
||||||
|
template<typename CHAR, typename TRAITS>
|
||||||
|
gmio_stream_t gmio_ostream_cpp(std::basic_ostream<CHAR, TRAITS>* s);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Implementation
|
// Implementation
|
||||||
//
|
//
|
||||||
|
#ifndef DOXYGEN
|
||||||
|
|
||||||
|
namespace gmio {
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
gmio_bool_t gmio_stream_cpp_at_end(void* cookie)
|
gmio_bool_t stream_cpp_at_end(void* cookie)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
STREAM* s = static_cast<STREAM*>(cookie);
|
||||||
return s->eof();
|
return s->eof();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
int gmio_stream_cpp_error(void* cookie)
|
int stream_cpp_error(void* cookie)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
STREAM* s = static_cast<STREAM*>(cookie);
|
||||||
return s->rdstate();
|
return s->rdstate();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
size_t gmio_stream_cpp_read(
|
size_t istream_cpp_read(
|
||||||
void* cookie, void* ptr, size_t item_size, size_t item_count)
|
void* cookie, void* ptr, size_t item_size, size_t item_count)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
STREAM* s = static_cast<STREAM*>(cookie);
|
||||||
@ -60,7 +72,7 @@ size_t gmio_stream_cpp_read(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
size_t gmio_stream_cpp_write(
|
size_t ostream_cpp_write(
|
||||||
void* cookie, const void* ptr, size_t item_size, size_t item_count)
|
void* cookie, const void* ptr, size_t item_size, size_t item_count)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
STREAM* s = static_cast<STREAM*>(cookie);
|
||||||
@ -70,7 +82,7 @@ size_t gmio_stream_cpp_write(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
size_t gmio_stream_cpp_size(void* cookie)
|
size_t istream_cpp_size(void* cookie)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
STREAM* s = static_cast<STREAM*>(cookie);
|
||||||
std::streampos pos = s->tellg();
|
std::streampos pos = s->tellg();
|
||||||
@ -78,44 +90,102 @@ size_t gmio_stream_cpp_size(void* cookie)
|
|||||||
std::streampos begin_pos = s->tellg();
|
std::streampos begin_pos = s->tellg();
|
||||||
s->seekg(0, std::ios_base::end);
|
s->seekg(0, std::ios_base::end);
|
||||||
std::streampos end_pos = s->tellg();
|
std::streampos end_pos = s->tellg();
|
||||||
s->seekg(pos, std::ios_base::beg);
|
s->seekg(pos, std::ios_base::beg); // Restore pos
|
||||||
return end_pos - begin_pos;
|
return end_pos - begin_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
int gmio_stream_cpp_get_pos(void* cookie, gmio_stream_pos_t* pos)
|
size_t ostream_cpp_size(void* cookie)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
STREAM* s = static_cast<STREAM*>(cookie);
|
||||||
std::streampos spos = s->tellg();
|
std::streampos pos = s->tellp();
|
||||||
|
s->seekp(0, std::ios_base::beg);
|
||||||
|
std::streampos begin_pos = s->tellp();
|
||||||
|
s->seekp(0, std::ios_base::end);
|
||||||
|
std::streampos end_pos = s->tellp();
|
||||||
|
s->seekp(pos, std::ios_base::beg); // Restore pos
|
||||||
|
return end_pos - begin_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
GMIO_INLINE void copy_cpp_streampos(gmio_stream_pos_t* pos, std::streampos spos)
|
||||||
|
{
|
||||||
std::memcpy(&pos->cookie[0], &spos, sizeof(std::streampos));
|
std::memcpy(&pos->cookie[0], &spos, sizeof(std::streampos));
|
||||||
|
}
|
||||||
|
|
||||||
|
GMIO_INLINE std::streampos to_cpp_streampos(const gmio_stream_pos_t* pos)
|
||||||
|
{
|
||||||
|
std::streampos spos;
|
||||||
|
std::memcpy(&spos, &pos->cookie[0], sizeof(std::streampos));
|
||||||
|
return spos;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename STREAM>
|
||||||
|
int istream_cpp_get_pos(void* cookie, gmio_stream_pos_t* pos)
|
||||||
|
{
|
||||||
|
copy_cpp_streampos(pos, static_cast<STREAM*>(cookie)->tellg());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
static int gmio_stream_cpp_set_pos(void* cookie, const gmio_stream_pos_t* pos)
|
int istream_cpp_set_pos(void* cookie, const gmio_stream_pos_t* pos)
|
||||||
{
|
{
|
||||||
STREAM* s = static_cast<STREAM*>(cookie);
|
static_cast<STREAM*>(cookie)->seekg(to_cpp_streampos(pos));
|
||||||
std::streampos spos;
|
|
||||||
std::memcpy(&spos, &pos->cookie[0], sizeof(std::streampos));
|
|
||||||
s->seekg(spos);
|
|
||||||
s->seekp(spos);
|
|
||||||
return 0; // TODO: return error code
|
return 0; // TODO: return error code
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename STREAM>
|
template<typename STREAM>
|
||||||
gmio_stream_t gmio_stream_cpp(STREAM* s)
|
int ostream_cpp_get_pos(void* cookie, gmio_stream_pos_t* pos)
|
||||||
{
|
{
|
||||||
gmio_stream_t stream = gmio_stream_null();
|
copy_cpp_streampos(pos, static_cast<STREAM*>(cookie)->tellp());
|
||||||
stream.cookie = s;
|
return 0;
|
||||||
stream.func_at_end = gmio_stream_cpp_at_end<STREAM>;
|
}
|
||||||
stream.func_error = gmio_stream_cpp_error<STREAM>;
|
|
||||||
stream.func_read = gmio_stream_cpp_read<STREAM>;
|
template<typename STREAM>
|
||||||
stream.func_write = gmio_stream_cpp_write<STREAM>;
|
static int ostream_cpp_set_pos(void* cookie, const gmio_stream_pos_t* pos)
|
||||||
stream.func_size = gmio_stream_cpp_size<STREAM>;
|
{
|
||||||
stream.func_get_pos = gmio_stream_cpp_get_pos<STREAM>;
|
static_cast<STREAM*>(cookie)->seekp(to_cpp_streampos(pos));
|
||||||
stream.func_set_pos = gmio_stream_cpp_set_pos<STREAM>;
|
return 0; // TODO: return error code
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename STREAM>
|
||||||
|
void stream_cpp_init_common(STREAM* s, gmio_stream_t* stream)
|
||||||
|
{
|
||||||
|
*stream = gmio_stream_null();
|
||||||
|
stream->cookie = s;
|
||||||
|
stream->func_at_end = gmio::internal::stream_cpp_at_end<STREAM>;
|
||||||
|
stream->func_error = gmio::internal::stream_cpp_error<STREAM>;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
} // namespace gmio
|
||||||
|
|
||||||
|
template<typename CHAR, typename TRAITS>
|
||||||
|
gmio_stream_t gmio_istream_cpp(std::basic_istream<CHAR, TRAITS>* s)
|
||||||
|
{
|
||||||
|
typedef std::basic_istream<CHAR, TRAITS> CppStream;
|
||||||
|
gmio_stream_t stream;
|
||||||
|
gmio::internal::stream_cpp_init_common(s, &stream);
|
||||||
|
stream.func_size = gmio::internal::istream_cpp_size<CppStream>;
|
||||||
|
stream.func_read = gmio::internal::istream_cpp_read<CppStream>;
|
||||||
|
stream.func_get_pos = gmio::internal::istream_cpp_get_pos<CppStream>;
|
||||||
|
stream.func_set_pos = gmio::internal::istream_cpp_set_pos<CppStream>;
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename CHAR, typename TRAITS>
|
||||||
|
gmio_stream_t gmio_ostream_cpp(std::basic_ostream<CHAR, TRAITS>* s)
|
||||||
|
{
|
||||||
|
typedef std::basic_ostream<CHAR, TRAITS> CppStream;
|
||||||
|
gmio_stream_t stream;
|
||||||
|
gmio::internal::stream_cpp_init_common(s, &stream);
|
||||||
|
stream.func_size = gmio::internal::ostream_cpp_size<CppStream>;
|
||||||
|
stream.func_write = gmio::internal::ostream_cpp_write<CppStream>;
|
||||||
|
stream.func_get_pos = gmio::internal::ostream_cpp_get_pos<CppStream>;
|
||||||
|
stream.func_set_pos = gmio::internal::ostream_cpp_set_pos<CppStream>;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !DOXYGEN
|
||||||
|
|
||||||
#endif /* GMIO_SUPPORT_STREAM_CPP_H */
|
#endif /* GMIO_SUPPORT_STREAM_CPP_H */
|
||||||
/*! @} */
|
/*! @} */
|
||||||
|
@ -30,7 +30,7 @@ add_executable(
|
|||||||
qt/QtCore/QtGlobal
|
qt/QtCore/QtGlobal
|
||||||
../../src/gmio_support/stl_occ.cpp
|
../../src/gmio_support/stl_occ.cpp
|
||||||
../../src/gmio_support/stream_qt.cpp)
|
../../src/gmio_support/stream_qt.cpp)
|
||||||
link_libraries(gmio)
|
target_link_libraries(fake_support gmio)
|
||||||
include_directories(
|
include_directories(
|
||||||
${CMAKE_SOURCE_DIR}/src
|
${CMAKE_SOURCE_DIR}/src
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/opencascade
|
${CMAKE_CURRENT_SOURCE_DIR}/opencascade
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
#include "../../src/gmio_stl/stl_io.h"
|
#include "../../src/gmio_stl/stl_io.h"
|
||||||
#include "../../src/gmio_support/stl_occ.h"
|
#include "../../src/gmio_support/stl_occ.h"
|
||||||
|
#include "../../src/gmio_support/stream_qt.h"
|
||||||
|
#include "../../src/gmio_support/stream_cpp.h"
|
||||||
|
|
||||||
#include <QtCore/QFile>
|
#include <QtCore/QFile>
|
||||||
#include "../../src/gmio_support/stream_qt.h"
|
|
||||||
|
|
||||||
#include <Handle_StlMesh_Mesh.hxx>
|
#include <Handle_StlMesh_Mesh.hxx>
|
||||||
|
|
||||||
#include "../../src/gmio_support/stream_cpp.h"
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -20,8 +18,10 @@ int main()
|
|||||||
gmio_stream_qiodevice(&file);
|
gmio_stream_qiodevice(&file);
|
||||||
|
|
||||||
// C++
|
// C++
|
||||||
//std::ifstream s("test.txt");
|
std::ifstream is("test.txt");
|
||||||
//gmio_stream_t stream = gmio_stream_cpp(&s);
|
gmio_istream_cpp(&is);
|
||||||
|
std::ofstream os("test.txt");
|
||||||
|
gmio_ostream_cpp(&os);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user