Exemple #1
0
        public static IList <CSVRow> Compare(CSVFile oldFile, CSVFile newFile)
        {
            IList <CSVRow> unmatchedValues = new List <CSVRow>();

            foreach (CSVRow oldRow in oldFile.Rows)
            {
                var match = newFile.Rows.FirstOrDefault(newRow => newRow == oldRow);
                if (match == null)
                {
                    unmatchedValues.Add(oldRow);
                }
            }
            return(unmatchedValues);
        }
Exemple #2
0
        public void LoadFileLines(String csvFileFullPath, IList <string> columnNames, String regExLineIdentifierPattern, String keyName, Char delimiter = ',')
        {
            FileStream fs = System.IO.File.OpenRead(csvFileFullPath);
            bool       isKeyNameSpecified = !String.IsNullOrEmpty(keyName);

            using (fs)
            {
                StreamReader sr = new StreamReader(fs);

                using (sr)
                {
                    #region First, identify the lines and add to Lines collection.  Also, identify
                    //the columns that the caller is interested in and find the indexes of
                    //each column and store in column index array.
                    IDictionary <String, int> columnNameIndexes = new Dictionary <String, int>();
                    StringBuilder             currentLineBuffer = new StringBuilder();
                    IList <String>            lines             = new List <string>();
                    String currentLine = String.Empty;
                    int    lineIdx     = 0;
                    while (!sr.EndOfStream)
                    {
                        currentLine = sr.ReadLine();

                        if (lineIdx == 0)
                        {
                            String[] columns       = currentLine.Split(delimiter, StringSplitOptions.TrimEntries);
                            int      columnNameIdx = 0;

                            foreach (String name in columns)
                            {
                                if (columnNames == null || columnNames.Any() == false || columnNames.Count(n => n.ToLower() == name.ToLower()) > 0)
                                {
                                    columnNameIndexes.Add(name.ToLower(), columnNameIdx);
                                }
                                columnNameIdx++;
                            }
                        }
                        else
                        {
                            MatchCollection isNewLine    = null;
                            bool            skipMatching = (String.IsNullOrWhiteSpace(regExLineIdentifierPattern) == true);
                            if (skipMatching == false)
                            {
                                isNewLine = Regex.Matches(currentLine, regExLineIdentifierPattern, RegexOptions.IgnoreCase);
                            }

                            if (skipMatching || isNewLine.Count == 1)
                            {
                                if (currentLineBuffer.Length > 0)
                                {
                                    //If we have a match meaning that we've found a new line and
                                    //currentLineBuffer contains text, we need to save the content
                                    //as a valid line before clearing for new line.
                                    lines.Add(currentLineBuffer.ToString());
                                    //Clear to start new line
                                    currentLineBuffer.Clear();
                                }
                            }
                            currentLineBuffer.Append(currentLine);
                        }

                        lineIdx++;
                    }
                    if (currentLineBuffer.Length > 0)
                    {
                        //If we have a match meaning that we've found a new line and
                        //currentLineBuffer contains text, we need to save the content
                        //as a valid line before clearing for new line.
                        lines.Add(currentLineBuffer.ToString());
                        //Clear to start new line
                        currentLineBuffer.Clear();
                    }
                    #endregion

                    #region
                    foreach (String nextLine in lines)
                    {
                        if (nextLine.IndexOf("A CAJUN LIFE: Blackening Rub, 8 oz") != -1)
                        {
                        }

                        int    charArrayLen = nextLine.ToCharArray().Length;
                        char[] charArray    = nextLine.ToCharArray();
                        CSVRow csvRow       = new CSVRow();
                        #region Locate columns and add to list
                        foreach (String columnName in columnNameIndexes.Keys)
                        {
                            int  columnNameIdx    = columnNameIndexes[columnName];
                            bool openDoubleQuotes = false;
                            bool openSingleQuotes = false;
                            int  columnIdx        = 0;

                            StringBuilder columnNameValue = new StringBuilder();

                            for (int charIdx = 0; charIdx < charArrayLen; charIdx++)
                            {
                                Char bc = '|';
                                Char cc = '|';
                                Char ac = '|';

                                if (charIdx > 0)
                                {
                                    bc = charArray[charIdx - 1];
                                }

                                cc = charArray[charIdx];

                                if (charIdx != charArrayLen - 1)
                                {
                                    ac = charArray[charIdx + 1];
                                }

                                bool insideQuotes = (openDoubleQuotes || openSingleQuotes);

                                if (cc == delimiter && !insideQuotes)
                                {
                                    columnIdx++;
                                }
                                else if (cc == ADoubleQuote && openSingleQuotes == false)
                                {
                                    if (openDoubleQuotes == true)
                                    {
                                        openDoubleQuotes = false;
                                    }
                                    else
                                    {
                                        openDoubleQuotes = true;
                                    }
                                }
                                else if (cc == ASingleQuote && openDoubleQuotes == false)
                                {
                                    if ((bc == ' ' || bc == delimiter || bc == '|') &&
                                        (ac == ' ' || ac == delimiter || ac == '|'))
                                    {
                                        if (openSingleQuotes == true)
                                        {
                                            openSingleQuotes = false;
                                        }
                                        else
                                        {
                                            openSingleQuotes = true;
                                        }
                                    }
                                }

                                if (columnIdx == columnNameIdx)
                                {
                                    if (cc == delimiter && insideQuotes)
                                    {
                                        columnNameValue.Append(cc);
                                    }
                                    else if (cc != delimiter)
                                    {
                                        columnNameValue.Append(cc);
                                    }
                                }
                                else if (columnIdx > columnNameIdx)
                                {
                                    break;
                                }
                            }
                            CSVColumn newColumn = csvRow.CreateColumn(columnName, columnNameValue, keyName == columnName);
                        }
                        #endregion

                        Rows.Add(csvRow);

                        if (isKeyNameSpecified)
                        {
                            String keyNameValue = CSVFile.GetKey(csvRow[keyName].Value.ToString());
                            if (!KeyNameToRowIndex.ContainsKey(keyNameValue))
                            {
                                KeyNameToRowIndex.Add(keyNameValue, Rows.Count - 1);
                            }
                        }
                    }
                    #endregion
                }
            }
        }