/// <summary> /// Instantiates an uninitialized instance. /// </summary> /// <param name="delimiter">Data items delimiter</param> public CsvDataHolder(char delimiter) { DataDelimiter = delimiter; ColNameCollection = new DelimitedStringValues(DataDelimiter); DataRowCollection = new List <DelimitedStringValues>(); return; }
/// <summary> /// Appends data rows from given stream reader /// </summary> /// <param name="streamReader">Data stream reader</param> /// <param name="maxRows">Maximum rows to be loaded. If GT 0 is specified then loading stops when maxRows is reached.</param> /// <returns>Number of loaded rows</returns> public int AppendFromStream(StreamReader streamReader, int maxRows = 0) { int numOfLoadedRows = 0; while (!streamReader.EndOfStream) { //Add data row if (DataDelimiter == AutoDetectDelimiter) { //Unknown delimiter DelimitedStringValues dsv = new DelimitedStringValues(streamReader.ReadLine()); //Set recognized delimiter DataDelimiter = dsv.Delimiter; DataRowCollection.Add(dsv); } else { //Known delimiter DataRowCollection.Add(new DelimitedStringValues(streamReader.ReadLine(), DataDelimiter)); } ++numOfLoadedRows; if (maxRows > 0 && numOfLoadedRows == maxRows) { //Maximim limit reached break; } } return(numOfLoadedRows); }
/// <summary> /// Checks if string values contain data items /// </summary> /// <param name="dsv">String values</param> private bool ContainsDataItems(DelimitedStringValues dsv) { foreach (string item in dsv.StringValueCollection) { //Numerical if (!double.IsNaN(item.ParseDouble(false))) { return(true); } else if (item.ParseInt(false) != int.MinValue) { return(true); } //Datetime else if (item.ParseDateTime(false) != DateTime.MinValue) { return(true); } //Boolean try { item.ParseBool(true, "failed"); return(true); } catch { //Do nothing ; } } return(false); }
//Methods private void InitFromStream(StreamReader streamReader, bool header, char delimiter = AutoDetectDelimiter) { DataRowCollection = new List <DelimitedStringValues>(); DataDelimiter = delimiter; AppendFromStream(streamReader); if (header && DataRowCollection.Count > 0) { ColNameCollection = DataRowCollection[0]; DataRowCollection.RemoveAt(0); } else { ColNameCollection = new DelimitedStringValues(DataDelimiter); } return; }
/// <summary> /// Initializes column names /// </summary> private void InitColNames() { if (DataRowCollection.Count == 0) { //No data ColNameCollection = new DelimitedStringValues(DataDelimiter); } if (ContainsDataItems(DataRowCollection[0])) { //First row contains data -> Empty column names ColNameCollection = new DelimitedStringValues(DataRowCollection[0].Delimiter); } else { //First row probably contains column names ColNameCollection = DataRowCollection[0]; DataRowCollection.RemoveAt(0); } return; }