Salome HOME
On the road of homogeneous containers.
[modules/yacs.git] / src / runtime / PyStdout.cxx
1 // Copyright (C) 2006-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PyStdout.hxx"
21 #include <structmember.h>
22 #include <string>
23
24 namespace YACS
25 {
26   namespace ENGINE
27     {
28
29     typedef struct {
30       PyObject_HEAD
31       int softspace;
32       std::string *out;
33     } PyStdOut;
34
35 static void
36 PyStdOut_dealloc(PyStdOut *self)
37 {
38   PyObject_Del(self);
39 }
40
41 static PyObject *
42 PyStdOut_write(PyStdOut *self, PyObject *args)
43 {
44   char *c;
45   int l;
46   if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
47     return NULL;
48
49   //std::cerr << c ;
50   *(self->out)=*(self->out)+std::string(c);
51
52   Py_INCREF(Py_None);
53   return Py_None;
54 }
55
56 static PyMethodDef PyStdOut_methods[] = {
57   {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
58     PyDoc_STR("write(string) -> None")},
59   {NULL,    NULL}   /* sentinel */
60 };
61
62 static PyMemberDef PyStdOut_memberlist[] = {
63   {(char*)"softspace", T_INT,  offsetof(PyStdOut, softspace), 0,
64    (char*)"flag indicating that a space needs to be printed; used by print"},
65   {NULL} /* Sentinel */
66 };
67
68 static PyTypeObject PyStdOut_Type = {
69   /* The ob_type field must be initialized in the module init function
70    * to be portable to Windows without using C++. */
71   PyObject_HEAD_INIT(NULL)
72   0,      /*ob_size*/
73   "PyOut",   /*tp_name*/
74   sizeof(PyStdOut),  /*tp_basicsize*/
75   0,      /*tp_itemsize*/
76   /* methods */
77   (destructor)PyStdOut_dealloc, /*tp_dealloc*/
78   0,      /*tp_print*/
79   0, /*tp_getattr*/
80   0, /*tp_setattr*/
81   0,      /*tp_compare*/
82   0,      /*tp_repr*/
83   0,      /*tp_as_number*/
84   0,      /*tp_as_sequence*/
85   0,      /*tp_as_mapping*/
86   0,      /*tp_hash*/
87         0,                      /*tp_call*/
88         0,                      /*tp_str*/
89         PyObject_GenericGetAttr,                      /*tp_getattro*/
90         /* softspace is writable:  we must supply tp_setattro */
91         PyObject_GenericSetAttr,    /* tp_setattro */
92         0,                      /*tp_as_buffer*/
93         Py_TPFLAGS_DEFAULT,     /*tp_flags*/
94         0,                      /*tp_doc*/
95         0,                      /*tp_traverse*/
96         0,                      /*tp_clear*/
97         0,                      /*tp_richcompare*/
98         0,                      /*tp_weaklistoffset*/
99         0,                      /*tp_iter*/
100         0,                      /*tp_iternext*/
101         PyStdOut_methods,                      /*tp_methods*/
102         PyStdOut_memberlist,                      /*tp_members*/
103         0,                      /*tp_getset*/
104         0,                      /*tp_base*/
105         0,                      /*tp_dict*/
106         0,                      /*tp_descr_get*/
107         0,                      /*tp_descr_set*/
108         0,                      /*tp_dictoffset*/
109         0,                      /*tp_init*/
110         0,                      /*tp_alloc*/
111         0,                      /*tp_new*/
112         0,                      /*tp_free*/
113         0,                      /*tp_is_gc*/
114 };
115
116
117 #define PyStdOut_Check(v)  ((v)->ob_type == &PyStdOut_Type)
118
119 PyObject * newPyStdOut( std::string& out )
120 {
121   PyStdOut *self;
122   self = PyObject_New(PyStdOut, &PyStdOut_Type);
123   if (self == NULL)
124     return NULL;
125   self->softspace = 0;
126   self->out=&out;
127   return (PyObject*)self;
128 }
129
130 }
131 }
132