PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/scim-python-config.cpp

http://scim-python.googlecode.com/
C++ | 252 lines | 190 code | 37 blank | 25 comment | 27 complexity | e446005e0573ae06e42788175f98082e 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-config.h"
  28. using namespace std;
  29. struct PyConfig {
  30. PyObject_HEAD
  31. /* Type-specific fields go here. */
  32. ConfigPointer config;
  33. };
  34. const ConfigPointer &
  35. PyConfig_from_pyobject (PyObject *object)
  36. {
  37. PyConfig *config = (PyConfig *)object;
  38. return config->config;
  39. }
  40. PyObject *
  41. PyConfig_write (PyConfig *self, PyObject *args)
  42. {
  43. char *key;
  44. PyObject *value;
  45. PyObject *result = Py_False;
  46. ConfigPointer &config = self->config;
  47. if (!PyArg_ParseTuple (args, "sO:write", &key, &value))
  48. return NULL;
  49. if (PyString_Check (value)) {
  50. if (config->write (String (key), String (PyString_AsString (value))))
  51. result = Py_True;
  52. }
  53. else if (PyBool_Check (value)) {
  54. if (config->write (String (key), value == Py_True))
  55. result = Py_True;
  56. }
  57. else if (PyInt_Check (value)) {
  58. if (config->write (String (key), (int) PyInt_AsLong (value)))
  59. result = Py_True;
  60. }
  61. else if (PyFloat_Check (value)) {
  62. if (config->write (String (key), PyFloat_AsDouble (value)))
  63. result = Py_True;
  64. }
  65. else {
  66. PyErr_SetString(PyExc_TypeError,
  67. "The value must be string, int, float or bool");
  68. return NULL;
  69. }
  70. Py_INCREF (result);
  71. return result;
  72. }
  73. PyObject *
  74. PyConfig_read (PyConfig *self, PyObject *args)
  75. {
  76. char *key;
  77. PyObject *value;
  78. PyObject *result = Py_None;
  79. ConfigPointer &config = self->config;
  80. if (!PyArg_ParseTuple (args, "sO:read", &key, &value))
  81. return NULL;
  82. if (PyString_Check (value)) {
  83. String retval (config->read (String (key), String (PyString_AsString (value))));
  84. result = PyString_FromString (retval.c_str ());
  85. }
  86. else if (PyBool_Check (value)) {
  87. bool retval = config->read (String (key), value == Py_True);
  88. result = retval ? Py_True: Py_False;
  89. Py_INCREF (result);
  90. }
  91. else if (PyInt_Check (value)) {
  92. int retval = config->read (String (key), (int) PyInt_AsLong (value));
  93. result = PyInt_FromLong ((long)retval);
  94. }
  95. else if (PyFloat_Check (value)) {
  96. double retval = config->read (String (key), PyFloat_AsDouble (value));
  97. result = PyFloat_FromDouble (retval);
  98. }
  99. else {
  100. PyErr_SetString (PyExc_TypeError,
  101. "The value must be string, int, float or bool");
  102. return NULL;
  103. }
  104. return result;
  105. }
  106. static PyObject *
  107. PyConfig_flush (PyConfig *self, PyObject *args)
  108. {
  109. PyObject *result = NULL;
  110. if (self->config->flush ()) {
  111. result = Py_True;
  112. }
  113. else {
  114. result = Py_False;
  115. }
  116. Py_INCREF (result);
  117. return result;
  118. }
  119. static int
  120. PyConfig_init(PyConfig *self, PyObject *args, PyObject *kwds)
  121. {
  122. new (&self->config) ConfigPointer (NULL);
  123. return 0;
  124. }
  125. #if 0
  126. static PyObject *
  127. PyConfig_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  128. {
  129. PyConfig *self;
  130. self = (PyConfig *)type->tp_alloc (type, 0);
  131. PyConfig_init (self, args, kwds);
  132. return (PyObject *)self;
  133. }
  134. #endif
  135. static int
  136. PyConfig_clear (PyConfig *self)
  137. {
  138. self->config = NULL;
  139. return 0;
  140. }
  141. static void
  142. PyConfig_dealloc (PyConfig* self)
  143. {
  144. PyConfig_clear (self);
  145. ((PyObject*)self)->ob_type->tp_free (self);
  146. }
  147. static PyMethodDef PyConfig_methods[] = {
  148. { "write", (PyCFunction)PyConfig_write, METH_VARARGS,
  149. "write a value related to a key"
  150. },
  151. { "read", (PyCFunction)PyConfig_read, METH_VARARGS,
  152. "read a value related to a key, you must give a default value"
  153. },
  154. { "flush", (PyCFunction)PyConfig_flush, METH_NOARGS,
  155. "flush"
  156. },
  157. {NULL} /* Sentinel */
  158. };
  159. static PyMemberDef PyConfig_members[] = {
  160. {NULL} /* Sentinel */
  161. };
  162. static PyTypeObject PyConfigType = {
  163. PyObject_HEAD_INIT (NULL)
  164. 0, /*ob_size*/
  165. "scim.Config", /*tp_name*/
  166. sizeof (PyConfig), /*tp_basicsize*/
  167. 0, /*tp_itemsize*/
  168. (destructor)PyConfig_dealloc, /*tp_dealloc*/
  169. 0, /*tp_print*/
  170. 0, /*tp_getattr*/
  171. 0, /*tp_setattr*/
  172. 0, /*tp_compare*/
  173. 0, /*tp_repr*/
  174. 0, /*tp_as_number*/
  175. 0, /*tp_as_sequence*/
  176. 0, /*tp_as_mapping*/
  177. 0, /*tp_hash */
  178. 0, /*tp_call*/
  179. 0, //(reprfunc)PyConfig_str, /*tp_str*/
  180. 0, /*tp_getattro*/
  181. 0, /*tp_setattro*/
  182. 0, /*tp_as_buffer*/
  183. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  184. "Config objects", /* tp_doc */
  185. 0, /* tp_traverse */
  186. 0, /* tp_clear */
  187. 0, /* tp_richcompare */
  188. 0, /* tp_weaklistoffset */
  189. 0, /* tp_iter */
  190. 0, /* tp_iternext */
  191. PyConfig_methods, /* tp_methods */
  192. PyConfig_members, /* tp_members */
  193. 0, /* tp_getset */
  194. 0, /* tp_base */
  195. 0, /* tp_dict */
  196. 0, /* tp_descr_get */
  197. 0, /* tp_descr_set */
  198. 0, /* tp_dictoffset */
  199. 0, /* tp_init */
  200. 0, /* tp_alloc */
  201. 0, /* tp_new */
  202. PyObject_Del, /* tp_free */
  203. };
  204. PyObject *PyConfig_New (const ConfigPointer &config)
  205. {
  206. PyConfig *obj = PyObject_New (PyConfig, &PyConfigType);
  207. new (&obj->config) ConfigPointer (config);
  208. return (PyObject *)obj;
  209. }
  210. void init_config (PyObject *module)
  211. {
  212. if (PyType_Ready (&PyConfigType) < 0)
  213. return;
  214. Py_INCREF (&PyConfigType);
  215. }