// -------------------------------------------------------------------------------------------------------------------------- private static List <CSVColToPropertyMap> CreatePropertyList(Type t, CSVColumnMap colMap) { List <CSVColToPropertyMap> propList; var props = t.GetProperties(); propList = new List <CSVColToPropertyMap>(); foreach (var m in props) { var match = (from x in colMap.Names where (x.Replace(" ", "") == m.Name) select x).SingleOrDefault(); if (match != null) { propList.Add(new CSVColToPropertyMap() { ColName = match, PropInfo = m }); } } return(propList); }
// -------------------------------------------------------------------------------------------------------------------------- public CSVLine(CSVColumnMap colMap_, string values, string separator) { // NOTE: This also doesn't limit the total number of entries! It probably should! ColumnMap = colMap_; Values = ParseLine(values, separator); while (Values.Count < ColumnMap.Count) { Values.Add(string.Empty); } }
// -------------------------------------------------------------------------------------------------------------------------- internal static List <CSVColToPropertyMap> GetPropList(Type t, CSVColumnMap colMap) { if (!TypesToPropList.TryGetValue(t, out List <CSVColToPropertyMap> propList)) { propList = CreatePropertyList(t, colMap); TypesToPropList.Add(t, propList); } return(propList); }
// -------------------------------------------------------------------------------------------------------------------------- public CSVLine(CSVColumnMap colMap_) { ColumnMap = colMap_; // Yes, we should be using a dictionary. Values = new List <string>(ColumnMap.Count); for (int i = 0; i < ColumnMap.Count; i++) { Values.Add(null); } }
// -------------------------------------------------------------------------------------------------------------------------- public CSVFile(IList <string> colNames) { var cols = new List <CSVColumnMapping>(); int index = 0; foreach (var c in colNames) { cols.Add(new CSVColumnMapping() { Index = index, Name = c }); ++index; } Columns = cols; ColumnMap = new CSVColumnMap(Columns); }
// -------------------------------------------------------------------------------------------------------------------------- public CSVFile(string path, string separator = ",", bool includeHeader = true, bool trimWhitespace = true) { if (!File.Exists(path)) { throw new FileNotFoundException($"The file at path: {path} does not exist!"); } string[] splitWith = new[] { separator }; Separator = separator; using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) { // r.Read( string line = GetNextLine(r); string[] firstParts = line.Split(splitWith, StringSplitOptions.None); if (includeHeader) { int index = 0; foreach (var p in firstParts) { var cmap = new CSVColumnMapping() { Index = index, Name = GetColumnValue(firstParts, index, trimWhitespace) }; Columns.Add(cmap); ++index; } // it.MoveNext(); } else { // The first line is to be treated as data, so we will do that, but generate column names. for (int i = 0; i < firstParts.Length; i++) { var cmap = new CSVColumnMapping() { Index = i, Name = $"Column_{i + 1}", }; Columns.Add(cmap); } } // Now we can process all of the data...... Lines = new List <CSVLine>(); CSVColumnMap useCols = new CSVColumnMap(Columns); while ((line = GetNextLine(r)) != null) { CSVLine csvLine = new CSVLine(useCols, line, separator); Lines.Add(csvLine); } } return; }
// -------------------------------------------------------------------------------------------------------------------------- public CSVFile(List <CSVColumnMapping> cols) { Columns = new List <CSVColumnMapping>(cols); ColumnMap = new CSVColumnMap(Columns); Separator = ","; }
// -------------------------------------------------------------------------------------------------------------------------- // TODO: Check the counts, etc. public CSVLine(CSVColumnMap colMap_, IList <string> values) { ColumnMap = colMap_; Values = values.ToList(); }