private void ReadStructures(string[] sFile) { bool bStartFound = false; bool bInHashIf = false; bool bInComment = false; bool bSkipUnionExtraLines = false; bool bFirstLineOfUnion = false; int iUnionRef = 0; int iOpenStructureCount = 0; stTableStructure stCurrentTable = new stTableStructure(); string sTestBuffer = ""; bStartFound = false; for (int i = 0; i < sFile.Count(); i++) { string sLine = sFile[i].Replace('\t', ' '); string sStructureName; if (sLine.Contains("/*")) { bInComment = true; } sTestBuffer += sLine + "\r\n"; if (bInComment == false) { if (sLine.Trim().ToUpper().StartsWith("union".ToUpper())) { // oh no bSkipUnionExtraLines = true; iUnionRef = iOpenStructureCount; bFirstLineOfUnion = true; } if (sLine.ToUpper().Contains("typedef struct".ToUpper()) || sLine.StartsWith ("struct")==true ) { bStartFound = true; bInHashIf = false; stCurrentTable = new stTableStructure(); stCurrentTable.sFields = new List<string>(); stCurrentTable.sFieldsType = new List<string>(); stCurrentTable.sDescription = new List<string>(); stCurrentTable.bBitField = false; if (sLine.Trim().EndsWith("{")) { iOpenStructureCount++; } if (sLine.StartsWith("struct") == true) { stCurrentTable.sStructureName = sLine.Replace("{", "").Trim(); } } if (bStartFound) { int iStartPos = sLine.IndexOf("sizeof("); int iEndPos = 0; if (sLine.Trim().StartsWith("{")) { iOpenStructureCount++; } if (sLine.Trim().StartsWith("}")) { iOpenStructureCount--; if (iOpenStructureCount == iUnionRef) { bSkipUnionExtraLines = false; } else { bFirstLineOfUnion = false; } if (iOpenStructureCount == 0) { iEndPos = sLine.Trim().IndexOf(";"); sStructureName = sLine.Trim().Substring(1, iEndPos - 1).Trim(); if (sStructureName.Length > 0) { stCurrentTable.sStructureName = sStructureName; } m_TableStructures.Add(stCurrentTable); bStartFound = false; sTestBuffer = ""; } } else { if (sLine.ToUpper().Contains("#if".ToUpper())) { string[] sTokens = sLine.Replace("\r", "").Split(' '); sTokens[1] = ReplaceDefines(sTokens[1]); sTokens[1] = ReplaceDefines(sTokens[1]); NCalc.Expression a = new NCalc.Expression(sTokens[1]);// TODO: make better int t; try { t = Convert.ToInt32(a.Evaluate()); } catch (Exception ex) { t = 1; } if (t == 0) { bInHashIf = true; } } // just ignore #else variable for now if (sLine.ToUpper().Contains("#else".ToUpper()) || sLine.ToUpper().Contains("#elif".ToUpper())) //#elif { bInHashIf = true; } if (sLine.ToUpper().Contains("#endif".ToUpper())) { bInHashIf = false; } if (sLine.Contains(";") && bInHashIf == false && sLine.Trim().Contains(" ") && sLine.Trim().StartsWith("//") == false && (bSkipUnionExtraLines == false || (bSkipUnionExtraLines == true && bFirstLineOfUnion == true))) { if (iOpenStructureCount == iUnionRef+1) { bFirstLineOfUnion = false; } // unsigned char hour; int iSemiColonPos = sLine.IndexOf(';'); int iSpacePos = sLine.LastIndexOf(' ', iSemiColonPos); int iCommandPos = sLine.LastIndexOf("//"); if (iCommandPos > 0) { stCurrentTable.sDescription.Add(sLine.Substring(iCommandPos + 2).Replace("\r", "")); } else { stCurrentTable.sDescription.Add(""); } string sFieldType = GetVaribleType(sLine); if (sLine.Substring(iSpacePos - 1, iSemiColonPos - iSpacePos).Trim().Contains(":")) { // contains bit field :( stCurrentTable.bBitField = true; stCurrentTable.sFields.Add(sLine); stCurrentTable.sFieldsType.Add(sLine); } else { stCurrentTable.sFields.Add(sLine.Substring(iSpacePos, iSemiColonPos - iSpacePos).Trim()); stCurrentTable.sFieldsType.Add(sLine.Substring(0, iSpacePos).Trim()); } } } } } if (sLine.Contains("*/")) { bInComment = false; } } }
/// <summary> /// Loads a previously saved xml configuration that describes all the tables in a meter /// </summary> /// <param name="sFileName"></param> public void LoadXML(string sFileName) { XmlDocument docXML = new XmlDocument(); docXML.Load(sFileName); m_Defines.Clear(); foreach (XmlNode node in docXML.SelectNodes("OpticalInterface/Defines/Define")) { stDefine newDefine = new stDefine(); newDefine.sName = node.SelectSingleNode("Name").InnerText; newDefine.iValue = int.Parse(node.SelectSingleNode("Value").InnerText); m_Defines.Add(newDefine); } m_AllTablesID.Clear(); foreach (XmlNode node in docXML.SelectNodes("OpticalInterface/AllTables/Table")) { stTableID newTable = new stTableID(); newTable.sName = node.SelectSingleNode("Name").InnerText; newTable.iValue = int.Parse(node.SelectSingleNode("Value").InnerText); m_AllTablesID.Add(newTable); } m_SupportedTables.Clear(); foreach (XmlNode node in docXML.SelectNodes("OpticalInterface/SupportedTables/SupportedTable")) { stTable newTable = new stTable(); newTable.sName = node.SelectSingleNode("Name").InnerText; newTable.sStructure = node.SelectSingleNode("StructureName").InnerText; m_SupportedTables.Add(newTable); } m_TableStructures.Clear(); foreach (XmlNode node in docXML.SelectNodes("OpticalInterface/Tables/Table")) { stTableStructure newTable = new stTableStructure(); newTable.sFields = new List<string>(); newTable.sFieldsType = new List<string>(); newTable.sDescription = new List<string>(); newTable.sStructureName = node.SelectSingleNode("Name").InnerText; newTable.bBitField = bool.Parse(node.SelectSingleNode("BitField").InnerText); foreach (XmlNode fieldnode in node.SelectNodes("Fields/Field")) { newTable.sFields.Add(fieldnode.SelectSingleNode("Name").InnerText); newTable.sFieldsType.Add(fieldnode.SelectSingleNode("Type").InnerText); newTable.sDescription.Add(fieldnode.SelectSingleNode("Description").InnerText); } m_TableStructures.Add(newTable); } }