diff options
author | Matej Cepl <mcepl@redhat.com> | 2008-02-26 17:27:08 +0100 |
---|---|---|
committer | Matej Cepl <mcepl@redhat.com> | 2008-02-26 17:27:08 +0100 |
commit | c50ce75f2e06b6b8abe49e34775d343b74f7e8cd (patch) | |
tree | 14c314e39296b0c3abe781e4dac194949e220136 /wlp/C/wlp.c | |
download | pygn-c50ce75f2e06b6b8abe49e34775d343b74f7e8cd.tar.gz |
The original upstream from Debian orig.tar.gz tarball.0.9.7
Diffstat (limited to 'wlp/C/wlp.c')
-rw-r--r-- | wlp/C/wlp.c | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/wlp/C/wlp.c b/wlp/C/wlp.c new file mode 100644 index 0000000..8c0779d --- /dev/null +++ b/wlp/C/wlp.c @@ -0,0 +1,183 @@ +/* + * wlp.c - Copyright 2000, 2001 by Cosimo Alfarano <Alfarano@CS.UniBo.It> + * You can use this software under the terms of the GPL. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. + * + * Thanks to md for this useful formula. Beer is beer. + */ + +//#include <python/Python.h> +#include <Python.h> +#include <stdio.h> +#include <unistd.h> + +#include "structs.h" +#include "macro.h" + + +static FILE *fd = NULL; + +struct wlp_list_t *list; + +static PyObject *node2dict(struct wlp_node_t *node); +static PyObject *node2dict2(struct wlp_node_t *node); + + + + +/* + * wlp_setfilebyname(): get FILE* fd from filename string. + */ + +static PyObject *wlp_setfilebyname(PyObject *self, PyObject *args) { + char *file; + + + DBG("setfilebyname\n"); + if (!PyArg_ParseTuple(args, "s", &file)) + return NULL; + + if(fd = fopen(file,"r")) { + return Py_None; + } else { + PyErr_SetFromErrno(PyExc_Exception); +/* + PyErr_SetString(PyExc_Exception, + (errno<=sys_nerr-1)? + sys_errlist[errno]: + "Unknown Error on fopen() of confifuration file"); +*/ + return NULL; + } +} + + + +/* + * wlp_setfilebydf(): get FILE* fd from FileObject. + */ + +static PyObject *wlp_setfilebyfd(PyObject *self, PyObject *args) { + PyObject *file = NULL; + + if(!PyArg_ParseTuple(args, "O", &file)) + return NULL; + + if(!file) + return NULL; + + if(!PyFile_Check(file)) + return NULL; + + fd = PyFile_AsFile(file); + + return Py_None; +} + +/* + * wlp_mkdict(): make a dictonary of the form + * {ownername: {var1: val1, var2: val2,...}} + */ + +static PyObject *wlp_mkdict(PyObject *self, PyObject *args) { + PyObject *pydicttmp = NULL; + PyObject *pydict = PyDict_New(); + struct wlp_node_t *tmp; + int count; + + if(!pydict) + return NULL; + + /* fopen()*/ + if(fd) + parse(fd); + else + return Py_None; + + if(list) + for(tmp = list->head, count = 0; + tmp != list->head || count == 0; + tmp = tmp->next, count++) { + DBG("FOUND(%d) '%s' ('%s': '%s')\n",count,tmp->owner,tmp->left,tmp->right); + pydicttmp = PyDict_GetItem(pydict, + PyString_FromString(tmp->owner)); + + if(!pydicttmp) { + DBG("%s: owner not found, create new item\n", + tmp->owner); + PyDict_SetItemString(pydict, + tmp->owner, + node2dict(tmp)); + } else { + DBG("%s: owner found,appendig items\n", + tmp->owner); + PyDict_SetItemString(pydicttmp, + tmp->left, + Py_BuildValue("s",tmp->right)); + PyDict_SetItemString(pydict, + tmp->owner, + pydicttmp); + } + } + + return pydict; +} + +/* + * node2dict(): transoform a wlp_node_t node in a python dictionary of the form + * { var: val } + * to be used by mkdict() + */ + +static PyObject *node2dict(struct wlp_node_t *node) { + PyObject *dict = PyDict_New(); + + if(!dict) + dict = Py_None; + else { + PyDict_SetItem(dict, + Py_BuildValue("s",node->left), + Py_BuildValue("s",node->right)); + } + + return dict; + +} + + +/* + * node2dict2(): transoform a wlp_node_t node in a python dictionary of the form + * { 'owner': ownername, var: val} + * it's currently unused (obsoleted) + */ + +static PyObject *node2dict2(struct wlp_node_t *node) { + PyObject *dict = PyDict_New(); + + if(!dict) + dict = Py_None; + else { + PyDict_SetItem(dict, + Py_BuildValue("s","owner"), + Py_BuildValue("s",node->owner)); + + PyDict_SetItem(dict, + Py_BuildValue("s",node->left), + Py_BuildValue("s",node->right)); + } + + return dict; + +} + +static PyMethodDef wlp_methods[] = { + {"mkdict", wlp_mkdict, METH_VARARGS}, + {"setfilebyname", wlp_setfilebyname, METH_VARARGS}, + {"setfilebyfd", wlp_setfilebyfd, METH_VARARGS}, + {NULL,NULL} +}; + + +void initwlp() { + (void) Py_InitModule("wlp",wlp_methods); +} |