Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / runtime / PyStdout.cxx
1
2 #include "PyStdout.hxx"
3 #include <structmember.h>
4
5 namespace YACS
6 {
7   namespace ENGINE
8     {
9
10     typedef struct {
11       PyObject_HEAD
12       int softspace;
13       std::string *out;
14     } PyStdOut;
15
16 static void
17 PyStdOut_dealloc(PyStdOut *self)
18 {
19   PyObject_Del(self);
20 }
21
22 static PyObject *
23 PyStdOut_write(PyStdOut *self, PyObject *args)
24 {
25   char *c;
26   int l;
27   if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
28     return NULL;
29
30   std::cerr << c ;
31   *(self->out)=*(self->out)+c;
32
33   Py_INCREF(Py_None);
34   return Py_None;
35 }
36
37 static PyMethodDef PyStdOut_methods[] = {
38   {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
39     PyDoc_STR("write(string) -> None")},
40   {NULL,    NULL}   /* sentinel */
41 };
42
43 static PyMemberDef PyStdOut_memberlist[] = {
44   {"softspace", T_INT,  offsetof(PyStdOut, softspace), 0,
45    "flag indicating that a space needs to be printed; used by print"},
46   {NULL} /* Sentinel */
47 };
48
49 static PyTypeObject PyStdOut_Type = {
50   /* The ob_type field must be initialized in the module init function
51    * to be portable to Windows without using C++. */
52   PyObject_HEAD_INIT(NULL)
53   0,      /*ob_size*/
54   "PyOut",   /*tp_name*/
55   sizeof(PyStdOut),  /*tp_basicsize*/
56   0,      /*tp_itemsize*/
57   /* methods */
58   (destructor)PyStdOut_dealloc, /*tp_dealloc*/
59   0,      /*tp_print*/
60   0, /*tp_getattr*/
61   0, /*tp_setattr*/
62   0,      /*tp_compare*/
63   0,      /*tp_repr*/
64   0,      /*tp_as_number*/
65   0,      /*tp_as_sequence*/
66   0,      /*tp_as_mapping*/
67   0,      /*tp_hash*/
68         0,                      /*tp_call*/
69         0,                      /*tp_str*/
70         PyObject_GenericGetAttr,                      /*tp_getattro*/
71         /* softspace is writable:  we must supply tp_setattro */
72         PyObject_GenericSetAttr,    /* tp_setattro */
73         0,                      /*tp_as_buffer*/
74         Py_TPFLAGS_DEFAULT,     /*tp_flags*/
75         0,                      /*tp_doc*/
76         0,                      /*tp_traverse*/
77         0,                      /*tp_clear*/
78         0,                      /*tp_richcompare*/
79         0,                      /*tp_weaklistoffset*/
80         0,                      /*tp_iter*/
81         0,                      /*tp_iternext*/
82         PyStdOut_methods,                      /*tp_methods*/
83         PyStdOut_memberlist,                      /*tp_members*/
84         0,                      /*tp_getset*/
85         0,                      /*tp_base*/
86         0,                      /*tp_dict*/
87         0,                      /*tp_descr_get*/
88         0,                      /*tp_descr_set*/
89         0,                      /*tp_dictoffset*/
90         0,                      /*tp_init*/
91         0,                      /*tp_alloc*/
92         0,                      /*tp_new*/
93         0,                      /*tp_free*/
94         0,                      /*tp_is_gc*/
95 };
96
97
98 #define PyStdOut_Check(v)  ((v)->ob_type == &PyStdOut_Type)
99
100 PyObject * newPyStdOut( std::string& out )
101 {
102   PyStdOut *self;
103   self = PyObject_New(PyStdOut, &PyStdOut_Type);
104   if (self == NULL)
105     return NULL;
106   self->softspace = 0;
107   self->out=&out;
108   return (PyObject*)self;
109 }
110
111 }
112 }
113