/// <summary> /// Processes a line from the input file, entering the data into the holdingdate objects passed. /// </summary> /// <param name="toProcess">String to process</param> /// <param name="holdingDate">CHoldingDate for the data</param> static void ProcessLine(String toProcess, CHoldingDate holdingDate) { if (_reportParser == null) { // Using a regex, as the data seems to move vs the headings. (Normally its better to parse the headings to find the data.) _reportParser = new Regex(@"^ (?<securityName>.{28})\s (?<securityType>.{16})\s* (?<cusip>[A-Z0-9 ]{11})\s+ (?<mktval>\d+)\s+ (?<amt>\d+)\s+ (?<amtType>\w+)\s+ (?<invDescretion>\w+)\s+ (?<otherManager>\d?)\s+ (?<votingSole>\d+)\s+ (?<votingShared>\d+)\s+ (?<votingNone>\d+)\s? $ ", RegexOptions.IgnorePatternWhitespace); } Match m = _reportParser.Match(toProcess); if (m.Success) { CHolding toAdd = new CHolding( m.Groups["securityName"].Value.Trim(), m.Groups["securityType"].Value.Trim(), m.Groups["cusip"].Value.Trim(), ParseDouble(m.Groups["mktval"].Value), ParseDouble(m.Groups["amt"].Value), m.Groups["amtType"].Value, m.Groups["invDescretion"].Value, ParseInt(m.Groups["otherManager"].Value, true), ParseInt(m.Groups["votingSole"].Value, false), ParseInt(m.Groups["votingShared"].Value, false), ParseInt(m.Groups["votingNone"].Value, false) ); holdingDate.AddHolding(toAdd); } else { throw new Exception("Could not parse the line into holdings data."); } }
/// <summary> /// Read in a specific file to create the object model. /// </summary> /// <param name="filename">Filename to process</param> static void ProcessFile(String filename) { CHoldingDate thisDate = new CHoldingDate(); bool readingBody = false; bool completed = false; int lineCounter = 0; StreamReader fs; try { fs = new StreamReader(filename, Encoding.ASCII); } catch (Exception e) { throw new Exception(String.Format("Could not open the file: {0}", e.Message)); } while (!fs.EndOfStream) { String toProcess = fs.ReadLine().Trim(); if (toProcess != "") { if (!readingBody) // header { switch (toProcess.Substring(0, 3)) { case "CON": thisDate.ReportDate = ExtractDate(toProcess); Holdings.Add(thisDate.ReportDate, thisDate); break; case "FIL": thisDate.FileDate = ExtractDate(toProcess); break; case "<S>": readingBody = true; break; } } else { if (toProcess.StartsWith("</TABLE>")) { completed = true; break; } else { try { ProcessLine(toProcess, thisDate); } catch (Exception e) { throw new Exception(String.Format("Error on line {0}: {1}", lineCounter, e.Message)); } } } } lineCounter++; } fs.Close(); if (!completed) { throw new Exception("Unexpected end of file, is the input file damaged?"); } }