Back to index

salome-paravis  6.5.0
vtkParseMain.c
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkParseMain.c
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 
00016 /*
00017 
00018 This file provides a unified front-end for the wrapper generators.
00019 It contains the main() function and argument parsing, and calls
00020 the code that parses the header file.
00021 
00022 */
00023 
00024 #include "vtkParse.h"
00025 #include "vtkParseMain.h"
00026 #include "vtkParseInternal.h"
00027 #include "vtkConfigure.h" // VTK_VERSION
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <sys/stat.h>
00032 
00033 /* This is the struct that contains the options */
00034 OptionInfo options;
00035 
00036 /* Flags for --help and --version */
00037 int vtk_parse_help = 0;
00038 int vtk_parse_version = 0;
00039 
00040 /* This method provides the back-end for the generators */
00041 extern void vtkParseOutput(FILE *, FileInfo *);
00042 
00043 /* Check the options */
00044 static int check_options(int argc, char *argv[])
00045 {
00046   int i;
00047   size_t j;
00048 
00049   options.InputFileName = NULL;
00050   options.OutputFileName = NULL;
00051   options.IsAbstract = 0;
00052   options.IsConcrete = 0;
00053   options.IsVTKObject = 0;
00054   options.IsSpecialObject = 0;
00055   options.HierarchyFileName = 0;
00056   options.HintFileName = 0;
00057 
00058   for (i = 1; i < argc && argv[i][0] == '-'; i++)
00059     {
00060     if (strcmp(argv[i], "--concrete") == 0)
00061       {
00062       options.IsConcrete = 1;
00063       }
00064     else if (strcmp(argv[i], "--abstract") == 0)
00065       {
00066       options.IsAbstract = 1;
00067       }
00068     else if (strcmp(argv[i], "--vtkobject") == 0)
00069       {
00070       options.IsVTKObject = 1;
00071       }
00072     else if (strcmp(argv[i], "--special") == 0)
00073       {
00074       options.IsSpecialObject = 1;
00075       }
00076     else if (strcmp(argv[i], "--hints") == 0)
00077       {
00078       i++;
00079       if (i >= argc || argv[i][0] == '-')
00080         {
00081         return -1;
00082         }
00083       options.HintFileName = argv[i];
00084       }
00085     else if (strcmp(argv[i], "--types") == 0)
00086       {
00087       i++;
00088       if (i >= argc || argv[i][0] == '-')
00089         {
00090         return -1;
00091         }
00092       options.HierarchyFileName = argv[i];
00093       }
00094     else if (strcmp(argv[i], "-I") == 0)
00095       {
00096       i++;
00097       if (i >= argc || argv[i][0] == '-')
00098         {
00099         return -1;
00100         }
00101       vtkParse_IncludeDirectory(argv[i]);
00102       }
00103     else if (strcmp(argv[i], "-D") == 0)
00104       {
00105       i++;
00106       j = 0;
00107       if (i >= argc || argv[i][0] == '-')
00108         {
00109         return -1;
00110         }
00111       while (argv[i][j] != '\0' && argv[i][j] != '=') { j++; }
00112       if (argv[i][j] == '=') { j++; }
00113       vtkParse_DefineMacro(argv[i], &argv[i][j]);
00114       }
00115     else if (strcmp(argv[i], "-U") == 0)
00116       {
00117       i++;
00118       if (i >= argc || argv[i][0] == '-')
00119         {
00120         return -1;
00121         }
00122       vtkParse_UndefineMacro(argv[i]);
00123       }
00124     else if (strcmp(argv[i], "--help") == 0)
00125       {
00126       vtk_parse_help = 1;
00127       }
00128     else if (strcmp(argv[i], "--version") == 0)
00129       {
00130       vtk_parse_version = 1;
00131       }
00132     }
00133 
00134   return i;
00135 }
00136 
00137 /* Return a pointer to the static OptionInfo struct */
00138 OptionInfo *vtkParse_GetCommandLineOptions()
00139 {
00140   return &options;
00141 }
00142 
00143 static void vtk_parse_print_help(FILE *stream, const char *cmd)
00144 {
00145   fprintf(stream,
00146     "Usage: %s [options] input_file output_file\n"
00147     "  --help          print this help message\n"
00148     "  --version       print the VTK version\n"
00149     "  --concrete      force concrete class\n"
00150     "  --abstract      force abstract class\n"
00151     "  --vtkobject     vtkObjectBase-derived class\n"
00152     "  --special       non-vtkObjectBase class\n"
00153     "  --hints <file>  the hints file to use\n"
00154     "  --types <file>  the type hierarchy file to use\n"
00155     "  -I <dir>        add an include directory\n"
00156     "  -D <macro>      define a preprocessor macro\n"
00157     "  -U <macro>      undefine a preprocessor macro\n",
00158     cmd);
00159 }
00160 
00161 int main(int argc, char *argv[])
00162 {
00163   int argi;
00164   int has_options = 0;
00165   FILE *ifile;
00166   FILE *ofile;
00167   FILE *hfile = 0;
00168   const char *cp;
00169   char *classname;
00170   size_t i;
00171   FileInfo *data;
00172 
00173   argi = check_options(argc, argv);
00174   if (argi > 1 && argc - argi == 2)
00175     {
00176     has_options = 1;
00177     }
00178   else if (argi < 0 || argc > 5 ||
00179            (argc < 3 && !vtk_parse_help && !vtk_parse_version))
00180     {
00181     vtk_parse_print_help(stderr, argv[0]);
00182     exit(1);
00183     }
00184 
00185   if (vtk_parse_version)
00186     {
00187     const char *ver = VTK_VERSION;
00188     const char *exename = argv[0];
00189     /* remove directory part of exe name */
00190     for (exename += strlen(exename); exename > argv[0]; --exename)
00191       {
00192       char pc = *(exename - 1);
00193       if (pc == ':' || pc == '/' || pc == '\\')
00194         {
00195         break;
00196         }
00197       }
00198     fprintf(stdout, "%s %s\n", exename, ver);
00199     exit(0);
00200     }
00201   if (vtk_parse_help)
00202     {
00203     vtk_parse_print_help(stdout, argv[0]);
00204     exit(0);
00205     }
00206 
00207   options.InputFileName = argv[argi++];
00208 
00209   ifile = fopen(options.InputFileName, "r");
00210   if (!ifile)
00211     {
00212     fprintf(stderr,"Error opening input file %s\n", options.InputFileName);
00213     exit(1);
00214     }
00215 
00216   if (!has_options)
00217     {
00218     if (argc == 5)
00219       {
00220       options.HintFileName = argv[argi++];
00221       }
00222     if (argc >= 4)
00223       {
00224       options.IsConcrete = atoi(argv[argi++]);
00225       options.IsAbstract = !options.IsConcrete;
00226       }
00227     }
00228 
00229   if (options.HintFileName && options.HintFileName[0] != '\0')
00230     {
00231     hfile = fopen(options.HintFileName, "r");
00232     if (!hfile)
00233       {
00234       fprintf(stderr, "Error opening hint file %s\n", options.HintFileName);
00235       fclose(ifile);
00236       exit(1);
00237       }
00238     }
00239 
00240   options.OutputFileName = argv[argi++];
00241   ofile = fopen(options.OutputFileName, "w");
00242 
00243   if (!ofile)
00244     {
00245     fprintf(stderr, "Error opening output file %s\n", options.OutputFileName);
00246     fclose(ifile);
00247     if (hfile)
00248       {
00249       fclose(hfile);
00250       }
00251     exit(1);
00252     }
00253 
00254   if (options.IsConcrete)
00255     {
00256     cp = options.InputFileName;
00257     i = strlen(cp);
00258     classname = (char *)malloc(i+1);
00259     while (i > 0 &&
00260            cp[i-1] != '/' && cp[i-1] != '\\' && cp[i-1] != ':') { i--; }
00261     strcpy(classname, &cp[i]);
00262     i = 0;
00263     while (classname[i] != '\0' && classname[i] != '.') { i++; }
00264     classname[i] = '\0';
00265 
00266     vtkParse_SetClassProperty(classname, "concrete");
00267     }
00268 
00269   vtkParse_SetIgnoreBTX(0);
00270   if (options.HierarchyFileName)
00271     {
00272     vtkParse_SetIgnoreBTX(1);
00273     }
00274 
00275   data = vtkParse_ParseFile(options.InputFileName, ifile, stderr);
00276 
00277   if (!data)
00278     {
00279     fclose(ifile);
00280     fclose(ofile);
00281     if (hfile)
00282       {
00283       fclose(hfile);
00284       }
00285     exit(1);
00286     }
00287 
00288   if (hfile)
00289     {
00290     vtkParse_ReadHints(data, hfile, stderr);
00291     }
00292 
00293   if (options.IsConcrete && data->MainClass)
00294     {
00295     data->MainClass->IsAbstract = 0;
00296     }
00297   else if (options.IsAbstract && data->MainClass)
00298     {
00299     data->MainClass->IsAbstract = 1;
00300     }
00301 
00302   vtkParseOutput(ofile, data);
00303 
00304   fclose(ofile);
00305 
00306   vtkParse_Free(data);
00307 
00308   return 0;
00309 }