]> Creatis software - gdcm.git/blob - gdcmPython/vtkWrapPythonInit.c
Fix mistypings
[gdcm.git] / gdcmPython / vtkWrapPythonInit.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 char *names[1000];
5 char *kitName;
6 int anindex = 0;
7
8 /* warning this code is also in getclasses.cxx under pcmaker */
9 void stuffit()
10 {
11   int i;
12   
13   for (i = 0; i < anindex; i++)
14     {
15     fprintf(stdout,"extern \"C\" { PyObject *PyVTKClass_%sNew(char *); }\n",
16             names[i]);
17     }
18
19   fprintf(stdout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
20           kitName);
21   fprintf(stdout,"{NULL, NULL}};\n\n");
22   
23   fprintf(stdout,"extern \"C\" { void init%s();}\n\n",kitName);
24
25   /* module init function */
26   fprintf(stdout,"void init%s()\n{\n",kitName);
27   fprintf(stdout,"  PyObject *m, *d, *c;\n\n");
28   fprintf(stdout,"  static char modulename[] = \"%s\";\n",kitName);
29   fprintf(stdout,"  m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
30           kitName);
31   
32   fprintf(stdout,"  d = PyModule_GetDict(m);\n");
33   fprintf(stdout,"  if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
34           kitName);
35
36   for (i = 0; i < anindex; i++)
37     {
38     fprintf(stdout,"  if ((c = PyVTKClass_%sNew(modulename)))\n",names[i]);
39     fprintf(stdout,"    if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
40             names[i]);
41     fprintf(stdout,"      Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
42             names[i]);
43     }
44   fprintf(stdout,"}\n\n");
45 }
46
47 int main(int argc,char *argv[])
48 {
49   int i,j;
50   char tmp[128];
51   FILE *file;
52
53   if (argc < 3)
54     {
55     fprintf(stderr,"Usage: %s kit_name file1 file2 file3 ...\n",argv[0]);
56     exit(1);
57     }
58   
59   kitName = strdup(argv[1]);
60   
61   /* fill in the correct arrays */
62   for (i = 2, j = 0; i < argc; i++)
63     {
64     strcpy(tmp,argv[i]);
65     strcpy(tmp+strlen(tmp)-2,"Python.cxx");
66     file = fopen(tmp,"r");
67     if (file) 
68       {
69       fgets(tmp,22,file);
70       fclose(file);
71       }
72     if (strcmp(tmp,"// python wrapper for") != 0)
73       {
74       continue;
75       }
76     /* remove the .h and store */
77     names[j] = strdup(argv[i]);
78     names[j++][strlen(argv[i])-2] = '\0';
79     }
80   anindex = j;
81
82   fprintf(stdout,"#include <string.h>\n");
83   fprintf(stdout,"#include \"Python.h\"\n\n");
84
85   stuffit();
86   
87   return 0;
88 }
89