PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms 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. struct PyAttribute {
  29. PyObject_HEAD
  30. /* Type-specific fields go here. */
  31. Attribute attribute;
  32. };
  33. AttributeList
  34. Attributes_FromTupleOrList (PyObject *object)
  35. {
  36. int size;
  37. AttributeList attrs;
  38. if (object == NULL || object == Py_None)
  39. return attrs;
  40. if (PyTuple_Check (object)) {
  41. size = PyTuple_Size (object);
  42. for (int i = 0; i < size; i++) {
  43. attrs.push_back (Attribute_FromPyObject (PyTuple_GetItem (object, i)));
  44. }
  45. }
  46. else if (PyList_Check (object)) {
  47. size = PyList_Size (object);
  48. for (int i = 0; i < size; i++) {
  49. attrs.push_back (Attribute_FromPyObject (PyList_GetItem (object, i)));
  50. }
  51. }
  52. return attrs;
  53. }
  54. static PyObject *
  55. PyAttribute_str (const PyAttribute* self)
  56. {
  57. char buf[128];
  58. sprintf (buf, "Attribute (start=%d, length=%d, type=%d, value=%d)",
  59. self->attribute.get_start (),
  60. self->attribute.get_length (),
  61. self->attribute.get_type (),
  62. self->attribute.get_value ());
  63. return PyString_FromString (buf);
  64. }
  65. static PyObject *
  66. PyAttribute_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
  67. {
  68. PyAttribute *self;
  69. self = (PyAttribute *)type->tp_alloc (type, 0);
  70. return (PyObject *)self;
  71. }
  72. static int
  73. PyAttribute_init (PyAttribute *self, PyObject *args, PyObject *kwds)
  74. {
  75. unsigned start = 0;
  76. unsigned int length = 0;
  77. AttributeType type = SCIM_ATTR_NONE;
  78. unsigned int value = 0;
  79. if (!PyArg_ParseTuple (args, "|IIII:__init__", &start, &length, &type, &value))
  80. return -1;
  81. new (&self->attribute) Attribute (start, length, type, value);
  82. return 0;
  83. }
  84. static void
  85. PyAttribute_dealloc (PyAttribute *self)
  86. {
  87. self->attribute.~Attribute ();
  88. ((PyObject *)self)->ob_type->tp_free (self);
  89. }
  90. static PyObject *
  91. PyAttribute_get_start (PyAttribute *self, void *closure)
  92. {
  93. return PyInt_FromLong ((long)self->attribute.get_start ());
  94. }
  95. static int
  96. PyAttribute_set_start (PyAttribute *self, PyObject *value, void *closure)
  97. {
  98. if (value == NULL) {
  99. PyErr_SetString (PyExc_TypeError,
  100. "Cannot delete the start attribute");
  101. return -1;
  102. }
  103. if (! PyInt_Check (value)) {
  104. PyErr_SetString (PyExc_TypeError,
  105. "The start attribute value must be an int");
  106. return -1;
  107. }
  108. self->attribute.set_start ((uint32)PyInt_AS_LONG (value));
  109. return 0;
  110. }
  111. static PyObject *
  112. PyAttribute_get_length (PyAttribute *self, void *closure)
  113. {
  114. return PyInt_FromLong ((long)self->attribute.get_length ());
  115. }
  116. static int
  117. PyAttribute_set_length (PyAttribute *self, PyObject *value, void *closure)
  118. {
  119. if (value == NULL) {
  120. PyErr_SetString (PyExc_TypeError,
  121. "Cannot delete the length attribute");
  122. return -1;
  123. }
  124. if (! PyInt_Check (value)) {
  125. PyErr_SetString (PyExc_TypeError,
  126. "The length attribute value must be an int");
  127. return -1;
  128. }
  129. self->attribute.set_length ((uint32)PyInt_AS_LONG (value));
  130. return 0;
  131. }
  132. static PyObject *
  133. PyAttribute_get_type (PyAttribute *self, void *closure)
  134. {
  135. return PyInt_FromLong ((long)self->attribute.get_type ());
  136. }
  137. static int
  138. PyAttribute_set_type (PyAttribute *self, PyObject *value, void *closure)
  139. {
  140. if (value == NULL) {
  141. PyErr_SetString (PyExc_TypeError,
  142. "Cannot delete the type attribute");
  143. return -1;
  144. }
  145. if (! PyInt_Check (value)) {
  146. PyErr_SetString (PyExc_TypeError,
  147. "The type attribute value must be an int");
  148. return -1;
  149. }
  150. self->attribute.set_type ((AttributeType)PyInt_AS_LONG (value));
  151. return 0;
  152. }
  153. static PyObject *
  154. PyAttribute_get_value (PyAttribute *self, void *closure)
  155. {
  156. return PyInt_FromLong ((long)self->attribute.get_value ());
  157. }
  158. static int
  159. PyAttribute_set_value (PyAttribute *self, PyObject *value, void *closure)
  160. {
  161. if (value == NULL) {
  162. PyErr_SetString (PyExc_TypeError,
  163. "Cannot delete the value attribute");
  164. return -1;
  165. }
  166. if (! PyInt_Check (value)) {
  167. PyErr_SetString (PyExc_TypeError,
  168. "The value attribute value must be an int");
  169. return -1;
  170. }
  171. self->attribute.set_value ((uint32)PyInt_AS_LONG (value));
  172. return 0;
  173. }
  174. static PyGetSetDef PyAttribute_getseters[] = {
  175. {"start", (getter)PyAttribute_get_start, (setter)PyAttribute_set_start,
  176. "start", NULL},
  177. {"length", (getter)PyAttribute_get_length, (setter)PyAttribute_set_length,
  178. "length", NULL},
  179. {"type", (getter)PyAttribute_get_type, (setter)PyAttribute_set_type,
  180. "type", NULL},
  181. {"value", (getter)PyAttribute_get_value, (setter)PyAttribute_set_value,
  182. "value", NULL},
  183. {NULL} /* Sentinel */
  184. };
  185. static PyTypeObject PyAttributeType = {
  186. PyObject_HEAD_INIT (NULL)
  187. 0, /*ob_size*/
  188. "scim.Attribute", /*tp_name*/
  189. sizeof (PyAttribute), /*tp_basicsize*/
  190. 0, /*tp_itemsize*/
  191. (destructor)PyAttribute_dealloc, /*tp_dealloc*/
  192. 0, /*tp_print*/
  193. 0, /*tp_getattr*/
  194. 0, /*tp_setattr*/
  195. 0, /*tp_compare*/
  196. 0, /*tp_repr*/
  197. 0, /*tp_as_number*/
  198. 0, /*tp_as_sequence*/
  199. 0, /*tp_as_mapping*/
  200. 0, /*tp_hash */
  201. 0, /*tp_call*/
  202. (reprfunc)PyAttribute_str, /*tp_str*/
  203. 0, /*tp_getattro*/
  204. 0, /*tp_setattro*/
  205. 0, /*tp_as_buffer*/
  206. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  207. "Attribute objects", /* tp_doc */
  208. 0, /* tp_traverse */
  209. 0, /* tp_clear */
  210. 0, /* tp_richcompare */
  211. 0, /* tp_weaklistoffset */
  212. 0, /* tp_iter */
  213. 0, /* tp_iternext */
  214. 0, /* tp_methods */
  215. 0, /* tp_members */
  216. PyAttribute_getseters, /* tp_getset */
  217. 0, /* tp_base */
  218. 0, /* tp_dict */
  219. 0, /* tp_descr_get */
  220. 0, /* tp_descr_set */
  221. 0, /* tp_dictoffset */
  222. (initproc)PyAttribute_init, /* tp_init */
  223. 0, /* tp_alloc */
  224. PyAttribute_new, /* tp_new */
  225. PyObject_Del, /* tp_new */
  226. };
  227. Attribute
  228. Attribute_FromPyObject (PyObject *object)
  229. {
  230. return ((PyAttribute *)object)->attribute;
  231. }
  232. static void
  233. setint (PyObject *d, const char *name, long value)
  234. {
  235. PyObject *o = PyInt_FromLong (value);
  236. if (o && PyDict_SetItemString (d, name, o) == 0) {
  237. Py_DECREF (o);
  238. }
  239. }
  240. void init_attribute (PyObject *module)
  241. {
  242. PyObject *dict;
  243. if (PyType_Ready (&PyAttributeType) < 0)
  244. return;
  245. Py_INCREF (&PyAttributeType);
  246. PyModule_AddObject (module, "Attribute", (PyObject *)&PyAttributeType);
  247. dict = PyModule_GetDict (module);
  248. setint (dict, "ATTR_NONE", SCIM_ATTR_NONE);
  249. setint (dict, "ATTR_DECORATE", SCIM_ATTR_DECORATE);
  250. setint (dict, "ATTR_FOREGROUND",SCIM_ATTR_FOREGROUND);
  251. setint (dict, "ATTR_BACKGROUND",SCIM_ATTR_BACKGROUND);
  252. setint (dict, "ATTR_DECORATE_NONE", SCIM_ATTR_DECORATE_NONE);
  253. setint (dict, "ATTR_DECORATE_UNDERLINE",SCIM_ATTR_DECORATE_UNDERLINE);
  254. setint (dict, "ATTR_DECORATE_HIGLIGHT", SCIM_ATTR_DECORATE_HIGHLIGHT);
  255. setint (dict, "ATTR_DECORATE_REVERSE", SCIM_ATTR_DECORATE_REVERSE);
  256. }