Exemplo n.º 1
0
 public bool Compare(CSVData csvData)
 {
     return(DataHeaders() == csvData.DataHeaders() && DataValues() == csvData.DataValues());
 }
Exemplo n.º 2
0
        public void LoadFileLines(String csvFileFullPath, IList <string> columnNames, String regExLineIdentifierPattern, String keyName)
        {
            FileStream fs = System.IO.File.OpenRead(csvFileFullPath);

            using (fs)
            {
                StringBuilder columnNameValue   = new StringBuilder();
                StringBuilder currentLineBuffer = new StringBuilder();
                StreamReader  sr      = new StreamReader(fs);
                int           lineIdx = 0;
                using (sr)
                {
                    IDictionary <String, int> columnNameIndexes = new Dictionary <String, int>();

                    while (!sr.EndOfStream)
                    {
                        String nextLine = sr.ReadLine();
                        if (lineIdx == 0)
                        {
                            String[] columns       = nextLine.Split(',', StringSplitOptions.TrimEntries);
                            int      columnNameIdx = 0;

                            foreach (String name in columns)
                            {
                                if (columnNames.Count(n => n.ToLower() == name.ToLower()) > 0)
                                {
                                    columnNameIndexes.Add(name, columnNameIdx);
                                }

                                columnNameIdx++;
                            }
                        }
                        else
                        {
                            MatchCollection matches = Regex.Matches(nextLine, regExLineIdentifierPattern, RegexOptions.IgnoreCase);

                            if (matches.Count == 1)
                            {
                                CSVData columnCsvData = null;

                                foreach (String columnName in columnNameIndexes.Keys)
                                {
                                    int columnNameIdx = columnNameIndexes[columnName];

                                    if (currentLineBuffer.Length > 0)
                                    {
                                        String currentLine           = currentLineBuffer.ToString();
                                        bool   notInsideDoubleQuotes = false;
                                        bool   notInsideSingleQuotes = false;
                                        int    columnIdx             = 0;
                                        foreach (Char c in currentLine.ToCharArray())
                                        {
                                            if (c == AComma && (notInsideDoubleQuotes == false && notInsideSingleQuotes == false))
                                            {
                                                columnIdx++;
                                            }
                                            else if (c == ADoubleQuote && !notInsideSingleQuotes)
                                            {
                                                notInsideDoubleQuotes = !notInsideDoubleQuotes;
                                            }
                                            else if (c == ASingleQuote && !notInsideDoubleQuotes)
                                            {
                                                notInsideSingleQuotes = !notInsideSingleQuotes;
                                            }

                                            if (columnIdx == columnNameIdx && c != AComma)
                                            {
                                                columnNameValue.Append(c);
                                            }
                                            else if (columnIdx > columnNameIdx)
                                            {
                                                break;
                                            }
                                        }

                                        String value = columnNameValue.ToString();
                                        if (String.IsNullOrWhiteSpace(value) == false)
                                        {
                                            if (columnCsvData == null)
                                            {
                                                columnCsvData = new CSVData(keyName);
                                            }

                                            columnCsvData.Add(columnName, value);
                                            columnNameValue.Clear();
                                        }

                                        currentLineBuffer.Clear();
                                    }

                                    currentLineBuffer.Append(nextLine);
                                }

                                if (columnCsvData != null)
                                {
                                    CsvDataList.Add(columnCsvData);
                                }
                            }
                            else if (currentLineBuffer.Length > 0)
                            {
                                currentLineBuffer.Append(nextLine);
                            }
                        }

                        lineIdx++;
                    }
                }
            }
        }