summaryrefslogtreecommitdiff
path: root/navit/binding/python/binding_python.c
blob: cb33f83b36fadb8b992f5e2b5ab9395a203f5ceb (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "config.h"
#include <glib.h>
#include <Python.h>
#include <fcntl.h>
#include "coord.h"
#include "projection.h"
#include "map.h"
#include "mapset.h"
#include "plugin.h"

#if defined(MS_WINDOWS) || defined(__CYGWIN__)
#define Obj_HEAD PyObject_HEAD_INIT(NULL);
#else
#define Obj_HEAD PyObject_HEAD_INIT(&PyType_Type)
#endif

/* *** coord *** */

typedef struct {
	PyObject_HEAD
	struct coord *c;
} coordObject;

static void coord_destroy_py(coordObject *self);

PyTypeObject coord_Type = {
	Obj_HEAD
	.tp_name="coord",
	.tp_basicsize=sizeof(coordObject),
	.tp_dealloc=(destructor)coord_destroy_py,
};


/* *** map *** */

typedef struct {
	PyObject_HEAD
	struct map *m;
} mapObject;

static void map_destroy_py(mapObject *self);
static PyObject *map_getattr_py(PyObject *self, char *name);

PyTypeObject map_Type = {
	Obj_HEAD
	.tp_name="map",
	.tp_basicsize=sizeof(mapObject),
	.tp_dealloc=(destructor)map_destroy_py,
	.tp_getattr=map_getattr_py,
};

/* *** IMPLEMENTATIONS *** */

/* *** coord *** */

static PyObject *
coord_new_py(PyObject *self, PyObject *args)
{
	coordObject *ret;
	int x,y;
	if (!PyArg_ParseTuple(args, "ii:navit.coord",&x,&y))
		return NULL;
	ret=PyObject_NEW(coordObject, &coord_Type);
	ret->c=coord_new(x,y);
	return (PyObject *)ret;
}

static void
coord_destroy_py(coordObject *self)
{
		coord_destroy(self->c);
}

/* *** coord_rect *** */

typedef struct {
	PyObject_HEAD
	struct coord_rect *r;
} coord_rectObject;


static void coord_rect_destroy_py(coord_rectObject *self);

PyTypeObject coord_rect_Type = {
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
	PyObject_HEAD_INIT(NULL);
#else
	PyObject_HEAD_INIT(&PyType_Type)
#endif
	.tp_name="coord_rect",
	.tp_basicsize=sizeof(coord_rectObject),
	.tp_dealloc=(destructor)coord_rect_destroy_py,
};

static PyObject *
coord_rect_new_py(PyObject *self, PyObject *args)
{
	coord_rectObject *ret;
	coordObject *lu,*rd;
	if (!PyArg_ParseTuple(args, "O!O!:navit.coord_rect_rect",&coord_Type,&lu,&coord_Type,&rd))
		return NULL;
	ret=PyObject_NEW(coord_rectObject, &coord_rect_Type);
	ret->r=coord_rect_new(lu->c,rd->c);
	return (PyObject *)ret;
}

static void
coord_rect_destroy_py(coord_rectObject *self)
{
	coord_rect_destroy(self->r);
}

/* *** map_rect *** */

typedef struct {
	PyObject_HEAD
	struct map_rect *mr;
} map_rectObject;


static void map_rect_destroy_py(map_rectObject *self);

PyTypeObject map_rect_Type = {
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
	PyObject_HEAD_INIT(NULL);
#else
	PyObject_HEAD_INIT(&PyType_Type)
#endif
	.tp_name="map_rect",
	.tp_basicsize=sizeof(map_rectObject),
	.tp_dealloc=(destructor)map_rect_destroy_py,
};

static PyObject *
map_rect_new_py(mapObject *self, PyObject *args)
{
	map_rectObject *ret;
	coord_rectObject *r;
	if (!PyArg_ParseTuple(args, "O!:navit.map_rect_rect",&coord_rect_Type,&r))
		return NULL;
	ret=PyObject_NEW(map_rectObject, &map_rect_Type);
	ret->mr=map_rect_new(self->m, NULL);
	return (PyObject *)ret;
}

static void
map_rect_destroy_py(map_rectObject *self)
{
	map_rect_destroy(self->mr);
}


/* *** map *** */



static PyMethodDef map_methods[] = {
	{"map_rect_new",	(PyCFunction) map_rect_new_py, METH_VARARGS },
	{NULL, NULL },
};

static PyObject *
map_getattr_py(PyObject *self, char *name)
{
	return Py_FindMethod(map_methods, self, name);
}


static PyObject *
map_new_py(PyObject *self, PyObject *args)
{
	mapObject *ret;
	char *type, *filename;
	
	if (!PyArg_ParseTuple(args, "ss:navit.map", &type, &filename))
		return NULL;
	ret=PyObject_NEW(mapObject, &map_Type);
	ret->m=map_new(type,NULL);
	return (PyObject *)ret;
}

static void
map_destroy_py(mapObject *self)
{
	map_destroy(self->m);
}

/* *** mapset *** */


typedef struct {
	PyObject_HEAD
	struct mapset *ms;
} mapsetObject;


static void mapset_destroy_py(mapsetObject *self);
static PyObject *mapset_getattr_py(PyObject *self, char *name);

PyTypeObject mapset_Type = {
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
	PyObject_HEAD_INIT(NULL);
#else
	PyObject_HEAD_INIT(&PyType_Type)
#endif
	.tp_name="mapset",
	.tp_basicsize=sizeof(mapsetObject),
	.tp_dealloc=(destructor)mapset_destroy_py,
	.tp_getattr=mapset_getattr_py,
};

static PyObject *
mapset_add_py(mapsetObject *self, PyObject *args)
{
	mapObject *map;
	if (!PyArg_ParseTuple(args, "O:navit.mapset", &map))
		return NULL;
	Py_INCREF(map);
	mapset_add(self->ms, map->m);
	return Py_BuildValue("");
}

static PyMethodDef mapset_methods[] = {
	{"add",	(PyCFunction) mapset_add_py, METH_VARARGS },
	{NULL, NULL },
};

static PyObject *
mapset_getattr_py(PyObject *self, char *name)
{
	return Py_FindMethod(mapset_methods, self, name);
}

static PyObject *
mapset_new_py(PyObject *self, PyObject *args)
{
	mapsetObject *ret;
	if (!PyArg_ParseTuple(args, ":navit.mapset"))
		return NULL;
	ret=PyObject_NEW(mapsetObject, &mapset_Type);
	ret->ms=mapset_new();
	return (PyObject *)ret;
}

static void
mapset_destroy_py(mapsetObject *self)
{
	mapset_destroy(self->ms);
}



static PyMethodDef navitMethods[]={
	{"coord", coord_new_py, METH_VARARGS, "Create a new coordinate point."},
	{"coord_rect", coord_rect_new_py, METH_VARARGS, "Create a new coordinate rectangle."},
	{"map", map_new_py, METH_VARARGS, "Create a new map."},
	{"mapset", mapset_new_py, METH_VARARGS, "Create a new mapset."},
	{NULL, NULL, 0, NULL}
};


void
plugin_init(void)
{
	int fd,size;
	char buffer[65536];

	return;

	Py_Initialize();
	Py_InitModule("navit", navitMethods);
	fd=open("startup.py",O_RDONLY);
	if (fd >= 0) {
		size=read(fd, buffer, 65535);
		if (size > 0) {
			buffer[size]='\0';
			PyRun_SimpleString(buffer);
		}
	}
	
	Py_Finalize();
	exit(0);
}