/// <summary> /// Creates a new unit group and adds it to the group table. /// </summary> /// <param name="groupName">Name of the new group.</param> /// <returns>Unit result value.</returns> private UnitResult CreateNewGroup(string groupName) { // Create the new group UnitGroup newgroup = new UnitGroup(); newgroup.Name = groupName; // Add it to the group table m_UnitGroups[groupName] = newgroup; return(UnitResult.NoError); }
/// <summary> /// Parses a group node in the unit XML document. /// </summary> /// <param name="groupnode">The XML node to parse.</param> /// <returns>A unit result value.</returns> private UnitResult ParseGroupXMLNode(string filePath, XmlNode groupnode) { int i = 0; // Check the group has a name. if (groupnode.Attributes["name"] == null) { SendUnitFileWarning("found a group with no name, ignoring group.", filePath, null); return(UnitResult.GenericError); } else { // Create the group. UnitResult res = CreateNewGroup(groupnode.Attributes["name"].Value); // Make sure the group was created. if (res != UnitResult.NoError) { SendUnitFileWarning("failed to create group entry, skipping group.", filePath, null); return(UnitResult.GenericError); } // Get a reference to the group we just created. UnitGroup group = this.m_UnitGroups[groupnode.Attributes["name"].Value]; // Parse all the units. for (i = 0; i < groupnode.ChildNodes.Count; i++) { // Get the node reference for the current unit. XmlNode unitnode = groupnode.ChildNodes[i]; // Ignore comments. if (unitnode.Name.ToLower() == "#comment") { continue; } if (unitnode.Name.ToLower() != "unit") { SendUnitFileWarning("bad tag found while parsing units of group '{0}' (tag was '{1}'), tag ignored.", filePath, new object[] { group.Name, unitnode.Name }); } else { // Parse out the unit res = ParseUnitXMLNode(filePath, group, unitnode); } } } // Completed successfully. return(UnitResult.NoError); }
/// <summary> /// Gets an array of all the groups in the group table. /// </summary> /// <returns>Array of UnitGroup objects representing all of the groups in the group table.</returns> public UnitGroup[] GetAllGroups() { UnitGroup[] unitGroups; // Lock the table (so only we can use it). lock (this.Dictionary.SyncRoot) { unitGroups = new UnitGroup[this.Count]; int i = 0; // Build an array of all the groups in the table. foreach (UnitGroup unitGroup in this.Dictionary.Values) { unitGroups[i] = unitGroup; i++; } } // Return our findings. return(unitGroups); }
/// <summary> /// Adds the named unit to the specified group. /// </summary> /// <param name="unitName">Name of the unit.</param> /// <param name="groupName">Name of the group to add the unit to.</param> /// <returns>Unit result value.</returns> private UnitResult AddUnitToGroup(string unitName, string groupName) { UnitEntry unit = this.m_Units[unitName]; UnitGroup group = this.m_UnitGroups[groupName]; // Make sure the unit exists. if (unit == null) { return(UnitResult.UnitNotFound); } // Make sure the group exists. if (group == null) { return(UnitResult.GroupNotFound); } // Add the unit. group.AddUnit(unit); return(UnitResult.NoError); }
/// <summary> /// Gets an array of all the groups in the group table. /// </summary> /// <returns>Array of UnitGroup objects representing all of the groups in the group table.</returns> public UnitGroup[] GetAllGroups() { UnitGroup[] unitGroups; // Lock the table (so only we can use it). lock (this.Dictionary.SyncRoot) { unitGroups = new UnitGroup[this.Count]; int i = 0; //Build an array of all the groups in the table foreach (UnitGroup unitGroup in this.Dictionary.Values) { unitGroups[i] = unitGroup; i++; } } // Return our findings. return unitGroups; }
/// <summary> /// Parses an XML node that represents a unit. /// </summary> /// <param name="groupName">Name of the group this unit is in.</param> /// <param name="unitnode">The node containing the unit information.</param> /// <returns>A unit result value.</returns> private UnitResult ParseUnitXMLNode(string filePath, UnitGroup group, XmlNode unitnode) { int i = 0; UnitEntry unit = new UnitEntry(); // Make sure the unit has a name. if (unitnode.Attributes["name"] == null) { SendUnitFileWarning("found a unit in group '{0}' with no name, ignored.", filePath, new object[] { group.Name }); return(UnitResult.GenericError); } // Store off the name. unit.Name = unitnode.Attributes["name"].Value; unit.DefaultSymbol = unit.Name.ToLower(); // Don't allow duplicate units. if (GetUnitByName(unit.Name) != null) { SendUnitFileWarning("duplicate unit with name '{0}' was found and ignored.", filePath, new object[] { unit.Name }); return(UnitResult.UnitExists); } // Get every unit property. for (i = 0; i < unitnode.ChildNodes.Count; i++) { XmlNode unitprop = unitnode.ChildNodes[i]; //Ignore comments. if (unitprop.Name.ToLower() == "#comment") { continue; } try { if (unitprop.Name.ToLower() == "multiply") { double x; if (ParseNumberString(unitprop.InnerText, out x) != UnitResult.NoError) { throw new System.Exception(); } unit.Multiplier = x; //unit.m_Multiplier = Convert.ToDouble(unitprop.InnerText); } else { if (unitprop.Name.ToLower() == "add") { unit.Adder = Convert.ToDouble(unitprop.InnerText); } else { if (unitprop.Name.ToLower() == "preadd") { unit.PreAdder = Convert.ToDouble(unitprop.InnerText); } } } } catch { SendUnitFileWarning("unit '{0}' has invalid '{1}' value. Unit skipped.", filePath, new object[] { unit.Name, unitprop.Name }); return(UnitResult.GenericError); } // Parse the symbol properties. if (unitprop.Name.ToLower() == "symbol") { //Put the value into the symbol table if ((unitprop.InnerText != "") && (unitprop.InnerText != null)) { if (this.m_SymbolTable[unitprop.InnerText] != null) { SendUnitFileWarning("while parsing unit '{0}' - a duplicate symbol was found and ignored ({1}).", filePath, new object[] { unit.Name, unitprop.InnerText }); } else { this.m_SymbolTable[unitprop.InnerText] = unit; // Is this unit the default unit? if (unitprop.Attributes["default"] != null) { unit.DefaultSymbol = unitprop.InnerText; } } } else { SendUnitFileWarning("unit '{0}' has an invalid symbol specified, symbol skipped.", filePath, new object[] { unit.Name }); } } } // Add the unit to the unit table. m_Units[unit.Name] = unit; // Add the unit to the group AddUnitToGroup(unit.Name, group.Name); // All done! return(UnitResult.NoError); }
/// <summary> /// Parses an XML node that represents a unit. /// </summary> /// <param name="groupName">Name of the group this unit is in.</param> /// <param name="unitnode">The node containing the unit information.</param> /// <returns>A unit result value.</returns> private UnitResult ParseUnitXMLNode(string filePath, UnitGroup group, XmlNode unitnode) { int i = 0; UnitEntry unit = new UnitEntry(); //Make sure the unit has a name if (unitnode.Attributes["name"] == null) { SendUnitFileWarning("found a unit in group '{0}' with no name, ignored.", filePath, new object[] { group.Name }); return UnitResult.GenericError; } //Store off the name unit.Name = unitnode.Attributes["name"].Value; unit.DefaultSymbol = unit.Name.ToLower(); //Dont allow duplicate units if (GetUnitByName(unit.Name) != null) { SendUnitFileWarning("duplicate unit with name '{0}' was found and ignored.", filePath, new object[] { unit.Name }); return UnitResult.UnitExists; } //Get every unit property for (i = 0; i < unitnode.ChildNodes.Count; i++) { XmlNode unitprop = unitnode.ChildNodes[i]; //Ignore comments. if (unitprop.Name.ToLower() == "#comment") continue; try { if (unitprop.Name.ToLower() == "multiply") { double x; if (ParseNumberString(unitprop.InnerText, out x) != UnitResult.NoError) throw new System.Exception(); unit.Multiplier = x; //unit.m_Multiplier = Convert.ToDouble(unitprop.InnerText); } else if (unitprop.Name.ToLower() == "add") unit.Adder = Convert.ToDouble(unitprop.InnerText); else if (unitprop.Name.ToLower() == "preadd") unit.PreAdder = Convert.ToDouble(unitprop.InnerText); } catch { SendUnitFileWarning("unit '{0}' has invalid '{1}' value. Unit skipped.", filePath, new object[] { unit.Name, unitprop.Name }); return UnitResult.GenericError; } //Parse the symbol properties if (unitprop.Name.ToLower() == "symbol") { //Put the value into the symbol table if ((unitprop.InnerText != "") && (unitprop.InnerText != null)) { if (this.m_SymbolTable[unitprop.InnerText] != null) SendUnitFileWarning("while parsing unit '{0}' - a duplicate symbol was found and ignored ({1}).", filePath, new object[] { unit.Name, unitprop.InnerText }); else { this.m_SymbolTable[unitprop.InnerText] = unit; //Is this unit the default unit? if (unitprop.Attributes["default"] != null) unit.DefaultSymbol = unitprop.InnerText; } } else SendUnitFileWarning("unit '{0}' has an invalid symbol specified, symbol skipped.", filePath, new object[] { unit.Name }); } } //Add the unit to the unit table. this.m_Units[unit.Name] = unit; //Add the unit to the group AddUnitToGroup(unit.Name, group.Name); //All done! return UnitResult.NoError; }
/// <summary> /// Creates a new unit group and adds it to the group table. /// </summary> /// <param name="groupName">Name of the new group.</param> /// <returns>Unit result value.</returns> private UnitResult CreateNewGroup(string groupName) { //Create the new group UnitGroup newgroup = new UnitGroup(); newgroup.Name = groupName; //Add it to the group table this.m_UnitGroups[groupName] = newgroup; return UnitResult.NoError; }