コード例 #1
0
ファイル: Program.cs プロジェクト: nowwwak/CSVParser
        static void Main(string[] args)
        {
            //validate input parameters
            if (!ValidateParamsLength(args))
            {
                return;
            }

            //read input parameters
            string path = args[0];
            int    filterColumnIndex = 0;
            string filterColumnValue = null;

            if (args.Length == 3)
            {
                if (!Int32.TryParse(args[1], out filterColumnIndex) || filterColumnIndex < 1)
                {
                    ReportError($"Param {filterColumnIndexParamName} must be an integer, starting from 1.");
                    return;
                }

                filterColumnValue = args[2];
            }

            try
            {
                //read file
                CSVFileParser        parser  = new CSVFileParser(path);
                CommaSeparatedValues csvData = parser.Read();

                //output to console depending on input parameters
                if (filterColumnValue == null)
                {
                    Console.Write(csvData.ToString());
                }
                else
                {
                    //validate filterColumnIndex
                    if (filterColumnIndex < 1 || filterColumnIndex > csvData.ColumnsCount)
                    {
                        throw new IndexOutOfRangeException(string.Format("{0} is out of range.", nameof(filterColumnIndex)));
                    }
                    int filterColumnIndex0Based = filterColumnIndex - 1;
                    Console.Write(csvData.ToString(filterColumnIndex0Based, filterColumnValue));
                }

                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error during processing. Exception message: {0} ", ex.Message));
            }
        }
コード例 #2
0
ファイル: CSVParserTest.cs プロジェクト: nowwwak/CSVParser
        public void TestRead(string inputFilePath, string[][] expectedCSVFields, Encoding encoding)
        {
            CSVFileParser        parser = new CSVFileParser(Path.Combine(Helper.GetAssemblyPath(), inputFilePath), encoding);
            CommaSeparatedValues csv    = parser.Read();

            Assert.AreEqual(expectedCSVFields.Length > 0 ? expectedCSVFields[0].Length : 0, csv.ColumnsCount);
            Assert.AreEqual(expectedCSVFields.Length, csv.RowsCount);

            for (int i = 0; i < expectedCSVFields.Length; i++)
            {
                for (int j = 0; j < expectedCSVFields[i].Length; j++)
                {
                    Assert.AreEqual(expectedCSVFields[i][j], csv.GetField(i, j));
                }
            }
        }
コード例 #3
0
ファイル: CSVParserTest.cs プロジェクト: nowwwak/CSVParser
        public void TestReadFail(string inputFilePath, Encoding encoding)
        {
            CSVFileParser parser = new CSVFileParser(Path.Combine(Helper.GetAssemblyPath(), inputFilePath), encoding);

            Assert.Throws(typeof(CSVFormatException), () => parser.Read());
        }