Ejemplo n.º 1
0
        private void WriteHeaders(CsvParser parser, char outputDelimiter, LineBuffer buffer, ICollection <int> captureIndexes)
        {
            var outHeaders = new List <string>();

            for (var i = 0; i < parser.Headers.Length; i++)
            {
                // only get the headers for which we have mappings
                if (!_configuration.TryGetValue(parser.Headers[i], out var format) || !format.IsMapped)
                {
                    continue;
                }
                outHeaders.Add(format.Destination);
                captureIndexes.Add(i);
            }

            buffer.AddAsync(string.Join(outputDelimiter, outHeaders)).Wait();
        }
Ejemplo n.º 2
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();
        }