Пример #1
0
        /// <summary>
        /// Reads a blank line. This accounts for empty lines
        /// and commented out lines.
        /// </summary>
        protected virtual void ReadBlankLine()
        {
            if (context.ParserConfiguration.IgnoreBlankLines)
            {
                context.Row++;
            }

            while (true)
            {
                if (context.C == '\r' || context.C == '\n')
                {
                    ReadLineEnding();
                    fieldReader.SetFieldStart();
                    return;
                }

                if (context.C == -1)
                {
                    return;
                }

                // If the buffer runs, it appends the current data to the field.
                // We don't want to capture any data on a blank line, so we
                // need to set the field start every char.
                fieldReader.SetFieldStart();
                context.C = fieldReader.GetChar();
            }
        }
Пример #2
0
        /// <summary>
        /// Reads a line of the CSV file.
        /// </summary>
        /// <returns>The CSV line.</returns>
        protected virtual string[] ReadLine()
        {
            context.RecordBuilder.Clear();
            context.Row++;
            context.RawRow++;

            while (true)
            {
                if (fieldReader.IsBufferEmpty && !fieldReader.FillBuffer())
                {
                    // End of file.
                    if (context.RecordBuilder.Length > 0)
                    {
                        // There was no line break at the end of the file.
                        // We need to return the last record first.
                        context.RecordBuilder.Add(fieldReader.GetField());
                        return(context.RecordBuilder.ToArray());
                    }

                    return(null);
                }

                context.C = fieldReader.GetChar();

                if (context.RecordBuilder.Length == 0 && ((context.C == context.ParserConfiguration.Comment && context.ParserConfiguration.AllowComments) || context.C == '\r' || context.C == '\n'))
                {
                    ReadBlankLine();
                    if (!context.ParserConfiguration.IgnoreBlankLines)
                    {
                        break;
                    }

                    continue;
                }

                // Trim start outside of quotes.
                if (context.C == ' ' && (context.ParserConfiguration.TrimOptions & TrimOptions.Trim) == TrimOptions.Trim)
                {
                    ReadSpaces();
                    fieldReader.SetFieldStart(-1);
                }

                if (context.C == context.ParserConfiguration.Quote && !context.ParserConfiguration.IgnoreQuotes)
                {
                    if (ReadQuotedField())
                    {
                        break;
                    }
                }
                else
                {
                    if (ReadField())
                    {
                        break;
                    }
                }
            }

            return(context.RecordBuilder.ToArray());
        }