Beispiel #1
0
        /// <summary>
        ///     Reads a reader as comma separated values.
        /// </summary>
        /// <param name="context">
        ///     The context.
        /// </param>
        /// <param name="rowBuilder">
        ///     The data Set.
        /// </param>
        /// <param name="reader">
        ///     The stream.
        /// </param>
        /// <param name="fields">
        ///     The fields, or <c>null</c> to retrieve all fields.
        /// </param>
        /// <returns>
        ///     The rows.
        /// </returns>
        public IEnumerable <Row> Read(IFileFormatExecutionContext context, IRowBuilder rowBuilder, StreamReader reader, HashSet <string> fields)
        {
            var separator = context.GetDefault("SEPARATOR", false) as string ?? ",";
            var splitter  = GetSplitter(separator);
            var headers   = GetHeaders(splitter, reader, separator);

            if (headers.Length == 1 && string.IsNullOrEmpty(headers[0]))
            {
                yield break;
            }

            var idx = 0L;

            do
            {
                var line = splitter.Matches($"{reader.ReadLine()}{separator}")
                           .Cast <Match>()
                           .Select(match => match.Groups[1].Value)
                           .Select(value => value.Trim())
                           .Select(value => EscapedString.IsMatch(value) ? value.Substring(1, value.Length - 2).Replace("\"\"", "\"") : value)
                           .ToArray();

                if (line.Length == headers.Length)
                {
                    // ReSharper disable once AccessToDisposedClosure
                    yield return(rowBuilder.CreateRow(idx++, headers.Select((header, i) => new KeyValuePair <string, object>(header, line[i]))));
                }
            }while (!reader.EndOfStream);
        }
Beispiel #2
0
        /// <summary>
        ///     Gets the descriptor for this data source.
        /// </summary>
        /// <param name="alias">
        ///     The alias.
        /// </param>
        /// <param name="context">
        ///     The execution context.
        /// </param>
        /// <param name="reader">
        ///     The reader.
        /// </param>
        /// <returns>
        ///     The <see cref="System.Threading.Tasks.Task" />.
        /// </returns>
        public Task <IDataSourceDescriptor> GetDataSourceDescriptorAsync(string alias, IFileFormatExecutionContext context, StreamReader reader)
        {
            var separator = context.GetDefault("SEPARATOR", false) as string ?? ",";

            return(Task.FromResult(Descriptor.ForDataSource(alias, GetHeaders(GetSplitter(separator), reader, separator).Where(header => header.Length > 0).Select(column => Descriptor.ForColumn(column, typeof(string))))));
        }