/// <summary> /// Asynchronously writes the items in <paramref name="this"/> to <paramref name="csvWriter"/>. /// </summary> /// <remarks> /// <para> /// This overload provides maximum flexibility in how items are written CSV. /// </para> /// </remarks> /// <typeparam name="T"> /// The type of the items to be written to <paramref name="csvWriter"/>. /// </typeparam> /// <param name="this"> /// The items to write. /// </param> /// <param name="csvWriter"> /// The <see cref="CsvWriter"/>. /// </param> /// <param name="header"> /// If non-<see langword="null"/>, this will be written to <paramref name="csvWriter"/> before any data records are written. /// </param> /// <param name="objectToRecordConverter"> /// Converts an item in <paramref name="this"/> to a CSV record. /// </param> /// <returns> /// The number of items written. /// </returns> public async static Task <int> WriteCsvAsync <T>(this IEnumerable <T> @this, CsvWriter csvWriter, IEnumerable <string> header, Func <T, IEnumerable <string> > objectToRecordConverter) { @this.AssertNotNull("@this"); csvWriter.AssertNotNull("csvWriter"); objectToRecordConverter.AssertNotNull("objectToRecordConverter"); HeaderRecord headerRecord = null; if (header != null) { headerRecord = new HeaderRecord(header); await csvWriter.WriteRecordAsync(headerRecord).ConfigureAwait(false); } var num = 0; var buffer = new DataRecord[16]; var bufferOffset = 0; foreach (var item in @this) { var record = new DataRecord(headerRecord, objectToRecordConverter(item)); buffer[bufferOffset++] = record; if (bufferOffset == buffer.Length) { // buffer full await csvWriter.WriteRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false); bufferOffset = 0; } ++num; } // write any outstanding data in buffer await csvWriter.WriteRecordsAsync(buffer, 0, bufferOffset).ConfigureAwait(false); return(num); }
/// <summary> /// Reads the first record from the underlying CSV data and assigns it to <see cref="HeaderRecord"/>. /// </summary> /// <remarks> /// <para> /// If successful, all <see cref="DataRecord"/>s read by this <c>CsvReader</c> will have their <see cref="DataRecord.HeaderRecord"/> set accordingly. /// </para> /// <para> /// Any attempt to call this method when this <c>CsvReader</c> has already read a record will result in an exception. /// </para> /// </remarks> /// <returns> /// The <see cref="KBCsv.HeaderRecord"/> that was read, also available via the <see cref="HeaderRecord"/> property. If no records are left, this method returns <see langword="null"/>. /// </returns> public HeaderRecord ReadHeaderRecord() { this.EnsureNotDisposed(); this.EnsureNotPassedFirstRecord(); if (this.parser.ParseRecords(null, this.buffer, 0, 1) == 1) { ++this.recordNumber; this.headerRecord = new HeaderRecord(this.buffer[0]); return this.headerRecord; } return null; }
/// <summary> /// Initializes a new instance of the DataRecord class. /// </summary> /// <remarks> /// The resultant data record will the specified values, and is not read-only. It will use the specified <see cref="KBCsv.HeaderRecord"/> (which will therefore /// be returned from <see cref="HeaderRecord"/>). /// </remarks> /// <param name="headerRecord"> /// An optional <see cref="KBCsv.HeaderRecord"/> associated with this <c>DataRecord</c>. /// </param> /// <param name="values"> /// The values comprising this <c>DataRecord</c>. /// </param> public DataRecord(HeaderRecord headerRecord, IEnumerable <string> values) : this(headerRecord, false, values) { }
/// <summary> /// Initializes a new instance of the DataRecord class. /// </summary> /// <remarks> /// The resultant data record will have the specified values, and may or may not be read-only. It will use the specified <see cref="KBCsv.HeaderRecord"/> (which will therefore /// be returned from <see cref="HeaderRecord"/>). /// </remarks> /// <param name="headerRecord"> /// An optional <see cref="KBCsv.HeaderRecord"/> associated with this <c>DataRecord</c>. /// </param> /// <param name="readOnly"> /// <see langword="true"/> to mark this <c>DataRecord</c> as read-only. /// </param> /// <param name="values"> /// The values comprising this <c>DataRecord</c>. /// </param> public DataRecord(HeaderRecord headerRecord, bool readOnly, params string[] values) : this(headerRecord, readOnly, (IEnumerable <string>)values) { }
/// <summary> /// Initializes a new instance of the DataRecord class. /// </summary> /// <remarks> /// The resultant data record will the specified values, and is not read-only. It will use the specified <see cref="KBCsv.HeaderRecord"/> (which will therefore /// be returned from <see cref="HeaderRecord"/>). /// </remarks> /// <param name="headerRecord"> /// An optional <see cref="KBCsv.HeaderRecord"/> associated with this <c>DataRecord</c>. /// </param> /// <param name="values"> /// The values comprising this <c>DataRecord</c>. /// </param> public DataRecord(HeaderRecord headerRecord, params string[] values) : this(headerRecord, false, values) { }
/// <summary> /// Initializes a new instance of the DataRecord class. /// </summary> /// <remarks> /// The resultant data record will have no values, but is not read-only. It will use the specified <see cref="KBCsv.HeaderRecord"/> (which will therefore /// be returned from <see cref="HeaderRecord"/>). /// </remarks> /// <param name="headerRecord"> /// An optional <see cref="KBCsv.HeaderRecord"/> associated with this <c>DataRecord</c>. /// </param> public DataRecord(HeaderRecord headerRecord) : this(headerRecord, false) { }
// used internally by the parser to speed up the creation of parsed records internal DataRecord(HeaderRecord headerRecord, IList <string> values) : base(values) { this.headerRecord = headerRecord; }
/// <summary> /// Initializes a new instance of the DataRecord class. /// </summary> /// <remarks> /// The resultant data record will have the specified values, and may or may not be read-only. It will use the specified <see cref="KBCsv.HeaderRecord"/> (which will therefore /// be returned from <see cref="HeaderRecord"/>). /// </remarks> /// <param name="headerRecord"> /// An optional <see cref="KBCsv.HeaderRecord"/> associated with this <c>DataRecord</c>. /// </param> /// <param name="readOnly"> /// <see langword="true"/> to mark this <c>DataRecord</c> as read-only. /// </param> /// <param name="values"> /// The values comprising this <c>DataRecord</c>. /// </param> public DataRecord(HeaderRecord headerRecord, bool readOnly, IEnumerable <string> values) : base(readOnly, values) { this.headerRecord = headerRecord; }