// Copyright (C) Stichting Deltares 2020. All rights reserved. // // This file is part of the Layer On Slope Tool. // // The Layer On Slope Tool is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; namespace Deltares.LayerOnSlopeTool.Importer { /// /// Holds the utility functions for reading csv files. /// public class CsvReaderUtilities { /// /// Ensures the proper header for dynamic record length in CSV file. /// /// Name of the file. public static void EnsureProperHeaderForDynamicRecordLengthInCsvFile(string fileName) { if (!File.Exists(fileName)) { return; } var maxFields = 0; var fileContents = new List(); using (var sr = new StreamReader(fileName)) { String input; while ((input = sr.ReadLine()) != null) { fileContents.Add(input); string[] items = input.Split(';'); maxFields = Math.Max(maxFields, items.Count()); } } if (maxFields > 0) { var header = fileContents[0]; string[] items = header.Split(';'); for (var i = items.Count(); i < maxFields; i++) { header += string.Format("A{0};", i); } fileContents[0] = header; } using (var outfile = new StreamWriter(fileName)) { foreach (var line in fileContents) { outfile.WriteLine(line); } } } /// /// Determines the culture for file. /// /// Name of the file. /// public static CultureInfo DetermineCultureForFile(string fileName) { if (!File.Exists(fileName)) { return null; } var fileContents = new List(); bool isDetermined = false; CultureInfo cultureInfo = null; using (var sr = new StreamReader(fileName)) { String input; while (((input = sr.ReadLine()) != null) && (!isDetermined)) { fileContents.Add(input); string[] items = input.Split(';'); foreach (var item in items) { try { if (item.Contains('.')) { var pi = item.IndexOf('.'); // check for only one point if (pi == item.LastIndexOf('.')) { // point must be followed by a number if (Char.IsDigit(item[pi + 1])) { cultureInfo = new CultureInfo(CultureInfo.InvariantCulture.LCID, false); isDetermined = true; break; } } } else { if (item.Contains(',')) { var ci = item.IndexOf(','); // check for only one comma if (ci == item.LastIndexOf(',')) { // comma must be followed by a number if (Char.IsDigit(item[ci + 1])) { cultureInfo = new CultureInfo("nl-NL", false); isDetermined = true; break; } } } } } catch (Exception) { // This meant to be empty! } } } } var returnCulture = cultureInfo ?? new CultureInfo("en-US", false); return returnCulture; } /// /// Match a column name to the header of the csv file and get the corresponding index. /// /// All headers a appearing in the csv file. /// Header to retrieve index from. /// Last matching item index in ; -1 if not match found. public static int GetHeaderIndexByString(string[] headers, string headerName) { for (int i = 0; i < headers.Count(); i++) { if (headerName.ToUpper() == headers[i].ToUpper()) { return i; } } return -1; } } }