Panda3D
dcparse.cxx
Go to the documentation of this file.
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 
14 #include "dcbase.h"
15 #include "dcFile.h"
16 #include "dcClass.h"
17 #include "dcTypedef.h"
18 #include "memoryUsage.h"
19 #include "indent.h"
20 #include "panda_getopt.h"
21 
22 using std::cerr;
23 using std::cout;
24 
25 void
26 usage() {
27  cerr <<
28  "\n"
29  "Usage:\n\n"
30  "dcparse [options] [file1 file2 ...]\n"
31  "dcparse -h\n\n";
32 }
33 
34 void
35 help() {
36  usage();
37  cerr <<
38  "This program reads one or more DC files, which are used to describe the\n"
39  "communication channels in the distributed class system. By default,\n"
40  "the file(s) are read and concatenated, and a single hash code is printed\n"
41  "corresponding to the file's contents.\n\n"
42 
43  "Options:\n\n"
44 
45  " -v Writes a complete parseable version of the file to standard\n"
46  " output instead of printing a hash code.\n\n"
47 
48  " -b Writes a brief parseable version of the file instead of a full\n"
49  " version. This is semantically the same as the output produced\n"
50  " the above -v option--reading it would produce exactly the same\n"
51  " results--but it is designed to be slightly obfuscated. The\n"
52  " comments and parameter names are not included.\n\n"
53 
54  " -c Write a list of class names, showing the inheritance hierarchy.\n"
55  " Some class names will be listed twice in the presence of multiple\n"
56  " inheritance.\n\n"
57 
58  " -f Write a complete list of field names available for each class,\n"
59  " including all inherited fields.\n\n";
60 }
61 
62 void
63 write_class_hierarchy(int indent_level, const DCFile &file,
64  const DCClass *this_dclass) {
65  indent(cout, indent_level)
66  << this_dclass->get_name() << "\n";
67 
68  int num_classes = file.get_num_classes();
69  for (int i = 0; i < num_classes; ++i) {
70  const DCClass *dclass = file.get_class(i);
71  bool is_my_child = false;
72  int num_parents = dclass->get_num_parents();
73  for (int j = 0; j < num_parents && !is_my_child; ++j) {
74  is_my_child = (dclass->get_parent(j) == this_dclass);
75  }
76 
77  if (is_my_child) {
78  write_class_hierarchy(indent_level + 2, file, dclass);
79  }
80  }
81 }
82 
83 void
84 write_class_hierarchy(const DCFile &file) {
85  int num_classes = file.get_num_classes();
86  for (int i = 0; i < num_classes; ++i) {
87  const DCClass *dclass = file.get_class(i);
88  if (dclass->get_num_parents() == 0) {
89  write_class_hierarchy(0, file, dclass);
90  cout << "\n";
91  }
92  }
93 }
94 
95 void
96 write_complete_field_list(const DCFile &file) {
97  int num_classes = file.get_num_classes();
98  for (int i = 0; i < num_classes; ++i) {
99  const DCClass *dclass = file.get_class(i);
100  cout << "\n" << dclass->get_name() << "\n";
101  int num_inherited_fields = dclass->get_num_inherited_fields();
102  for (int j = 0; j < num_inherited_fields; ++j) {
103  const DCField *field = dclass->get_inherited_field(j);
104  cout << " ";
105  if (field->get_class() != dclass) {
106  cout << field->get_class()->get_name() << "::";
107  }
108  cout << field->get_name();
109  if (field->as_atomic_field() != nullptr ||
110  field->as_molecular_field() != nullptr) {
111  // It's a "method".
112  cout << "()";
113  }
114  field->output_keywords(cout);
115  cout << "\n";
116  }
117  }
118 }
119 
120 int
121 main(int argc, char *argv[]) {
122  // extern char *optarg;
123  extern int optind;
124  const char *optstr = "bvcfh";
125 
126  bool dump_verbose = false;
127  bool dump_brief = false;
128  bool dump_classes = false;
129  bool dump_fields = false;
130 
131  int flag = getopt(argc, argv, optstr);
132 
133  while (flag != EOF) {
134  switch (flag) {
135  case 'b':
136  dump_brief = true;
137  break;
138 
139  case 'v':
140  dump_verbose = true;
141  break;
142 
143  case 'c':
144  dump_classes = true;
145  break;
146 
147  case 'f':
148  dump_fields = true;
149  break;
150 
151  case 'h':
152  help();
153  exit(1);
154 
155  default:
156  exit(1);
157  }
158  flag = getopt(argc, argv, optstr);
159  }
160 
161  argc -= (optind-1);
162  argv += (optind-1);
163 
164  if (argc < 2) {
165  usage();
166  exit(1);
167  }
168 
169  DCFile file;
170  for (int i = 1; i < argc; i++) {
171  if (!file.read(argv[i])) {
172  return (1);
173  }
174  }
175 
176  if (!file.all_objects_valid() && !dump_brief) {
177  cerr << "File is incomplete. The following objects are undefined:\n";
178 
179  int num_typedefs = file.get_num_typedefs();
180  int i;
181  for (i = 0; i < num_typedefs; i++) {
182  DCTypedef *dtypedef = file.get_typedef(i);
183  if (dtypedef->is_bogus_typedef()) {
184  cerr << " " << dtypedef->get_name() << "\n";
185  }
186  }
187 
188  int num_classes = file.get_num_classes();
189  for (i = 0; i < num_classes; i++) {
190  DCClass *dclass = file.get_class(i);
191  if (dclass->is_bogus_class()) {
192  cerr << " " << dclass->get_name() << "\n";
193  }
194  }
195 
196  return 1;
197  }
198 
199  if (dump_verbose || dump_brief) {
200  if (!file.write(cout, dump_brief)) {
201  return 1;
202  }
203 
204  } else if (dump_classes) {
205  write_class_hierarchy(file);
206 
207  } else if (dump_fields) {
208  write_complete_field_list(file);
209 
210  } else {
211  unsigned long hash = file.get_hash();
212  cerr << "File hash is " << hash << " (signed " << (long)hash << ")\n";
213  }
214 
215 #ifdef DO_MEMORY_USAGE
216  if (MemoryUsage::is_tracking()) {
217  file.clear();
219  for (int i = 1; i < argc; i++) {
220  file.read(argv[i]);
221  }
222  file.clear();
224  }
225 #endif
226 
227  return 0;
228 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents a single typedef declaration in the dc file.
Definition: dcTypedef.h:26
const std::string & get_name() const
Returns the name of this field, or empty string if the field is unnamed.
DCClass * get_class() const
Returns the DCClass pointer for the class that contains this field.
Definition: dcField.I:27
DCClass * get_parent(int n) const
Returns the nth parent class this class inherits from.
Definition: dcClass.cxx:143
A single field of a Distributed Class, either atomic or molecular.
Definition: dcField.h:37
virtual DCAtomicField * as_atomic_field()
Returns the same field pointer converted to an atomic field pointer, if this is in fact an atomic fie...
Definition: dcField.cxx:112
bool read(Filename filename)
Opens and reads the indicated .dc file by name.
Definition: dcFile.cxx:124
Defines a particular DistributedClass as read from an input .dc file.
Definition: dcClass.h:44
int get_num_typedefs() const
Returns the number of typedefs read from the .dc file(s).
Definition: dcFile.cxx:349
bool write(Filename filename, bool brief) const
Opens the indicated filename for output and writes a parseable description of all the known distribut...
Definition: dcFile.cxx:185
virtual DCMolecularField * as_molecular_field()
Returns the same field pointer converted to a molecular field pointer, if this is in fact a molecular...
Definition: dcField.cxx:130
DCTypedef * get_typedef(int n) const
Returns the nth typedef read from the .dc file(s).
Definition: dcFile.cxx:357
bool all_objects_valid() const
Returns true if all of the classes read from the DC file were defined and valid, or false if any of t...
Definition: dcFile.I:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents the complete list of Distributed Class descriptions as read from a .dc file.
Definition: dcFile.h:32
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DCClass * get_class(int n) const
Returns the nth class read from the .dc file(s).
Definition: dcFile.cxx:252
DCField * get_inherited_field(int n) const
Returns the nth field field in the class and all of its ancestors.
Definition: dcClass.cxx:288
unsigned long get_hash() const
Returns a 32-bit hash index associated with this file.
Definition: dcFile.cxx:419
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
bool is_bogus_typedef() const
Returns true if the typedef has been flagged as a bogus typedef.
Definition: dcTypedef.cxx:89
const std::string & get_name() const
Returns the name of this typedef.
Definition: dcTypedef.cxx:68
int get_num_parents() const
Returns the number of base classes this class inherits from.
Definition: dcClass.cxx:135
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_num_inherited_fields() const
Returns the total number of field fields defined in this class and all ancestor classes.
Definition: dcClass.cxx:255
void clear()
Removes all of the classes defined within the DCFile and prepares it for reading a new file.
Definition: dcFile.cxx:59
int get_num_classes() const
Returns the number of classes read from the .dc file(s).
Definition: dcFile.cxx:244
const std::string & get_name() const
Returns the name of this class.
Definition: dcClass.I:26
static void show_current_types()
Shows the breakdown of types of all of the active pointers.
Definition: memoryUsage.I:347
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_bogus_class() const
Returns true if the class has been flagged as a bogus class.
Definition: dcClass.I:55
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.