/// <summary> /// Opens a reader to the given source file. /// </summary> /// <param name="sourceFile">The source file to be loaded.</param> /// <param name="importerConfig">The configuration of the importer.</param> internal static StreamReader OpenReader(ResourceLink sourceFile, CsvImporterConfig importerConfig) { if (importerConfig.Encoding != null) { return(new StreamReader(sourceFile.OpenInputStream(), importerConfig.Encoding)); } else { return(new StreamReader(sourceFile.OpenInputStream())); } }
/// <summary> /// Tries to laod the given table file. /// Null is returned if opening is not possible. /// </summary> /// <param name="tableFileSource">The file to be loaded.</param> /// <param name="importerConfig">Configuration for the import process.</param> public ITableFile OpenTableFile(ResourceLink tableFileSource, TableImporterConfig importerConfig) { CsvImporterConfig csvImporterConfig = importerConfig as CsvImporterConfig; if (csvImporterConfig == null) { throw new SeeingSharpException(string.Format("Invalid configuration object: {0}", importerConfig)); } return(new CsvTableFile(tableFileSource, csvImporterConfig)); }
/// <summary> /// Creates a default importer configuration object. /// </summary> /// <param name="sourceFile">The source file for which the default configuration should be created.</param> public TableImporterConfig CreateDefaultConfig(ResourceLink sourceFile) { switch (sourceFile.FileExtension.ToLower()) { case "csv": return(new CsvImporterConfig()); case "txt": CsvImporterConfig result = new CsvImporterConfig(); result.SeparationChar = '\t'; return(result); default: return(new CsvImporterConfig()); } }
/// <summary> /// Initializes a new instance of the <see cref="CsvTableFile"/> class. /// </summary> /// <param name="tableFileSource">The file from which to load the table data.</param> /// <param name="importerConfig">The configuration for the import process.</param> internal CsvTableFile(ResourceLink tableFileSource, CsvImporterConfig importerConfig) { m_importerConfig = importerConfig; m_tableFileSource = tableFileSource; using (StreamReader inStreamReader = CsvUtil.OpenReader(tableFileSource, importerConfig)) { // Read until we reach the header row for (int loop = 0; loop < m_importerConfig.HeaderRowIndex; loop++) { inStreamReader.ReadLine(); } // Read the header row and ensure that we have something there string headerRow = inStreamReader.ReadLine(); if (string.IsNullOrEmpty(headerRow)) { throw new SeeingSharpException(string.Format("No header row found in csv file{0}", tableFileSource)); } m_headerRow = new CsvTableHeaderRow(this, headerRow); } }