public void Write_WhenHeadersShouldBeIncludedAndWritingSecondDataLine_ShouldWriteDataInSameOrderAsHeaderRow()
        {
            var writer = new StringWriter();
            var stream = new CsvStream(writer, true);
            var data1  = new RelationalObject
            {
                Data = new Dictionary <string, object>
                {
                    { "prop1", "value1.1" },
                    { "prop2", "value2.1" }
                }
            };
            var data2 = new RelationalObject
            {
                Data = new Dictionary <string, object>
                {
                    { "prop2", "value2.2" },
                    { "prop1", "value1.2" }
                }
            };

            stream.Write(data1);
            stream.Write(data2);

            var lines      = writer.GetStringBuilder().ToString().Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
            var secondLine = lines.Skip(1).FirstOrDefault();
            var thirdLine  = lines.Skip(2).FirstOrDefault();

            Assert.That(secondLine, Is.EqualTo("value1.1,value2.1"));
            Assert.That(thirdLine, Is.EqualTo("value1.2,value2.2"));
            Assert.That(lines.Count(), Is.EqualTo(3));
        }
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };
            _input =
                @"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington";

            byte[] stringAsByteArray = Encoding.UTF8.GetBytes(_input);
            Stream stream            = new MemoryStream(stringAsByteArray);

            _sr         = new StreamReader(stream, Encoding.UTF8);
            _dataAccess = new FileDataAccess(_sr, _description);

            _cs = new CsvStream(_sr, null, _description.SeparatorChar,
                                _description.IgnoreTrailingSeparatorChar);
            _row    = new DataRow();
            _fm     = new FieldMapperReading <Person>(_description, null, false);
            _ae     = new AggregatedException(typeof(Person).ToString(), null, _description.MaximumNbrExceptions);
            _reader = new RowReader <Person>(_description, _ae);
        }
Exemple #3
0
    public override void ParseCSV(CsvStream csvStream)
    {
        TableSample tableSample = new TableSample();

        tableSample.Load(csvStream);
        m_DataDic[tableSample.index] = tableSample;
    }
        public void Write_WhenGivenNullData_ShouldThrow()
        {
            var writer = new StringWriter();
            var stream = new CsvStream(writer, true);

            Assert.That(() => stream.Write(null), Throws.ArgumentNullException);
        }
 public static DataTable Parse(TextReader stream, bool headers)
 {
     DataTable table = new DataTable();
     CsvStream csv = new CsvStream(stream);
     string[] row = csv.GetNextRow();
     if (row == null)
         return null;
     if (headers)
     {
         foreach (string header in row)
         {
             if (!String.IsNullOrEmpty(header) && !table.Columns.Contains(header))
                 table.Columns.Add(header, typeof(string));
             else
                 table.Columns.Add(GetNextColumnHeader(table), typeof(string));
         }
         row = csv.GetNextRow();
     }
     while (row != null)
     {
         while (row.Length > table.Columns.Count)
             table.Columns.Add(GetNextColumnHeader(table), typeof(string));
         table.Rows.Add(row);
         row = csv.GetNextRow();
     }
     return table;
 }
Exemple #6
0
 public FileDataAccess(StreamReader stream, CsvFileDescription fileDescription)
 {
     _stream         = stream;
     FileDescription = fileDescription;
     Cs = new CsvStream(stream, null, fileDescription.SeparatorChar,
                        fileDescription.IgnoreTrailingSeparatorChar);
 }
        private static DataTable ParseCSV(DataTable table, TextReader stream, bool headers)
        {
            CsvStream csv = new CsvStream(stream);

            string[] row = csv.GetNextRow();
            if (row == null)
            {
                return(null);
            }
            if (headers)
            {
                foreach (string header in row)
                {
                    if (header != null && header.Length > 0 && !table.Columns.Contains(header))
                    {
                        table.Columns.Add(header, typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                row = csv.GetNextRow();
            }
            while (row != null)
            {
                while (row.Length > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }
                table.Rows.Add(row);
                row = csv.GetNextRow();
            }
            return(table);
        }
        private void testToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var    fileName    = openFileDialog1.ShowDialog();
            string item_dbPath = openFileDialog1.FileName;

            csvs = new CsvStream(item_dbPath);
            csvs.dbLoad(item_DataSet);
        }
 // resets resourses
 public override void Reset()
 {
     if (fileName != null)
     {
         sr = new StreamReader(fileName);
         csvStrm = new CsvStream(sr, separator);
     }
 }
Exemple #10
0
 // resets resourses
 public override void Reset()
 {
     if (fileName != null)
     {
         sr      = new StreamReader(fileName);
         csvStrm = new CsvStream(sr, separator);
     }
 }
Exemple #11
0
 public void Load(CsvStream csvStream)
 {
     csvStream.GetVar(ref index);
     csvStream.GetVar(ref data1);
     csvStream.GetVar(ref data2);
     csvStream.GetVar(ref data3);
     csvStream.GetVar(ref data4);
     csvStream.GetVar(ref data5);
 }
Exemple #12
0
 public void Load(CsvStream csvStream)
 {
     csvStream.GetVar(ref index);
     csvStream.GetVar(ref data1);
     csvStream.GetVar(ref data2);
     csvStream.GetVar(ref data3);
     csvStream.GetVar(ref data4);
     csvStream.GetVar(ref data5);
 }
Exemple #13
0
        public string Contain_Csv(LastNewLine lastNewLine, bool header)
        {
            var persons = new[]
            {
                new Person("Alice", 27),
                new Person("Bob", 26)
            };

            using var csvStream = CsvStream
                                  .Of(persons, Configuration(header), lastNewLine)
                                  .Build();
            using var reader = new StreamReader(csvStream, Encoding.UTF8);

            var csv = reader.ReadToEnd();

            return(csv);
        }
Exemple #14
0
        public static DataTable Parse(TextReader stream, CsvHeadersAction Headers, char delimiter)
        {
            DataTable table = new DataTable();
            CsvStream csv   = new CsvStream(stream);

            string[] row = csv.GetNextRow(delimiter);
            if (row == null)
            {
                return(null);
            }
            if (Headers == CsvHeadersAction.SkipHeaderLine)
            {
                row = csv.GetNextRow(delimiter);
            }
            if (Headers == CsvHeadersAction.UseAsColumnNames)
            {
                //foreach (string header in row)
                for (int i = 0; i < row.Length; i++)
                {
                    if (row[i] != null && row[i].Length > 0 && !table.Columns.Contains(row[i]))
                    {
                        table.Columns.Add(row[i], typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                row = csv.GetNextRow(delimiter);
            }
            while (row != null)
            {
                while (row.Length > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }
                table.Rows.Add(row);
                row = csv.GetNextRow(delimiter);
            }
            stream.Close();
            stream.Dispose();
            return(table);
        }
        public void Write_WhenPropertyContainsADoubleQuoteAndComma_ShouldWriteHeaderWithQuotesAndExistingQuotesEscaped()
        {
            var writer = new StringWriter();
            var stream = new CsvStream(writer, true);
            var data   = new RelationalObject
            {
                Data = new Dictionary <string, object>
                {
                    { "prop,1 \"2\"", "value1" }
                }
            };

            stream.Write(data);

            var lines     = writer.GetStringBuilder().ToString().Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
            var firstLine = lines.FirstOrDefault();

            Assert.That(firstLine, Is.EqualTo("\"prop,1 \"\"2\"\"\""));
            Assert.That(lines.Count(), Is.EqualTo(2));
        }
        public void Write_WhenHeadersShouldBeIncluded_ShouldWriteColumnsHeaders()
        {
            var writer = new StringWriter();
            var stream = new CsvStream(writer, true);
            var data   = new RelationalObject
            {
                Data = new Dictionary <string, object>
                {
                    { "prop1", "value1" },
                    { "prop2", "value2" }
                }
            };

            stream.Write(data);

            var lines     = writer.GetStringBuilder().ToString().Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
            var firstLine = lines.FirstOrDefault();

            Assert.That(firstLine, Is.EqualTo("prop1,prop2"));
        }
Exemple #17
0
        public async Task <string> Contain_Csv_Async(LastNewLine lastNewLine, bool header)
        {
            var persons = new[]
            {
                new Person("Charlie", 9),
                new Person("Daisy", 7)
            };

#if !NET472
            await
#endif
            using var csvStream = CsvStream
                                  .Of(persons.ToAsyncEnumerable(), Configuration(header), lastNewLine)
                                  .Build();
            using var reader = new StreamReader(csvStream, Encoding.UTF8);

            var csv = await reader.ReadToEndAsync();

            return(csv);
        }
Exemple #18
0
    public void Load(CsvStream csvStream)
    {
        csvStream.GetVar(ref index);
        csvStream.GetVar(ref damage);
        csvStream.GetVar(ref attack_time);
        csvStream.GetVar(ref motion_name);
        csvStream.GetVar(ref attack_range);

        csvStream.GetVar(ref  m_UnitAttribute.m_damage);
        csvStream.GetVar(ref  m_UnitAttribute.m_damageAdd);
        csvStream.GetVar(ref  m_UnitAttribute.m_damageMultiply);
        csvStream.GetVar(ref  m_UnitAttribute.m_attackSpeed);
        csvStream.GetVar(ref  m_UnitAttribute.m_attackSpeedAdd);
        csvStream.GetVar(ref  m_UnitAttribute.m_attackSpeedMultiply);
        csvStream.GetVar(ref  m_UnitAttribute.m_moveSpeed);
        csvStream.GetVar(ref  m_UnitAttribute.m_moveSpeedAdd);
        csvStream.GetVar(ref  m_UnitAttribute.m_moveSpeedMultiply);
        csvStream.GetVar(ref  m_UnitAttribute.m_hp);
        csvStream.GetVar(ref  m_UnitAttribute.m_hpAdd);
        csvStream.GetVar(ref  m_UnitAttribute.m_hpMultiple);
    }
Exemple #19
0
    public void Load(CsvStream csvStream)
    {
        csvStream.GetVar(ref index);
        csvStream.GetVar(ref damage);
        csvStream.GetVar(ref attack_time);
        csvStream.GetVar(ref motion_name);
        csvStream.GetVar(ref attack_range);

        csvStream.GetVar(ref m_UnitAttribute.m_damage);
        csvStream.GetVar(ref m_UnitAttribute.m_damageAdd);
        csvStream.GetVar(ref m_UnitAttribute.m_damageMultiply);
        csvStream.GetVar(ref m_UnitAttribute.m_attackSpeed);
        csvStream.GetVar(ref m_UnitAttribute.m_attackSpeedAdd);
        csvStream.GetVar(ref m_UnitAttribute.m_attackSpeedMultiply);
        csvStream.GetVar(ref m_UnitAttribute.m_moveSpeed);
        csvStream.GetVar(ref m_UnitAttribute.m_moveSpeedAdd);
        csvStream.GetVar(ref m_UnitAttribute.m_moveSpeedMultiply);
        csvStream.GetVar(ref m_UnitAttribute.m_hp);
        csvStream.GetVar(ref m_UnitAttribute.m_hpAdd);
        csvStream.GetVar(ref m_UnitAttribute.m_hpMultiple);
    }
        public void Write_WhenPropertyHasNullValue_ShouldWriteEmptyValue()
        {
            var writer = new StringWriter();
            var stream = new CsvStream(writer, false);
            var data   = new RelationalObject
            {
                Data = new Dictionary <string, object>
                {
                    { "null-prop", null },
                    { "empty-prop", "" }
                }
            };

            stream.Write(data);

            var lines     = writer.GetStringBuilder().ToString().Split(new[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries);
            var firstLine = lines.FirstOrDefault();

            Assert.That(firstLine, Is.EqualTo(","));
            Assert.That(lines.Count(), Is.EqualTo(1));
        }
Exemple #21
0
        public void Contain_Anonymous_Type_Rows()
        {
            var drinks = new[]
            {
                new
                {
                    Name    = "Green tea",
                    Feature = "Improve brain function",
                    Origin  = "China"
                },
                new
                {
                    Name    = "Coffee",
                    Feature = "Invigorate",
                    Origin  = "Africa"
                },
                new
                {
                    Name    = "Kvass",
                    Feature = "Good for okroshka",
                    Origin  = "Russia"
                }
            };

            using var csvStream = CsvStream.Of(
                      drinks,
                      Configuration()
                      )
                                  .Build();
            using var reader = new StreamReader(csvStream, Encoding.UTF8);

            var csv = reader.ReadToEnd();

            csv.Should().Be(
                "Name,Feature,Origin\n" +
                "Green tea,Improve brain function,China\n" +
                "Coffee,Invigorate,Africa\n" +
                "Kvass,Good for okroshka,Russia"
                );
        }
Exemple #22
0
        public static DataTable Parse(TextReader stream, bool headers)
        {
            var stringResourceImporter = new StringResourceImporter();

            var table = stringResourceImporter.BuildEmptyStringResourceDataTable();

            CsvStream csv = new CsvStream(stream);

            string[] row = csv.GetNextRow();
            if (row == null)
            {
                return(null);
            }
            if (headers)
            {
                foreach (string header in row)
                {
                    if (header != null && header.Length > 0 && !table.Columns.Contains(header))
                    {
                        table.Columns.Add(header, typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                row = csv.GetNextRow();
            }
            while (row != null)
            {
                while (row.Length > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }
                table.Rows.Add(row);
                row = csv.GetNextRow();
            }
            return(table);
        }
Exemple #23
0
        public static DataTable Parse(TextReader stream, bool headers)
        {
            var table = new DataTable();
            var csv   = new CsvStream(stream);
            var row   = csv.GetNextRow();

            if (row == null)
            {
                return(null);
            }

            if (headers)
            {
                foreach (var header in row)
                {
                    if (!string.IsNullOrEmpty(header) && !table.Columns.Contains(header))
                    {
                        table.Columns.Add(header, typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                row = csv.GetNextRow();
            }

            while (row != null)
            {
                while (row.Count > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }

                table.Rows.Add(row);
                row = csv.GetNextRow();
            }
            return(table);
        }
        public static Hashtable StringSplitCsv(
            [DekiScriptParam("comma separated values")] string text
            )
        {
            ArrayList rows        = new ArrayList();
            int       columncount = 0;
            CsvStream csv         = new CsvStream(new StringReader(text));

            string[] row = null;
            do
            {
                row = csv.GetNextRow();
                if (row != null)
                {
                    rows.Add(new ArrayList(row));
                    columncount = Math.Max(columncount, row.Length);
                }
            } while(row != null);


            // ensure that each row has columncount values
            for (int i = 0; i < rows.Count; ++i)
            {
                ArrayList r = (ArrayList)rows[i];
                while (r.Count < columncount)
                {
                    r.Add(string.Empty);
                }
            }

            // set values in result object
            Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase);

            result.Add("rowcount", rows.Count);
            result.Add("columncount", columncount);
            result.Add("values", rows);
            return(result);
        }
Exemple #25
0
        private static void WriteData <T>(IEnumerable <T> values, string fileName, TextWriter stream,
                                          CsvFileDescription fileDescription)
        {
            FieldMapper <T> fm = new FieldMapper <T>(fileDescription, fileName, true);
            CsvStream       cs = new CsvStream(null, stream, fileDescription.SeparatorChar,
                                               fileDescription.IgnoreTrailingSeparatorChar);

            List <string> row = new List <string>();

            // If first line has to carry the field names, write the field names now.
            if (fileDescription.FirstLineHasColumnNames)
            {
                fm.WriteNames(row);
                cs.WriteRow(row, fileDescription.QuoteAllFields);
            }

            foreach (T obj in values)
            {
                // Convert obj to row
                fm.WriteObject(obj, row);
                cs.WriteRow(row, fileDescription.QuoteAllFields);
            }
        }
Exemple #26
0
        public static DataTable Parse(TextReader stream, bool headers)
        {
            DataTable table   = new DataTable();
            CsvStream stream2 = new CsvStream(stream);

            string[] nextRow = stream2.GetNextRow();
            if (nextRow == null)
            {
                return(null);
            }
            if (headers)
            {
                foreach (string str in nextRow)
                {
                    if (!string.IsNullOrEmpty(str) && !table.Columns.Contains(str))
                    {
                        table.Columns.Add(str, typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                nextRow = stream2.GetNextRow();
            }
            while (nextRow != null)
            {
                while (nextRow.Length > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }
                table.Rows.Add(nextRow);
                nextRow = stream2.GetNextRow();
            }
            return(table);
        }
Exemple #27
0
 public abstract void ParseCSV(CsvStream csvStream);
Exemple #28
0
 public CsvStreamBuffer(CsvStream stream)
 {
     this.stream = stream;
 }
Exemple #29
0
 public CsvStreamBuffer(CsvStream stream)
 {
     _stream = stream;
 }
Exemple #30
0
        public static DataTable Parse(Stream strm, bool headers, int RowsToParse, int HeaderLineNum)
        {
            int       aHeaderLineNum = HeaderLineNum;
            DataTable table          = new DataTable();

            TextReader stream = new StreamReader(strm);

            // Get Delimiter row
            String hdr = stream.ReadLine();

            // Skip lines before header
            while (aHeaderLineNum > 1)
            {
                hdr = stream.ReadLine();
                aHeaderLineNum--;
            }

            List <int> lst = new List <int>();

            Delimiter = ',';
            int commapos = hdr.IndexOf(','); if (commapos != -1)
            {
                lst.Add(commapos);
            }
            int semicolonpos = hdr.IndexOf(';'); if (semicolonpos != -1)
            {
                lst.Add(semicolonpos);
            }
            int fencepos = hdr.IndexOf('|'); if (fencepos != -1)
            {
                lst.Add(fencepos);
            }
            int tabpos = hdr.IndexOf('\t'); if (tabpos != -1)

            {
                lst.Add(tabpos);
            }

            // First found delimiter will be CSV delimiter
            if (lst.Count > 0)
            {
                Delimiter = hdr[lst.Min()];
            }


            //stream.Close();
            strm.Seek(0, SeekOrigin.Begin);
            stream = new StreamReader(strm);

            CsvStream csv = new CsvStream(stream, Delimiter);

            string[] row = csv.GetNextRow();

            // Skip lines before header
            aHeaderLineNum = HeaderLineNum;
            while (aHeaderLineNum > 1)
            {
                row = csv.GetNextRow();
                aHeaderLineNum--;
            }

            if (row == null)
            {
                return(null);
            }
            if (headers)
            {
                foreach (string header in row)
                {
                    if (header != null && header.Length > 0 && !table.Columns.Contains(header))
                    {
                        table.Columns.Add(header.Trim(), typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                row = csv.GetNextRow();
            }

            int counter = RowsToParse;

            while (row != null)
            {
                while (row.Length > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }
                table.Rows.Add(row);
                row = csv.GetNextRow();

                if (RowsToParse > 0)
                {
                    counter--;
                    if (counter == 0)
                    {
                        break;
                    }
                }
            }

            stream.Close();

            return(table);
        }
Exemple #31
0
 public abstract void ParseCSV(CsvStream csvStream);
Exemple #32
0
 public override void ParseCSV(CsvStream csvStream)
 {
     TableSample tableSample = new TableSample();
     tableSample.Load(csvStream);
     m_DataDic[tableSample.index] = tableSample;
 }
        public static Hashtable StringSplitCsv(
            [DekiScriptParam("comma separated values")] string text
        ) {
            ArrayList rows = new ArrayList();
            int columncount = 0;
            CsvStream csv = new CsvStream(new StringReader(text));

            string[] row = null;
            do {
                row = csv.GetNextRow();
                if(row != null) {
                    rows.Add(new ArrayList(row));
                    columncount = Math.Max(columncount, row.Length);
                }
            } while(row != null);


            // ensure that each row has columncount values
            for(int i = 0; i < rows.Count; ++i) {
                ArrayList r = (ArrayList)rows[i];
                while(r.Count < columncount) {
                    r.Add(string.Empty);
                }
            }

            // set values in result object
            Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase);
            result.Add("rowcount", rows.Count);
            result.Add("columncount", columncount);
            result.Add("values", rows);
            return result;
        }
Exemple #34
0
 public static DataTable Parse(TextReader stream, CsvHeadersAction Headers, char delimiter)
 {
     DataTable table = new DataTable();
     CsvStream csv = new CsvStream(stream);
     string[] row = csv.GetNextRow(delimiter);
     if (row == null)
         return null;
     if (Headers == CsvHeadersAction.SkipHeaderLine)
     {
         row = csv.GetNextRow(delimiter);
     }
     if (Headers == CsvHeadersAction.UseAsColumnNames)
     {
         //foreach (string header in row)
         for (int i = 0;i<row.Length;i++)
         {
             if (row[i] != null && row[i].Length > 0 && !table.Columns.Contains(row[i]))
                 table.Columns.Add(row[i], typeof(string));
             else
                 table.Columns.Add(GetNextColumnHeader(table), typeof(string));
         }
         row = csv.GetNextRow(delimiter);
     }
     while (row != null)
     {
         while (row.Length > table.Columns.Count)
             table.Columns.Add(GetNextColumnHeader(table), typeof(string));
         table.Rows.Add(row);
         row = csv.GetNextRow(delimiter);
     }
     stream.Close();
     stream.Dispose();
     return table;
 }
Exemple #35
0
 public CsvStreamBuffer(CsvStream stream)
 {
     this.stream = stream;
 }