Files
LeafWeb/Core/Parsers/CntrlComparisonParser.cs
T
2015-12-04 10:15:51 -05:00

80 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
using LeafWeb.Core.Models;
using LeafWeb.Core.Utility;
namespace LeafWeb.Core.Parsers
{
public class CntrlComparisonParser : CsvParserBase
{
public CntrlComparisonParser(FileSystemInfo csvFile) : base(csvFile)
{
}
public CntrlComparison[] Parse()
{
var fittingTitles = GetNextCsvRowValues();
if (fittingTitles == null)
throw new ParseException($"Could not read data header row on line number {CsvReader.Row}");
var fitUnits = GetNextCsvRowValues();
if (fitUnits == null)
throw new ParseException($"Could not read data units row on line number {CsvReader.Row}");
return ParseCntrlComparisonSet(fittingTitles).ToArray();
}
private IEnumerable<CntrlComparison> ParseCntrlComparisonSet(string[] fittingTitles)
{
var endOfFile = false;
while (!endOfFile)
{
// First Section
var fittingValues = new List<string[]>();
string[] photosyntheticTitles;
while (true)
{
var values = GetNextCsvRowValues();
if (CsvReader.IsRecordEmpty())
throw new ParseException($"Encountered empty line while readding fitting info on line {CsvReader.Row}");
if (values == null) // end of file
yield break;
if (ParsedObjectFactory<CntrlComparisonPhotosyntheticInfo>.IsPropertiesTitlesMatch(values))
{
photosyntheticTitles = values;
break;
}
fittingValues.Add(values);
}
var photosyntheticValues = new List<string[]>();
while (true)
{
var values = GetNextCsvRowValues();
if (CsvReader.IsRecordEmpty()) // end of set
break;
if (values == null)
{
endOfFile = true;
break;
}
photosyntheticValues.Add(values);
}
var fittingInfo =
ParsedObjectFactory<CntrlComparisonFittingInfo>
.Create(fittingTitles, fittingValues.ToArray());
var photosyntheticInfo =
ParsedObjectFactory<CntrlComparisonPhotosyntheticInfo>
.Create(photosyntheticTitles, photosyntheticValues.ToArray());
yield return new CntrlComparison
{
FittingInfo = fittingInfo,
PhotosyntheticInfo = photosyntheticInfo
};
}
}
}
}