예제 #1
0
        public CsvReader(TextReader reader,
                         int bufferSize           = DefaultBufferSize,
                         CsvLayout layout         = null,
                         CsvBehaviour behaviour   = null,
                         string defaultHeaderName = null)
        {
            if (layout == null)
            {
                layout = CsvLayout.Default;
            }
            if (behaviour == null)
            {
                behaviour = CsvBehaviour.Default;
            }
            _line = CsvLine.Empty;

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize), bufferSize, ExceptionMessage.BufferSizeTooSmall);
            }

            BufferSize = bufferSize;

            if (reader is StreamReader streamReader)
            {
                Stream stream = streamReader.BaseStream;

                if (stream.CanSeek)
                {
                    // Handle bad implementations returning 0 or less
                    if (stream.Length > 0)
                    {
                        BufferSize = (int)Math.Min(bufferSize, stream.Length);
                    }
                }
            }

            CurrentRecordIndex = -1;

            _csvLayout  = layout;
            _behaviour  = behaviour;
            _parser     = new CsvParser(reader, _csvLayout, _behaviour, defaultHeaderName);
            _enumerator = _parser.GetEnumerator();
        }
예제 #2
0
 /// <summary>
 /// Read a file as CSV, using specific behaviour, layout and conversion options 
 /// </summary>
 /// <param name="path">The full or relative path name</param>
 /// <param name="encoding">The encoding</param>
 /// <param name="csvLayout">Csv layout info (quote, delimiter, ...)</param>
 /// <param name="csvBehaviour">Csv behaviour info (e.g. what to do when fields are missing)</param>
 /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
 /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
 /// <returns>a datareader instance to read the contents of the CSV file</returns>
 public static IDataReader ReadFileAsCsv(this string path, Encoding encoding, CsvLayout csvLayout, CsvBehaviour csvBehaviour, Converter converter, int bufferSize = 4096)
 {
     StreamReader reader = null;
     FileStream stream = null;
     try
     {
         stream = File.OpenRead(path);
         reader = new StreamReader(stream, encoding);
         return ReadStreamAsCsv(reader, csvLayout, csvBehaviour, converter, bufferSize);
     }
     catch
     {
         if (reader != null) reader.Dispose();
         else if (stream != null) stream.Dispose();
         throw;
     }
 }
예제 #3
0
파일: ReadCsv.cs 프로젝트: MaziarM/netcsv
        /// <summary>
        /// Read a string as CSV, using specific behaviour, layout and conversion options
        /// </summary>
        /// <param name="input">The CSV input</param>
        /// <param name="quote">The quote character. Default '"'</param>
        /// <param name="delimiter">Field delimiter. Default ','</param>
        /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param>
        /// <param name="comment">Comment marker. Default '#'</param>
        /// <param name="hasHeaders">Is the first line a header line (default false)?</param>
        /// <param name="trimmingOptions">How should fields be trimmed?</param>
        /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param>
        /// <param name="skipEmptyLines">Should empty lines be skipped?</param>
        /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param>
        /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
        /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
        /// <returns>a datareader instance to read the contents of the CSV file</returns>
        public static IDataReader FromString(
            string input,
            char quote      = '"',
            char delimiter  = ',',
            char escape     = '"',
            char comment    = '#',
            bool hasHeaders = false,
            ValueTrimmingOptions trimmingOptions  = ValueTrimmingOptions.UnquotedOnly,
            MissingFieldAction missingFieldAction = MissingFieldAction.ParseError,
            bool skipEmptyLines = true,
            QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore,
            IConverter converter = null, int bufferSize = 4096)
        {
            var reader    = new StringReader(input);
            var layout    = new CsvLayout(quote, delimiter, escape, comment, hasHeaders);
            var behaviour = new CsvBehaviour(trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction);

            return(FromReader(reader, layout, behaviour, converter ?? Converter.Default, bufferSize));
        }
예제 #4
0
파일: ReadCsv.cs 프로젝트: lanicon/netcsv
        /// <summary>
        /// Read a string as CSV, using specific behaviour, layout and conversion options
        /// </summary>
        /// <param name="input">The CSV input</param>
        /// <param name="quote">The quote character. Default '"'</param>
        /// <param name="delimiter">Field delimiter. Default ','</param>
        /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param>
        /// <param name="comment">Comment marker. Default '#'</param>
        /// <param name="hasHeaders">Is the first line a header line (default false)?</param>
        /// <param name="trimmingOptions">How should fields be trimmed?</param>
        /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param>
        /// <param name="skipEmptyLines">Should empty lines be skipped?</param>
        /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param>
        /// <param name="schema">The CSV schema.</param>
        /// <param name="cultureInfo">Culture info to be used for parsing culture-sensitive data (such as date/time and decimal numbers)</param>
        /// <returns>a DataReader instance to read the contents of the CSV file</returns>
        public static IDataReader FromString(
            string input,
            char quote      = '"',
            char delimiter  = ',',
            char escape     = '"',
            char comment    = '#',
            bool hasHeaders = false,
            ValueTrimmingOptions trimmingOptions  = ValueTrimmingOptions.None,
            MissingFieldAction missingFieldAction = MissingFieldAction.ParseError,
            bool skipEmptyLines = true,
            QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore,
            CsvSchema schema        = null,
            CultureInfo cultureInfo = null)
        {
            var reader    = new StringReader(input);
            var layout    = new CsvLayout(quote, delimiter, escape, comment, hasHeaders, schema);
            var behaviour = new CsvBehaviour(trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction);

            return(FromReader(reader, layout, behaviour, cultureInfo));
        }
예제 #5
0
파일: ReadCsv.cs 프로젝트: MaziarM/netcsv
        /// <summary>
        /// Read a file as CSV, using specific behaviour, layout and conversion options. Make sure to dispose the datareader.
        /// </summary>
        /// <param name="path">The full or relative path name</param>
        /// <param name="encoding">The encoding of the file. Default is UTF8.</param>
        /// <param name="quote">The quote character. Default '"'</param>
        /// <param name="delimiter">Field delimiter. Default ','</param>
        /// <param name="escape">Quote escape character (for quotes inside fields). Default '\'</param>
        /// <param name="comment">Comment marker. Default '#'</param>
        /// <param name="hasHeaders">Is the first line a header line (default false)?</param>
        /// <param name="trimmingOptions">How should fields be trimmed?</param>
        /// <param name="missingFieldAction">What should happen when a field is missing from a line?</param>
        /// <param name="skipEmptyLines">Should empty lines be skipped?</param>
        /// <param name="quotesInsideQuotedFieldAction">What should happen when a quote is found inside a quoted field?</param>
        /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader). When none is specified, System.Convert is used.</param>
        /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
        /// <returns>a datareader instance to read the contents of the CSV file</returns>
        public static IDataReader FromFile(
            string path,
            Encoding encoding = null,
            char quote        = '"',
            char delimiter    = ',',
            char escape       = '"',
            char comment      = '#',
            bool hasHeaders   = false,
            ValueTrimmingOptions trimmingOptions  = ValueTrimmingOptions.UnquotedOnly,
            MissingFieldAction missingFieldAction = MissingFieldAction.ParseError,
            bool skipEmptyLines = true,
            QuotesInsideQuotedFieldAction quotesInsideQuotedFieldAction = QuotesInsideQuotedFieldAction.Ignore,
            IConverter converter = null,
            int bufferSize       = 4096)
        {
            // caller should dispose IDataReader, which will indirectly also close the stream
            var layout    = new CsvLayout(quote, delimiter, escape, comment, hasHeaders);
            var behaviour = new CsvBehaviour(trimmingOptions, missingFieldAction, skipEmptyLines, quotesInsideQuotedFieldAction);
            var stream    = File.OpenRead(path);
            var reader    = new StreamReader(stream, encoding ?? Encoding.UTF8);

            return(FromReader(reader, layout, behaviour, converter ?? Converter.Default, bufferSize));
        }
예제 #6
0
 /// <summary>
 /// Read a string as CSV, using specific behaviour, layout and conversion options 
 /// </summary>
 /// <param name="input">The CSV input</param>
 /// <param name="csvLayout">Csv layout info (quote, delimiter, ...)</param>
 /// <param name="csvBehaviour">Csv behaviour info (e.g. what to do when fields are missing)</param>
 /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
 /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
 /// <returns>a datareader instance to read the contents of the CSV file</returns>
 public static IDataReader ReadStringAsCsv(this string input, CsvLayout csvLayout, CsvBehaviour csvBehaviour, Converter converter, int bufferSize = 4096)
 {
     StringReader reader = null;
     try
     {
         reader = new StringReader(input);
         return ReadStreamAsCsv(reader, csvLayout, csvBehaviour, converter, bufferSize);
     }
     catch
     {
         if (reader != null) reader.Dispose();
         throw;
     }
 }
예제 #7
0
 /// <summary>
 /// Read a stream as CSV, using specific behaviour, layout and conversion options 
 /// </summary>
 /// <param name="reader">A <see cref="TextReader"/> instance</param>
 /// <param name="csvLayout">Csv layout info (quote, delimiter, ...)</param>
 /// <param name="csvBehaviour">Csv behaviour info (e.g. what to do when fields are missing)</param>
 /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
 /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
 /// <returns>a datareader instance to read the contents of the CSV file</returns>
 public static IDataReader ReadStreamAsCsv(this TextReader reader, CsvLayout csvLayout, CsvBehaviour csvBehaviour, Converter converter, int bufferSize = 4096)
 {
     var parser = new CsvParser(reader, csvLayout, csvBehaviour);
     return new CsvDataReader(parser, converter);
 }
예제 #8
0
파일: ReadCsv.cs 프로젝트: MaziarM/netcsv
 internal static IDataReader FromReader(TextReader reader, CsvLayout csvLayout, CsvBehaviour csvBehaviour, IConverter converter, int bufferSize)
 {
     return(new CsvDataReader(reader, csvLayout, csvBehaviour, converter));
 }
예제 #9
0
파일: ReadCsv.cs 프로젝트: lanicon/netcsv
 internal static IDataReader FromReader(TextReader reader, CsvLayout csvLayout, CsvBehaviour csvBehaviour, CultureInfo cultureInfo = null)
 => new CsvDataReader(reader, csvLayout, csvBehaviour, cultureInfo);
예제 #10
0
 public static IDataReader ReadStringAsCsv(this string input, CsvLayout csvLayout, CsvBehaviour csvBehaviour, IConverter converter, int bufferSize = 4096)
 {
     return(ReadCsv.FromString(input,
                               csvLayout.Quote, csvLayout.Delimiter, csvLayout.Escape, csvLayout.Comment, csvLayout.HasHeaders,
                               csvBehaviour.TrimmingOptions, csvBehaviour.MissingFieldAction, csvBehaviour.SkipEmptyLines, csvBehaviour.QuotesInsideQuotedFieldAction,
                               converter, bufferSize));
 }
예제 #11
0
 public static IDataReader ReadStreamAsCsv(this TextReader reader, CsvLayout csvLayout, CsvBehaviour csvBehaviour, IConverter converter, int bufferSize = 4096)
 {
     return(ReadCsv.FromReader(reader, csvLayout, csvBehaviour, converter, bufferSize));
 }