private static DataReaderConfig GetConfig(CsvDaLayer layer, ClassMapping mapping)
{
CsvDataReaderConfig retVal = new CsvDataReaderConfig();
try
{
retVal.Reader = layer.GetReader(mapping);
if (CsvDaLayer.UseNamedColumns(mapping))
{
// If the CSV has row headers, read the header row.
IList colNameRow = ReadRawCsvRow(retVal.Reader);
for (int x = 0; x < colNameRow.Count; x++)
{
retVal.IndexesByName[colNameRow[x].ToString()] = x;
}
}
else
{
// No row headers, so we must be mapped to column numbers.
// In that case, just map the column number strings to the ints.
foreach (string colStr in mapping.AllDataColsInOrder)
{
// Remember the mapping column numbers are 1-based but
// the internal column numbers are 0-based.
retVal.IndexesByName[colStr] = int.Parse(colStr) - 1;
}
}
return retVal;
}
catch (Exception e)
{
// Problem setting up, close the reader.
if (retVal.Reader != null)
{
layer.DoneWithReader(retVal.Reader);
}
throw new LoggingException("Unable to begin reading from CSV file.", e);
}
}