Exemplo n.º 1
0
        public async Task RemapAsync(char outputDelimiter, int lineBufferSize = 4000)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(CsvMapper));
            }

            var currentLine = new List <string>();
            var wroteHeader = false;

            var captureIndexes = new List <int>();
            var lineBuffer     = new LineBuffer(_outStream, lineBufferSize);

            while (_parser.Read()) // each record
            {
                if (!wroteHeader)
                {
                    WriteHeaders(_parser, outputDelimiter, lineBuffer, captureIndexes);
                    wroteHeader = true;
                }

                currentLine.Clear();
                for (var i = 0; i < _parser.CurrentRecord.Length; i++) // process each field
                {
                    // skip if we don't have any info for the field
                    if (!_configuration.TryGetValue(_parser.Headers[i], out var format) ||
                        !format.IsMapped ||
                        !captureIndexes.Contains(i))
                    {
                        continue;
                    }

                    currentLine.Add($"{(_parser.UsingQuotedFields ? $"\"{Mutate(_parser.CurrentRecord[i], format)}\"" : Mutate(_parser.CurrentRecord[i], format))}");
                }

                await lineBuffer.AddAsync(string.Join(outputDelimiter, currentLine));
            }

            await lineBuffer.FlushAsync();
        }