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