0000572: add a method to Part module to read BRep data from string

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5410 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
wmayer
2012-01-16 20:23:26 +00:00
parent 445dd03929
commit 546faca0de
6 changed files with 125 additions and 0 deletions

View File

@@ -40,6 +40,7 @@
#include "Stream.h"
#include "Swap.h"
#include "FileInfo.h"
#include <CXX/Objects.hxx>
using namespace Base;
@@ -533,6 +534,58 @@ IODeviceIStreambuf::seekpos(std::streambuf::pos_type pos,
return seekoff(pos, std::ios_base::beg);
}
// ---------------------------------------------------------
PyStreambuf::PyStreambuf(PyObject* o) : inp(o)
{
setg (buffer+pbSize,
buffer+pbSize,
buffer+pbSize);
}
int PyStreambuf::underflow()
{
if (gptr() < egptr()) {
return *gptr();
}
int numPutback;
numPutback = gptr() - eback();
if (numPutback > pbSize) {
numPutback = pbSize;
}
memcpy (buffer+(pbSize-numPutback), gptr()-numPutback, numPutback);
int num=0;
for (int i=0; i<bufSize; i++) {
char c;
Py::Tuple arg(1);
arg.setItem(0, Py::Int(1));
Py::Callable meth(Py::Object(inp).getAttr("read"));
try {
Py::Char res(meth.apply(arg));
c = static_cast<std::string>(res)[0];
num++;
buffer[pbSize+i] = c;
if (c == '\n')
break;
}
catch (Py::Exception& e) {
e.clear();
if (num == 0)
return EOF;
break;
}
}
setg (buffer+(pbSize-numPutback),
buffer+pbSize,
buffer+pbSize+num);
return *gptr();
}
// ---------------------------------------------------------
Streambuf::Streambuf(const std::string& data)

View File

@@ -35,6 +35,7 @@
class QByteArray;
class QIODevice;
class QBuffer;
typedef struct _object PyObject;
namespace Base {
@@ -232,6 +233,21 @@ protected:
static const int bufSize = 1024; // size of the data buffer
char buffer[bufSize+pbSize]; // data buffer
};
class BaseExport PyStreambuf : public std::streambuf
{
public:
PyStreambuf(PyObject* o);
protected:
int underflow();
private:
static const int pbSize = 4;
static const int bufSize = 1024;
char buffer[bufSize+pbSize];
PyObject* inp;
};
class BaseExport Streambuf : public std::streambuf
{