From cb15006f5375054829e6d2c8f8d001bf4b425cd8 Mon Sep 17 00:00:00 2001 From: mfranz Date: Thu, 28 Jun 2007 11:26:40 +0000 Subject: [PATCH] allow to specify a node type: --prop:[type:]name=value where type is one of string (default), double, float, long, int, bool Using only the first letter works, too. --prop:foo=123 ... sets property foo to string 123 (old behavior) --prop:string:foo=123 ... verbose version of above --prop:s:foo=123 ... slightly less verbose version of above --prop:bool:foo=1 ... makes property a bool of value 'true' I hope this isn't considered a silly gimmick. I need this often and maybe other developers do, too. It's useful in cases where the difference really matters. if (getprop("/foo")), for instance, is TRUE even when the property contains string "false", as all strings are TRUE. --- src/Main/options.cxx | 50 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Main/options.cxx b/src/Main/options.cxx index 37f2d4630..a32745ce3 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -1398,6 +1398,46 @@ struct OptionDesc { }; +// Set a property for the --prop: option. Syntax: --prop:[:]= +// can be "double" etc. but also only the first letter "d". +// Examples: --prop:alpha=1 --prop:bool:beta=true --prop:d:gamma=0.123 +static bool +set_property(const string& arg) +{ + string::size_type pos = arg.find('='); + if (pos == arg.npos || pos == 0 || pos + 1 == arg.size()) + return false; + + string name = arg.substr(0, pos); + string value = arg.substr(pos + 1); + string type = "string"; + pos = name.find(':'); + if (pos != name.npos && pos != 0 && pos + 1 != name.size()) { + type = name.substr(0, pos); + name = name.substr(pos + 1); + } + if (type == "s" || type == "string") { + fgSetString(name.c_str(), value.c_str()); + return true; + } + if (type == "d" || type == "double") + fgSetDouble(name.c_str(), 0.0); + else if (type == "f" || type == "float") + fgSetFloat(name.c_str(), 0.0f); + else if (type == "l" || type == "long") + fgSetLong(name.c_str(), 0L); + else if (type == "i" || type == "int") + fgSetInt(name.c_str(), 0); + else if (type == "b" || type == "bool") + fgSetBool(name.c_str(), false); + else + return false; + + fgSetString(name.c_str(), value.c_str()); + return true; +} + + // Parse a single option static int parse_option (const string& arg) @@ -1422,15 +1462,9 @@ parse_option (const string& arg) } else if ( arg.find( "--show-aircraft") == 0) { return(FG_OPTIONS_SHOW_AIRCRAFT); } else if ( arg.find( "--prop:" ) == 0 ) { - string assign = arg.substr(7); - string::size_type pos = assign.find('='); - if ( pos == arg.npos || pos == 0 || pos + 1 == assign.size() ) { - SG_LOG( SG_GENERAL, SG_ALERT, "Bad property assignment: " << arg ); + if (!set_property(arg.substr(7))) { + SG_LOG( SG_GENERAL, SG_ALERT, "Bad property assignment: " << arg ); return FG_OPTIONS_ERROR; - } else { - string name = assign.substr(0, pos); - string value = assign.substr(pos + 1); - fgSetString(name.c_str(), value.c_str()); } } else if ( arg.find( "--" ) == 0 ) { size_t pos = arg.find( '=' ); -- 2.39.5