Add range checks in XMLReader

This commit is contained in:
Flamefire committed 2014-09-06 12:48:56 +02:00
1 parent 388bd6310f
commit 9f8dfaa50c
2 files changed
+22 -11

No files matched your search

+6
View File
@@ -64,6 +64,12 @@ namespace VocaluxeLib
return (int)result;
}
public static bool InRange<T>(this T val, T min, T max) where T : IComparable<T>
{
Debug.Assert(min.CompareTo(max) <= 0);
return val.CompareTo(min) >= 0 && val.CompareTo(max) <= 0;
}
/// <summary>
/// Makes sure val is between min and max
/// Asserts that min&lt;=max
+16 -11
View File
@@ -88,20 +88,25 @@ namespace VocaluxeLib
return GetValue(xPath, out val, value.ToString()) && CHelper.TryParse(val, out value);
}
/// <summary>
/// Gets a normalized (0&lt;=x&lt;=1) float value
/// </summary>
/// <param name="xPath"></param>
/// <param name="value"></param>
/// <returns>True if exists and is normalized</returns>
public bool TryGetNormalizedFloatValue(string xPath, ref float value)
{
string val;
return GetValue(xPath, out val, value.ToString()) && CHelper.TryParse(val, out value) && value.InRange(0f, 1f);
}
public bool TryGetColorFromRGBA(string xPath, ref SColorF value)
{
bool result = true;
result &= TryGetFloatValue(xPath + "/R", ref value.R);
result &= TryGetFloatValue(xPath + "/G", ref value.G);
result &= TryGetFloatValue(xPath + "/B", ref value.B);
result &= TryGetFloatValue(xPath + "/A", ref value.A);
if (result)
{
value.R = value.R.Clamp(0f, 1f);
value.G = value.G.Clamp(0f, 1f);
value.B = value.B.Clamp(0f, 1f);
value.A = value.A.Clamp(0f, 1f);
}
result &= TryGetNormalizedFloatValue(xPath + "/R", ref value.R);
result &= TryGetNormalizedFloatValue(xPath + "/G", ref value.G);
result &= TryGetNormalizedFloatValue(xPath + "/B", ref value.B);
result &= TryGetNormalizedFloatValue(xPath + "/A", ref value.A);
return result;
}