1 /* GObject introspection: scanner
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.
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.
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.
25 #include "sourcescanner.h"
28 #define NEW_CLASS(ctype, name, cname) \
29 static PyMethodDef _Py##cname##_methods[]; \
30 PyTypeObject Py##cname##_Type = { \
31 PyObject_HEAD_INIT(NULL) \
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
37 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
42 0, 0, NULL, NULL, 0, 0, \
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)) \
52 PyDict_SetItemString (d, name, (PyObject *)&type); \
62 GISourceSymbol *symbol;
67 GISourceScanner *scanner;
70 NEW_CLASS(PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
71 NEW_CLASS(PyGISourceType, "SourceType", GISourceType);
72 NEW_CLASS(PyGISourceScanner, "SourceScanner", GISourceScanner);
78 symbol_get_type(PyGISourceSymbol *self,
81 return PyInt_FromLong(self->symbol->type);
85 symbol_get_ident(PyGISourceSymbol *self,
88 return PyString_FromString(self->symbol->ident);
92 symbol_get_base_type(PyGISourceSymbol *self,
96 item = (PyGISourceType *)PyObject_GC_New(PyGISourceType,
97 &PyGISourceType_Type);
98 item->type = self->symbol->base_type;
99 return (PyObject*)item;
102 static PyGetSetDef _PyGISourceSymbol_getsets[] = {
103 { "type", (getter)symbol_get_type, NULL, NULL},
104 { "ident", (getter)symbol_get_ident, NULL, NULL},
105 { "base_type", (getter)symbol_get_base_type, NULL, NULL},
114 type_get_type(PyGISourceType *self,
117 return PyInt_FromLong(self->type->type);
121 type_get_name(PyGISourceType *self,
124 if (!self->type->name)
130 return PyString_FromString(self->type->name);
133 static PyGetSetDef _PyGISourceType_getsets[] = {
134 { "type", (getter)type_get_type, NULL, NULL},
135 { "name", (getter)type_get_name, NULL, NULL},
144 pygi_source_scanner_init (PyGISourceScanner *self,
148 if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
151 self->scanner = gi_source_scanner_new ();
157 pygi_source_scanner_parse_file (PyGISourceScanner *self,
163 if (!PyArg_ParseTuple (args, "s:SourceScanner.__init__", &filename))
166 fp = fopen (filename, "r");
169 PyErr_SetFromErrnoWithFilename (PyExc_OSError, filename);
173 self->scanner->filenames =
174 g_list_append (self->scanner->filenames, g_strdup (filename));
175 self->scanner->current_filename = self->scanner->filenames->data;
177 if (!gi_source_scanner_parse_file (self->scanner, fp))
179 g_print ("Something went wrong..\n");
190 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
195 if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", ¯o_scan))
198 gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
205 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
211 symbols = gi_source_scanner_get_symbols (self->scanner);
212 list = PyList_New (g_slist_length (symbols));
214 for (l = symbols; l; l = l->next)
216 PyGISourceSymbol *item;
217 item = (PyGISourceSymbol *)PyObject_GC_New(PyGISourceSymbol,
218 &PyGISourceSymbol_Type);
219 item->symbol = l->data;
220 PyList_SetItem(list, i++, (PyObject*)item);
226 static PyMethodDef _PyGISourceScanner_methods[] = {
227 { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
228 { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
229 { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
236 PyMethodDef pyscanner_functions[] = {
237 { NULL, NULL, 0, NULL }
245 m = Py_InitModule ("giscanner._giscanner", pyscanner_functions);
246 d = PyModule_GetDict (m);
248 PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
249 PyGISourceScanner_Type.tp_methods = _PyGISourceScanner_methods;
250 REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
252 PyGISourceSymbol_Type.tp_getset = _PyGISourceSymbol_getsets;
253 REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
255 PyGISourceType_Type.tp_getset = _PyGISourceType_getsets;
256 REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);