Salome HOME
Merge from V6_main 11/02/2013
[modules/paravis.git] / src / ParaView / vtkParseMain.c
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    vtkParseMain.c
5
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13
14 =========================================================================*/
15
16 /*
17
18 This file provides a unified front-end for the wrapper generators.
19 It contains the main() function and argument parsing, and calls
20 the code that parses the header file.
21
22 */
23
24 #include "vtkParse.h"
25 #include "vtkParseMain.h"
26 #include "vtkParseInternal.h"
27 #include "vtkConfigure.h" // VTK_VERSION
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/stat.h>
32
33 /* This is the struct that contains the options */
34 OptionInfo options;
35
36 /* Flags for --help and --version */
37 int vtk_parse_help = 0;
38 int vtk_parse_version = 0;
39
40 /* This method provides the back-end for the generators */
41 extern void vtkParseOutput(FILE *, FileInfo *);
42
43 /* Check the options */
44 static int check_options(int argc, char *argv[])
45 {
46   int i;
47   size_t j;
48
49   options.InputFileName = NULL;
50   options.OutputFileName = NULL;
51   options.IsAbstract = 0;
52   options.IsConcrete = 0;
53   options.IsVTKObject = 0;
54   options.IsSpecialObject = 0;
55   options.HierarchyFileName = 0;
56   options.HintFileName = 0;
57
58   for (i = 1; i < argc && argv[i][0] == '-'; i++)
59     {
60     if (strcmp(argv[i], "--concrete") == 0)
61       {
62       options.IsConcrete = 1;
63       }
64     else if (strcmp(argv[i], "--abstract") == 0)
65       {
66       options.IsAbstract = 1;
67       }
68     else if (strcmp(argv[i], "--vtkobject") == 0)
69       {
70       options.IsVTKObject = 1;
71       }
72     else if (strcmp(argv[i], "--special") == 0)
73       {
74       options.IsSpecialObject = 1;
75       }
76     else if (strcmp(argv[i], "--hints") == 0)
77       {
78       i++;
79       if (i >= argc || argv[i][0] == '-')
80         {
81         return -1;
82         }
83       options.HintFileName = argv[i];
84       }
85     else if (strcmp(argv[i], "--types") == 0)
86       {
87       i++;
88       if (i >= argc || argv[i][0] == '-')
89         {
90         return -1;
91         }
92       options.HierarchyFileName = argv[i];
93       }
94     else if (strcmp(argv[i], "-I") == 0)
95       {
96       i++;
97       if (i >= argc || argv[i][0] == '-')
98         {
99         return -1;
100         }
101       vtkParse_IncludeDirectory(argv[i]);
102       }
103     else if (strcmp(argv[i], "-D") == 0)
104       {
105       i++;
106       j = 0;
107       if (i >= argc || argv[i][0] == '-')
108         {
109         return -1;
110         }
111       while (argv[i][j] != '\0' && argv[i][j] != '=') { j++; }
112       if (argv[i][j] == '=') { j++; }
113       vtkParse_DefineMacro(argv[i], &argv[i][j]);
114       }
115     else if (strcmp(argv[i], "-U") == 0)
116       {
117       i++;
118       if (i >= argc || argv[i][0] == '-')
119         {
120         return -1;
121         }
122       vtkParse_UndefineMacro(argv[i]);
123       }
124     else if (strcmp(argv[i], "--help") == 0)
125       {
126       vtk_parse_help = 1;
127       }
128     else if (strcmp(argv[i], "--version") == 0)
129       {
130       vtk_parse_version = 1;
131       }
132     }
133
134   return i;
135 }
136
137 /* Return a pointer to the static OptionInfo struct */
138 OptionInfo *vtkParse_GetCommandLineOptions()
139 {
140   return &options;
141 }
142
143 static void vtk_parse_print_help(FILE *stream, const char *cmd)
144 {
145   fprintf(stream,
146     "Usage: %s [options] input_file output_file\n"
147     "  --help          print this help message\n"
148     "  --version       print the VTK version\n"
149     "  --concrete      force concrete class\n"
150     "  --abstract      force abstract class\n"
151     "  --vtkobject     vtkObjectBase-derived class\n"
152     "  --special       non-vtkObjectBase class\n"
153     "  --hints <file>  the hints file to use\n"
154     "  --types <file>  the type hierarchy file to use\n"
155     "  -I <dir>        add an include directory\n"
156     "  -D <macro>      define a preprocessor macro\n"
157     "  -U <macro>      undefine a preprocessor macro\n",
158     cmd);
159 }
160
161 int main(int argc, char *argv[])
162 {
163   int argi;
164   int has_options = 0;
165   FILE *ifile;
166   FILE *ofile;
167   FILE *hfile = 0;
168   const char *cp;
169   char *classname;
170   size_t i;
171   FileInfo *data;
172
173   argi = check_options(argc, argv);
174   if (argi > 1 && argc - argi == 2)
175     {
176     has_options = 1;
177     }
178   else if (argi < 0 || argc > 5 ||
179            (argc < 3 && !vtk_parse_help && !vtk_parse_version))
180     {
181     vtk_parse_print_help(stderr, argv[0]);
182     exit(1);
183     }
184
185   if (vtk_parse_version)
186     {
187     const char *ver = VTK_VERSION;
188     const char *exename = argv[0];
189     /* remove directory part of exe name */
190     for (exename += strlen(exename); exename > argv[0]; --exename)
191       {
192       char pc = *(exename - 1);
193       if (pc == ':' || pc == '/' || pc == '\\')
194         {
195         break;
196         }
197       }
198     fprintf(stdout, "%s %s\n", exename, ver);
199     exit(0);
200     }
201   if (vtk_parse_help)
202     {
203     vtk_parse_print_help(stdout, argv[0]);
204     exit(0);
205     }
206
207   options.InputFileName = argv[argi++];
208
209   ifile = fopen(options.InputFileName, "r");
210   if (!ifile)
211     {
212     fprintf(stderr,"Error opening input file %s\n", options.InputFileName);
213     exit(1);
214     }
215
216   if (!has_options)
217     {
218     if (argc == 5)
219       {
220       options.HintFileName = argv[argi++];
221       }
222     if (argc >= 4)
223       {
224       options.IsConcrete = atoi(argv[argi++]);
225       options.IsAbstract = !options.IsConcrete;
226       }
227     }
228
229   if (options.HintFileName && options.HintFileName[0] != '\0')
230     {
231     hfile = fopen(options.HintFileName, "r");
232     if (!hfile)
233       {
234       fprintf(stderr, "Error opening hint file %s\n", options.HintFileName);
235       fclose(ifile);
236       exit(1);
237       }
238     }
239
240   options.OutputFileName = argv[argi++];
241   ofile = fopen(options.OutputFileName, "w");
242
243   if (!ofile)
244     {
245     fprintf(stderr, "Error opening output file %s\n", options.OutputFileName);
246     fclose(ifile);
247     if (hfile)
248       {
249       fclose(hfile);
250       }
251     exit(1);
252     }
253
254   if (options.IsConcrete)
255     {
256     cp = options.InputFileName;
257     i = strlen(cp);
258     classname = (char *)malloc(i+1);
259     while (i > 0 &&
260            cp[i-1] != '/' && cp[i-1] != '\\' && cp[i-1] != ':') { i--; }
261     strcpy(classname, &cp[i]);
262     i = 0;
263     while (classname[i] != '\0' && classname[i] != '.') { i++; }
264     classname[i] = '\0';
265
266     vtkParse_SetClassProperty(classname, "concrete");
267     }
268
269   vtkParse_SetIgnoreBTX(0);
270   if (options.HierarchyFileName)
271     {
272     vtkParse_SetIgnoreBTX(1);
273     }
274
275   data = vtkParse_ParseFile(options.InputFileName, ifile, stderr);
276
277   if (!data)
278     {
279     fclose(ifile);
280     fclose(ofile);
281     if (hfile)
282       {
283       fclose(hfile);
284       }
285     exit(1);
286     }
287
288   if (hfile)
289     {
290     vtkParse_ReadHints(data, hfile, stderr);
291     }
292
293   if (options.IsConcrete && data->MainClass)
294     {
295     data->MainClass->IsAbstract = 0;
296     }
297   else if (options.IsAbstract && data->MainClass)
298     {
299     data->MainClass->IsAbstract = 1;
300     }
301
302   vtkParseOutput(ofile, data);
303
304   fclose(ofile);
305
306   vtkParse_Free(data);
307
308   return 0;
309 }