// file: $(NEDC_NFC)/class/cpp/Cmdl/cmdl_00.cc // // This file contains basic required methods such as constructors // and destructors. // Revision History // 20200518 (JP): added the boost parser // 20150209 (JP): revised and restructured // 20150208 (SL): initial version // // local include files // #include "Cmdl.h" //----------------------------------------------------------------------------- // // basic required methods // //----------------------------------------------------------------------------- // method: constructor with arguments // Cmdl::Cmdl(const char* usage_a, const char* help_a) { // display debugging information // if (dbgl_d == Dbgl::FULL) { fprintf(stdout, "%s (line: %d) %s: initializing a Cmdl object (%p)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, this); } // initialize class variables // usage_fname_d = (char*)NULL; help_fname_d = (char*)NULL; // set the message filenames // if (set_msgs(usage_a, help_a) == false) { fprintf(stdout, "Error: %s (line: %d) %s: error setting msgs\n", __FILE__, __LINE__, __PRETTY_FUNCTION__); exit(EXIT_FAILURE); } // initialize the boost parser-related data structures // bst_desc_d = new bpo::options_description(""); bst_pos_d = new bpo::positional_options_description(); // add the default arguments // add_argument(ARG_USAGE); add_argument(ARG_HELP); add_argument(ARG_DBGL); add_argument(ARG_VRBL); add_argument>(ARG_ARGS); // set the positional arguments: // this used to grab argument values not associated with a specific // argument (e.g., filenames interspersed on the command line) // bst_pos_d->add(ARG_ARGS, -1); // display debugging information // if (dbgl_d == Dbgl::FULL) { fprintf(stdout, "%s (line: %d) %s: done initializing a Cmdl object (%p)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, this); } // exit gracefully // }; // method: destructor // // arguments: none // // return: none // // This method implements the destructor. // Cmdl::~Cmdl() { // display debugging information // if (dbgl_d == Dbgl::FULL) { fprintf(stdout, "%s (line: %d) %s: begin destroying a Cmdl object (%p)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, this); } // clean up filename memory // if (dbgl_d == Dbgl::FULL) { fprintf(stdout, "%s (line: %d) %s: destroying filename storage\n", __FILE__, __LINE__, __PRETTY_FUNCTION__); } if (usage_fname_d != (char*)NULL) { delete [] usage_fname_d; usage_fname_d = (char*)NULL; } if (help_fname_d != (char*)NULL) { delete [] help_fname_d; help_fname_d = (char*)NULL; } // delete the parser stuff // if (dbgl_d == Dbgl::FULL) { fprintf(stdout, "%s (line: %d) %s: destroying parser storage\n", __FILE__, __LINE__, __PRETTY_FUNCTION__); } if (bst_desc_d != (bpo::options_description*)NULL) { delete bst_desc_d; bst_desc_d = (bpo::options_description*)NULL; } if (bst_pos_d != (bpo::positional_options_description*)NULL) { delete bst_pos_d; bst_pos_d = (bpo::positional_options_description*)NULL; } // display debugging information // if (dbgl_d == Dbgl::FULL) { fprintf(stdout, "%s (line: %d) %s: end of destroying a Cmdl object\n", __FILE__, __LINE__, __PRETTY_FUNCTION__); } // exit gracefully // } // method: set_msgs // // arguments: // char* usage: usage filename to be displayed (input) // char* help: help filename to be displayed (input) // // return: a boolean value indicating status // // This method sets the filenames. Not that environment variables are expanded. // bool Cmdl::set_msgs(const char* usage_a, const char* help_a) { // display a debug message // if (dbgl_d > Dbgl::BRIEF) { fprintf(stdout, "%s (line: %d) %s: usage file input name (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, usage_a); fprintf(stdout, "%s (line: %d) %s: help file input name (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, help_a); } // free space if already allocated // if (usage_fname_d != (char*)NULL) { delete [] usage_fname_d; } if (help_fname_d != (char*)NULL) { delete [] help_fname_d; } // create space // usage_fname_d = new char[MAX_FNAME_SIZE]; help_fname_d = new char[MAX_FNAME_SIZE]; // expand the usage filename // if (usage_a != (char*)NULL) { expand_filename(usage_fname_d, usage_a); } else { usage_fname_d = (char*)NULL; } // expand the help filename // if (help_a != (char*)NULL) { expand_filename(help_fname_d, help_a); } else { help_fname_d = (char*)NULL; } // display a debug message // if (dbgl_d > Dbgl::BRIEF) { fprintf(stdout, "%s (line: %d) %s: usage file real name (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, usage_fname_d); fprintf(stdout, "%s (line: %d) %s: help file real name (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, help_fname_d); } // exit gracefully // return true; } //----------------------------------------------------------------------------- // // define non-integral constants in the default constructor // //----------------------------------------------------------------------------- // constants: class name // const char* Cmdl::CLASS_NAME = "Cmdl"; // constants: help and usage // note that help includes an abbreviation while the others don't. this // was done by design to make usage, debug and vebosity more hidden. // // const char* Cmdl::ARG_USAGE = "usage"; const char* Cmdl::ARG_HELP = "help,h"; const char* Cmdl::ARG_HELP_NAME = "help"; const char* Cmdl::ARG_DBGL = "debug_level"; const char* Cmdl::ARG_VRBL = "verbosity_level"; const char* Cmdl::ARG_ARGS = "args"; // constants: debug level, verbosity level // Dbgl Cmdl::dbgl_d; Vrbl Cmdl::vrbl_d; // // end of file