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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkParseHierarchy.c
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright (c) 2010 David Gobbi.
Contributed to the VisualizationToolkit by the author in June 2010
under the terms of the Visualization Toolkit 2008 copyright.
-------------------------------------------------------------------------*/
#include "vtkParseHierarchy.h"
#include "vtkParseExtras.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
static size_t skip_space(const char *text)
{
size_t i = 0;
while (isspace(text[i]) && text[i] != '\n') { i++; }
return i;
}
static size_t skip_expression(const char *text, const char *delims)
{
char newdelims[2];
size_t i = 0;
size_t j;
int use_angle = 0;
char c;
for (j = 0; delims[j] != '\0'; j++)
{
if (delims[j] == '>')
{
use_angle = 1;
}
}
while (text[i] != '\0')
{
c = text[i];
j = 0;
while (c != delims[j] && delims[j] != '\0') { j++; }
if (delims[j] != '\0' || c == '\0') { break; }
if (c == '\"' || c == '\'')
{
char d = c;
i++;
while (text[i] != d && text[i] != '\0')
{
if (text[i] == '\\' && text[i+1] != '\0') { i++; }
i++;
}
c = text[i];
if (c == '\0') { break; }
}
i++;
if (c == '(' || c == '[' || c == '{' || (use_angle && c == '<'))
{
if (c == '(') { newdelims[0] = ')'; }
if (c == '[') { newdelims[0] = ']'; }
if (c == '{') { newdelims[0] = '}'; }
if (c == '<') { newdelims[0] = '>'; }
newdelims[1] = '\0';
i += skip_expression(&text[i], newdelims);
if (text[i] == newdelims[0]) { i++; } else { break; }
}
}
return i;
}
/* helper: comparison of entries */
static int compare_hierarchy_entries(const void *vp1, const void *vp2)
{
const HierarchyEntry *entry1 = (const HierarchyEntry *)vp1;
const HierarchyEntry *entry2 = (const HierarchyEntry *)vp2;
return strcmp(entry1->Name, entry2->Name);
}
/* helper: sort the entries to facilitate searching */
static void sort_hierarchy_entries(HierarchyInfo *info)
{
qsort(info->Entries, info->NumberOfEntries, sizeof(HierarchyEntry),
&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)
{
HierarchyEntry key;
HierarchyEntry *entry;
size_t i, n;
char name[32];
char *cp;
/* use classname as-is for the search if possible */
cp = (char *)classname;
/* get portion of name before final template parameters */
n = vtkParse_UnscopedNameLength(classname);
i = 0;
while (classname[i+n] == ':' && classname[i+n+1] == ':')
{
i += n + 2;
n = vtkParse_UnscopedNameLength(&classname[i]);
}
i += vtkParse_IdentifierLength(&classname[i]);
/* create a new (shorter) search string if necessary */
if (classname[i] != '\0')
{
/* use stack space if possible */
cp = name;
/* otherwise, use malloc */
if (i > 31)
{
cp = (char *)malloc(i+1);
}
strncpy(cp, classname, i);
cp[i] = '\0';
}
key.Name = cp;
entry = (HierarchyEntry *)bsearch(&key, info->Entries,
info->NumberOfEntries, sizeof(HierarchyEntry),
&compare_hierarchy_entries);
if (cp != classname && cp != name)
{
free(cp);
}
return entry;
}
/* 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;
FILE *fp;
char *line;
char *cp;
const char *ccp;
size_t maxlen = 15;
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 0;
}
line = (char *)malloc(maxlen);
while (fgets(line, (int)maxlen, fp))
{
n = strlen(line);
/* if buffer not long enough, increase it */
while (n == maxlen-1 && line[n-1] != '\n' && !feof(fp))
{
maxlen *= 2;
line = (char *)realloc(line, maxlen);
if (!fgets(&line[n], (int)(maxlen-n), fp)) { break; }
n += strlen(&line[n]);
}
while (n > 0 && isspace(line[n-1]))
{
n--;
}
line[n] = '\0';
if (line[0] == '\0')
{
continue;
}
if (info->NumberOfEntries == info->MaxNumberOfEntries)
{
info->MaxNumberOfEntries *= 2;
info->Entries = (HierarchyEntry *)realloc(
info->Entries, sizeof(HierarchyEntry)*info->MaxNumberOfEntries);
}
entry = &info->Entries[info->NumberOfEntries++];
entry->Name = NULL;
entry->HeaderFile = NULL;
entry->Module = NULL;
entry->NumberOfTemplateParameters = 0;
entry->TemplateParameters = NULL;
entry->TemplateDefaults = NULL;
entry->NumberOfProperties = 0;
entry->Properties = NULL;
entry->NumberOfSuperClasses = 0;
entry->SuperClasses = NULL;
entry->SuperClassIndex = NULL;
entry->Typedef = NULL;
entry->IsTypedef = 0;
entry->IsEnum = 0;
i = skip_space(line);
n = vtkParse_NameLength(&line[i]);
for (m = 0; m < n; m++)
{
if (line[i+m] == '<') { break; }
}
entry->Name = vtkParse_CacheString(info->Strings, &line[i], m);
i += m;
if (line[i] == '<')
{
i++;
i += skip_space(&line[i]);
for (j = 0; line[i] != '>' && line[i] != '\0'; j++)
{
if (j == 0)
{
entry->TemplateParameters = (const char **)malloc(sizeof(char *));
entry->TemplateDefaults = (const char **)malloc(sizeof(char *));
}
else
{
entry->TemplateParameters = (const char **)realloc(
(char **)entry->TemplateParameters, (j+1)*sizeof(char *));
entry->TemplateDefaults = (const char **)realloc(
(char **)entry->TemplateDefaults, (j+1)*sizeof(char *));
}
entry->NumberOfTemplateParameters++;
entry->TemplateDefaults[j] = NULL;
m = skip_expression(&line[i], delims);
while (m > 0 && (line[i+m-1] == ' ' || line[i+m-1] == '\t'))
{
--m;
}
entry->TemplateParameters[j] =
vtkParse_CacheString(info->Strings, &line[i], m);
i += m;
i += skip_space(&line[i]);
if (line[i] == '=')
{
i++;
i += skip_space(&line[i]);
m = skip_expression(&line[i], delims);
while (m > 0 && (line[i+m-1] == ' ' || line[i+m-1] == '\t'))
{
--m;
}
entry->TemplateDefaults[j] =
vtkParse_CacheString(info->Strings, &line[i], m);
i += m;
i += skip_space(&line[i]);
}
if (line[i] == ',')
{
i++;
i += skip_space(&line[i]);
}
}
if (line[i] == '>')
{
i++;
i += skip_space(&line[i]);
}
if (line[i] == ':' && line[i+1] == ':')
{
i += 2;
m = vtkParse_NameLength(&line[i]);
n = strlen(entry->Name);
cp = vtkParse_NewString(info->Strings, n+m+2);
strcpy(cp, entry->Name);
strcpy(&cp[n], "::");
strncpy(&cp[n+2], &line[i], m);
i += m;
cp[n+m+2] = '\0';
entry->Name = cp;
}
}
i += skip_space(&line[i]);
/* classes (and possibly enums) */
if (line[i] == ':')
{
i++;
i += skip_space(&line[i]);
n = vtkParse_NameLength(&line[i]);
/* check for enum indicators */
if ((n == 3 && strncmp(&line[i], "int", n) == 0) ||
(n == 4 && strncmp(&line[i], "enum", n) == 0))
{
entry->IsEnum = 1;
i += n;
i += skip_space(&line[i]);
}
/* else check for superclasses */
else for (j = 0; ; j++)
{
if (j == 0)
{
entry->SuperClasses = (const char **)malloc(sizeof(char *));
entry->SuperClassIndex = (int *)malloc(sizeof(int));
}
else
{
entry->SuperClasses = (const char **)realloc(
(char **)entry->SuperClasses, (j+1)*sizeof(char *));
entry->SuperClassIndex = (int *)realloc(
entry->SuperClassIndex, (j+1)*sizeof(int));
}
entry->NumberOfSuperClasses++;
i += skip_space(&line[i]);
n = vtkParse_NameLength(&line[i]);
entry->SuperClasses[j] =
vtkParse_CacheString(info->Strings, &line[i], n);
entry->SuperClassIndex[j] = -1;
i += n;
i += skip_space(&line[i]);
if (line[i] != ',')
{
break;
}
i++;
}
}
/* read typedefs */
else if (line[i] == '=')
{
i++;
i += skip_space(&line[i]);
entry->IsTypedef = 1;
entry->Typedef = (ValueInfo *)malloc(sizeof(ValueInfo));
vtkParse_InitValue(entry->Typedef);
/* type is a reference (does this ever occur?) */
if (line[i] == '&')
{
i++;
i += skip_space(&line[i]);
entry->Typedef->Type |= VTK_PARSE_REF;
}
/* type has array dimensions */
if (line[i] == '[')
{
entry->Typedef->Count = 1;
}
while (line[i] == '[')
{
i++;
n = 0;
while (line[i+n] != ']' && line[i+n] != '\n' && line[i+n] != '\0')
{
n++;
}
ccp = vtkParse_CacheString(info->Strings, &line[i], n);
vtkParse_AddStringToArray(&entry->Typedef->Dimensions,
&entry->Typedef->NumberOfDimensions, ccp);
if (ccp[0] >= '0' && ccp[0] <= '9')
{
entry->Typedef->Count *= (int)strtol(ccp, NULL, 0);
}
else
{
entry->Typedef->Count = 0;
}
i += n;
if (line[i] == ']')
{
i++;
}
}
i += skip_space(&line[i]);
/* look for pointers (and const pointers) */
bits = 0;
while (line[i] == '*' || strncmp(&line[i], "const*", 6) == 0)
{
bits = (bits << 2);
if (line[i] == '*')
{
bits = (bits | VTK_PARSE_POINTER);
}
else
{
bits = (bits | VTK_PARSE_CONST_POINTER);
i += 5;
}
bits = (bits & VTK_PARSE_POINTER_MASK);
i++;
i += skip_space(&line[i]);
}
/* need to reverse to get correct pointer order */
pointers = 0;
while (bits)
{
pointers = (pointers << 2);
pointers = (pointers | (bits & VTK_PARSE_POINTER_LOWMASK));
bits = ((bits >> 2) & VTK_PARSE_POINTER_MASK);
}
/* add pointer indirection to correspond to first array dimension */
if (entry->Typedef->NumberOfDimensions > 1)
{
pointers = ((pointers << 2) | VTK_PARSE_ARRAY);
}
else if (entry->Typedef->NumberOfDimensions == 1)
{
pointers = ((pointers << 2) | VTK_PARSE_POINTER);
}
/* include the pointers in the type */
entry->Typedef->Type |= (pointers & VTK_PARSE_POINTER_MASK);
/* read the base type (and const) */
bits = 0;
i += vtkParse_BasicTypeFromString(&line[i], &bits, &ccp, &n);
entry->Typedef->Class = vtkParse_CacheString(info->Strings, ccp, n);
entry->Typedef->Type |= bits;
}
/* get the header file */
if (line[i] == ';')
{
i++;
i += skip_space(&line[i]);
n = 0;
while(line[i+n] != '\0' && line[i+n] != ';' &&
!isspace(line[i+n])) { n++; };
entry->HeaderFile = vtkParse_CacheString(info->Strings, &line[i], n);
i += n;
i += skip_space(&line[i]);
/* get the module */
if (line[i] == ';')
{
i++;
i += skip_space(&line[i]);
n = 0;
while(line[i+n] != '\0' && line[i+n] != ';' &&
!isspace(line[i+n])) { n++; };
entry->Module = vtkParse_CacheString(info->Strings, &line[i], n);
i += n;
i += skip_space(&line[i]);
}
/* get all flags */
while (line[i] == ';')
{
i++;
i += skip_space(&line[i]);
if (entry->NumberOfProperties == 0)
{
entry->Properties = (const char **)malloc(sizeof(char **));
}
else
{
entry->Properties = (const char **)realloc(
(char **)entry->Properties,
(entry->NumberOfProperties+1)*sizeof(char **));
}
n = 0;
while (line[i+n] != '\0' && line[i+n] != '\n' && line[i+n] != ';')
{ n++; }
if (n && skip_space(&line[i]) != n)
{
entry->Properties[entry->NumberOfProperties++] =
vtkParse_CacheString(info->Strings, &line[i], n);
}
i += n;
}
}
}
free(line);
if (!feof(fp))
{
success = 0;
}
fclose(fp);
return success;
}
/* free a HierarchyInfo struct */
void vtkParseHierarchy_Free(HierarchyInfo *info)
{
HierarchyEntry *entry;
int i;
for (i = 0; i < info->NumberOfEntries; i++)
{
entry = &info->Entries[i];
if (entry->NumberOfTemplateParameters)
{
free((char **)entry->TemplateParameters);
free((char **)entry->TemplateDefaults);
}
if (entry->NumberOfSuperClasses)
{
free((char **)entry->SuperClasses);
free(entry->SuperClassIndex);
}
if (entry->NumberOfProperties)
{
free((char **)entry->Properties);
}
}
free(info->Entries);
free(info);
}
/* Check whether class is derived from baseclass. You must supply
* the entry for the class (returned by FindEntry) as well as the
* classname. If the class is templated, the classname can include
* template args in angle brackets. If you provide a pointer for
* baseclass_with_args, then it will be used to return the name of
* name of the baseclass with template args in angle brackets. */
int vtkParseHierarchy_IsTypeOfTemplated(
const HierarchyInfo *info,
const HierarchyEntry *entry, const char *classname,
const char *baseclass, const char **baseclass_with_args)
{
HierarchyEntry *tmph;
const char *name = NULL;
const char *supername;
char *tmp;
int templated;
int baseclass_is_template_parameter;
int supername_needs_free = 0;
int classname_needs_free = 0;
int i, j, k;
int nargs;
const char **args;
size_t m;
int iterating = 1;
int rval = 0;
while (iterating)
{
iterating = 0;
templated = 0;
baseclass_is_template_parameter = 0;
nargs = 0;
args = NULL;
/* if classname is the same as baseclass, done! */
if (strcmp(entry->Name, baseclass) == 0)
{
if (baseclass_with_args)
{
if (!classname_needs_free)
{
tmp = (char *)malloc(strlen(classname) + 1);
strcpy(tmp, classname);
classname = tmp;
}
*baseclass_with_args = classname;
classname_needs_free = 0;
}
rval = 1;
break;
}
else if (entry->NumberOfSuperClasses == 0)
{
rval = 0;
break;
}
/* if class is templated */
if (entry->NumberOfTemplateParameters)
{
/* check for template args for classname */
m = strlen(entry->Name);
if (classname[m] == '<')
{
templated = 1;
nargs = entry->NumberOfTemplateParameters;
vtkParse_DecomposeTemplatedType(classname, &name, nargs, &args,
entry->TemplateDefaults);
}
}
/* check all baseclasses */
for (j = 0; j < entry->NumberOfSuperClasses && rval == 0; j++)
{
supername = entry->SuperClasses[j];
if (templated)
{
for (k = 0; k < entry->NumberOfTemplateParameters; k++)
{
/* check if the baseclass itself is a template parameter */
m = strlen(entry->TemplateParameters[k]);
if (strncmp(entry->TemplateParameters[k], supername, m) == 0 &&
!isalnum(supername[m]) && supername[m] != '_')
{
baseclass_is_template_parameter = 1;
break;
}
}
/* use the class template args to find baseclass template args */
supername = vtkParse_StringReplace(
supername, entry->NumberOfTemplateParameters, entry->TemplateParameters, args);
if (supername != entry->SuperClasses[j])
{
supername_needs_free = 1;
}
}
/* check the cached index for the baseclass entry */
i = entry->SuperClassIndex[j];
if (i == -1)
{
/* index was not set yet, so search for the entry */
tmph = vtkParseHierarchy_FindEntry(info, supername);
while (tmph && tmph->IsTypedef)
{
if (tmph->Typedef->Class)
{
tmph = vtkParseHierarchy_FindEntry(info, tmph->Typedef->Class);
continue;
}
break;
}
if (tmph)
{
i = (int)(tmph - info->Entries);
}
else
{
/* entry not found, don't try again */
/* i = -2; messes things up for templates */
/* fprintf(stderr, "not found \"%s\"\n", entry->SuperClasses[j]); */
}
/* if baseclass is a template parameter, its entry cannot be cached */
if (!baseclass_is_template_parameter)
{
/* cache the position of the baseclass */
((HierarchyEntry *)entry)->SuperClassIndex[j] = i;
}
}
/* if entry was found, continue down the chain */
if (i >= 0)
{
if (classname_needs_free)
{
free((char *)classname);
}
classname = supername;
classname_needs_free = supername_needs_free;
supername_needs_free = 0;
/* use the iteration loop instead of recursion */
if (j+1 >= entry->NumberOfSuperClasses)
{
entry = &info->Entries[i];
iterating = 1;
}
/* recurse for multiple inheritance */
else
{
rval = vtkParseHierarchy_IsTypeOfTemplated(
info, &info->Entries[i], classname, baseclass,
baseclass_with_args);
}
}
if (supername_needs_free)
{
free((char *)supername);
supername_needs_free = 0;
}
} /* end of loop over superclasses */
if (templated)
{
vtkParse_FreeTemplateDecomposition(name, nargs, args);
}
} /* end of "while (iterating)" */
if (classname_needs_free)
{
free((char *)classname);
}
if (baseclass_with_args && !rval)
{
*baseclass_with_args = NULL;
}
return rval;
}
int vtkParseHierarchy_IsTypeOf(
const HierarchyInfo *info, const HierarchyEntry *entry,
const char *baseclass)
{
return vtkParseHierarchy_IsTypeOfTemplated(
info, entry, entry->Name, baseclass, NULL);
}
/* Free args returned by IsTypeOfTemplated */
void vtkParseHierarchy_FreeTemplateArgs(int n, const char *args[])
{
int i;
for (i = 0; i < n; i++)
{
free((char *)args[i]);
}
free((char **)args);
}
/* Given a classname with template parameters, get the superclass name
* with corresponding template parameters. Returns null if 'i' is out
* of range, i.e. greater than or equal to the number of superclasses.
* The returned classname must be freed with "free()". */
const char *vtkParseHierarchy_TemplatedSuperClass(
const HierarchyEntry *entry, const char *classname, int i)
{
const char *supername = NULL;
const char *name;
const char **args;
char *cp;
size_t j;
if (i < entry->NumberOfSuperClasses)
{
supername = entry->SuperClasses[i];
j = vtkParse_IdentifierLength(classname);
if (classname[j] == '<')
{
vtkParse_DecomposeTemplatedType(classname, &name,
entry->NumberOfTemplateParameters, &args, entry->TemplateDefaults);
supername = vtkParse_StringReplace(entry->SuperClasses[i],
entry->NumberOfTemplateParameters, entry->TemplateParameters, args);
vtkParse_FreeTemplateDecomposition(
name, entry->NumberOfTemplateParameters, args);
}
if (supername == entry->SuperClasses[i])
{
cp = (char *)malloc(strlen(supername) + 1);
strcpy(cp, supername);
supername = cp;
}
}
return supername;
}
/* get the specified property, or return NULL */
const char *vtkParseHierarchy_GetProperty(
const HierarchyEntry *entry, const char *property)
{
int i;
size_t k;
if (entry)
{
for (i = 0; i < entry->NumberOfProperties; i++)
{
/* skip the property name, everything after is the property */
k = vtkParse_NameLength(entry->Properties[i]);
if (k == strlen(property) &&
strncmp(entry->Properties[i], property, k) == 0)
{
if (entry->Properties[i][k] == ' ' ||
entry->Properties[i][k] == '=') { k++; }
return &entry->Properties[i][k];
}
}
}
return NULL;
}
/* Expand all unrecognized types in a ValueInfo struct by
* using the typedefs in the HierarchyInfo struct. */
int vtkParseHierarchy_ExpandTypedefsInValue(
const HierarchyInfo *info, ValueInfo *val, StringCache *cache,
const char *scope)
{
char text[128];
char *cp;
const char *newclass;
size_t n, m;
int i;
HierarchyEntry *entry;
int scope_needs_free = 0;
int result = 1;
while (((val->Type & VTK_PARSE_BASE_TYPE) == VTK_PARSE_OBJECT ||
(val->Type & VTK_PARSE_BASE_TYPE) == VTK_PARSE_UNKNOWN) &&
val->Class != 0)
{
entry = 0;
/* search for the type in the provided scope */
while (entry == 0 && scope != 0)
{
cp = text;
n = strlen(scope);
m = strlen(val->Class);
/* only malloc if more than 128 chars needed */
if (n + m + 2 >= 128)
{
cp = (char *)malloc(n+m+3);
}
/* scope the name */
strncpy(cp, scope, n);
cp[n++] = ':';
cp[n++] = ':';
strncpy(&cp[n], val->Class, m);
cp[n+m] = '\0';
entry = vtkParseHierarchy_FindEntry(info, cp);
if (cp != text) { free(cp); }
/* if not found, try inherited scopes */
if (entry == 0)
{
entry = vtkParseHierarchy_FindEntry(info, scope);
scope = 0;
scope_needs_free = 0;
if (entry && entry->NumberOfSuperClasses)
{
for (i = 0; i+1 < entry->NumberOfSuperClasses; i++)
{
if (scope_needs_free) { free((char *)scope); }
scope = vtkParseHierarchy_ExpandTypedefsInName(
info, entry->SuperClasses[i], NULL);
scope_needs_free = (scope != entry->SuperClasses[i]);
/* recurse if more than one superclass */
if (vtkParseHierarchy_ExpandTypedefsInValue(
info, val, cache, scope))
{
if (scope_needs_free) { free((char *)scope); }
return 1;
}
}
if (scope_needs_free) { free((char *)scope); }
scope = vtkParseHierarchy_ExpandTypedefsInName(
info, entry->SuperClasses[i], NULL);
scope_needs_free = (scope != entry->SuperClasses[i]);
}
entry = 0;
}
}
/* if not found, try again with no scope */
if (entry == 0)
{
entry = vtkParseHierarchy_FindEntry(info, val->Class);
}
if (entry && entry->IsTypedef)
{
vtkParse_ExpandTypedef(val, entry->Typedef);
}
else if (entry)
{
newclass = vtkParseHierarchy_ExpandTypedefsInName(
info, val->Class, scope);
if (newclass != val->Class)
{
val->Class = vtkParse_CacheString(cache, newclass, strlen(newclass));
free((char *)newclass);
}
result = 1;
break;
}
else
{
result = 0;
break;
}
}
if (scope_needs_free) { free((char *)scope); }
return result;
}
/* Expand typedefs found in an expression stored as a string.
* The value of "text" will be returned if no expansion occurred,
* else a new string is returned that must be freed with "free()". */
const char *vtkParseHierarchy_ExpandTypedefsInName(
const HierarchyInfo *info, const char *name, const char *scope)
{
char text[128];
char *cp;
size_t n, m;
const char *newname = name;
HierarchyEntry *entry = NULL;
/* note: unlike ExpandTypedefsInValue, this does not yet recurse
* or look in superclass scopes */
/* doesn't yet handle names that are scoped or templated */
m = vtkParse_IdentifierLength(name);
if (name[m] != '\0')
{
return name;
}
if (scope)
{
cp = text;
n = strlen(scope);
m = strlen(name);
/* only malloc if more than 128 chars needed */
if (n + m + 2 >= 128)
{
cp = (char *)malloc(n+m+3);
}
/* scope the name */
strncpy(cp, scope, n);
cp[n++] = ':';
cp[n++] = ':';
strncpy(&cp[n], name, m);
cp[n+m] = '\0';
entry = vtkParseHierarchy_FindEntry(info, cp);
if (cp != text) { free(cp); }
}
if (!entry)
{
entry = vtkParseHierarchy_FindEntry(info, name);
}
newname = NULL;
if (entry && entry->IsTypedef && entry->Typedef->Class)
{
newname = entry->Typedef->Class;
}
if (newname)
{
cp = (char *)malloc(strlen(newname) + 1);
strcpy(cp, newname);
name = cp;
}
return name;
}
/* -------------------------------------------------------------------- */
const char *vtkParseHierarchy_QualifiedEnumName(
HierarchyInfo *hinfo, ClassInfo *data, StringCache *cache,
const char *name)
{
// check to see if this is an enum defined in the class
if (data)
{
int j;
for (j = 0; j < data->NumberOfEnums; j++)
{
EnumInfo *info = data->Enums[j];
if (name && info->Name && strcmp(name, info->Name) == 0)
{
char *scoped_name;
size_t scoped_len = strlen(data->Name) + strlen(info->Name) + 2;
scoped_name = vtkParse_NewString(cache, scoped_len);
sprintf(scoped_name, "%s::%s", data->Name, info->Name);
return scoped_name;
}
}
}
// check the hierarchy information for the enum type
if (hinfo)
{
HierarchyEntry *entry;
entry = vtkParseHierarchy_FindEntry(hinfo, name);
if (entry && entry->IsEnum)
{
return name;
}
}
return NULL;
}
|