Add constants and wrap a few more SymbolType fields
[gnome.gobject-introspection] / giscanner / giscannermodule.c
1 /* GObject introspection: scanner
2  *
3  * Copyright (C) 2008
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 #include "sourcescanner.h"
26 #include <Python.h>
27
28 #define NEW_CLASS(ctype, name, cname)         \
29 static PyMethodDef _Py##cname##_methods[];    \
30 PyTypeObject Py##cname##_Type = {             \
31     PyObject_HEAD_INIT(NULL)                  \
32     0,                                        \
33     "scanner." name,                          \
34     sizeof(ctype),                    \
35     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,             \
36     0, 0, 0, 0, 0, 0,                         \
37     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
38     NULL, 0, 0, 0,                            \
39     0,        \
40     0, 0,                                     \
41     0,                                        \
42     0, 0, NULL, NULL, 0, 0,                   \
43     0             \
44 }
45
46 #define REGISTER_TYPE(d, name, type)          \
47     type.ob_type = &PyType_Type;              \
48     type.tp_alloc = PyType_GenericAlloc;      \
49     type.tp_new = PyType_GenericNew;          \
50     if (PyType_Ready (&type))                 \
51         return;                               \
52     PyDict_SetItemString (d, name, (PyObject *)&type); \
53     Py_INCREF (&type);
54
55 typedef struct {
56   PyObject_HEAD
57   GISourceType *type;
58 } PyGISourceType;
59
60 typedef struct {
61   PyObject_HEAD
62   GISourceSymbol *symbol;
63 } PyGISourceSymbol;
64
65 typedef struct {
66   PyObject_HEAD
67   GISourceScanner *scanner;
68 } PyGISourceScanner;
69
70 NEW_CLASS(PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
71 NEW_CLASS(PyGISourceType, "SourceType", GISourceType);
72 NEW_CLASS(PyGISourceScanner, "SourceScanner", GISourceScanner);
73
74
75 /* Symbol */
76
77 static PyObject *
78 symbol_get_type(PyGISourceSymbol *self,
79                 void             *context)
80 {
81   return PyInt_FromLong(self->symbol->type);
82 }
83
84 static PyObject *
85 symbol_get_ident(PyGISourceSymbol *self,
86                   void            *context)
87 {
88   return PyString_FromString(self->symbol->ident);
89 }
90
91 static PyObject *
92 symbol_get_base_type(PyGISourceSymbol *self,
93                      void             *context)
94 {
95   PyGISourceType *item;
96   item = (PyGISourceType *)PyObject_GC_New(PyGISourceType,
97                                            &PyGISourceType_Type);
98   item->type = self->symbol->base_type;
99   return (PyObject*)item;
100 }
101
102 static PyGetSetDef _PyGISourceSymbol_getsets[] = {
103   /* int ref_count; */
104   { "type", (getter)symbol_get_type, NULL, NULL},
105   /* int id; */
106   { "ident", (getter)symbol_get_ident, NULL, NULL},
107   { "base_type", (getter)symbol_get_base_type, NULL, NULL},
108   /* gboolean const_int_set; */
109   /* int const_int; */
110   /* char *const_string; */
111   /* GSList *directives; */
112   { 0 }
113 };
114
115
116
117 /* Type */
118
119 static PyObject *
120 type_get_type(PyGISourceType *self,
121               void           *context)
122 {
123   return PyInt_FromLong(self->type->type);
124 }
125
126 static PyObject *
127 type_get_storage_class_specifier(PyGISourceType *self,
128                                  void           *context)
129 {
130   return PyInt_FromLong(self->type->storage_class_specifier);
131 }
132
133 static PyObject *
134 type_get_type_qualifier(PyGISourceType *self,
135                         void           *context)
136 {
137   return PyInt_FromLong(self->type->type_qualifier);
138 }
139
140 static PyObject *
141 type_get_function_specifier(PyGISourceType *self,
142                             void           *context)
143 {
144   return PyInt_FromLong(self->type->function_specifier);
145 }
146
147 static PyObject *
148 type_get_name(PyGISourceType *self,
149               void           *context)
150 {
151   if (!self->type->name)
152     {
153       Py_INCREF(Py_None);
154       return Py_None;
155     }
156     
157   return PyString_FromString(self->type->name);
158 }
159
160 static PyObject *
161 type_get_base_type(PyGISourceType *self,
162                    void             *context)
163 {
164   PyGISourceType *item;
165   item = (PyGISourceType *)PyObject_GC_New(PyGISourceType,
166                                            &PyGISourceType_Type);
167   item->type = self->type->base_type;
168   return (PyObject*)item;
169 }
170
171 static PyGetSetDef _PyGISourceType_getsets[] = {
172   { "type", (getter)type_get_type, NULL, NULL},
173   { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
174   { "type_qualifier", (getter)type_get_type_qualifier, NULL, NULL},
175   { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
176   { "name", (getter)type_get_name, NULL, NULL},
177   { "base_type", (getter)type_get_base_type, NULL, NULL},
178   /* { "child_list", (getter)type_get_child_list, NULL, NULL}, */
179   { 0 }
180 };
181
182
183
184 /* Scanner */
185
186 static int
187 pygi_source_scanner_init (PyGISourceScanner *self,
188                           PyObject          *args,
189                           PyObject          *kwargs)
190 {
191   if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
192     return -1;
193
194   self->scanner = gi_source_scanner_new ();
195
196   return 0;
197 }
198
199 static PyObject *
200 pygi_source_scanner_parse_file (PyGISourceScanner *self,
201                                 PyObject          *args)
202 {
203   char *filename;
204   FILE *fp;
205   
206   if (!PyArg_ParseTuple (args, "s:SourceScanner.__init__", &filename))
207     return NULL;
208
209   fp = fopen (filename, "r");
210   if (!fp)
211     {
212       PyErr_SetFromErrnoWithFilename (PyExc_OSError, filename);
213       return NULL;
214     }
215
216   self->scanner->filenames =
217     g_list_append (self->scanner->filenames, g_strdup (filename));
218   self->scanner->current_filename = self->scanner->filenames->data;
219   
220   if (!gi_source_scanner_parse_file (self->scanner, fp))
221     {
222       g_print ("Something went wrong..\n");
223       return NULL;
224     }
225
226   fclose (fp);
227
228   Py_INCREF (Py_None);
229   return Py_None;
230 }
231
232 static PyObject *
233 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
234                                     PyObject          *args)
235 {
236   int macro_scan;
237   
238   if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", &macro_scan))
239     return NULL;
240
241   gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
242
243   Py_INCREF (Py_None);
244   return Py_None;
245 }
246
247 static PyObject *
248 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
249 {
250   GSList *l, *symbols;
251   PyObject *list;
252   int i = 0;
253   
254   symbols = gi_source_scanner_get_symbols (self->scanner);
255   list = PyList_New (g_slist_length (symbols));
256   
257   for (l = symbols; l; l = l->next)
258     {
259       PyGISourceSymbol *item;
260       item = (PyGISourceSymbol *)PyObject_GC_New(PyGISourceSymbol,
261                                                  &PyGISourceSymbol_Type);
262       item->symbol = l->data;
263       PyList_SetItem(list, i++, (PyObject*)item);
264     }
265   
266   return list;
267 }
268
269 static PyMethodDef _PyGISourceScanner_methods[] = {
270   { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
271   { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
272   { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
273   { NULL, NULL, 0 }
274 };
275
276
277 /* Module */
278
279 PyMethodDef pyscanner_functions[] = {
280   { NULL, NULL, 0, NULL }
281 };
282
283 DL_EXPORT(void)
284 init_giscanner(void)
285 {
286     PyObject *m, *d;
287
288     m = Py_InitModule ("giscanner._giscanner", pyscanner_functions);
289     d = PyModule_GetDict (m);
290
291     PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
292     PyGISourceScanner_Type.tp_methods = _PyGISourceScanner_methods;
293     REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
294
295     PyGISourceSymbol_Type.tp_getset = _PyGISourceSymbol_getsets;
296     REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
297
298     PyGISourceType_Type.tp_getset = _PyGISourceType_getsets;
299     REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);
300 }