Exemplo n.º 1
0
        /// <summary>
        /// Adds current row to provided CommaSeparatedValues object.
        /// </summary>
        /// <param name="csvFields">CommaSeparatedValues object where new row will be added.</param>
        private void AddRowToCSV(CommaSeparatedValues csvFields)
        {
            if (csvFields.RowsCount > 0)
            {
                if (row.Count != csvFields.ColumnsCount)
                {
                    throw new CSVFormatException("All rows must have same length as first row.", currentRowNumber, currentColumnNumber);
                }
            }

            csvFields.AddRow(row);
            row = new List <string>();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads file and returns CommaSeparatedValues.
        /// </summary>
        /// <returns>CommaSeparatedValues objects with valus from file.</returns>
        public CommaSeparatedValues Read()
        {
            CommaSeparatedValues csv = new CommaSeparatedValues();
            bool canAddNewValue      = true;//if true then we can add new field to CSV, else we can't add new field

            SetInitialValuesBeforRead();

            try
            {
                using (StreamReader reader = new StreamReader(path, encoding))
                {
                    while (ReadNextCharacter(reader) != null)
                    {
                        if (currentCharFromFile == DQuote)
                        {
                            string val = ReadEscapedField(reader);
                            if (canAddNewValue)
                            {
                                row.Add(val);
                                canAddNewValue = false;
                            }
                            else
                            {
                                throw new CSVFormatException("Invalid field. Probably missing field separator", currentRowNumber, currentColumnNumber);
                            }
                        }
                        else if (currentCharFromFile == Comma)
                        {
                            if (canAddNewValue)
                            {
                                row.Add(string.Empty);
                            }

                            canAddNewValue = true;
                        }
                        else if (currentCharFromFile == CR)
                        {
                            if (canAddNewValue)
                            {
                                row.Add(string.Empty);
                            }

                            AddRowToCSV(csv);
                            canAddNewValue = true;

                            //currenlty char is \r next should be \n
                            ReadNextCharacter(reader);
                            if (currentCharFromFile != LF)
                            {
                                throw new CSVFormatException($"Expecting {CR} character.", currentRowNumber, currentColumnNumber);
                            }
                        }
                        else
                        {
                            string val = ReadNonEscapedField(reader);
                            if (canAddNewValue)
                            {
                                row.Add(val);
                                canAddNewValue = false;
                            }
                            else
                            {
                                throw new CSVFormatException("Invalid field. Probably missing field separator.", currentRowNumber, currentColumnNumber);
                            }
                        }
                    }

                    if (row.Count > 0)
                    {
                        AddRowToCSV(csv);
                    }
                }
            }
            catch (CSVFormatException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception("Error when reading input file. Check inner exception for details", ex);
            }
            return(csv);
        }