|
1 | | -# https://stackoverflow.com/questions/18860816/technique-for-using-stdifstream-stdofstream-in-python-via-swig |
2 | | - |
3 | | -%fragment("iostream_header", "header") %{ |
4 | | -#include <stdio.h> |
5 | | -#include <memory.h> |
6 | | -#include <boost/iostreams/stream.hpp> |
7 | | -#include <boost/iostreams/device/file_descriptor.hpp> |
8 | | -using boost_ofd_stream = boost::iostreams::stream<boost::iostreams::file_descriptor_sink>; |
9 | | -using boost_ifd_stream = boost::iostreams::stream<boost::iostreams::file_descriptor_source>; |
10 | | -%} |
11 | | - |
12 | | -%typemap(in, fragment="iostream_header") std::ostream& (std::unique_ptr<boost_ofd_stream> stream) { |
13 | | - PyObject *flush_result = PyObject_CallMethod($input, const_cast<char*>("flush"), nullptr); |
14 | | - if (flush_result) Py_DECREF(flush_result); |
15 | | -%#if PY_VERSION_HEX < 0x03000000 |
16 | | - int fd = fileno(PyFile_AsFile($input)); |
17 | | -%#else |
18 | | - int fd = PyObject_AsFileDescriptor($input); |
19 | | -%#endif |
20 | | - if (fd < 0) { SWIG_Error(SWIG_TypeError, "File object expected."); SWIG_fail; } |
21 | | - stream = std::make_unique<boost_ofd_stream>(fd, boost::iostreams::never_close_handle); |
22 | | - $1 = stream.get(); |
23 | | -} |
24 | | - |
25 | | -%typemap(in, fragment="iostream_header") std::istream& (std::unique_ptr<boost_ifd_stream> stream) { |
26 | | -%#if PY_VERSION_HEX < 0x03000000 |
27 | | - int fd = fileno(PyFile_AsFile($input)); |
28 | | -%#else |
29 | | - int fd = PyObject_AsFileDescriptor($input); |
30 | | -%#endif |
31 | | - if (fd < 0) { SWIG_Error(SWIG_TypeError, "File object expected."); SWIG_fail; } |
32 | | - stream = std::make_unique<boost_ifd_stream>(fd, boost::iostreams::never_close_handle); |
33 | | - $1 = stream.get(); |
34 | | -} |
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright 2020 Thomas Paviot ([email protected]) |
| 4 | +
|
| 5 | +This file is part of pythonOCC. |
| 6 | +
|
| 7 | +pythonOCC is free software: you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation, either version 3 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +pythonOCC is distributed in the hope that it will be useful, |
| 13 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +
|
| 20 | +*/ |
| 21 | + |
| 22 | +%include <python/std_iostream.i> |
| 23 | +%include <python/std_string.i> |
| 24 | + |
| 25 | +/* |
| 26 | +Standard_OStream & function transformation |
| 27 | +The float number is returned in the output tuple |
| 28 | +*/ |
| 29 | +%typemap(argout) Standard_OStream &OutValue { |
| 30 | + PyObject *o, *o2, *o3; |
| 31 | + std::ostringstream *output = static_cast<std::ostringstream *> ($1); |
| 32 | + o = PyString_FromString(output->str().c_str()); |
| 33 | + if ((!$result) || ($result == Py_None)) { |
| 34 | + $result = o; |
| 35 | + } else { |
| 36 | + if (!PyTuple_Check($result)) { |
| 37 | + PyObject *o2 = $result; |
| 38 | + $result = PyTuple_New(1); |
| 39 | + PyTuple_SetItem($result,0,o2); |
| 40 | + } |
| 41 | + o3 = PyTuple_New(1); |
| 42 | + PyTuple_SetItem(o3,0,o); |
| 43 | + o2 = $result; |
| 44 | + $result = PySequence_Concat(o2,o3); |
| 45 | + Py_DECREF(o2); |
| 46 | + Py_DECREF(o3); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +%typemap(in, numinputs=0) Standard_OStream &OutValue (std::ostringstream temp) { |
| 51 | + $1 = &temp; |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | +Standard_IStream & function transformation |
| 56 | +takes a string as input |
| 57 | +*/ |
| 58 | +%typemap(in) Standard_IStream & { |
| 59 | + char * in = PyString_AsString($input); |
| 60 | + std::istringstream ss(in); |
| 61 | + $1 = &ss; |
| 62 | +} |
0 commit comments