Exemple #1
0
        public void DumpCompounds()
        {
            var csv    = new CSVData();
            var header = new CSVData.Row();

            header.Add("Scientific Name").Add("Symbolic Name").Add("Enthalpy").Add("Ionicity").Add("Melting Point").Add("Boiling Point").Add("Strength").Add("Density");
            csv.Add(header);
            foreach (var c in m_compounds)
            {
                var row = new CSVData.Row();
                row.Add(c.ScientificName).Add(c.SymbolicName).Add(c.Enthalpy).Add(c.Ionicity).Add(c.MeltingPoint).Add(c.BoilingPoint).Add(c.Strength).Add(c.Density(0, 0));
                csv.Add(row);
            }
            csv.Save(@"d:\deleteme\compounds.csv");
        }
Exemple #2
0
        public void DumpElements()
        {
            var csv    = new CSVData();
            var header = new Rylogic.Container.CSVData.Row();

            header.Add("Name").Add("AtomicNumber").Add("ValenceElectrons").Add("ValenceHoles").Add("ValenceOrbitalRadius").Add("MolarMass").Add("Electronegativity").Add("Melting Point").Add("Boiling Point").Add("SolidDensity");
            csv.Add(header);
            foreach (var e in m_elements)
            {
                var row = new CSVData.Row();
                row.Add(e.Name.Fullname).Add(e.AtomicNumber).Add(e.ValenceElectrons).Add(e.ValenceHoles).Add(e.ValenceOrbitalRadius).Add(e.MolarMass).Add(e.Electronegativity).Add(e.MeltingPoint).Add(e.BoilingPoint).Add(e.SolidDensity);
                csv.Add(row);
            }
            csv.Save(@"d:\deleteme\elements.csv");
        }
Exemple #3
0
        /// <summary>Export the current code lookup list to a file</summary>
        private void ExportCodeLookupList()
        {
            // Prompt for the file to save
            var dg = new SaveFileDialog {
                Title = "Export code lookup list", Filter = Constants.XmlOrCsvFileFilter
            };

            if (dg.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            try
            {
                string extn = Path.GetExtension(dg.FileName);
                extn = extn != null?extn.Trim(new[] { '.' }) : "";

                // If a csv file is being saved, write out csv
                if (string.CompareOrdinal(extn, "csv") == 0)
                {
                    // Create a CSV object
                    var csv = new CSVData {
                        AutoSize = true
                    };
                    csv.Reserve(Values.Count, 2);
                    foreach (var c in Values)
                    {
                        CSVData.Row row = new CSVData.Row();
                        row.Add(c.Key);
                        row.Add(c.Value);
                        csv.Add(row);
                    }
                    csv.Save(dg.FileName);
                }
                // Otherwise write out xml
                else
                {
                    XDocument doc = new XDocument(new XElement(XmlTag.Root));
                    if (doc.Root == null)
                    {
                        throw new Exception("Failed to create root xml node");
                    }
                    var codes = new XElement(XmlTag.CodeValues);
                    foreach (var v in Values)
                    {
                        codes.Add(new XElement(XmlTag.CodeValue,
                                               new XElement(XmlTag.Code, v.Key),
                                               new XElement(XmlTag.Value, v.Value)
                                               ));
                    }
                    doc.Root.Add(codes);
                    doc.Save(dg.FileName, SaveOptions.None);
                }
            }
            catch (Exception ex)
            {
                Misc.ShowMessage(this, "Export failed.", "Export Failed", MessageBoxIcon.Error, ex);
            }
        }
Exemple #4
0
        public CSVData Read()
        {
            var list = new CSVData();                        //declare dictionary listion

            var lines = Regex.Split(Payload, LINE_SPLIT_RE); // Split data.text into lines using LINE_SPLIT_RE characters

            if (lines.Length <= 1)
            {
                return(list);                             //Check that there is more than one line
            }
            var header = Regex.Split(lines[0], SPLIT_RE); //Split header (element 0)

            // Loops through lines
            for (var i = 1; i < lines.Length; i++)
            {
                var values = Regex.Split(lines[i], SPLIT_RE); //Split lines according to SPLIT_RE, store in var (usually string array)
                if (values.Length == 0 || values[0] == "")
                {
                    continue;             // Skip to end of loop (continue) if value is 0 length OR first value is empty
                }
                var entry = new CSVRow(); // Creates dictionary object

                // Loops through every value
                for (var j = 0; j < header.Length && j < values.Length; j++)
                {
                    string value = values[j];                                                  // Set local variable value
                    value = value.TrimStart(TRIM_CHARS).TrimEnd(TRIM_CHARS).Replace("\\", ""); // Trim characters
                    object finalvalue = value;                                                 //set final value

                    int n;                                                                     // Create int, to hold value if int

                    float f;                                                                   // Create float, to hold value if float

                    // If-else to attempt to parse value into int or float
                    if (int.TryParse(value, out n))
                    {
                        finalvalue = n;
                    }
                    else if (int.TryParse(value, System.Globalization.NumberStyles.HexNumber, new CultureInfo("en-US"), out n))
                    {
                        finalvalue = n;
                    }
                    else if (float.TryParse(value, out f))
                    {
                        finalvalue = f;
                    }
                    entry[header[j]] = finalvalue;
                }
                list.Add(entry); // Add Dictionary ("entry" variable) to list
            }
            return(list);        //Return list
        }
Exemple #5
0
        [Test] public void CSVRoundTrip()
        {
            var csv = new CSVData();

            csv.AddComment("This is a CSV file comment");
            csv.Add("One", "Two", "Three", "\"Four\"", "\",\r\n\"");
            csv.Add("1,1", "2\r2", "3\n3", "4\r\n");
            csv.Add(new CSVData.Row());
            csv.Add("1,1", "2\r2", "3\n3", "4\r\n");

            var tmp = Path.GetTempFileName();

            csv.Save(tmp);

            try
            {
                var load = CSVData.Load(tmp, ignore_comment_rows: true);
                csv.Rows.RemoveAt(0);                 // Delete the comment

                Assert.Equal(csv.RowCount, load.RowCount);
                for (var i = 0; i != csv.RowCount; ++i)
                {
                    var r0 = csv[i];
                    var r1 = load[i];
                    Assert.Equal(r0.Count, r1.Count);

                    for (var j = 0; j != r0.Count; ++j)
                    {
                        var e0 = r0[j];
                        var e1 = r1[j];
                        Assert.Equal(e0, e1);
                    }
                }
            }
            finally
            {
                File.Delete(tmp);
            }
        }
Exemple #6
0
    public static CSVData Load(Stream stream, Encoding encoding, char seperator = ',')
    {
        CSVData result;
        var     reader = new StreamReader(stream, encoding);

        string columnNames;

        do
        {
            columnNames = reader.ReadLine();
        }
        //update by csd 跳过由半角与全角空格组成的空行
        while ((isNullLine(columnNames) == true) || (columnNames.StartsWith("``")));
        //update end
        //while (columnNames.StartsWith("``"));

        result = new CSVData(columnNames, seperator);
        string row;

        while (reader.EndOfStream == false)
        {
            row = reader.ReadLine();
            if (row.StartsWith("``"))
            {
                continue;
            }
            //add by csd 跳过空行
            if (isNullLine(row))
            {
                continue;
            }
            //add end
            result.Add(row, seperator, encoding);
        }
        reader.Close();
        return(result);
    }