dcparse.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file dcparse.cxx
  10. * @author drose
  11. * @date 2000-10-05
  12. */
  13. #include "dcbase.h"
  14. #include "dcFile.h"
  15. #include "dcClass.h"
  16. #include "dcTypedef.h"
  17. #include "memoryUsage.h"
  18. #include "indent.h"
  19. #include "panda_getopt.h"
  20. using std::cerr;
  21. using std::cout;
  22. void
  23. usage() {
  24. cerr <<
  25. "\n"
  26. "Usage:\n\n"
  27. "dcparse [options] [file1 file2 ...]\n"
  28. "dcparse -h\n\n";
  29. }
  30. void
  31. help() {
  32. usage();
  33. cerr <<
  34. "This program reads one or more DC files, which are used to describe the\n"
  35. "communication channels in the distributed class system. By default,\n"
  36. "the file(s) are read and concatenated, and a single hash code is printed\n"
  37. "corresponding to the file's contents.\n\n"
  38. "Options:\n\n"
  39. " -v Writes a complete parseable version of the file to standard\n"
  40. " output instead of printing a hash code.\n\n"
  41. " -b Writes a brief parseable version of the file instead of a full\n"
  42. " version. This is semantically the same as the output produced\n"
  43. " the above -v option--reading it would produce exactly the same\n"
  44. " results--but it is designed to be slightly obfuscated. The\n"
  45. " comments and parameter names are not included.\n\n"
  46. " -c Write a list of class names, showing the inheritance hierarchy.\n"
  47. " Some class names will be listed twice in the presence of multiple\n"
  48. " inheritance.\n\n"
  49. " -f Write a complete list of field names available for each class,\n"
  50. " including all inherited fields.\n\n";
  51. }
  52. void
  53. write_class_hierarchy(int indent_level, const DCFile &file,
  54. const DCClass *this_dclass) {
  55. indent(cout, indent_level)
  56. << this_dclass->get_name() << "\n";
  57. int num_classes = file.get_num_classes();
  58. for (int i = 0; i < num_classes; ++i) {
  59. const DCClass *dclass = file.get_class(i);
  60. bool is_my_child = false;
  61. int num_parents = dclass->get_num_parents();
  62. for (int j = 0; j < num_parents && !is_my_child; ++j) {
  63. is_my_child = (dclass->get_parent(j) == this_dclass);
  64. }
  65. if (is_my_child) {
  66. write_class_hierarchy(indent_level + 2, file, dclass);
  67. }
  68. }
  69. }
  70. void
  71. write_class_hierarchy(const DCFile &file) {
  72. int num_classes = file.get_num_classes();
  73. for (int i = 0; i < num_classes; ++i) {
  74. const DCClass *dclass = file.get_class(i);
  75. if (dclass->get_num_parents() == 0) {
  76. write_class_hierarchy(0, file, dclass);
  77. cout << "\n";
  78. }
  79. }
  80. }
  81. void
  82. write_complete_field_list(const DCFile &file) {
  83. int num_classes = file.get_num_classes();
  84. for (int i = 0; i < num_classes; ++i) {
  85. const DCClass *dclass = file.get_class(i);
  86. cout << "\n" << dclass->get_name() << "\n";
  87. int num_inherited_fields = dclass->get_num_inherited_fields();
  88. for (int j = 0; j < num_inherited_fields; ++j) {
  89. const DCField *field = dclass->get_inherited_field(j);
  90. cout << " ";
  91. if (field->get_class() != dclass) {
  92. cout << field->get_class()->get_name() << "::";
  93. }
  94. cout << field->get_name();
  95. if (field->as_atomic_field() != nullptr ||
  96. field->as_molecular_field() != nullptr) {
  97. // It's a "method".
  98. cout << "()";
  99. }
  100. field->output_keywords(cout);
  101. cout << "\n";
  102. }
  103. }
  104. }
  105. int
  106. main(int argc, char *argv[]) {
  107. // extern char *optarg;
  108. extern int optind;
  109. const char *optstr = "bvcfh";
  110. bool dump_verbose = false;
  111. bool dump_brief = false;
  112. bool dump_classes = false;
  113. bool dump_fields = false;
  114. int flag = getopt(argc, argv, optstr);
  115. while (flag != EOF) {
  116. switch (flag) {
  117. case 'b':
  118. dump_brief = true;
  119. break;
  120. case 'v':
  121. dump_verbose = true;
  122. break;
  123. case 'c':
  124. dump_classes = true;
  125. break;
  126. case 'f':
  127. dump_fields = true;
  128. break;
  129. case 'h':
  130. help();
  131. exit(1);
  132. default:
  133. exit(1);
  134. }
  135. flag = getopt(argc, argv, optstr);
  136. }
  137. argc -= (optind-1);
  138. argv += (optind-1);
  139. if (argc < 2) {
  140. usage();
  141. exit(1);
  142. }
  143. DCFile file;
  144. for (int i = 1; i < argc; i++) {
  145. if (!file.read(argv[i])) {
  146. return (1);
  147. }
  148. }
  149. if (!file.all_objects_valid() && !dump_brief) {
  150. cerr << "File is incomplete. The following objects are undefined:\n";
  151. int num_typedefs = file.get_num_typedefs();
  152. int i;
  153. for (i = 0; i < num_typedefs; i++) {
  154. DCTypedef *dtypedef = file.get_typedef(i);
  155. if (dtypedef->is_bogus_typedef()) {
  156. cerr << " " << dtypedef->get_name() << "\n";
  157. }
  158. }
  159. int num_classes = file.get_num_classes();
  160. for (i = 0; i < num_classes; i++) {
  161. DCClass *dclass = file.get_class(i);
  162. if (dclass->is_bogus_class()) {
  163. cerr << " " << dclass->get_name() << "\n";
  164. }
  165. }
  166. return 1;
  167. }
  168. if (dump_verbose || dump_brief) {
  169. if (!file.write(cout, dump_brief)) {
  170. return 1;
  171. }
  172. } else if (dump_classes) {
  173. write_class_hierarchy(file);
  174. } else if (dump_fields) {
  175. write_complete_field_list(file);
  176. } else {
  177. unsigned long hash = file.get_hash();
  178. cerr << "File hash is " << hash << " (signed " << (long)hash << ")\n";
  179. }
  180. #ifdef DO_MEMORY_USAGE
  181. if (MemoryUsage::is_tracking()) {
  182. file.clear();
  183. MemoryUsage::show_current_types();
  184. for (int i = 1; i < argc; i++) {
  185. file.read(argv[i]);
  186. }
  187. file.clear();
  188. MemoryUsage::show_current_types();
  189. }
  190. #endif
  191. return 0;
  192. }