-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPyContext.cpp
95 lines (78 loc) · 1.98 KB
/
PyContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "StdAfx.h"
#include "PyContext.h"
#include "Wraper.h"
CPyContext::CPyContext(void)
{
};
CPyContext::CPyContext(Render_RenderObject* PyRenderObject, PyObject* retval)
{
m_retval = retval;
Py_INCREF(m_retval);
PyRenderObject->buff_pos = 0;
is_callable = PyCallable_Check(m_retval);
m_PyRenderObject = PyRenderObject;
Py_INCREF(m_PyRenderObject);
};
CPyContext::~CPyContext(void)
{
Py_DECREF(m_PyRenderObject);
Py_DECREF(m_retval);
}
IDbConnection* CPyContext::get_connection()
{
return m_PyRenderObject->dbcon;
};
bool CPyContext::get_data(CSimpleTypeVar* var)
{
wrap_python_var(m_PyRenderObject->data, var);
return true;
};
int CPyContext::write(const char* buff, int length)
{
if(buff)
{
if(is_callable)
{
int tail = BUFFMAXSIZE - m_PyRenderObject->buff_pos;
if(tail >= length)
{
memcpy(&(m_PyRenderObject->buff[m_PyRenderObject->buff_pos]), buff, length);
m_PyRenderObject->buff_pos += length;
}
else
{
memcpy(&(m_PyRenderObject->buff[m_PyRenderObject->buff_pos]), buff, tail);
int datatail = length - tail;
PyObject* result = PyEval_CallFunction(m_retval, "(s#)", m_PyRenderObject->buff, BUFFMAXSIZE);
Py_XDECREF(result);
m_PyRenderObject->buff_pos = 0;
if((length - tail) > BUFFMAXSIZE)
{
PyObject* result = PyEval_CallFunction(m_retval, "(s#)", &buff[tail], datatail);
Py_XDECREF(result);
}
else
{
memcpy(&(m_PyRenderObject->buff[0]), &buff[tail], datatail);
m_PyRenderObject->buff_pos = datatail;
};
};
}
else
{
PyObject* pystr = PyString_FromStringAndSize(buff, length);
PyList_Append(m_retval, pystr);
Py_DECREF(pystr);
};
};
return length;
};
void CPyContext::flush()
{
if(is_callable)
{
PyObject* result = PyEval_CallFunction(m_retval, "(s#)", m_PyRenderObject->buff, m_PyRenderObject->buff_pos);
Py_XDECREF(result);
m_PyRenderObject->buff_pos = 0;
};
};