bool core_options::parse_command_line(int argc, char **argv, int priority, astring &error_string) { // reset the errors and the command error_string.reset(); m_command.reset(); // iterate through arguments int unadorned_index = 0; bool retVal = true; for (int arg = 1; arg < argc; arg++) { // determine the entry name to search for const char *curarg = argv[arg]; bool is_unadorned = (curarg[0] != '-'); const char *optionname = is_unadorned ? core_options::unadorned(unadorned_index++) : &curarg[1]; // find our entry; if not found, indicate invalid option entry *curentry = m_entrymap.find(optionname); if (curentry == NULL) { error_string.catprintf("Error: unknown option: %s\n", curarg); retVal = false; if (!is_unadorned) arg++; continue; } // process commands first if (curentry->type() == OPTION_COMMAND) { // can only have one command if (m_command) { error_string.catprintf("Error: multiple commands specified -%s and %s\n", m_command.cstr(), curarg); return false; } m_command = curentry->name(); continue; } // get the data for this argument, special casing booleans const char *newdata; if (curentry->type() == OPTION_BOOLEAN) newdata = (strncmp(&curarg[1], "no", 2) == 0) ? "0" : "1"; else if (is_unadorned) newdata = curarg; else if (arg + 1 < argc) newdata = argv[++arg]; else { error_string.catprintf("Error: option %s expected a parameter\n", curarg); return false; } // set the new data validate_and_set_data(*curentry, newdata, priority, error_string); } return retVal; }
bool core_options::parse_ini_file(core_file &inifile, int priority, int ignore_priority, astring &error_string) { // loop over lines in the file char buffer[4096]; while (core_fgets(buffer, ARRAY_LENGTH(buffer), &inifile) != NULL) { // find the extent of the name char *optionname; for (optionname = buffer; *optionname != 0; optionname++) if (!isspace((UINT8)*optionname)) break; // skip comments if (*optionname == 0 || *optionname == '#') continue; // scan forward to find the first space char *temp; for (temp = optionname; *temp != 0; temp++) if (isspace((UINT8)*temp)) break; // if we hit the end early, print a warning and continue if (*temp == 0) { error_string.catprintf("Warning: invalid line in INI: %s", buffer); continue; } // NULL-terminate *temp++ = 0; char *optiondata = temp; // scan the data, stopping when we hit a comment bool inquotes = false; for (temp = optiondata; *temp != 0; temp++) { if (*temp == '"') inquotes = !inquotes; if (*temp == '#' && !inquotes) break; } *temp = 0; // find our entry entry *curentry = m_entrymap.find(optionname); if (curentry == NULL) { if (priority >= ignore_priority) error_string.catprintf("Warning: unknown option in INI: %s\n", optionname); continue; } // set the new data validate_and_set_data(*curentry, optiondata, priority, error_string); } return true; }
bool core_options::set_value(const char *name, const char *value, int priority, astring &error_string) { // find the entry first entry *curentry = m_entrymap.find(name); if (curentry == NULL) { error_string.catprintf("Attempted to set unknown option %s\n", name); return false; } // validate and set the item normally return validate_and_set_data(*curentry, value, priority, error_string); }
bool core_options::set_value(const char *name, const char *value, int priority, std::string &error_string) { // find the entry first auto curentry = m_entrymap.find(name); if (curentry == m_entrymap.end()) { strcatprintf(error_string, "Attempted to set unknown option %s\n", name); return false; } // validate and set the item normally return validate_and_set_data(*curentry->second, value, priority, error_string); }