public void SetConstant(string ConstantName, string ConstantValue) { // ------------------------------------- // Set a given constant's value. // ------------------------------------- foreach (APSIMConstant c in _Constants) { if (StringManip.StringsAreEqual(c.Name, ConstantName)) { c.Value = ConstantValue; } } }
public APSIMConstant Constant(string ConstantName) { // ------------------------------------- // Return a given constant to caller // ------------------------------------- foreach (APSIMConstant c in _Constants) { if (StringManip.StringsAreEqual(c.Name, ConstantName)) { return(c); } } return(null); }
int IComparer.Compare(object x, object y) { XmlNode Data1 = (XmlNode)x; XmlNode Data2 = (XmlNode)y; string ModuleName1 = Path.GetFileNameWithoutExtension(XmlHelper.Attribute(Data1, "executable")).ToLower(); string ModuleName2 = Path.GetFileNameWithoutExtension(XmlHelper.Attribute(Data2, "executable")).ToLower(); bool ambiguous = false; if (x == y) { return(0); } if (Data1.Name == "executable") { return(-1); } if (Data2.Name == "executable") { return(1); } if (ModuleName1 == ModuleName2) { int ChildIndex1 = Array.IndexOf(XmlHelper.ChildNames(Data1.ParentNode, ""), XmlHelper.Name(Data1)); int ChildIndex2 = Array.IndexOf(XmlHelper.ChildNames(Data2.ParentNode, ""), XmlHelper.Name(Data2)); if (ChildIndex1 < ChildIndex2) { return(-1); } else if (ChildIndex2 > ChildIndex1) { return(1); } else { ambiguous = true; } } if (XmlHelper.Type(Data1) == "title") { return(-1); } if (!ambiguous) { for (int i = 0; i != Components.Count; i++) { if (StringManip.StringsAreEqual(Components[i], ModuleName1)) { return(-1); } if (StringManip.StringsAreEqual(Components[i], ModuleName2)) { return(1); } } } // Neither are in list so keep original order intact i.e. Node1 comes before Node2!! // Find the relative positions of data1 and data2 in the parent list. int Data1Pos = 0; int Data2Pos = 0; for (int i = 0; i != Data1.ParentNode.ChildNodes.Count; i++) { if (Data1.ParentNode.ChildNodes[i] == Data1) { Data1Pos = i; } if (Data1.ParentNode.ChildNodes[i] == Data2) { Data2Pos = i; } } if (Data1Pos < Data2Pos) { return(-1); } else { return(1); } }
//--------------------------------------------------------------- // Evaluate the specified IF macro. Return true if it equates // to true. //--------------------------------------------------------------- bool EvaluateIf(string IfMacro) { StringCollection s = StringManip.SplitStringHonouringQuotes(IfMacro, " "); if (s.Count == 0) { return(false); } if (s.Count == 1 && s[0] == "\"\"") { return(false); } if (s.Count == 1) { return(s[0].IndexOf('[') == -1 || s[0].IndexOf(']') == -1); } if (s.Count == 2) { return(true); } // if (s.Count != 3) // throw new Exception("Badly formatted if statement: " + IfMacro); char[] operators = { '<', '>' }; string lhs = s[0]; string op = s[1]; string rhs = s[2]; lhs.Trim(); rhs.Trim(); if (op == "=") { if (rhs == "\"\"") { return(lhs.IndexOf('[') != -1 && lhs.IndexOf(']') != -1); } return(StringManip.StringsAreEqual(lhs, rhs)); } else if (op == "<>") { return(!StringManip.StringsAreEqual(lhs, rhs)); } else if (op == "!=") { return(!StringManip.StringsAreEqual(lhs, rhs)); } else if (op.IndexOfAny(operators) == -1) { return(s.Count >= 1); } else { double lhsValue, rhsValue; try { lhsValue = Convert.ToDouble(lhs); rhsValue = Convert.ToDouble(rhs); } catch (Exception) { return(false); } if (op == "<") { return(lhsValue < rhsValue); } else if (op == "<=") { return(lhsValue <= rhsValue); } else if (op == ">") { return(lhsValue > rhsValue); } else if (op == ">=") { return(lhsValue >= rhsValue); } else { throw new Exception("Unknown if macro operator: " + op); } } }