/// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
            {
                return(false);
            }

            int pos  = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    {
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                {
                    row[rows] = value;
                }
                else
                {
                    row.Add(value);
                }
                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                {
                    pos++;
                }
                if (pos < row.LineText.Length)
                {
                    pos++;
                }
            }
            // Delete any unused items
            while (row.Count > rows)
            {
                row.RemoveAt(rows);
            }

            // Return true if any columns read
            return(row.Count > 0);
        }
Exemple #2
0
        public void ProcessFinalResults(string outputPath)
        {
            if (String.IsNullOrEmpty(outputPath))
            {
                throw new Exception("The output path must be provided!");
            }
            try
            {
                List <FinalResultsData> finalResults = new List <FinalResultsData>();
                string[] files  = Directory.GetFiles(outputPath);
                int      rounds = 0;
                foreach (var file in files)
                {
                    rounds++;
                    // Read sample data from CSV file
                    using (CsvFileReader reader = new CsvFileReader(file))
                    {
                        CsvRow row     = new CsvRow();
                        int    counter = 0;

                        while (reader.ReadRow(row))
                        {
                            if (counter <= 1)
                            {
                                counter++;
                                continue;
                            }
                            string name = "";
                            for (int i = 0; i < row.Count; i++)
                            {
                                if (i == 1)
                                {
                                    name = row[i];
                                    if (rounds == 1)
                                    {
                                        finalResults.Add(new FinalResultsData {
                                            Name = name, firstRoundScore = 0, secondRoundScore = 0, thirdRoundScore = 0, finalScore = 0
                                        });
                                    }
                                }
                                if (i == 2)
                                {
                                    if (file.Contains("1st"))
                                    {
                                        finalResults.First(rec => rec.Name == name).firstRoundScore = Convert.ToInt32(row[i].Split(' ')[0]);
                                    }
                                    else if (file.Contains("2nd"))
                                    {
                                        finalResults.First(rec => rec.Name == name).secondRoundScore = Convert.ToInt32(row[i].Split(' ')[0]);
                                    }
                                    else if (file.Contains("3rd"))
                                    {
                                        finalResults.First(rec => rec.Name == name).thirdRoundScore = Convert.ToInt32(row[i].Split(' ')[0]);
                                    }
                                }
                            }
                        }
                    }
                }
                foreach (var result in finalResults)
                {
                    finalResults.First(rec => rec.Name == result.Name).finalScore = (result.firstRoundScore + result.secondRoundScore + result.thirdRoundScore) / rounds;
                }
                writeFinalResultsToCsv(finalResults, outputPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }