Commit 31b7b0ef authored by Max Smolens's avatar Max Smolens

python: Update wrapping tool to support multiple hierarchy and hint files

To keep the argument parsing code simple, each hierarchy and hint file has to be
specified using a dedicated --types.

This change is backwards compatible. Deprecated members and functions are marked
with "XXX DEPRECATED".
Co-authored-by: Jean-Christophe Fillion-Robin's avatarJean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Co-authored-by: Max Smolens's avatarMax Smolens <max.smolens@kitware.com>
parent 1ebfa5bc
......@@ -102,6 +102,10 @@ static void sort_hierarchy_entries(HierarchyInfo *info)
&compare_hierarchy_entries);
}
/* forward declaration */
static int vtkParseHierarchy_ReadFileIntoInfo(
HierarchyInfo* info, const char *filename);
/* Find an entry with a binary search */
HierarchyEntry *vtkParseHierarchy_FindEntry(
const HierarchyInfo *info, const char *classname)
......@@ -153,13 +157,53 @@ HierarchyEntry *vtkParseHierarchy_FindEntry(
return entry;
}
/* read a hierarchy file into a HeirarchyInfo struct, or return NULL */
/* read a hierarchy file into a HeirarchyInfo struct, or return NULL
* XXX DEPRECATED; use vtkParseHierarchy_ReadFiles
*/
HierarchyInfo *vtkParseHierarchy_ReadFile(const char *filename)
{
char *fn = (char *)filename;
return vtkParseHierarchy_ReadFiles(1, &fn);
}
/* read hierarchy files into a HierarchyInfo struct, or return NULL */
HierarchyInfo *vtkParseHierarchy_ReadFiles(int n, char **filenames)
{
HierarchyInfo *info;
int currentFile = 0;
info = (HierarchyInfo *)malloc(sizeof(HierarchyInfo));
info->MaxNumberOfEntries = 500;
info->NumberOfEntries = 0;
info->Entries =
(HierarchyEntry *)malloc(info->MaxNumberOfEntries*sizeof(HierarchyEntry));
info->Strings = (StringCache *)malloc(sizeof(StringCache));
vtkParse_InitStringCache(info->Strings);
for (currentFile = 0; currentFile < n; currentFile++)
{
if (!vtkParseHierarchy_ReadFileIntoInfo(info, filenames[currentFile]))
{
vtkParseHierarchy_Free(info);
info = NULL;
break;
}
}
if (info)
{
sort_hierarchy_entries(info);
}
return info;
}
/* read hierarchy file into a HierarchyInfo struct, return 1 if success */
static int vtkParseHierarchy_ReadFileIntoInfo(
HierarchyInfo* info, const char *filename)
{
HierarchyEntry *entry;
int maxClasses = 500;
FILE *fp;
char *line;
char *cp;
......@@ -168,22 +212,17 @@ HierarchyInfo *vtkParseHierarchy_ReadFile(const char *filename)
size_t i, j, n, m;
unsigned int bits, pointers;
static const char *delims = ">,=";
int success = 1;
fp = fopen(filename, "r");
if (fp == NULL)
{
return NULL;
return 0;
}
line = (char *)malloc(maxlen);
info = (HierarchyInfo *)malloc(sizeof(HierarchyInfo));
info->NumberOfEntries = 0;
info->Entries = (HierarchyEntry *)malloc(maxClasses*sizeof(HierarchyEntry));
info->Strings = (StringCache *)malloc(sizeof(StringCache));
vtkParse_InitStringCache(info->Strings);
while (fgets(line, (int)maxlen, fp))
{
n = strlen(line);
......@@ -208,11 +247,11 @@ HierarchyInfo *vtkParseHierarchy_ReadFile(const char *filename)
continue;
}
if (info->NumberOfEntries == maxClasses)
if (info->NumberOfEntries == info->MaxNumberOfEntries)
{
maxClasses *= 2;
info->MaxNumberOfEntries *= 2;
info->Entries = (HierarchyEntry *)realloc(
info->Entries, sizeof(HierarchyEntry)*maxClasses*2);
info->Entries, sizeof(HierarchyEntry)*info->MaxNumberOfEntries);
}
entry = &info->Entries[info->NumberOfEntries++];
......@@ -523,17 +562,12 @@ HierarchyInfo *vtkParseHierarchy_ReadFile(const char *filename)
if (!feof(fp))
{
vtkParseHierarchy_Free(info);
info = NULL;
}
else
{
sort_hierarchy_entries(info);
success = 0;
}
fclose(fp);
return info;
return success;
}
/* free a HierarchyInfo struct */
......
......@@ -69,6 +69,7 @@ typedef struct _HierarchyEntry
*/
typedef struct _HierarchyInfo
{
int MaxNumberOfEntries;
int NumberOfEntries;
HierarchyEntry *Entries;
StringCache *Strings;
......@@ -79,10 +80,16 @@ extern "C" {
#endif
/**
* Read a hierarchy file into a HeirarchyInfo struct, or return NULL
*/
* Read a hierarchy file into a HeirarchyInfo struct, or return NULL
* XXX DEPRECATED; use vtkParseHierarchy_ReadFiles
*/
HierarchyInfo *vtkParseHierarchy_ReadFile(const char *filename);
/**
* Read hierarchy files into a HierarchyInfo struct, or return NULL
*/
HierarchyInfo *vtkParseHierarchy_ReadFiles(int n, char **filenames);
/**
* Free a HierarchyInfo struct
*/
......
......@@ -859,9 +859,10 @@ int main(int argc, char *argv[])
}
/* get the hierarchy info for accurate typing */
if (options->HierarchyFileName)
if (options->HierarchyFileNames)
{
hierarchyInfo = vtkParseHierarchy_ReadFile(options->HierarchyFileName);
hierarchyInfo = vtkParseHierarchy_ReadFiles(
options->NumberOfHierarchyFileNames, options->HierarchyFileNames);
if (hierarchyInfo)
{
/* resolve using declarations within the header files */
......
......@@ -682,9 +682,10 @@ void vtkParseOutput(FILE *fp, FileInfo *file_info)
options = vtkParse_GetCommandLineOptions();
/* get the hierarchy info for accurate typing */
if (options->HierarchyFileName)
if (options->HierarchyFileNames)
{
hierarchyInfo = vtkParseHierarchy_ReadFile(options->HierarchyFileName);
hierarchyInfo = vtkParseHierarchy_ReadFiles(
options->NumberOfHierarchyFileNames, options->HierarchyFileNames);
}
fprintf(fp,"// java wrapper for %s object\n//\n",data->Name);
......
......@@ -267,7 +267,10 @@ static int parse_check_options(int argc, char *argv[], int multi)
options.InputFileName = NULL;
options.OutputFileName = NULL;
options.HierarchyFileName = 0;
options.HintFileName = 0;
options.NumberOfHierarchyFileNames = 0;
options.HierarchyFileNames = NULL;
options.NumberOfHintFileNames = 0;
options.HintFileNames = NULL;
for (i = 1; i < argc; i++)
{
......@@ -336,7 +339,16 @@ static int parse_check_options(int argc, char *argv[], int multi)
{
return -1;
}
options.HintFileName = argv[i];
if (options.NumberOfHintFileNames == 0)
{
options.HintFileNames = (char **)malloc(sizeof(char *));
}
else if ((options.NumberOfHintFileNames & (options.NumberOfHintFileNames - 1)) == 0)
{
options.HintFileNames = (char **)realloc(
options.HintFileNames, 2 * options.NumberOfHintFileNames*sizeof(char *));
}
options.HintFileNames[options.NumberOfHintFileNames++] = argv[i];
}
else if (!multi && strcmp(argv[i], "--types") == 0)
{
......@@ -345,7 +357,17 @@ static int parse_check_options(int argc, char *argv[], int multi)
{
return -1;
}
options.HierarchyFileName = argv[i];
if (options.NumberOfHierarchyFileNames == 0)
{
options.HierarchyFileNames = (char **)malloc(sizeof(char *));
options.HierarchyFileName = argv[i]; // legacy
}
else if ((options.NumberOfHierarchyFileNames & (options.NumberOfHierarchyFileNames - 1)) == 0)
{
options.HierarchyFileNames = (char **)realloc(
options.HierarchyFileNames, 2*options.NumberOfHierarchyFileNames*sizeof(char *));
}
options.HierarchyFileNames[options.NumberOfHierarchyFileNames++] = argv[i];
}
else if (strcmp(argv[i], "--vtkobject") == 0 ||
strcmp(argv[i], "--special") == 0 ||
......@@ -373,6 +395,9 @@ FileInfo *vtkParse_Main(int argc, char *argv[])
int expected_files;
FILE *ifile;
FILE *hfile = 0;
int nhfiles;
int ihfiles;
const char *hfilename;
FileInfo *data;
StringCache strings;
int argn;
......@@ -423,32 +448,17 @@ FileInfo *vtkParse_Main(int argc, char *argv[])
/* free the expanded args */
free(args);
/* open the hint file, if given on the command line */
if (options.HintFileName && options.HintFileName[0] != '\0')
{
if (!(hfile = fopen(options.HintFileName, "r")))
{
fprintf(stderr, "Error opening hint file %s\n", options.HintFileName);
fclose(ifile);
exit(1);
}
}
/* make sure than an output file was given on the command line */
if (options.OutputFileName == NULL)
{
fprintf(stderr, "No output file was specified\n");
fclose(ifile);
if (hfile)
{
fclose(hfile);
}
exit(1);
}
/* if a hierarchy is was given, then BTX/ETX can be ignored */
vtkParse_SetIgnoreBTX(0);
if (options.HierarchyFileName)
if (options.HierarchyFileNames)
{
vtkParse_SetIgnoreBTX(1);
}
......@@ -461,10 +471,24 @@ FileInfo *vtkParse_Main(int argc, char *argv[])
exit(1);
}
/* fill in some blanks by using the hints file */
if (hfile)
/* open and parse each hint file, if given on the command line */
nhfiles = options.NumberOfHintFileNames;
for (ihfiles = 0; ihfiles < nhfiles; ihfiles++)
{
vtkParse_ReadHints(data, hfile, stderr);
hfilename = options.HintFileNames[ihfiles];
if (hfilename && hfilename[0] != '\0')
{
if (!(hfile = fopen(hfilename, "r")))
{
fprintf(stderr, "Error opening hint file %s\n", hfilename);
fclose(ifile);
vtkParse_FreeFile(data);
exit(1);
}
/* fill in some blanks by using the hints file */
vtkParse_ReadHints(data, hfile, stderr);
}
}
if (data->MainClass)
......
......@@ -51,12 +51,15 @@
*/
typedef struct _OptionInfo
{
int NumberOfFiles; /* the total number of file arguments */
char **Files; /* all of the file arguments */
char *InputFileName; /* the first file argument */
char *OutputFileName; /* the second file, or the "-o" file */
char *HintFileName; /* the file preceded by "--hints" */
char *HierarchyFileName; /* the file preceded by "--types" */
int NumberOfFiles; /* the total number of file arguments */
char **Files; /* all of the file arguments */
char *InputFileName; /* the first file argument */
char *OutputFileName; /* the second file, or the "-o" file */
int NumberOfHintFileNames; /* the total number of hints arguments */
char **HintFileNames; /* all of the hints arguments */
char *HierarchyFileName; /* the file preceded by "--types" XXX DEPRECATED */
int NumberOfHierarchyFileNames; /* the total number of types argument */
char **HierarchyFileNames; /* the file preceded by "--types" */
} OptionInfo;
#ifdef __cplusplus
......
......@@ -518,7 +518,8 @@ int vtkParseMerge_Merge(
* superclasses to "merge" */
void vtkParseMerge_MergeHelper(
FileInfo *finfo, const NamespaceInfo *data, const HierarchyInfo *hinfo,
const char *classname, FILE *hintfile, MergeInfo *info, ClassInfo *merge)
const char *classname, int nhintfiles, char **hintfiles, MergeInfo *info,
ClassInfo *merge)
{
FILE *fp = NULL;
ClassInfo *cinfo = NULL;
......@@ -532,6 +533,9 @@ void vtkParseMerge_MergeHelper(
const char *filename;
int i, j, n, m;
int recurse;
FILE *hintfile = NULL;
int ihintfiles = 0;
const char *hintfilename = NULL;
/* Note: this method does not deal with scoping yet.
* "classname" might be a scoped name, in which case the
......@@ -597,7 +601,6 @@ void vtkParseMerge_MergeHelper(
header = entry->HeaderFile;
if (!header)
{
if (hintfile) { fclose(hintfile); }
fprintf(stderr, "Null header file for class %s!\n", classname);
exit(1);
}
......@@ -605,7 +608,6 @@ void vtkParseMerge_MergeHelper(
filename = vtkParse_FindIncludeFile(header);
if (!filename)
{
if (hintfile) { fclose(hintfile); }
fprintf(stderr, "Couldn't locate header file %s\n", header);
exit(1);
}
......@@ -613,7 +615,6 @@ void vtkParseMerge_MergeHelper(
fp = fopen(filename, "r");
if (!fp)
{
if (hintfile) { fclose(hintfile); }
fprintf(stderr, "Couldn't open header file %s\n", header);
exit(1);
}
......@@ -623,14 +624,27 @@ void vtkParseMerge_MergeHelper(
if (!finfo)
{
if (hintfile) { fclose(hintfile); }
exit(1);
}
if (hintfile)
if (nhintfiles > 0 && hintfiles)
{
rewind(hintfile);
vtkParse_ReadHints(finfo, hintfile, stderr);
for (ihintfiles = 0; ihintfiles < nhintfiles; ihintfiles++)
{
hintfilename = hintfiles[ihintfiles];
if (hintfilename && hintfilename[0] != '\0')
{
if (!(hintfile = fopen(hintfilename, "r")))
{
fprintf(stderr, "Error opening hint file %s\n", hintfilename);
vtkParse_FreeFile(finfo);
exit(1);
}
vtkParse_ReadHints(finfo, hintfile, stderr);
fclose(hintfile);
}
}
}
data = finfo->Contents;
......@@ -709,7 +723,7 @@ void vtkParseMerge_MergeHelper(
for (i = 0; i < n; i++)
{
vtkParseMerge_MergeHelper(finfo, data, hinfo, cinfo->SuperClasses[i],
hintfile, info, merge);
nhintfiles, hintfiles, info, merge);
}
}
}
......@@ -725,7 +739,6 @@ void vtkParseMerge_MergeHelper(
MergeInfo *vtkParseMerge_MergeSuperClasses(
FileInfo *finfo, NamespaceInfo *data, ClassInfo *classInfo)
{
FILE *hintfile = NULL;
HierarchyInfo *hinfo = NULL;
MergeInfo *info = NULL;
OptionInfo *oinfo = NULL;
......@@ -733,14 +746,10 @@ MergeInfo *vtkParseMerge_MergeSuperClasses(
oinfo = vtkParse_GetCommandLineOptions();
if (oinfo->HierarchyFileName)
if (oinfo->HierarchyFileNames)
{
hinfo = vtkParseHierarchy_ReadFile(oinfo->HierarchyFileName);
if (oinfo->HintFileName)
{
hintfile = fopen(oinfo->HintFileName, "r");
}
hinfo = vtkParseHierarchy_ReadFiles(
oinfo->NumberOfHierarchyFileNames, oinfo->HierarchyFileNames);
info = vtkParseMerge_CreateMergeInfo(classInfo);
......@@ -749,12 +758,9 @@ MergeInfo *vtkParseMerge_MergeSuperClasses(
{
vtkParseMerge_MergeHelper(finfo, data, hinfo,
classInfo->SuperClasses[i],
hintfile, info, classInfo);
}
if (hintfile)
{
fclose(hintfile);
oinfo->NumberOfHintFileNames,
oinfo->HintFileNames,
info, classInfo);
}
}
......
......@@ -72,7 +72,8 @@ void vtkParseMerge_FreeMergeInfo(MergeInfo *info);
*/
void vtkParseMerge_MergeHelper(
FileInfo *finfo, const NamespaceInfo *data, const HierarchyInfo *hinfo,
const char *classname, FILE *hintfile, MergeInfo *info, ClassInfo *merge);
const char *classname, int nhintfiles, char **hintfiles, MergeInfo *info,
ClassInfo *merge);
#ifdef __cplusplus
} /* extern "C" */
......
......@@ -871,7 +871,7 @@ void vtkWrap_ApplyUsingDeclarations(
{
vtkParseMerge_MergeHelper(
finfo, finfo->Contents, hinfo, data->SuperClasses[i],
NULL, NULL, data);
0, NULL, NULL, data);
}
}
}
......
......@@ -1274,9 +1274,10 @@ int main(int argc, char *argv[])
}
/* get the hierarchy info for accurate typing */
if (options->HierarchyFileName)
if (options->HierarchyFileNames)
{
hierarchyInfo = vtkParseHierarchy_ReadFile(options->HierarchyFileName);
hierarchyInfo = vtkParseHierarchy_ReadFiles(
options->NumberOfHierarchyFileNames, options->HierarchyFileNames);
if (hierarchyInfo)
{
/* resolve using declarations within the header files */
......
......@@ -315,9 +315,10 @@ int main(int argc, char *argv[])
}
/* get the hierarchy info for accurate typing */
if (options->HierarchyFileName)
if (options->HierarchyFileNames)
{
hinfo = vtkParseHierarchy_ReadFile(options->HierarchyFileName);
hinfo = vtkParseHierarchy_ReadFiles(
options->NumberOfHierarchyFileNames, options->HierarchyFileNames);
}
/* get the filename without the extension */
......
......@@ -169,7 +169,8 @@ int main(int argc,char *argv[])
}
/* extra functions, types, etc. for the CommonCore module */
if (strcmp(libName, "vtkCommonCorePython") == 0)
if (strcmp(libName, "vtkCommonCorePython") == 0 ||
strcmp(libName, "vtkCommonKitPython") == 0)
{
files[numFiles] = strdup("PyVTKExtras");
numFiles++;
......
......@@ -1119,9 +1119,10 @@ int main(int argc, char *argv[])
}
/* get the hierarchy info for accurate typing */
if (options->HierarchyFileName)
if (options->HierarchyFileNames)
{
hierarchyInfo = vtkParseHierarchy_ReadFile(options->HierarchyFileName);
hierarchyInfo = vtkParseHierarchy_ReadFiles(
options->NumberOfHierarchyFileNames, options->HierarchyFileNames);
if (hierarchyInfo)
{
/* resolve using declarations within the header files */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment