aboutsummaryrefslogtreecommitdiffstats
path: root/wlp/C/wlp.c
blob: 8c0779dd01a66036b30dc80be96b98ef43a2f4fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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);
}