diff options
author | 2015-04-22 11:30:19 +0200 | |
---|---|---|
committer | 2015-04-22 11:30:45 +0200 | |
commit | 54f8b3ae5df6736c58b2a8398f9ac85da425c96d (patch) | |
tree | 5924033acf314625b14317b674adc9e9730c97cc /src/lib/util/options.c | |
parent | 946958136d85f2a1485dfb962487cba43ac6c048 (diff) |
moved all to std::string (nw)
Diffstat (limited to 'src/lib/util/options.c')
-rw-r--r-- | src/lib/util/options.c | 104 |
1 files changed, 53 insertions, 51 deletions
diff --git a/src/lib/util/options.c b/src/lib/util/options.c index 3ba8956eb3f..d229624017a 100644 --- a/src/lib/util/options.c +++ b/src/lib/util/options.c @@ -13,7 +13,8 @@ #include <ctype.h> #include <assert.h> #include "options.h" -#include "astring.h" +#include "corestr.h" +#include <string> @@ -63,24 +64,24 @@ core_options::entry::entry(const char *name, const char *description, UINT32 fla if (name != NULL) { // first extract any range - astring namestr(name); - int lparen = namestr.chr(0, '('); - int dash = namestr.chr(lparen + 1, '-'); - int rparen = namestr.chr(dash + 1, ')'); + std::string namestr(name); + int lparen = namestr.find_first_of('(',0); + int dash = namestr.find_first_of('-',lparen + 1); + int rparen = namestr.find_first_of(')',dash + 1); if (lparen != -1 && dash != -1 && rparen != -1) { - m_minimum.cpysubstr(namestr, lparen + 1, dash - (lparen + 1)).trimspace(); - m_maximum.cpysubstr(namestr, dash + 1, rparen - (dash + 1)).trimspace(); - namestr.del(lparen, rparen + 1 - lparen); + strtrimspace(m_minimum.assign(namestr.substr(lparen + 1, dash - (lparen + 1)))); + strtrimspace(m_maximum.assign(namestr.substr(dash + 1, rparen - (dash + 1)))); + namestr.erase(lparen, rparen + 1 - lparen); } // then chop up any semicolon-separated names int semi; int nameindex = 0; - while ((semi = namestr.chr(0, ';')) != -1 && nameindex < ARRAY_LENGTH(m_name)) + while ((semi = namestr.find_first_of(';')) != -1 && nameindex < ARRAY_LENGTH(m_name)) { - m_name[nameindex++].cpysubstr(namestr, 0, semi); - namestr.del(0, semi + 1); + m_name[nameindex++].assign(namestr.substr(0, semi)); + namestr.erase(0, semi + 1); } // finally add the last item @@ -332,11 +333,11 @@ void core_options::set_description(const char *name, const char *description) // command line arguments //------------------------------------------------- -bool core_options::parse_command_line(int argc, char **argv, int priority, astring &error_string) +bool core_options::parse_command_line(int argc, char **argv, int priority, std::string &error_string) { // reset the errors and the command - error_string.reset(); - m_command.reset(); + error_string.clear(); + m_command.clear(); // iterate through arguments int unadorned_index = 0; @@ -352,7 +353,7 @@ bool core_options::parse_command_line(int argc, char **argv, int priority, astri entry *curentry = m_entrymap.find(optionname); if (curentry == NULL) { - error_string.catprintf("Error: unknown option: %s\n", curarg); + strcatprintf(error_string, "Error: unknown option: %s\n", curarg); retval = false; if (!is_unadorned) arg++; continue; @@ -364,7 +365,7 @@ bool core_options::parse_command_line(int argc, char **argv, int priority, astri // can only have one command if (!m_command.empty()) { - error_string.catprintf("Error: multiple commands specified -%s and %s\n", m_command.c_str(), curarg); + strcatprintf(error_string,"Error: multiple commands specified -%s and %s\n", m_command.c_str(), curarg); return false; } m_command = curentry->name(); @@ -381,7 +382,7 @@ bool core_options::parse_command_line(int argc, char **argv, int priority, astri newdata = argv[++arg]; else { - error_string.catprintf("Error: option %s expected a parameter\n", curarg); + strcatprintf(error_string, "Error: option %s expected a parameter\n", curarg); return false; } @@ -397,7 +398,7 @@ bool core_options::parse_command_line(int argc, char **argv, int priority, astri // an INI file //------------------------------------------------- -bool core_options::parse_ini_file(core_file &inifile, int priority, int ignore_priority, astring &error_string) +bool core_options::parse_ini_file(core_file &inifile, int priority, int ignore_priority, std::string &error_string) { // loop over lines in the file char buffer[4096]; @@ -422,7 +423,7 @@ bool core_options::parse_ini_file(core_file &inifile, int priority, int ignore_p // if we hit the end early, print a warning and continue if (*temp == 0) { - error_string.catprintf("Warning: invalid line in INI: %s", buffer); + strcatprintf(error_string,"Warning: invalid line in INI: %s", buffer); continue; } @@ -446,7 +447,7 @@ bool core_options::parse_ini_file(core_file &inifile, int priority, int ignore_p if (curentry == NULL) { if (priority >= ignore_priority) - error_string.catprintf("Warning: unknown option in INI: %s\n", optionname); + strcatprintf(error_string, "Warning: unknown option in INI: %s\n", optionname); continue; } @@ -476,10 +477,10 @@ void core_options::revert(int priority) // the optional diff //------------------------------------------------- -const char *core_options::output_ini(astring &buffer, const core_options *diff) +const char *core_options::output_ini(std::string &buffer, const core_options *diff) { // INI files are complete, so always start with a blank buffer - buffer.reset(); + buffer.clear(); int num_valid_headers = 0; int unadorned_index = 0; @@ -515,8 +516,8 @@ const char *core_options::output_ini(astring &buffer, const core_options *diff) if (last_header != NULL) { if (num_valid_headers++) - buffer.catprintf("\n"); - buffer.catprintf("#\n# %s\n#\n", last_header); + strcatprintf(buffer,"\n"); + strcatprintf(buffer, "#\n# %s\n#\n", last_header); last_header = NULL; } @@ -524,9 +525,9 @@ const char *core_options::output_ini(astring &buffer, const core_options *diff) if (!is_unadorned) { if (strchr(value, ' ') != NULL) - buffer.catprintf("%-25s \"%s\"\n", name, value); + strcatprintf(buffer,"%-25s \"%s\"\n", name, value); else - buffer.catprintf("%-25s %s\n", name, value); + strcatprintf(buffer,"%-25s %s\n", name, value); } } } @@ -540,21 +541,21 @@ const char *core_options::output_ini(astring &buffer, const core_options *diff) // output_help - output option help to a string //------------------------------------------------- -const char *core_options::output_help(astring &buffer) +const char *core_options::output_help(std::string &buffer) { // start empty - buffer.reset(); + buffer.clear(); // loop over all items for (entry *curentry = m_entrylist.first(); curentry != NULL; curentry = curentry->next()) { // header: just print if (curentry->is_header()) - buffer.catprintf("\n#\n# %s\n#\n", curentry->description()); + strcatprintf(buffer,"\n#\n# %s\n#\n", curentry->description()); // otherwise, output entries for all non-deprecated items else if (curentry->description() != NULL) - buffer.catprintf("-%-20s%s\n", curentry->name(), curentry->description()); + strcatprintf(buffer,"-%-20s%s\n", curentry->name(), curentry->description()); } return buffer.c_str(); } @@ -617,13 +618,13 @@ bool core_options::exists(const char *name) const // set_value - set the raw option value //------------------------------------------------- -bool core_options::set_value(const char *name, const char *value, int priority, astring &error_string) +bool core_options::set_value(const char *name, const char *value, int priority, std::string &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); + strcatprintf(error_string, "Attempted to set unknown option %s\n", name); return false; } @@ -631,17 +632,17 @@ bool core_options::set_value(const char *name, const char *value, int priority, return validate_and_set_data(*curentry, value, priority, error_string); } -bool core_options::set_value(const char *name, int value, int priority, astring &error_string) +bool core_options::set_value(const char *name, int value, int priority, std::string &error_string) { - astring tempstr; - tempstr.printf("%d", value); + std::string tempstr; + strprintf(tempstr,"%d", value); return set_value(name, tempstr.c_str(), priority, error_string); } -bool core_options::set_value(const char *name, float value, int priority, astring &error_string) +bool core_options::set_value(const char *name, float value, int priority, std::string &error_string) { - astring tempstr; - tempstr.printf("%f", value); + std::string tempstr; + strprintf(tempstr, "%f", value); return set_value(name, tempstr.c_str(), priority, error_string); } @@ -687,7 +688,7 @@ void core_options::append_entry(core_options::entry &newentry) // for boolean options add a "no" variant as well if (newentry.type() == OPTION_BOOLEAN) - m_entrymap.add(astring("no").cat(newentry.name(name)).c_str(), &newentry); + m_entrymap.add(std::string("no").append(newentry.name(name)).c_str(), &newentry); } } @@ -730,17 +731,18 @@ void core_options::copyfrom(const core_options &src) // then set it //------------------------------------------------- -bool core_options::validate_and_set_data(core_options::entry &curentry, const char *newdata, int priority, astring &error_string) +bool core_options::validate_and_set_data(core_options::entry &curentry, const char *newdata, int priority, std::string &error_string) { // trim any whitespace - astring data(newdata); - data.trimspace(); + std::string data(newdata); + strtrimspace(data); + // trim quotes - if (data.chr(0, '"') == 0 && data.rchr(0, '"') == data.len() - 1) + if (data.find_first_of('"') == 0 && data.find_last_of('"') == data.length() - 1) { - data.del(0, 1); - data.del(data.len() - 1, 1); + data.erase(0, 1); + data.erase(data.length() - 1, 1); } // validate the type of data and optionally the range @@ -752,7 +754,7 @@ bool core_options::validate_and_set_data(core_options::entry &curentry, const ch case OPTION_BOOLEAN: if (sscanf(data.c_str(), "%d", &ival) != 1 || ival < 0 || ival > 1) { - error_string.catprintf("Illegal boolean value for %s: \"%s\"; reverting to %s\n", curentry.name(), data.c_str(), curentry.value()); + strcatprintf(error_string, "Illegal boolean value for %s: \"%s\"; reverting to %s\n", curentry.name(), data.c_str(), curentry.value()); return false; } break; @@ -761,12 +763,12 @@ bool core_options::validate_and_set_data(core_options::entry &curentry, const ch case OPTION_INTEGER: if (sscanf(data.c_str(), "%d", &ival) != 1) { - error_string.catprintf("Illegal integer value for %s: \"%s\"; reverting to %s\n", curentry.name(), data.c_str(), curentry.value()); + strcatprintf(error_string, "Illegal integer value for %s: \"%s\"; reverting to %s\n", curentry.name(), data.c_str(), curentry.value()); return false; } if (curentry.has_range() && (ival < atoi(curentry.minimum()) || ival > atoi(curentry.maximum()))) { - error_string.catprintf("Out-of-range integer value for %s: \"%s\" (must be between %s and %s); reverting to %s\n", curentry.name(), data.c_str(), curentry.minimum(), curentry.maximum(), curentry.value()); + strcatprintf(error_string, "Out-of-range integer value for %s: \"%s\" (must be between %s and %s); reverting to %s\n", curentry.name(), data.c_str(), curentry.minimum(), curentry.maximum(), curentry.value()); return false; } break; @@ -775,12 +777,12 @@ bool core_options::validate_and_set_data(core_options::entry &curentry, const ch case OPTION_FLOAT: if (sscanf(data.c_str(), "%f", &fval) != 1) { - error_string.catprintf("Illegal float value for %s: \"%s\"; reverting to %s\n", curentry.name(), data.c_str(), curentry.value()); + strcatprintf(error_string, "Illegal float value for %s: \"%s\"; reverting to %s\n", curentry.name(), data.c_str(), curentry.value()); return false; } if (curentry.has_range() && (fval < atof(curentry.minimum()) || fval > atof(curentry.maximum()))) { - error_string.catprintf("Out-of-range float value for %s: \"%s\" (must be between %s and %s); reverting to %s\n", curentry.name(), data.c_str(), curentry.minimum(), curentry.maximum(), curentry.value()); + strcatprintf(error_string, "Out-of-range float value for %s: \"%s\" (must be between %s and %s); reverting to %s\n", curentry.name(), data.c_str(), curentry.minimum(), curentry.maximum(), curentry.value()); return false; } break; @@ -793,7 +795,7 @@ bool core_options::validate_and_set_data(core_options::entry &curentry, const ch case OPTION_INVALID: case OPTION_HEADER: default: - error_string.catprintf("Attempted to set invalid option %s\n", curentry.name()); + strcatprintf(error_string, "Attempted to set invalid option %s\n", curentry.name()); return false; } |