Skip to content
Snippets Groups Projects
Commit 72543e91 authored by Jiří Kalvoda's avatar Jiří Kalvoda
Browse files

ConfigParser: More As... functions

parent 590d0dcb
No related branches found
No related tags found
No related merge requests found
......@@ -222,6 +222,17 @@ class ConfigValue
throw new ConfigParseAttributeException(this, "String is not color");
}
}
public bool AsBool()
{
if(Value == null) return true;
string v = Value.ToLower();
if(v == "true" || v == "yes" || v == "use" || v == "1")
return true;
if(v == "false" || v == "no" || v == "unuse" || v == "0")
return false;
throw new ConfigParseAttributeException(this, "String is not bool");
}
public double AsDouble()
{
try
......@@ -229,8 +240,22 @@ class ConfigValue
return double.Parse(AsString());
} catch (ArgumentException)
{
throw new ConfigParseAttributeException(this, "String is not color");
throw new ConfigParseAttributeException(this, "String is not number");
}
}
public int AsInt()
{
try
{
return int.Parse(AsString());
} catch (ArgumentException)
{
throw new ConfigParseAttributeException(this, "String is not int");
}
}
public int AsMs()
{
return (int)(1000 * AsDouble());
}
public ConfigParser AsConfig()
{
......
......@@ -272,7 +272,7 @@ text = ERR
throw new ConfigMistake(section["name"], "Duplicit i3status name.");
if(section.Optional("global_cache") != null)
{
int cache_ms = (int)(section["global_cache"].AsDouble()*1000);
int cache_ms = section["global_cache"].AsMs();
if(g.cache_ms != null && g.cache_ms != cache_ms)
throw new ConfigMistake(section["global_cache"], "Collision definition of global value.");
g.cache_ms = cache_ms;
......@@ -349,7 +349,7 @@ text = ERR
}
override protected void parseConfig(ConfigParser p)
{
refresh_ms = (int)((p.MainSection["refresh"]?.AsDouble() ?? 1.0)*1000);
refresh_ms = p.MainSection["refresh"]?.AsMs() ?? 1000;
base.parseConfig(p);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment