private static EvalFileHeaderRecord ReadHeader(byte[] bytes, int recordNum) { var pEvalHeader = recordNum * 8; var thisHeader = new EvalFileHeaderRecord { evalHdrNbr = BitConverter.ToUInt16(bytes, pEvalHeader) }; pEvalHeader += 2; thisHeader.numEvals = BitConverter.ToUInt16(bytes, pEvalHeader); pEvalHeader += 2; thisHeader.evalOffset = BitConverter.ToUInt32(bytes, pEvalHeader); pEvalHeader += 4; thisHeader.data = new EvalFileDataRecord[thisHeader.numEvals]; var pEvalData = (int)thisHeader.evalOffset; for (var i = 0; i < thisHeader.numEvals; i++) { var thisData = new EvalFileDataRecord { evalElem = BitConverter.ToInt16(bytes, pEvalData) }; pEvalData += 2; thisData.fragNbr = BitConverter.ToUInt16(bytes, pEvalData); pEvalData += 2; thisHeader.data[i] = thisData; } return(thisHeader); }
public static EvalFile LoadFromXml(string evalXmlFilePath) { var toReturn = new EvalFile(); var headers = new EvalFileHeaderRecord[0]; using (var fs = new FileStream(evalXmlFilePath, FileMode.Open, FileAccess.Read)) using (XmlReader xr = new XmlTextReader(fs)) { var thisHeader = new EvalFileHeaderRecord(); var dataRecords = new EvalFileDataRecord[0]; long thisEvalElement = 0; while (xr.Read()) { if (xr.NodeType == XmlNodeType.Element && xr.Name == "EvalFile") { //string numEvalsString = xr.GetAttribute("numEvals"); //parsed = Int64.TryParse(numEvalsString, out val); //if (parsed) //{ // headers = new EvalFileHeaderRecord[val]; //} //else //{ // throw new IOException(string.Format("Could not parse {0}, bad or missing @numEvals attribute in /EvalFile root element.", evalXmlFilePath)); //} headers = new EvalFileHeaderRecord[0]; } long val; bool parsed; if (xr.NodeType == XmlNodeType.Element && xr.Name == "Eval") { thisHeader = new EvalFileHeaderRecord(); var evalIdString = xr.GetAttribute("id"); parsed = Int64.TryParse(evalIdString, out val); if (parsed) { thisHeader.evalHdrNbr = (ushort)val; } else { throw new IOException( string.Format( "Could not parse {0}, bad or missing @id attribute in /EvalFile/Eval element.", evalXmlFilePath)); } //string numEvalsString = xr.GetAttribute("numElements"); //parsed = Int64.TryParse(numEvalsString, out val); //if (parsed) //{ // dataRecords = new EvalFileDataRecord[val]; // thisEvalElement = 0; //} //else //{ // throw new IOException(string.Format("Could not parse {0}, bad or missing @numElements attribute in /EvalFile/Eval element.", evalXmlFilePath)); //} dataRecords = new EvalFileDataRecord[0]; thisEvalElement = 0; } else if (xr.NodeType == XmlNodeType.Element && xr.Name == "Element") { var thisDataRecord = new EvalFileDataRecord(); var evalElem = xr.GetAttribute("evalElem"); parsed = Int64.TryParse(evalElem, out val); if (parsed) { thisDataRecord.evalElem = (short)val; } else { throw new IOException( string.Format( "Could not parse {0}, bad or missing @evalElem attribute in /EvalFile/Eval/Element element.", evalXmlFilePath)); } var fragNbr = xr.GetAttribute("fragId"); parsed = Int64.TryParse(fragNbr, out val); if (parsed) { thisDataRecord.fragNbr = (ushort)val; } else { throw new IOException( string.Format( "Could not parse {0}, bad or missing @fragId attribute in /EvalFile/Eval/Element element.", evalXmlFilePath)); } if (thisEvalElement > dataRecords.Length - 1) { //throw new IOException(string.Format("Could not parse {0}, number of /EvalFile/Eval/Element elements exceeds @numElements attribute value declared in parent /EvalFile/Eval tag.", evalXmlFilePath)); Array.Resize(ref dataRecords, (int)thisEvalElement + 1); } //else //{ dataRecords[thisEvalElement] = thisDataRecord; thisEvalElement++; //} } else if (xr.NodeType == XmlNodeType.EndElement && xr.Name == "Eval") { thisHeader.data = dataRecords; if (thisHeader.evalHdrNbr > headers.Length - 1) { //throw new IOException(string.Format("Could not parse {0}, @id attribute in /EvalFile/Eval element exceeds (@numEvals-1) attribute value declared in /EvalFile root element.", evalXmlFilePath)); Array.Resize(ref headers, thisHeader.evalHdrNbr + 1); } //else //{ headers[thisHeader.evalHdrNbr] = thisHeader; //} } } } toReturn.Headers = headers; toReturn.FixupOffsets(); return(toReturn); }