File: vtkWrapPythonInit.c

package info (click to toggle)
vtk 5.0.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 51,080 kB
  • ctags: 67,442
  • sloc: cpp: 522,627; ansic: 221,292; tcl: 43,377; python: 14,072; perl: 3,102; java: 1,436; yacc: 1,033; sh: 469; lex: 248; makefile: 181; asm: 154
file content (113 lines) | stat: -rw-r--r-- 2,964 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* warning this code is also in getclasses.cxx under pcmaker */
/* this roputine creates the init file */
static void CreateInitFile(const char *libName, 
  int numConcrete, char **concrete, 
  FILE *fout) 
{
  int i;

#if defined(_WIN32) && !defined(__CYGWIN__)
  const char* prefix = "";
#else
  const char* prefix = "lib";
#endif

#if defined(_WIN32)
  const char* dllexp = "__declspec(dllexport) ";
#else
  const char* dllexp = "";
#endif
  
  fprintf(fout,"// Generated by vtkWrapPythonInit in VTK/Wrapping\n");
  fprintf(fout,"#include \"vtkPython.h\"\n\n");
  fprintf(fout,"#include \"vtkSystemIncludes.h\"\n");
  fprintf(fout,"#include <string.h>\n");
  fprintf(fout,"// Handle compiler warning messages, etc.\n"
          "#if defined( _MSC_VER ) && !defined(VTK_DISPLAY_WIN32_WARNINGS)\n"
          "#pragma warning ( disable : 4706 )\n"
          "#endif // Windows Warnings \n\n");
  
for (i = 0; i < numConcrete; i++)
    {
    fprintf(fout,"extern  \"C\" {%sPyObject *PyVTKClass_%sNew(char *); }\n", dllexp, concrete[i]);
    }
  
  fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n", libName);
  fprintf(fout,"{NULL, NULL, 0, NULL}};\n\n");

  fprintf(fout,"extern  \"C\" {%svoid init%s%s();}\n\n", dllexp, prefix, libName);
  fprintf(fout,"void init%s%s()\n{\n", prefix, libName);
  
  /* module init function */
  fprintf(fout,"  PyObject *m, *d, *c;\n\n");
  fprintf(fout,"  static const char modulename[] = \"%s%s\";\n", prefix, libName);
  fprintf(fout,"  m = Py_InitModule((char*)modulename, Py%s_ClassMethods);\n",
    libName);

  fprintf(fout,"  d = PyModule_GetDict(m);\n");
  fprintf(fout,"  if (!d) Py_FatalError((char*)\"can't get dictionary for module %s!\");\n\n",
    libName);

  for (i = 0; i < numConcrete; i++)
    {
    fprintf(fout,"  if ((c = PyVTKClass_%sNew((char*)modulename)))\n",
      concrete[i]);
    fprintf(fout,"    if (-1 == PyDict_SetItemString(d, (char*)\"%s\", c))\n",
      concrete[i]);
    fprintf(fout,"      Py_FatalError((char*)\"can't add class %s to dictionary!\");\n\n",
      concrete[i]);
    }
  fprintf(fout,"}\n\n");
}


int main(int argc,char *argv[])
{
  FILE *file;
  FILE *fout;
  int numConcrete = 0;
  char libName[250];
  char tmpVal[250];
  char *concrete[4000];

  if (argc < 3)
    {
    fprintf(stderr,"Usage: %s input_file output_file\n",argv[0]);
    return 1;
    }

  file = fopen(argv[1],"r");
  if (!file) 
    {
    fprintf(stderr,"Input file %s could not be opened\n",argv[1]);
    return 1;
    }

  fout = fopen(argv[2],"w");
  if (!fout)
    {
    return 1;
    }

  /* read the info from the file */
  fscanf(file,"%s",libName);

  /* read in the classes */
  while (fscanf(file,"%s",tmpVal) != EOF)
    {
    concrete[numConcrete] = strdup(tmpVal);
    numConcrete++;
    }
  /* close the file */
  fclose(file);

  CreateInitFile(libName, numConcrete, concrete, fout);
  fclose(fout);

  return 0;
}