add dcparse -b and -v

This commit is contained in:
David Rose 2003-10-17 11:26:10 +00:00
parent f5ef9d148e
commit 49f10b70e7
10 changed files with 136 additions and 60 deletions

View File

@ -16,16 +16,74 @@
//
////////////////////////////////////////////////////////////////////
#include <dcbase.h>
#include <dcFile.h>
#include "dcbase.h"
#include "dcFile.h"
#ifndef HAVE_GETOPT
#include <gnu_getopt.h>
#else
#include <getopt.h>
#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);
}

View File

@ -22,26 +22,6 @@
#include <math.h>
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";
}
////////////////////////////////////////////////////////////////////

View File

@ -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;

View File

@ -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";

View File

@ -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);

View File

@ -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:

View File

@ -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();
}
////////////////////////////////////////////////////////////////////

View File

@ -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);

View File

@ -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";
}

View File

@ -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: