From 49f10b70e7baf576c73cd6d57ee2c9415f2be6da Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 17 Oct 2003 11:26:10 +0000 Subject: [PATCH] add dcparse -b and -v --- direct/src/dcparse/dcparse.cxx | 83 ++++++++++++++++++++---- direct/src/dcparser/dcAtomicField.cxx | 59 ++++++++++------- direct/src/dcparser/dcAtomicField.h | 4 +- direct/src/dcparser/dcClass.cxx | 11 +++- direct/src/dcparser/dcClass.h | 2 +- direct/src/dcparser/dcField.h | 2 +- direct/src/dcparser/dcFile.cxx | 19 ++---- direct/src/dcparser/dcFile.h | 4 +- direct/src/dcparser/dcMolecularField.cxx | 10 ++- direct/src/dcparser/dcMolecularField.h | 2 +- 10 files changed, 136 insertions(+), 60 deletions(-) diff --git a/direct/src/dcparse/dcparse.cxx b/direct/src/dcparse/dcparse.cxx index 63d7db0bae..a5280e4df8 100644 --- a/direct/src/dcparse/dcparse.cxx +++ b/direct/src/dcparse/dcparse.cxx @@ -16,16 +16,74 @@ // //////////////////////////////////////////////////////////////////// -#include -#include +#include "dcbase.h" +#include "dcFile.h" + +#ifndef HAVE_GETOPT +#include +#else +#include +#endif + +void +usage() { + cerr << + "\n" + "Usage:\n\n" + "dcparse [-v | -b] [file1 file2 ...]\n" + "dcparse -h\n\n"; +} + +void +help() { + usage(); + cerr << + "This program reads one or more DC files, which are used to describe the\n" + "communication channels in the distributed class system. By default,\n" + "the file(s) are read and concatenated, and a single hash code is printed\n" + "corresponding to the file's contents.\n\n" + + "With -b, this writes a brief version of the file to standard output\n" + "instead. With -v, this writes a more verbose version.\n\n"; +} int main(int argc, char *argv[]) { + extern char *optarg; + extern int optind; + const char *optstr = "bvh"; + + bool dump_verbose = false; + bool dump_brief = false; + + int flag = getopt(argc, argv, optstr); + + while (flag != EOF) { + switch (flag) { + case 'b': + dump_brief = true; + break; + + case 'v': + dump_verbose = true; + break; + + case 'h': + help(); + exit(1); + + default: + exit(1); + } + flag = getopt(argc, argv, optstr); + } + + argc -= (optind-1); + argv += (optind-1); + if (argc < 2) { - cerr << - "dcparse - a simple program to read one or more .dc files and report their\n" - "contents to standard output.\n\n"; - return (1); + usage(); + exit(1); } DCFile file; @@ -35,12 +93,15 @@ main(int argc, char *argv[]) { } } - if (!file.write(cout, "standard output")) { - return (1); - } + if (dump_verbose || dump_brief) { + if (!file.write(cout, dump_brief)) { + return (1); + } - long hash = file.get_hash(); - cerr << "File hash is " << hash << "\n"; + } else { + long hash = file.get_hash(); + cerr << "File hash is " << hash << "\n"; + } return (0); } diff --git a/direct/src/dcparser/dcAtomicField.cxx b/direct/src/dcparser/dcAtomicField.cxx index 8d81f0be34..18c493c4ea 100644 --- a/direct/src/dcparser/dcAtomicField.cxx +++ b/direct/src/dcparser/dcAtomicField.cxx @@ -22,26 +22,6 @@ #include -ostream & -operator << (ostream &out, const DCAtomicField::ElementType &et) { - out << et._type; - if (et._divisor != 1) { - out << " / " << et._divisor; - } - if (!et._name.empty()) { - out << " " << et._name; - } - if (et._has_default_value) { - out << " = <" << hex; - string::const_iterator si; - for (si = et._default_value.begin(); si != et._default_value.end(); ++si) { - out << setw(2) << setfill('0') << (int)(unsigned char)(*si); - } - out << dec << ">"; - } - return out; -} - //////////////////////////////////////////////////////////////////// // Function: DCAtomicField::ElementType::Constructor // Access: Public @@ -230,6 +210,32 @@ end_array() { } } +//////////////////////////////////////////////////////////////////// +// Function: DCAtomicField::ElementType::output +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void DCAtomicField::ElementType:: +output(ostream &out, bool brief) const { + out << _type; + if (_divisor != 1) { + out << " / " << _divisor; + } + if (!brief) { + if (!_name.empty()) { + out << " " << _name; + } + if (_has_default_value) { + out << " = <" << hex; + string::const_iterator si; + for (si = _default_value.begin(); si != _default_value.end(); ++si) { + out << setw(2) << setfill('0') << (int)(unsigned char)(*si); + } + out << dec << ">"; + } + } +} + //////////////////////////////////////////////////////////////////// // Function: DCAtomicField::ElementType::format_default_value // Access: Private @@ -586,16 +592,17 @@ DCAtomicField() { // the indicated output stream. //////////////////////////////////////////////////////////////////// void DCAtomicField:: -write(ostream &out, int indent_level) const { +write(ostream &out, bool brief, int indent_level) const { indent(out, indent_level) << _name << "("; if (!_elements.empty()) { Elements::const_iterator ei = _elements.begin(); - out << (*ei); + (*ei).output(out, brief); ++ei; while (ei != _elements.end()) { - out << ", " << (*ei); + out << ", "; + (*ei).output(out, brief); ++ei; } } @@ -626,7 +633,11 @@ write(ostream &out, int indent_level) const { out << " ownsend"; } - out << "; // field " << _number << "\n"; + out << ";"; + if (!brief) { + out << " // field " << _number; + } + out << "\n"; } //////////////////////////////////////////////////////////////////// diff --git a/direct/src/dcparser/dcAtomicField.h b/direct/src/dcparser/dcAtomicField.h index 8498caf806..e25fabb8d5 100644 --- a/direct/src/dcparser/dcAtomicField.h +++ b/direct/src/dcparser/dcAtomicField.h @@ -56,7 +56,7 @@ PUBLISHED: public: DCAtomicField(); - virtual void write(ostream &out, int indent_level = 0) const; + virtual void write(ostream &out, bool brief, int indent_level) const; virtual void generate_hash(HashGenerator &hash) const; public: @@ -74,6 +74,8 @@ public: bool add_default_value_literal(const string &str); bool end_array(); + void output(ostream &out, bool brief) const; + DCSubatomicType _type; string _name; int _divisor; diff --git a/direct/src/dcparser/dcClass.cxx b/direct/src/dcparser/dcClass.cxx index ca1816e1ac..aae13df8dc 100644 --- a/direct/src/dcparser/dcClass.cxx +++ b/direct/src/dcparser/dcClass.cxx @@ -189,7 +189,7 @@ DCClass:: // the indicated output stream. //////////////////////////////////////////////////////////////////// void DCClass:: -write(ostream &out, int indent_level) const { +write(ostream &out, bool brief, int indent_level) const { indent(out, indent_level) << "dclass " << _name; @@ -202,11 +202,16 @@ write(ostream &out, int indent_level) const { ++pi; } } - out << " { // index " << _number << "\n"; + + out << " {"; + if (!brief) { + out << " // index " << _number; + } + out << "\n"; Fields::const_iterator fi; for (fi = _fields.begin(); fi != _fields.end(); ++fi) { - (*fi)->write(out, indent_level + 2); + (*fi)->write(out, brief, indent_level + 2); } indent(out, indent_level) << "};\n"; diff --git a/direct/src/dcparser/dcClass.h b/direct/src/dcparser/dcClass.h index ece2c94aa3..04d285e91d 100644 --- a/direct/src/dcparser/dcClass.h +++ b/direct/src/dcparser/dcClass.h @@ -48,7 +48,7 @@ public: DCClass(); ~DCClass(); - void write(ostream &out, int indent_level = 0) const; + void write(ostream &out, bool brief, int indent_level) const; void generate_hash(HashGenerator &hash) const; bool add_field(DCField *field); diff --git a/direct/src/dcparser/dcField.h b/direct/src/dcparser/dcField.h index cd68e1af8d..5101a43fbc 100644 --- a/direct/src/dcparser/dcField.h +++ b/direct/src/dcparser/dcField.h @@ -40,7 +40,7 @@ PUBLISHED: public: virtual ~DCField(); - virtual void write(ostream &out, int indent_level = 0) const=0; + virtual void write(ostream &out, bool brief, int indent_level) const=0; virtual void generate_hash(HashGenerator &hash) const; public: diff --git a/direct/src/dcparser/dcFile.cxx b/direct/src/dcparser/dcFile.cxx index 4432e9b56a..1e8ede8fa5 100644 --- a/direct/src/dcparser/dcFile.cxx +++ b/direct/src/dcparser/dcFile.cxx @@ -128,7 +128,7 @@ read(istream &in, const string &filename) { // written, false otherwise. //////////////////////////////////////////////////////////////////// bool DCFile:: -write(Filename filename) const { +write(Filename filename, bool brief) const { ofstream out; #ifdef WITHIN_PANDA @@ -142,34 +142,27 @@ write(Filename filename) const { cerr << "Can't open " << filename << " for output.\n"; return false; } - return write(out, filename); + return write(out, brief); } //////////////////////////////////////////////////////////////////// // Function: DCFile::write // Access: Published // Description: Writes a parseable description of all the known -// distributed classes to the file. The filename -// parameter is optional and is only used when reporting -// errors. +// distributed classes to the stream. // // Returns true if the description is successfully // written, false otherwise. //////////////////////////////////////////////////////////////////// bool DCFile:: -write(ostream &out, const string &filename) const { +write(ostream &out, bool brief) const { Classes::const_iterator ci; for (ci = _classes.begin(); ci != _classes.end(); ++ci) { - (*ci)->write(out); + (*ci)->write(out, brief, 0); out << "\n"; } - if (out.fail()) { - cerr << "I/O error writing " << filename << ".\n"; - return false; - } - - return true; + return !out.fail(); } //////////////////////////////////////////////////////////////////// diff --git a/direct/src/dcparser/dcFile.h b/direct/src/dcparser/dcFile.h index 28a996679b..963fefc518 100644 --- a/direct/src/dcparser/dcFile.h +++ b/direct/src/dcparser/dcFile.h @@ -37,8 +37,8 @@ PUBLISHED: bool read(Filename filename); bool read(istream &in, const string &filename = string()); - bool write(Filename filename) const; - bool write(ostream &out, const string &filename = string()) const; + bool write(Filename filename, bool brief) const; + bool write(ostream &out, bool brief) const; int get_num_classes(); DCClass *get_class(int n); diff --git a/direct/src/dcparser/dcMolecularField.cxx b/direct/src/dcparser/dcMolecularField.cxx index da27cfaf78..4995e1d99b 100644 --- a/direct/src/dcparser/dcMolecularField.cxx +++ b/direct/src/dcparser/dcMolecularField.cxx @@ -77,7 +77,7 @@ DCMolecularField() { // the indicated output stream. //////////////////////////////////////////////////////////////////// void DCMolecularField:: -write(ostream &out, int indent_level) const { +write(ostream &out, bool brief, int indent_level) const { indent(out, indent_level) << _name; if (!_fields.empty()) { @@ -89,8 +89,12 @@ write(ostream &out, int indent_level) const { ++fi; } } - - out << "; // field " << _number << "\n"; + + out << ";"; + if (!brief) { + out << " // field " << _number; + } + out << "\n"; } diff --git a/direct/src/dcparser/dcMolecularField.h b/direct/src/dcparser/dcMolecularField.h index 87ca26e827..967ca679e7 100644 --- a/direct/src/dcparser/dcMolecularField.h +++ b/direct/src/dcparser/dcMolecularField.h @@ -40,7 +40,7 @@ PUBLISHED: public: DCMolecularField(); - virtual void write(ostream &out, int indent_level = 0) const; + virtual void write(ostream &out, bool brief, int indent_level) const; virtual void generate_hash(HashGenerator &hash) const; public: