Mercurial > hg > octave-nkf
changeset 13236:8bb526fb3349
allow radio values for graphics properaties to be abbreviated
* grahpics.h.in (radio_values::validate): New argument to return
matched value.
(radio_values::contains (const std::string&)): New argument to return
matched value.
(radio_property::do_set): Accept abbreviated value names but set to
full name of one of the possible values if a match is found.
* graphics.cc (color_property::do_set): Likewise.
(double_radio_property::do_set): Likewise.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 27 Sep 2011 02:06:41 -0400 |
parents | 7b3afe09680b |
children | 1bfca2bbea8b |
files | src/graphics.cc src/graphics.h.in |
diffstat | 2 files changed, 57 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/src/graphics.cc +++ b/src/graphics.cc @@ -926,11 +926,13 @@ if (! s.empty ()) { - if (radio_val.contains (s)) + std::string match; + + if (radio_val.contains (s, match)) { - if (current_type != radio_t || current_val != s) + if (current_type != radio_t || match != current_val) { - current_val = s; + current_val = match; current_type = radio_t; return true; } @@ -990,10 +992,11 @@ if (val.is_string ()) { std::string s = val.string_value (); - - if (! s.empty () && radio_val.contains (s)) - { - if (current_type != radio_t || s != current_val) + std::string match; + + if (! s.empty () && radio_val.contains (s, match)) + { + if (current_type != radio_t || match != current_val) { current_val = s; current_type = radio_t;
--- a/src/graphics.h.in +++ b/src/graphics.h.in @@ -886,11 +886,11 @@ std::string default_value (void) const { return default_val; } - bool validate (const std::string& val) + bool validate (const std::string& val, std::string& match) { bool retval = true; - if (! contains (val)) + if (! contains (val, match)) { error ("invalid value = %s", val.c_str ()); retval = false; @@ -899,9 +899,46 @@ return retval; } - bool contains (const std::string& val) + bool contains (const std::string& val, std::string& match) { - return (possible_vals.find (val) != possible_vals.end ()); + size_t k = 0; + + size_t len = val.length (); + + std::string first_match; + + for (std::set<caseless_str>::const_iterator p = possible_vals.begin (); + p != possible_vals.end (); p++) + { + if (p->compare (val, len)) + { + if (len == p->length ()) + { + // We found a full match (consider the case of val == + // "replace" with possible values "replace" and + // "replacechildren"). Any other matches are + // irrelevant, so set match and return now. + + match = *p; + return true; + } + else + { + if (k == 0) + first_match = *p; + + k++; + } + } + } + + if (k == 1) + { + match = first_match; + return true; + } + else + return false; } std::string values_as_string (void) const; @@ -964,11 +1001,14 @@ if (newval.is_string ()) { std::string s = newval.string_value (); - if (vals.validate (s)) + + std::string match; + + if (vals.validate (s, match)) { - if (s != current_val) + if (match != current_val) { - current_val = s; + current_val = match; return true; } }