public static EvalFile LoadFromBinary(string evalFilePath) { var fi = new FileInfo(evalFilePath); if (!fi.Exists) { throw new FileNotFoundException(evalFilePath); } var evalFile = new EvalFile(); var bytes = new byte[fi.Length]; using (var fs = new FileStream(evalFilePath, FileMode.Open, FileAccess.Read)) { fs.Seek(0, SeekOrigin.Begin); fs.Read(bytes, 0, (int)fi.Length); } var fileLen = bytes.Length; var thisHeader = ReadHeader(bytes, 0); var numEvals = thisHeader.evalOffset / 8; evalFile.Headers = new EvalFileHeaderRecord[numEvals]; evalFile.Headers[0] = thisHeader; for (var i = 1; i < numEvals; i++) { evalFile.Headers[i] = ReadHeader(bytes, i); } return(evalFile); }
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); }