// CommandLine.cxx #include "dataset_util/XmlElement.h" #include "dataset_util/Text.h" #include "dataset_util/CommandLine.h" #include using std::string; using std::ostream; typedef CommandLine::Name Name; typedef CommandLine::Argument Argument; typedef CommandLine::ArgumentList ArgumentList; typedef CommandLine::CargList CargList; //********************************************************************** // Local definitions. //********************************************************************** namespace { // Fill char* data from name and arguments. void fill(const Name& name, const ArgumentList& args, CargList& argv) { argv.push_back(name.c_str()); for ( ArgumentList::const_iterator iarg=args.begin(); iarg!=args.end(); ++iarg ) { argv.push_back(iarg->c_str()); } argv.push_back(0); assert( argv.size() == args.size()+2 ); }; } //********************************************************************** // Static member functions. //********************************************************************** // DTD. const Text& CommandLine::dtd() { static Text txt; if ( txt.size() == 0 ) { txt.append(""); txt.append(""); txt.append(""); txt.append(""); txt.append(""); } return txt; } //********************************************************************** // Member functions. //********************************************************************** // Default constructor. CommandLine::CommandLine() : m_name("") { } //********************************************************************** // Constructor from name and arguments. CommandLine::CommandLine(Name name, ArgumentList args) : m_name(name), m_args(args) { fill(m_name, m_args, m_argv); } //********************************************************************** // Constructor from null-terminated char* array. CommandLine::CommandLine(const char* const* argv) : m_name(argv[0]) { if ( argv[0] == 0 ) return; int i = 0; while ( argv[++i] != 0 ) { m_args.push_back(argv[i]); assert( i < 10000 ); } fill(m_name, m_args, m_argv); } //********************************************************************** // Constructor from char* array of specified length. CommandLine::CommandLine(int narg, const char* const* argv) : m_name(argv[0]) { if ( narg < 1 ) { m_name = ""; } for ( int i=1; i 0; } //********************************************************************** // Return the name. Name CommandLine::name() const { return m_name; } //********************************************************************** // Return the name as a C-string const char* CommandLine::cname() const { return m_name.c_str(); } //********************************************************************** // Return the argument list. const ArgumentList& CommandLine::arguments() const { return m_args; } //********************************************************************** // Return a pointer to an an array of const char* holding the // name followed by the arguments. const char* const* CommandLine::argv() const { return &m_argv[0]; } //********************************************************************** // Return a pointer to an an array of char* holding the // name followed by the arguments. char* const* CommandLine::unsafe_argv() const { return const_cast(argv()); } //********************************************************************** // Create XML representation. const XmlElement* CommandLine::xml() const { if ( ! is_valid() ) return 0; XmlElement* pele = new XmlElement(xml_name()); pele->add_attribute("name", name()); for ( ArgumentList::const_iterator iarg=arguments().begin(); iarg!=arguments().end(); ++iarg ) { XmlElement* pxatt = new XmlElement("CommandLineArgument"); pxatt->add_attribute("value", *iarg); pele->add_child(pxatt); } return pele; } //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const CommandLine& rhs) { lhs << rhs.name(); for ( ArgumentList::const_iterator iarg=rhs.arguments().begin(); iarg!=rhs.arguments().end(); ++iarg ) { lhs << " " << *iarg; } return lhs; } //********************************************************************** // Equality bool operator==(const CommandLine& lhs, const CommandLine rhs) { return lhs.name() == rhs.name() && lhs.arguments() == rhs.arguments(); } //********************************************************************** // Inequality bool operator!=(const CommandLine& lhs, const CommandLine rhs) { return ! (lhs == rhs); } //**********************************************************************