PageRenderTime 48ms CodeModel.GetById 16ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/src/scim-python-attribute.cpp

http://scim-python.googlecode.com/
C++ | 305 lines | 231 code | 49 blank | 25 comment | 26 complexity | 5b6d3a5d2db4d941602e4244b6497b8c MD5 | raw file
  1/* vim:set noet ts=4: */
  2/** 
  3 * scim-python
  4 * 
  5 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6 *
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this program; if not, write to the
 20 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 21 * Boston, MA  02111-1307  USA
 22 *
 23 * $Id: $
 24 */
 25#include <Python.h>
 26#include "structmember.h"
 27#include "scim-python-attribute.h"
 28
 29struct PyAttribute {
 30	PyObject_HEAD
 31	/* Type-specific fields go here. */
 32	Attribute attribute;
 33};
 34
 35AttributeList
 36Attributes_FromTupleOrList (PyObject *object)
 37{
 38	int size;
 39	AttributeList attrs;
 40
 41	if (object == NULL || object == Py_None)
 42		return attrs;
 43
 44	if (PyTuple_Check (object)) {
 45		size = PyTuple_Size (object);
 46		for (int i = 0; i < size; i++) {
 47			attrs.push_back (Attribute_FromPyObject (PyTuple_GetItem (object, i)));
 48		}
 49	}
 50	else if (PyList_Check (object)) {
 51		size = PyList_Size (object);
 52		for (int i = 0; i < size; i++) {
 53			attrs.push_back (Attribute_FromPyObject (PyList_GetItem (object, i)));
 54		}
 55	}
 56	return attrs;
 57}
 58
 59static PyObject *
 60PyAttribute_str (const PyAttribute* self)
 61{
 62	char buf[128];
 63	sprintf (buf, "Attribute (start=%d, length=%d, type=%d, value=%d)",
 64				self->attribute.get_start (), 
 65				self->attribute.get_length (), 
 66				self->attribute.get_type (),
 67				self->attribute.get_value ());
 68
 69	return PyString_FromString (buf);
 70}
 71
 72static PyObject *
 73PyAttribute_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 74{
 75	PyAttribute *self;
 76
 77	self = (PyAttribute *)type->tp_alloc (type, 0);
 78	return (PyObject *)self;
 79}
 80
 81static int
 82PyAttribute_init (PyAttribute *self, PyObject *args, PyObject *kwds)
 83{
 84	unsigned start = 0;
 85	unsigned int  length = 0;
 86	AttributeType type  = SCIM_ATTR_NONE;
 87	unsigned int value = 0;
 88
 89	if (!PyArg_ParseTuple (args, "|IIII:__init__", &start, &length, &type, &value))
 90		return -1;
 91
 92	new (&self->attribute) Attribute (start, length, type, value);
 93
 94	return 0;
 95}
 96
 97static void
 98PyAttribute_dealloc (PyAttribute *self)
 99{
100	self->attribute.~Attribute ();
101	((PyObject *)self)->ob_type->tp_free (self);
102}
103
104static PyObject *
105PyAttribute_get_start (PyAttribute *self, void *closure)
106{
107	return PyInt_FromLong ((long)self->attribute.get_start ());
108}
109
110static int
111PyAttribute_set_start (PyAttribute *self, PyObject *value, void *closure)
112{
113	if (value == NULL) {
114		PyErr_SetString (PyExc_TypeError, 
115					"Cannot delete the start attribute");
116		return -1;
117	}
118
119	if (! PyInt_Check (value)) {
120		PyErr_SetString (PyExc_TypeError, 
121					"The start attribute value must be an int");
122		return -1;
123	}
124 
125	self->attribute.set_start ((uint32)PyInt_AS_LONG (value));
126
127	return 0;
128}
129
130static PyObject *
131PyAttribute_get_length (PyAttribute *self, void *closure)
132{
133
134	return PyInt_FromLong ((long)self->attribute.get_length ());
135}
136
137static int
138PyAttribute_set_length (PyAttribute *self, PyObject *value, void *closure)
139{
140	if (value == NULL) {
141		PyErr_SetString (PyExc_TypeError, 
142					"Cannot delete the length attribute");
143		return -1;
144	}
145
146	if (! PyInt_Check (value)) {
147		PyErr_SetString (PyExc_TypeError, 
148					"The length attribute value must be an int");
149		return -1;
150	}
151 
152	self->attribute.set_length ((uint32)PyInt_AS_LONG (value));
153
154	return 0;
155}
156
157
158static PyObject *
159PyAttribute_get_type (PyAttribute *self, void *closure)
160{
161
162	return PyInt_FromLong ((long)self->attribute.get_type ());
163}
164
165static int
166PyAttribute_set_type (PyAttribute *self, PyObject *value, void *closure)
167{
168	if (value == NULL) {
169		PyErr_SetString (PyExc_TypeError, 
170					"Cannot delete the type attribute");
171		return -1;
172	}
173
174	if (! PyInt_Check (value)) {
175		PyErr_SetString (PyExc_TypeError, 
176					"The type attribute value must be an int");
177		return -1;
178	}
179 
180	self->attribute.set_type ((AttributeType)PyInt_AS_LONG (value));
181
182	return 0;
183}
184
185
186static PyObject *
187PyAttribute_get_value (PyAttribute *self, void *closure)
188{
189
190	return PyInt_FromLong ((long)self->attribute.get_value ());
191}
192
193static int
194PyAttribute_set_value (PyAttribute *self, PyObject *value, void *closure)
195{
196	if (value == NULL) {
197		PyErr_SetString (PyExc_TypeError, 
198					"Cannot delete the value attribute");
199		return -1;
200	}
201
202	if (! PyInt_Check (value)) {
203		PyErr_SetString (PyExc_TypeError, 
204					"The value attribute value must be an int");
205		return -1;
206	}
207 
208	self->attribute.set_value ((uint32)PyInt_AS_LONG (value));
209
210	return 0;
211}
212
213static PyGetSetDef PyAttribute_getseters[] = {
214	{"start", (getter)PyAttribute_get_start, (setter)PyAttribute_set_start,
215		"start", NULL},
216	{"length", (getter)PyAttribute_get_length, (setter)PyAttribute_set_length,
217		"length", NULL},
218	{"type", (getter)PyAttribute_get_type, (setter)PyAttribute_set_type,
219		"type", NULL},
220	{"value", (getter)PyAttribute_get_value, (setter)PyAttribute_set_value,
221		"value", NULL},
222	{NULL}  /* Sentinel */
223};
224
225static PyTypeObject PyAttributeType = {
226	PyObject_HEAD_INIT (NULL)
227	0,						 				/*ob_size*/
228	"scim.Attribute", 						/*tp_name*/
229	sizeof (PyAttribute), 					/*tp_basicsize*/
230	0,						 				/*tp_itemsize*/
231	(destructor)PyAttribute_dealloc,		/*tp_dealloc*/
232	0,			  							/*tp_print*/
233	0,						 				/*tp_getattr*/
234	0,										/*tp_setattr*/
235	0,										/*tp_compare*/
236	0,			  							/*tp_repr*/
237	0,										/*tp_as_number*/
238	0,			  							/*tp_as_sequence*/
239	0,										/*tp_as_mapping*/
240	0,			  							/*tp_hash */
241	0,										/*tp_call*/
242	(reprfunc)PyAttribute_str,   			/*tp_str*/
243	0,					   					/*tp_getattro*/
244	0,										/*tp_setattro*/
245	0,					 					/*tp_as_buffer*/
246	Py_TPFLAGS_DEFAULT,						/*tp_flags*/
247	"Attribute objects",		/* tp_doc */
248	0,					   		/* tp_traverse */
249	0,					   		/* tp_clear */
250	0,					   		/* tp_richcompare */
251	0,					   		/* tp_weaklistoffset */
252	0,					   		/* tp_iter */
253	0,					   		/* tp_iternext */
254	0,			 				/* tp_methods */
255	0,			 				/* tp_members */
256	PyAttribute_getseters,		/* tp_getset */
257	0,						 	/* tp_base */
258	0,						 	/* tp_dict */
259	0,						 	/* tp_descr_get */
260	0,						 	/* tp_descr_set */
261	0,						 	/* tp_dictoffset */
262	(initproc)PyAttribute_init,	/* tp_init */
263	0,						 	/* tp_alloc */
264	PyAttribute_new,			/* tp_new */
265	PyObject_Del,				/* tp_new */
266};
267
268
269
270Attribute
271Attribute_FromPyObject (PyObject *object)
272{
273	return ((PyAttribute *)object)->attribute;
274}
275
276static void
277setint (PyObject *d, const char *name, long value)
278{
279	PyObject *o = PyInt_FromLong (value);
280	if (o && PyDict_SetItemString (d, name, o) == 0) {
281		Py_DECREF (o);
282	}
283}
284
285void init_attribute (PyObject *module)
286{
287	PyObject *dict;
288
289	if (PyType_Ready (&PyAttributeType) < 0)
290		return;
291
292	Py_INCREF (&PyAttributeType);
293	PyModule_AddObject (module, "Attribute", (PyObject *)&PyAttributeType);
294
295	dict = PyModule_GetDict (module);
296
297	setint (dict, "ATTR_NONE",		SCIM_ATTR_NONE);
298	setint (dict, "ATTR_DECORATE",	SCIM_ATTR_DECORATE);
299	setint (dict, "ATTR_FOREGROUND",SCIM_ATTR_FOREGROUND);
300	setint (dict, "ATTR_BACKGROUND",SCIM_ATTR_BACKGROUND);
301	setint (dict, "ATTR_DECORATE_NONE",		SCIM_ATTR_DECORATE_NONE);
302	setint (dict, "ATTR_DECORATE_UNDERLINE",SCIM_ATTR_DECORATE_UNDERLINE);
303	setint (dict, "ATTR_DECORATE_HIGLIGHT",	SCIM_ATTR_DECORATE_HIGHLIGHT);
304	setint (dict, "ATTR_DECORATE_REVERSE",	SCIM_ATTR_DECORATE_REVERSE);
305}