12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- #include "anyoption.h"
- AnyOption::AnyOption() { init(); }
- AnyOption::AnyOption(int maxopt) { init(maxopt, maxopt); }
- AnyOption::AnyOption(int maxopt, int maxcharopt) { init(maxopt, maxcharopt); }
- AnyOption::~AnyOption() {
- if (mem_allocated)
- cleanup();
- }
- void AnyOption::init() { init(DEFAULT_MAXOPTS, DEFAULT_MAXOPTS); }
- void AnyOption::init(int maxopt, int maxcharopt) {
- max_options = maxopt;
- max_char_options = maxcharopt;
- max_usage_lines = DEFAULT_MAXUSAGE;
- usage_lines = 0;
- argc = 0;
- argv = NULL;
- posix_style = true;
- verbose = false;
- filename = NULL;
- appname = NULL;
- option_counter = 0;
- optchar_counter = 0;
- new_argv = NULL;
- new_argc = 0;
- max_legal_args = 0;
- command_set = false;
- file_set = false;
- values = NULL;
- g_value_counter = 0;
- mem_allocated = false;
- opt_prefix_char = '-';
- file_delimiter_char = ':';
- file_comment_char = '#';
- equalsign = '=';
- comment = '#';
- delimiter = ':';
- endofline = '\n';
- whitespace = ' ';
- nullterminate = '\0';
- set = false;
- once = true;
- hasoptions = false;
- autousage = false;
- print_usage = false;
- print_help = false;
- strcpy(long_opt_prefix, "--");
- if (alloc() == false) {
- cout << endl << "OPTIONS ERROR : Failed allocating memory";
- cout << endl;
- cout << "Exiting." << endl;
- exit(0);
- }
- }
- bool AnyOption::alloc() {
- int i = 0;
- int size = 0;
- if (mem_allocated)
- return true;
- size = (max_options + 1) * sizeof(const char *);
- options = (const char **)malloc(size);
- optiontype = (int *)malloc((max_options + 1) * sizeof(int));
- optionindex = (int *)malloc((max_options + 1) * sizeof(int));
- if (options == NULL || optiontype == NULL || optionindex == NULL)
- return false;
- else
- mem_allocated = true;
- for (i = 0; i < max_options; i++) {
- options[i] = NULL;
- optiontype[i] = 0;
- optionindex[i] = -1;
- }
- optionchars = (char *)malloc((max_char_options + 1) * sizeof(char));
- optchartype = (int *)malloc((max_char_options + 1) * sizeof(int));
- optcharindex = (int *)malloc((max_char_options + 1) * sizeof(int));
- if (optionchars == NULL || optchartype == NULL || optcharindex == NULL) {
- mem_allocated = false;
- return false;
- }
- for (i = 0; i < max_char_options; i++) {
- optionchars[i] = '0';
- optchartype[i] = 0;
- optcharindex[i] = -1;
- }
- size = (max_usage_lines + 1) * sizeof(const char *);
- usage = (const char **)malloc(size);
- if (usage == NULL) {
- mem_allocated = false;
- return false;
- }
- for (i = 0; i < max_usage_lines; i++)
- usage[i] = NULL;
- return true;
- }
- void AnyOption::allocValues(int index, size_t length) {
- if (values[index] == NULL) {
- values[index] = (char *)malloc(length);
- } else {
- free(values[index]);
- values[index] = (char *)malloc(length);
- }
- }
- bool AnyOption::doubleOptStorage() {
- const char **options_saved = options;
- options = (const char **)realloc(options, ((2 * max_options) + 1) *
- sizeof(const char *));
- if (options == NULL) {
- free(options_saved);
- return false;
- }
- int *optiontype_saved = optiontype;
- optiontype =
- (int *)realloc(optiontype, ((2 * max_options) + 1) * sizeof(int));
- if (optiontype == NULL) {
- free(optiontype_saved);
- return false;
- }
- int *optionindex_saved = optionindex;
- optionindex =
- (int *)realloc(optionindex, ((2 * max_options) + 1) * sizeof(int));
- if (optionindex == NULL) {
- free(optionindex_saved);
- return false;
- }
-
- for (int i = max_options; i < 2 * max_options; i++) {
- options[i] = NULL;
- optiontype[i] = 0;
- optionindex[i] = -1;
- }
- max_options = 2 * max_options;
- return true;
- }
- bool AnyOption::doubleCharStorage() {
- char *optionchars_saved = optionchars;
- optionchars =
- (char *)realloc(optionchars, ((2 * max_char_options) + 1) * sizeof(char));
- if (optionchars == NULL) {
- free(optionchars_saved);
- return false;
- }
- int *optchartype_saved = optchartype;
- optchartype =
- (int *)realloc(optchartype, ((2 * max_char_options) + 1) * sizeof(int));
- if (optchartype == NULL) {
- free(optchartype_saved);
- return false;
- }
- int *optcharindex_saved = optcharindex;
- optcharindex =
- (int *)realloc(optcharindex, ((2 * max_char_options) + 1) * sizeof(int));
- if (optcharindex == NULL) {
- free(optcharindex_saved);
- return false;
- }
-
- for (int i = max_char_options; i < 2 * max_char_options; i++) {
- optionchars[i] = '0';
- optchartype[i] = 0;
- optcharindex[i] = -1;
- }
- max_char_options = 2 * max_char_options;
- return true;
- }
- bool AnyOption::doubleUsageStorage() {
- const char **usage_saved = usage;
- usage = (const char **)realloc(usage, ((2 * max_usage_lines) + 1) *
- sizeof(const char *));
- if (usage == NULL) {
- free(usage_saved);
- return false;
- }
- for (int i = max_usage_lines; i < 2 * max_usage_lines; i++)
- usage[i] = NULL;
- max_usage_lines = 2 * max_usage_lines;
- return true;
- }
- void AnyOption::cleanup() {
- free(options);
- free(optiontype);
- free(optionindex);
- free(optionchars);
- free(optchartype);
- free(optcharindex);
- free(usage);
- if (values != NULL) {
- for (int i = 0; i < g_value_counter; i++) {
- free(values[i]);
- values[i] = NULL;
- }
- free(values);
- }
- if (new_argv != NULL)
- free(new_argv);
- }
- void AnyOption::setCommandPrefixChar(char _prefix) {
- opt_prefix_char = _prefix;
- }
- void AnyOption::setCommandLongPrefix(const char *_prefix) {
- if (strlen(_prefix) > MAX_LONG_PREFIX_LENGTH) {
- strncpy(long_opt_prefix, _prefix, MAX_LONG_PREFIX_LENGTH);
- long_opt_prefix[MAX_LONG_PREFIX_LENGTH] = nullterminate;
- } else {
- strcpy(long_opt_prefix, _prefix);
- }
- }
- void AnyOption::setFileCommentChar(char _comment) {
- file_delimiter_char = _comment;
- }
- void AnyOption::setFileDelimiterChar(char _delimiter) {
- file_comment_char = _delimiter;
- }
- bool AnyOption::CommandSet() const { return (command_set); }
- bool AnyOption::FileSet() const { return (file_set); }
- void AnyOption::noPOSIX() { posix_style = false; }
- bool AnyOption::POSIX() const { return posix_style; }
- void AnyOption::setVerbose() { verbose = true; }
- void AnyOption::printVerbose() const {
- if (verbose)
- cout << endl;
- }
- void AnyOption::printVerbose(const char *msg) const {
- if (verbose)
- cout << msg;
- }
- void AnyOption::printVerbose(char *msg) const {
- if (verbose)
- cout << msg;
- }
- void AnyOption::printVerbose(char ch) const {
- if (verbose)
- cout << ch;
- }
- bool AnyOption::hasOptions() const { return hasoptions; }
- void AnyOption::autoUsagePrint(bool _autousage) { autousage = _autousage; }
- void AnyOption::useCommandArgs(int _argc, char **_argv) {
- argc = _argc;
- argv = _argv;
- command_set = true;
- appname = argv[0];
- if (argc > 1)
- hasoptions = true;
- }
- void AnyOption::useFiileName(const char *_filename) {
- filename = _filename;
- file_set = true;
- }
- void AnyOption::setCommandOption(const char *opt) {
- addOption(opt, COMMAND_OPT);
- g_value_counter++;
- }
- void AnyOption::setCommandOption(char opt) {
- addOption(opt, COMMAND_OPT);
- g_value_counter++;
- }
- void AnyOption::setCommandOption(const char *opt, char optchar) {
- addOption(opt, COMMAND_OPT);
- addOption(optchar, COMMAND_OPT);
- g_value_counter++;
- }
- void AnyOption::setCommandFlag(const char *opt) {
- addOption(opt, COMMAND_FLAG);
- g_value_counter++;
- }
- void AnyOption::setCommandFlag(char opt) {
- addOption(opt, COMMAND_FLAG);
- g_value_counter++;
- }
- void AnyOption::setCommandFlag(const char *opt, char optchar) {
- addOption(opt, COMMAND_FLAG);
- addOption(optchar, COMMAND_FLAG);
- g_value_counter++;
- }
- void AnyOption::setFileOption(const char *opt) {
- addOption(opt, FILE_OPT);
- g_value_counter++;
- }
- void AnyOption::setFileOption(char opt) {
- addOption(opt, FILE_OPT);
- g_value_counter++;
- }
- void AnyOption::setFileOption(const char *opt, char optchar) {
- addOption(opt, FILE_OPT);
- addOption(optchar, FILE_OPT);
- g_value_counter++;
- }
- void AnyOption::setFileFlag(const char *opt) {
- addOption(opt, FILE_FLAG);
- g_value_counter++;
- }
- void AnyOption::setFileFlag(char opt) {
- addOption(opt, FILE_FLAG);
- g_value_counter++;
- }
- void AnyOption::setFileFlag(const char *opt, char optchar) {
- addOption(opt, FILE_FLAG);
- addOption(optchar, FILE_FLAG);
- g_value_counter++;
- }
- void AnyOption::setOption(const char *opt) {
- addOption(opt, COMMON_OPT);
- g_value_counter++;
- }
- void AnyOption::setOption(char opt) {
- addOption(opt, COMMON_OPT);
- g_value_counter++;
- }
- void AnyOption::setOption(const char *opt, char optchar) {
- addOption(opt, COMMON_OPT);
- addOption(optchar, COMMON_OPT);
- g_value_counter++;
- }
- void AnyOption::setFlag(const char *opt) {
- addOption(opt, COMMON_FLAG);
- g_value_counter++;
- }
- void AnyOption::setFlag(const char opt) {
- addOption(opt, COMMON_FLAG);
- g_value_counter++;
- }
- void AnyOption::setFlag(const char *opt, char optchar) {
- addOption(opt, COMMON_FLAG);
- addOption(optchar, COMMON_FLAG);
- g_value_counter++;
- }
- void AnyOption::addOption(const char *opt, int type) {
- if (option_counter >= max_options) {
- if (doubleOptStorage() == false) {
- addOptionError(opt);
- return;
- }
- }
- options[option_counter] = opt;
- optiontype[option_counter] = type;
- optionindex[option_counter] = g_value_counter;
- option_counter++;
- }
- void AnyOption::addOption(char opt, int type) {
- if (!POSIX()) {
- printVerbose("Ignoring the option character \"");
- printVerbose(opt);
- printVerbose("\" ( POSIX options are turned off )");
- printVerbose();
- return;
- }
- if (optchar_counter >= max_char_options) {
- if (doubleCharStorage() == false) {
- addOptionError(opt);
- return;
- }
- }
- optionchars[optchar_counter] = opt;
- optchartype[optchar_counter] = type;
- optcharindex[optchar_counter] = g_value_counter;
- optchar_counter++;
- }
- void AnyOption::addOptionError(const char *opt) const {
- cout << endl;
- cout << "OPTIONS ERROR : Failed allocating extra memory " << endl;
- cout << "While adding the option : \"" << opt << "\"" << endl;
- cout << "Exiting." << endl;
- cout << endl;
- exit(0);
- }
- void AnyOption::addOptionError(char opt) const {
- cout << endl;
- cout << "OPTIONS ERROR : Failed allocating extra memory " << endl;
- cout << "While adding the option: \"" << opt << "\"" << endl;
- cout << "Exiting." << endl;
- cout << endl;
- exit(0);
- }
- void AnyOption::processOptions() {
- if (!valueStoreOK())
- return;
- }
- void AnyOption::processCommandArgs(int max_args) {
- max_legal_args = max_args;
- processCommandArgs();
- }
- void AnyOption::processCommandArgs(int _argc, char **_argv, int max_args) {
- max_legal_args = max_args;
- processCommandArgs(_argc, _argv);
- }
- void AnyOption::processCommandArgs(int _argc, char **_argv) {
- useCommandArgs(_argc, _argv);
- processCommandArgs();
- }
- void AnyOption::processCommandArgs() {
- if (!(valueStoreOK() && CommandSet()))
- return;
- if (max_legal_args == 0)
- max_legal_args = argc;
- new_argv = (int *)malloc((max_legal_args + 1) * sizeof(int));
- for (int i = 1; i < argc; i++) {
- if (argv[i][0] == long_opt_prefix[0] &&
- argv[i][1] == long_opt_prefix[1]) {
- int match_at = parseGNU(argv[i] + 2);
- if (match_at >= 0 && i < argc - 1)
- setValue(options[match_at], argv[++i]);
- } else if (argv[i][0] == opt_prefix_char) {
- if (POSIX()) {
- char ch = parsePOSIX(argv[i] + 1);
- if (ch != '0' && i < argc - 1)
- setValue(ch, argv[++i]);
- } else {
- int match_at = parseGNU(argv[i] + 1);
- if (match_at >= 0 && i < argc - 1)
- setValue(options[match_at], argv[++i]);
- }
- } else {
- if (new_argc < max_legal_args) {
- new_argv[new_argc] = i;
- new_argc++;
- } else {
- printVerbose("Ignoring extra argument: ");
- printVerbose(argv[i]);
- printVerbose();
- printAutoUsage();
- }
- printVerbose("Unknown command argument option : ");
- printVerbose(argv[i]);
- printVerbose();
- printAutoUsage();
- }
- }
- }
- char AnyOption::parsePOSIX(char *arg) {
- for (unsigned int i = 0; i < strlen(arg); i++) {
- char ch = arg[i];
- if (matchChar(ch)) {
-
- if (i == strlen(arg) - 1) {
- return ch;
- } else {
- i++;
- while (arg[i] == whitespace || arg[i] == equalsign)
- i++;
- setValue(ch, arg + i);
- return '0';
- }
- }
- }
- printVerbose("Unknown command argument option : ");
- printVerbose(arg);
- printVerbose();
- printAutoUsage();
- return '0';
- }
- int AnyOption::parseGNU(char *arg) {
- size_t split_at = 0;
-
- for (size_t i = 0; i < strlen(arg); i++) {
- if (arg[i] == equalsign) {
- split_at = i;
- i = strlen(arg);
- }
- }
- if (split_at > 0) {
- char *tmp = (char *)malloc((split_at + 1) * sizeof(char));
- for (size_t i = 0; i < split_at; i++)
- tmp[i] = arg[i];
- tmp[split_at] = '\0';
- if (matchOpt(tmp) >= 0) {
- setValue(options[matchOpt(tmp)], arg + split_at + 1);
- free(tmp);
- } else {
- printVerbose("Unknown command argument option : ");
- printVerbose(arg);
- printVerbose();
- printAutoUsage();
- free(tmp);
- return -1;
- }
- } else {
- return matchOpt(arg);
- }
- return -1;
- }
- int AnyOption::matchOpt(char *opt) {
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], opt) == 0) {
- if (optiontype[i] == COMMON_OPT ||
- optiontype[i] == COMMAND_OPT) {
- return i;
- } else if (optiontype[i] == COMMON_FLAG ||
- optiontype[i] == COMMAND_FLAG) {
- setFlagOn(opt);
- return -1;
- }
- }
- }
- printVerbose("Unknown command argument option : ");
- printVerbose(opt);
- printVerbose();
- printAutoUsage();
- return -1;
- }
- bool AnyOption::matchChar(char c) {
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == c) {
- if (optchartype[i] == COMMON_OPT ||
- optchartype[i] ==
- COMMAND_OPT) {
- return true;
- } else if (optchartype[i] == COMMON_FLAG ||
- optchartype[i] ==
- COMMAND_FLAG) {
- setFlagOn(c);
- return false;
- }
- }
- }
- printVerbose("Unknown command argument option : ");
- printVerbose(c);
- printVerbose();
- printAutoUsage();
- return false;
- }
- bool AnyOption::valueStoreOK() {
- if (!set) {
- if (g_value_counter > 0) {
- const int size = g_value_counter * sizeof(char *);
- values = (char **)malloc(size);
- for (int i = 0; i < g_value_counter; i++)
- values[i] = NULL;
- set = true;
- }
- }
- return set;
- }
- char *AnyOption::getValue(const char *option) {
- if (!valueStoreOK())
- return NULL;
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], option) == 0)
- return values[optionindex[i]];
- }
- return NULL;
- }
- bool AnyOption::getFlag(const char *option) {
- if (!valueStoreOK())
- return false;
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], option) == 0)
- return findFlag(values[optionindex[i]]);
- }
- return false;
- }
- char *AnyOption::getValue(char option) {
- if (!valueStoreOK())
- return NULL;
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == option)
- return values[optcharindex[i]];
- }
- return NULL;
- }
- bool AnyOption::getFlag(char option) {
- if (!valueStoreOK())
- return false;
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == option)
- return findFlag(values[optcharindex[i]]);
- }
- return false;
- }
- bool AnyOption::findFlag(char *val) {
- if (val == NULL)
- return false;
- if (strcmp(TRUE_FLAG, val) == 0)
- return true;
- return false;
- }
- bool AnyOption::setValue(const char *option, char *value) {
- if (!valueStoreOK())
- return false;
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], option) == 0) {
- size_t length = (strlen(value) + 1) * sizeof(char);
- allocValues(optionindex[i], length);
- strncpy(values[optionindex[i]], value, length);
- return true;
- }
- }
- return false;
- }
- bool AnyOption::setFlagOn(const char *option) {
- if (!valueStoreOK())
- return false;
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], option) == 0) {
- size_t length = (strlen(TRUE_FLAG) + 1) * sizeof(char);
- allocValues(optionindex[i], length);
- strncpy(values[optionindex[i]], TRUE_FLAG, length);
- return true;
- }
- }
- return false;
- }
- bool AnyOption::setValue(char option, char *value) {
- if (!valueStoreOK())
- return false;
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == option) {
- size_t length = (strlen(value) + 1) * sizeof(char);
- allocValues(optcharindex[i], length);
- strncpy(values[optcharindex[i]], value, length);
- return true;
- }
- }
- return false;
- }
- bool AnyOption::setFlagOn(char option) {
- if (!valueStoreOK())
- return false;
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == option) {
- size_t length = (strlen(TRUE_FLAG) + 1) * sizeof(char);
- allocValues(optcharindex[i], length);
- strncpy(values[optcharindex[i]], TRUE_FLAG, length);
- return true;
- }
- }
- return false;
- }
- int AnyOption::getArgc() const { return new_argc; }
- char *AnyOption::getArgv(int index) const {
- if (index < new_argc) {
- return (argv[new_argv[index]]);
- }
- return NULL;
- }
- bool AnyOption::processFile() {
- if (!(valueStoreOK() && FileSet()))
- return false;
- return hasoptions = (consumeFile(readFile()));
- }
- bool AnyOption::processFile(const char *_filename) {
- useFiileName(_filename);
- return (processFile());
- }
- char *AnyOption::readFile() { return (readFile(filename)); }
- char *AnyOption::readFile(const char *fname) {
- char *buffer;
- ifstream is;
- is.open(fname, ifstream::in);
- if (!is.good()) {
- is.close();
- return NULL;
- }
- is.seekg(0, ios::end);
- size_t length = (size_t)is.tellg();
- is.seekg(0, ios::beg);
- buffer = (char *)malloc((length + 1) * sizeof(char));
- is.read(buffer, length);
- is.close();
- buffer[length] = nullterminate;
- return buffer;
- }
- bool AnyOption::consumeFile(char *buffer) {
- if (buffer == NULL)
- return false;
- char *cursor = buffer;
- char *pline = NULL;
- int linelength = 0;
- bool newline = true;
- for (unsigned int i = 0; i < strlen(buffer); i++) {
- if (*cursor == endofline) {
- if (pline != NULL)
- processLine(pline, linelength);
- pline = NULL;
- newline = true;
- } else if (newline) {
- newline = false;
- if ((*cursor != comment)) {
- pline = cursor;
- linelength = 0;
- }
- }
- cursor++;
- linelength++;
- }
- free(buffer);
- return true;
- }
- void AnyOption::processLine(char *theline, int length) {
- char *pline = (char *)malloc((length + 1) * sizeof(char));
- for (int i = 0; i < length; i++)
- pline[i] = *(theline++);
- pline[length] = nullterminate;
- char *cursor = pline;
- if (*cursor == delimiter || *(cursor + length - 1) == delimiter) {
- justValue(pline);
- } else {
- bool found = false;
- for (int i = 1; i < length - 1 && !found; i++) {
- if (*cursor == delimiter) {
- *(cursor - 1) = nullterminate;
- found = true;
- valuePairs(pline, cursor + 1);
- }
- cursor++;
- }
- cursor++;
- if (!found)
- justValue(pline);
- }
- free(pline);
- }
- char *AnyOption::chomp(char *str) {
- while (*str == whitespace)
- str++;
- char *end = str + strlen(str) - 1;
- while (*end == whitespace)
- end--;
- *(end + 1) = nullterminate;
- return str;
- }
- void AnyOption::valuePairs(char *type, char *value) {
- if (strlen(chomp(type)) == 1) {
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == type[0]) {
- if (optchartype[i] == COMMON_OPT || optchartype[i] == FILE_OPT) {
- setValue(type[0], chomp(value));
- return;
- }
- }
- }
- }
-
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], type) == 0) {
- if (optiontype[i] == COMMON_OPT || optiontype[i] == FILE_OPT) {
- setValue(type, chomp(value));
- return;
- }
- }
- }
- printVerbose("Unknown option in resource file : ");
- printVerbose(type);
- printVerbose();
- }
- void AnyOption::justValue(char *type) {
- if (strlen(chomp(type)) == 1) {
- for (int i = 0; i < optchar_counter; i++) {
- if (optionchars[i] == type[0]) {
- if (optchartype[i] == COMMON_FLAG || optchartype[i] == FILE_FLAG) {
- setFlagOn(type[0]);
- return;
- }
- }
- }
- }
-
- for (int i = 0; i < option_counter; i++) {
- if (strcmp(options[i], type) == 0) {
- if (optiontype[i] == COMMON_FLAG || optiontype[i] == FILE_FLAG) {
- setFlagOn(type);
- return;
- }
- }
- }
- printVerbose("Unknown option in resource file : ");
- printVerbose(type);
- printVerbose();
- }
- void AnyOption::printAutoUsage() {
- if (autousage)
- printUsage();
- }
- void AnyOption::printUsage() {
- if (once) {
- once = false;
- cout << endl;
- for (int i = 0; i < usage_lines; i++)
- cout << usage[i] << endl;
- cout << endl;
- }
- }
- void AnyOption::addUsage(const char *line) {
- if (usage_lines >= max_usage_lines) {
- if (doubleUsageStorage() == false) {
- addUsageError(line);
- exit(1);
- }
- }
- usage[usage_lines] = line;
- usage_lines++;
- }
- void AnyOption::addUsageError(const char *line) {
- cout << endl;
- cout << "OPTIONS ERROR : Failed allocating extra memory " << endl;
- cout << "While adding the usage/help : \"" << line << "\"" << endl;
- cout << "Exiting." << endl;
- cout << endl;
- exit(0);
- }
|