Ejemplo n.º 1
0
        public void ReadFile()
        {
            if (FilePath == null) return;

            var parser = new TextFieldParser(FilePath)
            {
                TextFieldType = FieldType.Delimited,
                CommentTokens = new[] {"#"}
            };

            parser.SetDelimiters(",");
            parser.HasFieldsEnclosedInQuotes = false;

            parser.ReadLine();

            while (!parser.EndOfData)
            {
                var row = parser.ReadFields();
                if (row == null) continue;
                var newLine = new List<string>(row.Length);
                newLine.AddRange(row);

                _data.Add(newLine);
            }
        }
Ejemplo n.º 2
0
        public IEnumerable<Brand> Read()
        {
using (TextFieldParser parser = new TextFieldParser(path))
{
    parser.CommentTokens = new string[] { "#" };
    parser.SetDelimiters(new string[] { ";" });
    parser.HasFieldsEnclosedInQuotes = true;

    // Skip over header line.
    parser.ReadLine();

    while (!parser.EndOfData)
    { 
        string[] fields = parser.ReadFields();
        yield return new Brand() 
        {
            Name = fields[0],
            FactoryLocation = fields[1],
            EstablishedYear = int.Parse(fields[2]),
            Profit = double.Parse(fields[3], swedishCulture)
        };
    }
}
            
        }
        public void ReadLine_SampleWithNewlineInQuotedField()
        {
            const string input = @"Name,Birth Date
""Creed, Apollo"",1942-08-17
""Ivan 
Drago"",1961-11-03
""Robert """"Rocky"""" Balboa"",1945-07-06";

            var parserReader = new StringReader(input);
            var parser       = new NotVisualBasic.FileIO.CsvTextFieldParser(parserReader);
            var vbParser     = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(input));

            vbParser.SetDelimiters(",");

            Assert.Equal(vbParser.ReadFields(), parser.ReadFields());
            Assert.Equal(vbParser.ReadFields(), parser.ReadFields());
            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());

            // The readline should have read into the middle of the field, which changes the parsing output
            Assert.Equal(new[] { @"Drago""", "1961-11-03" }, vbParser.ReadFields());
            Assert.Equal(new[] { @"Drago""", "1961-11-03" }, parser.ReadFields());

            Assert.Equal(vbParser.ReadFields(), parser.ReadFields());
            Assert.Null(vbParser.ReadFields());
            Assert.Null(parser.ReadFields());
            Assert.True(vbParser.EndOfData);
            Assert.True(parser.EndOfData);
        }
        public void ReadLine_Sample()
        {
            const string input = @"Name,Birth Date
Apollo Creed,1942-08-17
Ivan Drago,1961-11-03";

            var parserReader = new StringReader(input);
            var parser       = new NotVisualBasic.FileIO.CsvTextFieldParser(parserReader);
            var vbParser     = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(input));

            vbParser.SetDelimiters(",");

            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());
            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());
            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());
            Assert.Null(vbParser.ReadFields());
            Assert.Null(parser.ReadFields());
            Assert.True(vbParser.EndOfData);
            Assert.True(parser.EndOfData);
        }
Ejemplo n.º 5
0
        static AnimationData()
        {
            Fallback = new Dictionary<ushort, ushort>();
            NameToId = new Dictionary<string, ushort>();
            IdToName = new Dictionary<ushort, string>();
            PlayThenStop = new HashSet<ushort>();
            PlayBackwards = new HashSet<ushort>();
            var assembly = Assembly.GetExecutingAssembly();
            var embeddedStream = assembly.GetManifestResourceStream("M2Lib.src.csv.AnimationData.csv");
            Debug.Assert(embeddedStream != null, "Could not open embedded ressource AnimationData");
            var csvParser = new TextFieldParser(embeddedStream) {CommentTokens = new[] {"#"}};
            csvParser.SetDelimiters(",");
            csvParser.HasFieldsEnclosedInQuotes = true;
            csvParser.ReadLine(); // Skip first line
            while (!csvParser.EndOfData)
            {
                var fields = csvParser.ReadFields();
                Debug.Assert(fields != null);
                var id = Convert.ToUInt16(fields[0]);
                var name = fields[1];
                var fallback = Convert.ToUInt16(fields[3]);
                Fallback[id] = fallback;
                NameToId[name] = id;
                IdToName[id] = name;
            }
            csvParser.Close();
            ushort[] playThenStopValues =
            {
                NameToId["Dead"],
                NameToId["SitGround"],
                NameToId["Sleep"],
                NameToId["KneelLoop"],
                NameToId["UseStandingLoop"],
                NameToId["Drowned"],
                NameToId["LootHold"]
            };
            foreach (var value in playThenStopValues) PlayThenStop.Add(value);
            ushort[] playBackwardsValues =
            {
                NameToId["Walkbackwards"],
                NameToId["SwimBackwards"],
                NameToId["SleepUp"],
                NameToId["LootUp"]
            };
            foreach (var value in playBackwardsValues) PlayBackwards.Add(value);

            //TODO FIXME There a loops by following the fallbacks in AnimationData.dbc. Happens with Close and FlyClose.
            Fallback[146] = 0;//Close
            Fallback[375] = 0;//FlyClose
        }
Ejemplo n.º 6
0
        public static IEnumerable<string[]> ParseCsv(string path, string delimiters = ",", bool hasHeader = true, bool hasFieldsEnclosedInQuotes = true)
        {
            using (var parser = new TextFieldParser(new StringReader(path)))
            {
                parser.SetDelimiters(delimiters);
                parser.HasFieldsEnclosedInQuotes = hasFieldsEnclosedInQuotes;
                if (hasHeader)
                    parser.ReadLine();
                while (!parser.EndOfData)
                {
                    var fields = parser.ReadFields();

                    if (fields != null)
                        yield return fields;
                }
            }
        }
        public IEnumerable<Quote> Parse(string csvBody)
        {
            using (TextFieldParser parser = new TextFieldParser(
                new MemoryStream(Encoding.UTF8.GetBytes(csvBody))))
            {
                parser.CommentTokens = new[] { "#" };
                parser.SetDelimiters(",");
                parser.HasFieldsEnclosedInQuotes = true;

                // Skip over header line.
                parser.ReadLine();

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    Quote q = null;
                    try
                    {
                        if (fields != null)
                        {
                            var date = ExtractDate(fields[0]);
                            if (date != DateTime.MinValue)
                            {
                                q = new Quote
                                {
                                    Date = date,
                                    Close = double.Parse(fields[1]),
                                    Volume = double.Parse(fields[2]),
                                    Open = double.Parse(fields[3]),
                                    High = double.Parse(fields[4]),
                                    Low = double.Parse(fields[5]),
                                };
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                    if (q != null)
                        yield return q;
                }
            }
        }
        /*
        "Symbol","Name","LastSale","MarketCap","ADR TSO","IPOyear","Sector","industry","Summary Quote",
        "DDD","3D Systems Corporation","28.38","3156341099.34","n/a","n/a","Technology","Computer Software: Prepackaged Software","http://www.nasdaq.com/symbol/ddd",
         */
        public IEnumerable<Company> Parse(string csvCompanyList)
        {
            using (var parser = new TextFieldParser(
                new MemoryStream(Encoding.UTF8.GetBytes(csvCompanyList))))
            {
                parser.CommentTokens = new[] { "#" };
                parser.SetDelimiters(",");
                parser.HasFieldsEnclosedInQuotes = true;

                // Skip over header line.
                parser.ReadLine();

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    Company company = null;
                    try
                    {
                        if (fields != null)
                        {
                            company = new Company
                                {
                                    Symbol = fields[0].Trim().ToUpper(),
                                    Name = fields[1].Trim(),
                                    LastSale = double.Parse(fields[2]),
                                    MarketCap = double.Parse(fields[3]),
                                    AdrTso = fields[4],
                                    IPOyear = fields[5],
                                    Sector = fields[6],
                                    Industry = fields[7],
                                    SummaryUrl = fields[8],
                                };
                        }
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                    if(company != null)
                        yield return company;
                }
            }
        }
		public static Dictionary<string, double> GetStateTaxRates(String filename)
		{
			if (! File.Exists(filename))
			{
				throw new Exception("file not found: " + filename, new FileNotFoundException());
			}

			var stateTaxRates = new Dictionary<string, double>();

			using (var parser = new TextFieldParser(filename))
			{
				parser.TextFieldType = FieldType.Delimited;
				parser.SetDelimiters(",");

				bool firstRow = true;
				while (! parser.EndOfData)
				{
					if (firstRow) // skip header
					{
						parser.ReadLine();
						firstRow = false;
						continue;
					} 
					
					String[] fields = parser.ReadFields();
					if (fields.Length >= 2) // (i.e. state, taxRate)
					{
						string state = fields[0].Trim();
						double taxRate = Convert.ToDouble(fields[1].Trim());
						stateTaxRates.Add(state, taxRate);
					}
					else
					{
						throw new Exception("unexpected field length" + fields.Length + " in " + filename);
					}
				}
			}

			return stateTaxRates;
		}
Ejemplo n.º 10
0
        public static Dictionary<string, List<ImageRow>> LoadImageDS()
        {
            var result = new Dictionary<string, List<ImageRow>>();

            using (var parser = new TextFieldParser(@"C:\Users\Paul\Desktop\Datasets\images.csv"))
            {
                parser.CommentTokens = new string[] { "#" };
                parser.SetDelimiters(new string[] { "," });
                parser.HasFieldsEnclosedInQuotes = true;

                parser.ReadLine();

                while (!parser.EndOfData)
                {
                    var fields = parser.ReadFields();

                    var row = new ImageRow();
                    row.Country = fields[0];
                    row.Caption = fields[1];
                    row.Credit = fields[2];
                    row.Url = fields[3];

                    if (result.ContainsKey(row.Country))
                    {
                        result[row.Country].Add(row);
                    }
                    else
                    {
                        var list = new List<ImageRow>();
                        list.Add(row);
                        result.Add(row.Country, list);
                    }
                }
            }

            return result;
        }
Ejemplo n.º 11
0
        private static IList<CountryOrRegionGdpData> ParseDataFile(FileInfo fileInfoData)
        {
            IList<CountryOrRegionGdpData> toReturn = new List<CountryOrRegionGdpData>();

            using (TextFieldParser parser = new TextFieldParser(fileInfoData.FullName))
            {
                parser.SetDelimiters("\t");

                // Skip first line since it only contains headers
                parser.ReadLine();

                NumberFormatInfo numberFormatInfo = new NumberFormatInfo()
                {
                    NumberDecimalSeparator = ".",
                };

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();

                    toReturn.Add(new CountryOrRegionGdpData()
                    {
                        CountryCode = fields[3],
                        CountryName = fields[2],
                        GdpYear1960 = string.IsNullOrWhiteSpace(fields[5]) ? (decimal?)null : Convert.ToDecimal(fields[5], numberFormatInfo),
                        GdpYear1970 = string.IsNullOrWhiteSpace(fields[15]) ? (decimal?)null : Convert.ToDecimal(fields[15], numberFormatInfo),
                        GdpYear1980 = string.IsNullOrWhiteSpace(fields[25]) ? (decimal?)null : Convert.ToDecimal(fields[25], numberFormatInfo),
                        GdpYear1990 = string.IsNullOrWhiteSpace(fields[35]) ? (decimal?)null : Convert.ToDecimal(fields[35], numberFormatInfo),
                        GdpYear2000 = string.IsNullOrWhiteSpace(fields[45]) ? (decimal?)null : Convert.ToDecimal(fields[45], numberFormatInfo),
                        GdpYear2010 = string.IsNullOrWhiteSpace(fields[55]) ? (decimal?)null : Convert.ToDecimal(fields[55], numberFormatInfo),
                    });
                }
            }

            return toReturn;
        }
Ejemplo n.º 12
0
        private void ImportGasRate(string path)
        {

            string fileLocation = Path.Combine(path, "GAS.csv");
            if (!(File.Exists(fileLocation)))
            {
                MessageBox.Show("GAS.CSV file is missing");
                return;
            }
            using (TextFieldParser csvParser = new TextFieldParser(fileLocation))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                // Skip the row if the first row has the column names
                if (headerComboBox.Text == "Yes")
                {
                    csvParser.ReadLine();
                }

                inserted = 0;
                failed = 0;
                //public static String utilCompanyTable = "UtilityCompany";
                //public static String[] utilCompanyColumns = {"CompanyName", "IsElectricity", "IsGas", "IsWater"};

                while (!csvParser.EndOfData)
                {
                    // Read current line fields, pointer moves to the next line.
                    string[] fields = csvParser.ReadFields();
                    if (fields.Length == 1) break; // real record should have more than one column
                    int count = 0;
                    foreach (string field in fields)
                    {
                        if (field == "") { fields[count] = "0.0"; }
                        count = count + 1;
                    }
                    //check if the Utility company already existed in the database
                    // if not, insert the new one
                    int result = checkIfUtilCompanyExist(fields[2], 'G');
                    int utilCompanyID = -1;
                    switch (result)
                    {
                        case 0:
                            updateUtilCompany(fields[2], 'G');
                            utilCompanyID = getUtilCompanyID(fields[2], 'G');
                            break;
                        case 1:
                            utilCompanyID = getUtilCompanyID(fields[2], 'G');
                            break;
                        case -1:
                            utilCompanyID = insertUtilCompany(fields[2], 'G');
                            inserted = inserted + 1;
                            break;
                        default:
                            // system error
                            return;
                    }

                    // insert utility rate
                    int gasTierSetID = 6;
                    int uRateID = insertUtilRate(fields, 'G', utilCompanyID, path, gasTierSetID);
                    object[] tierRateValue = { 0.0, 0.0, 0.0, 0.0, 0.0 };
                    tierRateValue[0] = fields[21];
                    tierRateValue[1] = fields[24];
                    tierRateValue[2] = fields[27];
                    insertTierRate(tierRateValue, 'G', "Regular",       uRateID);  // G for Generation
                    tierRateValue[0] = fields[22];
                    tierRateValue[1] = fields[25];
                    tierRateValue[2] = fields[28];
                    insertTierRate(tierRateValue, 'G', "CARE",          uRateID);
                    insertTierRate(tierRateValue, 'G', "CARE/Medical",  uRateID);
                    tierRateValue[0] = fields[23];
                    tierRateValue[1] = fields[26];
                    tierRateValue[2] = fields[29];
                    insertTierRate(tierRateValue, 'G', "Senior",        uRateID);
                    
                    DateTime summerStartDate = new DateTime(utilStartDate.Year, 05, 01);
                    DateTime winterStartDate = new DateTime(utilStartDate.Year, 11, 01);
                    int x = 38; 
                    
                    if ((utilStartDate > summerStartDate) & (utilStartDate < winterStartDate))  
                    { x = 38; }    // summer allowance rate column starts here
                    else
                    { x = 68; }    // winter allowance rate column starts here
                    Object[] value = 
                                    {uRateID,
                                    '1',                       // All Services allowance
                                    '1',                       // Zone
                                    fields[x]                  // Allowance
                                    };
                    for (int i = 0; i < 10; i = i + 1)
                    {
                        value[2] = i;
                        value[1] = '1';                             // All Services allowance
                        value[3] = fields[x + 10 + i];
                        DatabaseControl.executeInsertQuery(DatabaseControl.baselineAllowanceTable, DatabaseControl.baselineAllowanceColumns, value);
                        value[1] = '2';                             // Cooking only Services allowance
                        value[3] = fields[x + 20 + i];
                        DatabaseControl.executeInsertQuery(DatabaseControl.baselineAllowanceTable, DatabaseControl.baselineAllowanceColumns, value);
                        value[1] = '3';                             // Space Heating only Services allowance
                        value[3] = fields[x  + i];
                        DatabaseControl.executeInsertQuery(DatabaseControl.baselineAllowanceTable, DatabaseControl.baselineAllowanceColumns, value);
                    }
                    //insert medical baseline allowance
                    if ((utilStartDate > summerStartDate) & (utilStartDate < winterStartDate))
                    { x = 36; }    // summer medical baseline allowance rate column 
                    else
                    { x = 37; }    // winter medical baseline allowance rate column
                    value[2] = 'M';
                    value[1] = 'M';                                 // All Services allowance
                    value[3] = fields[x];
                    DatabaseControl.executeInsertQuery(DatabaseControl.baselineAllowanceTable, DatabaseControl.baselineAllowanceColumns, value);

                    //insert utility basic rates
                    value[1] = "Regular";                     // Tenant type
                    value[2] = 0;                             // minimum Services rate
                    value[3] = fields[18];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Regular";                     // Tenant type
                    value[2] = 1;                             // all Services rate
                    value[3] = fields[6];                     // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Regular";                     // Tenant type
                    value[2] = 2;                             // cooking only Services rate
                    value[3] = fields[9];                     // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Regular";                     // Tenant type
                    value[2] = 3;                             // space heating only Services rate
                    value[3] = fields[9];                     // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE";                        // Tenant type
                    value[2] = 0;                             // minimum Services rate
                    value[3] = fields[19];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE/Medical";                // Tenant type
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE";                        // Tenant type
                    value[2] = 1;                             // all Services rate
                    value[3] = fields[7];                     // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE/Medical";                // Tenant type
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE";                        // Tenant type
                    value[2] = 2;                             // cooking only Services rate
                    value[3] = fields[10];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE/Medical";                // Tenant type
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE";                        // Tenant type
                    value[2] = 3;                             // space heating only Services rate
                    value[3] = fields[10];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE/Medical";                // Tenant type
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Senior";                      // Tenant type
                    value[2] = 0;                             // minimum Services rate
                    value[3] = fields[20];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Senior";                      // Tenant type
                    value[2] = 1;                             // all Services rate
                    value[3] = fields[8];                     // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Senior";                      // Tenant type
                    value[2] = 2;                             // cooking only Services rate
                    value[3] = fields[11];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Senior";                      // Tenant type
                    value[2] = 3;                             // space heating only Services rate
                    value[3] = fields[11];                    // basic rate
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    inserted = inserted + 1;

                    //insert Surcharge record
                    Object[] values = 
                    {
                        uRateID,    
                        "Inspection Fee",            //Description
                        "All",                       //Tenant Type, Regular, CARE, Medical, ...
                        'D',
                        1,                           // 1- Flat Rate 2- Usage 3-Daily
                        fields[15]
                    };
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilSurchargeTable, DatabaseControl.utilSurchargeColumns, values);
                    values[1] = "LIRA Surcharge";    //Description
                    values[5] =  fields[33];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilSurchargeTable, DatabaseControl.utilSurchargeColumns, values);
                }
                string s = "File imported with " + inserted.ToString() + " records! ";
                if (failed > 0)
                {
                    s = s + " and " + failed.ToString() + " records failed!";
                }
                MessageBox.Show(s);

            }

        }
Ejemplo n.º 13
0
        private void openButton_Click(object sender, EventArgs e)
        {
            List <string[]> parsedData = new List <string[]>();

            try
            {
                // Detect the path of documents folder and assign it to initial directory path
                String myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFile.InitialDirectory = myDocument;

                if (openFile.ShowDialog() == DialogResult.OK)
                {
                    Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(openFile.FileName);
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.TrimWhiteSpace = true;
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();



                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        parsedData.Add(fields);
                        Trace.WriteLine(parsedData);

                        /*foreach (string field in fields)
                         * {
                         *  parsedData.Add(field.ToString());
                         *  Trace.WriteLine(field);
                         *
                         * }
                         */
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 14
0
        public void PeekTest()
        {
            using (StringReader reader = new StringReader ("abcd" + Constants.vbNewLine + "efgh" + Constants.vbNewLine + "'comment" + Constants.vbNewLine + "after comment" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                Assert.AreEqual ("a", t.PeekChars (1), "#01");
                Assert.AreEqual ("a", t.PeekChars (1), "#02");
                Assert.AreEqual ("ab", t.PeekChars (2), "#03");
                Assert.AreEqual ("abcd", t.PeekChars (10), "#04");
                Assert.AreEqual ("abcd", t.ReadLine (), "#05");
                Assert.AreEqual ("ef", t.PeekChars (2), "#06");

                try {
                    t.PeekChars (0);
                } catch (ArgumentException ex){
                    Helper.RemoveWarning (ex);
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#07 - Expected 'ArgumentException'");
                }

                try {
                    t.PeekChars (-1);
                } catch (ArgumentException ex) {
                    Helper.RemoveWarning (ex);
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#08 - Expected 'ArgumentException'");
                }

                Assert.AreEqual ("efgh", t.PeekChars (10), "#09");
                Assert.AreEqual ("efgh", t.ReadLine (), "#10");
                t.CommentTokens = new string [] {"'"};
                Assert.AreEqual ("afte", t.PeekChars (4), "#11");
                Assert.AreEqual ("'comment", t.ReadLine (), "#12");
                Assert.AreEqual ("af", t.PeekChars (2), "#13");
                Assert.AreEqual ("after comment", t.ReadLine (), "#14");
            }
        }
Ejemplo n.º 15
0
        private void ImportWatRate(string path)
        {
            string fileLocation = Path.Combine(path, "WATER.csv");
            if (!(File.Exists(fileLocation)))
            {
                MessageBox.Show("Water.CSV file is missing");
                return;
            }
            using (TextFieldParser csvParser = new TextFieldParser(fileLocation))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                // Skip the row if the first row has the column names
                if (headerComboBox.Text == "Yes")
                {
                    csvParser.ReadLine();
                }

                inserted = 0;
                failed = 0;
                //public static String utilCompanyTable = "UtilityCompany";
                //public static String[] utilCompanyColumns = {"CompanyName", "IsElectricity", "IsGas", "IsWater"};
        
                while (!csvParser.EndOfData)
                {
                    // Read current line fields, pointer moves to the next line.
                    string[] fields = csvParser.ReadFields();
                    if (fields.Length == 1) break; // real record should have more than one column
                    int count = 0;
                    foreach (string field in fields)
                    {
                        if (field == "") { fields[count] = "0.0"; }
                        count = count + 1;
                    }
                    //check if the Utility company already existed in the database
                    // if not, insert the new one
                    int result = checkIfUtilCompanyExist(fields[2], 'W');
                    int utilCompanyID =-1;
                    switch (result)
                    {
                        case 0:
                            updateUtilCompany(fields[2], 'W');
                            break;
                        case 1:
                            break;
                        case -1:
                            utilCompanyID = insertUtilCompany(fields[2], 'W');
                            inserted = inserted + 1;
                            break;
                        default:
                            break;
                    }
                    utilCompanyID = getUtilCompanyID(fields[2], 'W');
                    // insert utility rate
                    int watTierSetID = 4;
                    int uRateID = insertUtilRate(fields, 'W', utilCompanyID, path, watTierSetID);
                    object[] tierRateValue = {0.0,0.0,0.0,0.0,0.0};
                    tierRateValue[0] = fields[23];
                    tierRateValue[1] = fields[27];
                    tierRateValue[2] = fields[31];
                    insertTierRate(tierRateValue, 'G', "Regular", uRateID);
                    tierRateValue[0] = fields[24];
                    tierRateValue[1] = fields[28];
                    tierRateValue[2] = fields[32];
                    insertTierRate(tierRateValue, 'G', "CARE", uRateID);
                    insertTierRate(tierRateValue, 'G', "CARE/Medical", uRateID);
                    tierRateValue[0] = fields[25];
                    tierRateValue[1] = fields[29];
                    tierRateValue[2] = fields[33];
                    insertTierRate(tierRateValue, 'G', "Senior", uRateID);
                    tierRateValue[0] = fields[26];
                    tierRateValue[1] = fields[30];
                    tierRateValue[2] = fields[34];
                    insertTierRate(tierRateValue, 'S', "All", uRateID);  //Sewer charges

                    insertBaselineAllowance('1', 'S', uRateID, double.Parse(fields[45]));
                    insertBaselineAllowance('1', 'W', uRateID, double.Parse(fields[47]));
                    insertBaselineAllowance('2', 'S', uRateID, double.Parse(fields[49]));
                    insertBaselineAllowance('2', 'W', uRateID, double.Parse(fields[51]));
                    insertBaselineAllowance('3', 'S', uRateID, double.Parse(fields[46]));
                    insertBaselineAllowance('3', 'W', uRateID, double.Parse(fields[48]));
                    insertBaselineAllowance('4', 'S', uRateID, double.Parse(fields[50]));
                    insertBaselineAllowance('4', 'W', uRateID, double.Parse(fields[52]));
                    Object[] value = {  uRateID,
                                        "Regular",                 
                                        0,                  //minimum rate
                                        fields[20]          //Regulau Rate
                                     };
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[2] = 'W';                           //regular basic rate
                    value[3] = fields[6];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE"; 
                    value[2] = 0;                           //minimum basic rate
                    value[3] = fields[21];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE";
                    value[2] = 'W';                           //regular basic rate
                    value[3] = fields[7];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE/Medical";
                    value[2] = 0;                           //minimum basic rate
                    value[3] = fields[21];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "CARE/Medical";
                    value[2] = 'W';                           //regular basic rate
                    value[3] = fields[7];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Senior";
                    value[2] = 0;                           //minimum basic rate
                    value[3] = fields[22];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "Senior";
                    value[2] = 'W';                           //regular basic rate
                    value[3] = fields[8];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    value[1] = "All";
                    value[2] = '3';                           //Sewer basic rate
                    value[3] = fields[9];
                    DatabaseControl.executeInsertQuery(DatabaseControl.utilBasicRatesTable, DatabaseControl.utilBasicRatesColumns, value);
                    inserted = inserted + 1;
                }
                string s = "File imported with " + inserted.ToString() + " records! ";
                if (failed > 0)
                {
                    s = s + " and " + failed.ToString() + " records failed!";
                }
                MessageBox.Show(s);

            }
        }
Ejemplo n.º 16
0
        private void CheckForThemes()
        {
            try
            {
                var currentThemeParser =
                    new TextFieldParser(new FileStream("Theme\\theme.csv", FileMode.Open, FileAccess.Read,
                        FileShare.Read));

                currentThemeParser.SetDelimiters(",");

                // go through header
                currentThemeParser.ReadLine();

                CurrentTheme = GetThemeModel(currentThemeParser.ReadFields());
                OnPropertyChanged("CurrentTheme");

                currentThemeParser.Close();

                HasCurrentTheme = true;
                OnPropertyChanged("HasCurrentTheme");
            }
            catch
            {
                HasCurrentTheme = false;
                OnPropertyChanged("HasCurrentTheme");
            }

            try
            {
                var resp = browser.GetResponse(Constants.ThemeIndexUrl);
                if (resp == null) return;

                var parser = new TextFieldParser(new StringReader(resp))
                {
                    TextFieldType = FieldType.Delimited
                };

                parser.SetDelimiters(",");

                // go through header
                parser.ReadLine();

                Dispatcher.BeginInvoke((Action) (() => Themes.Clear()));
                while (!parser.EndOfData)
                {
                    var row = parser.ReadFields();
                    var model = GetThemeModel(row);

                    if (HasCurrentTheme && model.Name == CurrentTheme.Name && model.Version == CurrentTheme.Version)
                        continue;

                    Dispatcher.BeginInvoke((Action) (() => Themes.Add(model)));
                }

                parser.Close();
            }
            catch (WebException)
            {
            }
        }
Ejemplo n.º 17
0
        public static List<GodStat> ParseGods(string file)
        {
            List<GodStat> gods = new List<GodStat>();

            using(StringReader reader = new StringReader(client.DownloadString(file))) {
                using(TextFieldParser parser = new TextFieldParser(reader)) {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.ReadLine();

                    while(!parser.EndOfData) {
                        int i = 0;
                        string[] fields = parser.ReadFields();
                        if(fields == null) throw new ArgumentException("not a valid file", "file");

                        GodStat god = new GodStat();
                        god.Name = fields[i++];
                        god.PowerType = (GodType)Enum.Parse(typeof(GodType), fields[i++], true);

                        god.BaseHealth = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.HealthScaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        god.BasePhysProtection = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.PhysProtectionScaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        god.BaseMagicalProtection = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.MagicalProtectionScaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        god.BaseAttackSpeed = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.AttackSpeedScaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        god.BaseAttack = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.AttackScaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        god.BaseMana = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.ManaScaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        GodAbility[] abilities = new GodAbility[4];
                        for(int a = 0; a < 4; a++) {
                            abilities[a] = new GodAbility();

                            abilities[a].Name = fields[i++];
                            abilities[a].BaseDamage = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                            abilities[a].Rank = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                            abilities[a].Scaling = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                            abilities[a].AbilityType = (AbilityType)Enum.Parse(typeof(AbilityType), fields[i++], true);
                            abilities[a].Refire = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                            abilities[a].Precast = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                            abilities[a].Postcast = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                            string duration = fields[i++];
                            abilities[a].Duration = duration == "" ? 0 : duration == "Infinite" ? Double.PositiveInfinity : Double.Parse(duration, CultureInfo.InvariantCulture);
                        }

                        god.FirstAbility = abilities[0];
                        god.SecondAbility = abilities[1];
                        god.ThirdAbility = abilities[2];
                        god.UltimateAbility = abilities[3];

                        god.Passive = fields[i++];
                        god.BaseHp5 = Double.Parse(fields[i++], CultureInfo.InvariantCulture);
                        god.Hp5Scaling = Double.Parse(fields[i], CultureInfo.InvariantCulture);

                        gods.Add(god);
                    }
                }
            }

            using(StringReader reader = new StringReader(client.DownloadString("https://docs.google.com/spreadsheets/d/1a6LlTs8BNXHIwGicqM0TFXEvGwk77VXAxGxi0Xqd8Dk/export?format=csv&id=1a6LlTs8BNXHIwGicqM0TFXEvGwk77VXAxGxi0Xqd8Dk&gid=257000868"))) {
                using(TextFieldParser parser = new TextFieldParser(reader)) {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.ReadLine();

                    while(!parser.EndOfData) {
                        int i = 0;
                        string[] fields = parser.ReadFields();
                        if(fields == null) throw new ArgumentException("not a valid file", "file");

                        string name = fields[i++];
                        int god = gods.FindIndex(g => g.Name == name);

                        gods[god].FirstSteroid = new GodSteroid() {
                            Enabled = fields[i++],
                            Disabled = fields[i++]
                        };

                        gods[god].SecondSteroid = new GodSteroid() {
                            Enabled = fields[i++],
                            Disabled = fields[i++]
                        };

                        gods[god].FirstSpecial = new GodSpecial() {
                            Name = fields[i++],
                            BaseDamage = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            CastType = (AbilityType)Enum.Parse(typeof(AbilityType), CheckEnum(fields[i++]), true),
                            Precast = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            Postcast = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            Duration = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture)
                        };

                        gods[god].SecondSpecial = new GodSpecial() {
                            Name = fields[i++],
                            BaseDamage = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            CastType = (AbilityType)Enum.Parse(typeof(AbilityType), CheckEnum(fields[i++]), true),
                            Precast = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            Postcast = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            Duration = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture)
                        };

                        gods[god].ThirdSpecial = new GodSpecial() {
                            Name = fields[i++],
                            BaseDamage = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            CastType = (AbilityType)Enum.Parse(typeof(AbilityType), CheckEnum(fields[i++]), true),
                            Precast = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            Postcast = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture),
                            Duration = Double.Parse("0" + fields[i], CultureInfo.InvariantCulture)
                        };

                        string last = fields[fields.Length - 1];
                        double v;
                        if(!Double.TryParse(last, out v)) gods[god].Extra = last;
                    }
                }
            }

            return gods;
        }
Ejemplo n.º 18
0
        private static void TransactionsFromCSVFile(FileInfo fileInfo, out List<FineAntsCore.Transaction> transactions)
        {
            transactions = new List<FineAntsCore.Transaction>();

            TextFieldParser parser = new TextFieldParser(fileInfo.FullName);

            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");

            // Skip the first line, as it is just the headers.
            parser.ReadLine();

            while (!parser.EndOfData)
            {
                string[] fields = parser.ReadFields();

                // Generate a transaction from the line.
                FineAntsCore.Transaction transaction = TransactionFromCSVFields(fields);

                // Add it to the list.
                transactions.Add(transaction);
            }

            parser.Close();

            // Finally, sort the transactions on date, since they're in the wrong order in the CSV.
            transactions.Sort(new FineAntsCore.TransactionDateComparer());
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Ecnontra o csv da rodada em questão, pega o tempo médio do original e retorna
        /// </summary>
        /// <param name="biblioteca"></param>
        /// <param name="dirGa"></param>
        /// <param name="rodadaAlvo"></param>
        /// <param name="fitOrignal"></param>
        /// <returns></returns>
        private static string RecuperarTempoMedioeFitOriginal(DirectoryInfo biblioteca, DirectoryInfo dirGa, string rodadaAlvo, out string fitOrignal)
        {
            var fileList = dirGa.GetDirectories().First(d => d.Name.Equals(rodadaAlvo + "_Resultados" + biblioteca.Name)).GetFiles("resultados.csv", SearchOption.AllDirectories);
            var tempoOriginal = "0";
            fitOrignal = "0";

            var tempos = new List<TimeSpan>();
            var fits = new List<double>();

            if (fileList.Any())
            {
                using (var csv = new TextFieldParser(fileList.First().FullName))
                {
                    csv.ReadLine();
                    csv.ReadLine();

                    csv.TextFieldType = FieldType.Delimited;
                    csv.SetDelimiters(",");
                    csv.HasFieldsEnclosedInQuotes = true;

                    //Geracao,Individuo,Operacao,Fitness,Tempo,Testes
                    //nesse arquivo as 5 primeiras linhas são o orignal

                    for (int i = 0; i < 5; i++)
                    {
                        string[] currentRow = csv.ReadFields();

                        tempos.Add(TimeSpan.Parse(currentRow[4]));
                        fits.Add(Double.Parse(currentRow[3]));
                    }

                }
            }

            double mediaTempo = tempos.Average(t => t.Ticks);
            long longAverageTicks = Convert.ToInt64(mediaTempo);
            tempoOriginal = TimeSpan.FromTicks(longAverageTicks).ToString(@"hh\:mm\:ss\,ffff");

            fitOrignal = fits.Average().ToString();

            return tempoOriginal;
        }
Ejemplo n.º 20
0
        public static List<ItemStat> ParseItems(string file)
        {
            List<ItemStat> items = new List<ItemStat>();

            using(StringReader reader = new StringReader(client.DownloadString(file))) {
                using(TextFieldParser parser = new TextFieldParser(reader)) {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.ReadLine();

                    while(!parser.EndOfData) {
                        int i = 0;
                        string[] fields = parser.ReadFields();
                        if(fields == null) throw new ArgumentException("not a valid file", "file");

                        ItemStat item = new ItemStat();
                        item.Name = fields[i++];

                        if(item.Name == "") continue;

                        item.Cost = Double.Parse(fields[i++], CultureInfo.InvariantCulture);

                        string itemtype = fields[i++];
                        item.ItemType = (ItemType)Enum.Parse(typeof(ItemType), itemtype == "" ? "Both" : itemtype, true);
                        item.Health = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.PhysicalProtection = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.MagicalProtection = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.Power = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.AttackSpeed = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.PercentPenetration = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.FlatPenetration = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.CritChance = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.CooldownReduction = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.Lifesteal = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.Mana = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.Passive = fields[i++];
                        item.Hp5 = Double.Parse("0" + fields[i++], CultureInfo.InvariantCulture);
                        item.MoveSpeed = Double.Parse("0" + fields[i], CultureInfo.InvariantCulture);

                        items.Add(item);
                    }
                }
            }

            return items;
        }
Ejemplo n.º 21
0
        private static void TransactionsAndClosingBalanceFromCSVFile(FileInfo fileInfo, out List<FineAntsCore.Transaction> transactions, out int closingBalance)
        {
            transactions = new List<FineAntsCore.Transaction>();
            closingBalance = 0;

            TextFieldParser parser = new TextFieldParser(fileInfo.FullName);

            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");

            // Skip the first line, as it is just the headers.
            parser.ReadLine();

            while (!parser.EndOfData)
            {
                string[] fields = parser.ReadFields();

                // Generate a transaction from the line.
                FineAntsCore.Transaction transaction = TransactionFromCSVFields(fields);

                // Add it to the list.
                transactions.Add(transaction);

                // The 4th column holds the running total, and the file is sorted newest to oldest, so if this is the first line (after the headers, 1-based, and LineNumber is the line to be read next), store the balance as the closing balance.
                if (parser.LineNumber == 3)
                {
                    closingBalance = AmountFromString(fields[3]);
                }
            }

            parser.Close();

            // Finally, sort the transactions on date, since they're in the wrong order in the CSV.
            transactions.Sort(new FineAntsCore.TransactionDateComparer());
        }
Ejemplo n.º 22
0
        public FileContentResult CSV(HttpPostedFileBase file)
        {
            List<CSVResult> Results = new List<CSVResult>();

            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {

                using (TextFieldParser parser = new TextFieldParser(file.InputStream))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.HasFieldsEnclosedInQuotes = true;

                    //Skip first line
                    parser.ReadLine();

                    while (!parser.EndOfData)
                    {
                        //Column 5: Title
                        //Column 6: Author

                        CSVResult currentResult = new CSVResult();

                        string[] fields = parser.ReadFields();
                        currentResult.Title = fields[4];
                        currentResult.Author = fields[5];

                        Results.Add(currentResult);

                        if (Results.Count > 150) break;
                    }
                }

                //Do parallels awesomeness here
                Parallel.ForEach(Results, r =>
                {
                    try
                    {
                        r.Locations = OclcHelper.GetLocations(r.Author, r.Title);
                    }
                    catch (Exception ie)
                    {
                        //Handle the errors
                        r.ErrorMessage = ie.ToString();
                    }
                });

                string csvContents = "Auther,Title,Location,Error Message\n";
                for (int i = 0; i < Results.Count; i++)
                {
                    var item = Results[i];
                    var locationList = item.Locations.ToList();

                    if (locationList.Count == 0)
                    {
                        csvContents += "\""+item.Author + "\",\"" + item.Title + "\",,\"" + item.ErrorMessage + "\"\n";
                    }

                    for (int j = 0; j < locationList.Count; j++)
                    {
                        csvContents += "\"" + item.Author + "\",\"" + item.Title + "\",\"" + locationList[j].name.ToString().Replace("\"","'")+"\",\"" + item.ErrorMessage + "\"\n";
                    }
                }

                return File(new System.Text.UTF8Encoding().GetBytes(csvContents), "text/csv", "BookResults.csv");

            }

            return File(new System.Text.UTF8Encoding().GetBytes(""), "text/csv", "BadFileInput.csv");
        }
Ejemplo n.º 23
0
        // Parses the traffic csv file, and initializes all of the
        // device events.
        private static void ParseTrafficFile(string filePath)
        {
            TextFieldParser parser = new TextFieldParser(filePath);
            parser.SetDelimiters(",");

            // Skip over header line.
            parser.ReadLine();

            int i = 0;
            while (!parser.EndOfData)
            {

                string[] fields = parser.ReadFields();

                // allocate the memory for the event and add it to the list.
                Event eventToAdd = new Event(
                                            ulong.Parse(fields[(int)FieldTypes.TIME]),
                                            fields[(int)FieldTypes.OPERATION],
                                            int.Parse(fields[(int)FieldTypes.DEVICE]),
                                            int.Parse(fields[(int)FieldTypes.TS]),
                                            int.Parse(fields[(int)FieldTypes.TR_DATA_TAG]),
                                            i
                                            );
                _listOfEvents.Add(eventToAdd);
                i++;
            }
            numEvents = i;
        }
Ejemplo n.º 24
0
        public static List <CompressorInputLine> Create(Stream inputFile)
        {
            var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(inputFile);

            parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
            parser.Delimiters    = new string[] { "\t" };

            //skip the first line, it's header info
            parser.ReadLine();

            var inputLines = new List <CompressorInputLine>();

            while (!parser.EndOfData)
            {
                string[] tokens = parser.ReadFields();

                //column order from the most recent CSV file we've seen
                //Id Asset Name Local Timestamp UTC Milliseconds Compressor Oil Pressure Engine Oil Pressure Fuel Pressure Stage 1 Discharge Pressure  Stage 2 Discharge Pressure  Stage 3 Discharge Pressure  Suction Pressure    Max Discharge Pressure Max Suction Pressure    Compressor Oil Temp Cylinder 1 Discharge Temp   Cylinder 2 Discharge Temp   Cylinder 3 Discharge Temp   Cylinder 4 Discharge Temp   Engine Oil Temp Suction Temp RPM Max RPMs    Run Status  SD Status Code Runtime Hrs Downtime Hrs Yest   Gas Flow Rate Gas Flow Rate_RAW   Max Gas Flowrate Compressor Stages Horsepower  Unit Size   Last Successful Comm Time   Pct Successful Msgs Today   Successful Msgs Today Facility Desc Facility ID TOW Comp Name
                //Successful Msgs Today Facility Desc Facility ID TOW Comp Name


                inputLines.Add(new CompressorInputLine()
                {
                    Id                      = parseInt(tokens[0]),
                    AssetName               = tokens[1],
                    LocalTimestamp          = parseDate(tokens[2]),
                    UTCMilliseconds         = parseInt(tokens[3]),
                    CompressorOilPressure   = parseDouble(tokens[4]),
                    CompressorOilTemp       = parseDouble(tokens[5]),
                    CompressorStages        = parseInt(tokens[6]),
                    Cylinder1DischargeTemp  = parseDouble(tokens[7]),
                    Cylinder2DischargeTemp  = parseDouble(tokens[8]),
                    Cylinder3DischargeTemp  = parseDouble(tokens[9]),
                    Cylinder4DischargeTemp  = parseDouble(tokens[10]),
                    DowntimeHrsYest         = parseDouble(tokens[11]),
                    EngineOilPressure       = parseDouble(tokens[12]),
                    EngineOilTemp           = parseDouble(tokens[13]),
                    FacilityDesc            = tokens[14],
                    FacilityId              = tokens[15],
                    FuelPressure            = parseDouble(tokens[16]),
                    GasFlowRate             = parseDouble(tokens[17]),
                    GasFlowRate_RAW         = parseDouble(tokens[18]),
                    Horsepower              = parseDouble(tokens[19]),
                    LastSuccessfulCommTime  = parseDate(tokens[20]),
                    MaxDischargePressure    = parseDouble(tokens[21]),
                    MaxGasFlowrate          = parseDouble(tokens[22]),
                    MaxRPMs                 = parseDouble(tokens[23]),
                    MaxSuctionPressure      = parseDouble(tokens[24]),
                    PctSuccessfulMsgsToday  = parseDouble(tokens[25]),
                    RPM                     = parseDouble(tokens[26]),
                    RunStatus               = tokens[27],
                    RuntimeHrs              = parseDouble(tokens[28]),
                    SDStatusCode            = tokens[29],
                    Stage1DischargePressure = parseDouble(tokens[30]),
                    Stage2DischargePressure = parseDouble(tokens[31]),
                    Stage3DischargePressure = parseDouble(tokens[32]),
                    SuccessfulMsgsToday     = parseInt(tokens[33]),
                    SuctionPressure         = parseDouble(tokens[34]),
                    SuctionTemp             = parseDouble(tokens[35]),
                    TOWCompName             = tokens[36],
                    UnitSize                = tokens[37]
                });
            }

            return(inputLines);
        }
        public IList<Trade> Import(Stream stream, Market market, IList<Exception> exceps)
        {
            var trades = new List<Trade>();

            // need to convert stream first
            var CsvStream = new MemoryStream();
            DocumentConvererHelper.XlsToExt(stream, CsvStream, "csv", sheetIndex: 1);
            CsvStream.Position = 0;
            var parser = new TextFieldParser(CsvStream) { HasFieldsEnclosedInQuotes = true };
            parser.SetDelimiters(Separator);
            var emptyline = parser.ReadLine();   // skip first line
            var titles = parser.ReadFields();
            if (titles == null) return trades;
            //Build Positions
            var idx = 0;
            var headers = new Dictionary<string, int>();
            foreach (var s in titles) headers[s.ToLowerInvariant()] = idx++;

            var cultInfo = GetCultureInfo(_params);

            var asOfDate = SimpleDate.Today;
            if (_params != null && _params.ContainsKey("AsOfDate"))
            {
                var ss = _params["AsOfDate"];
                asOfDate = SimpleDate.Parse(ss);
            }
            while (!parser.EndOfData)
            {
                var items = parser.ReadFields();
                var trade = FromString(items, headers, exceps, market, cultInfo, asOfDate);
                if (trade != null) trades.Add(trade);

            }
            parser.Close();

            //Aggregate trades

            var allTrades = new List<Trade>();

            foreach (var tr in trades)
            {
                bool found = false;
                foreach (var vv in allTrades)
                {
                    if (vv.BookId != tr.BookId) continue;
                    var vvDpeDesc = vv.GetProperty(DPEDescription);
                    var trDpeDesc = tr.GetProperty(DPEDescription);
                    if (vv.Product.PricingType.Equals("Swap") || vv.Product.PricingType.Equals("MTMCurrencySwap")
                        || vv.Product.PricingType.Equals("FRA"))
                    {
                        if (vv.Product.Currency != tr.Product.Currency) continue;
                        if (vv.Product.ContractMaturity != tr.Product.ContractMaturity) continue;
                        var splits = vv.Product.Description.Split('(');
                        var osplits = tr.Product.Description.Split('(');
                        if (splits.Length > 0 && osplits.Length > 0)
                        {
                            if (splits[0] != osplits[0]) continue;
                        }
                        else continue;

                    }
                    else if (vv.Product is FXOption && vvDpeDesc != null && trDpeDesc != null)
                    {
                        if (!vvDpeDesc.Equals(trDpeDesc)) continue;
                    }
                    else if (vv.Product.Description != tr.Product.Description) continue;
                    vv.Quantity += tr.Quantity;
                    vv.SettleAmount += tr.SettleAmount;
                    if (vv.Product is SymmetryProduct && tr.Product is SymmetryProduct)
                    {
                        (tr.Product as SymmetryProduct).DetailQuantity += (vv.Product as SymmetryProduct).DetailQuantity;
                    }
                    if (vv.Product is FX)
                    {
                        var fx1 = vv.Product as FX;
                        var fx2 = tr.Product as FX;
                        fx1.PrimaryAmount += fx2.PrimaryAmount;
                        fx1.QuotingAmount += fx2.QuotingAmount;
                    }
                    found = true;
                    break;

                }
                if (!found) allTrades.Add(tr);
            }


            return allTrades;
        }
Ejemplo n.º 26
0
        private void saveBtn_Click(object sender, EventArgs e)
        {
            int i = typeComboBox.SelectedIndex;
            using (TextFieldParser csvParser = new TextFieldParser(inputFile.Text))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                // Skip the row if the first row has the column names
               
                if (headerComboBox.Text == "Yes")
                {
                    csvParser.ReadLine();
                }
                inserted = 0;
                failed = 0;
                while (!csvParser.EndOfData)
                {
                    // Read current line fields, pointer moves to the next line.
                    string[] fields = csvParser.ReadFields();
                    if (fields.Length == 1) break; // real record should have more than one column
                    int count = 0;
                    parkIDValidated = false;
                    foreach (string field in fields)
                    {
                        if (field == "") { fields[count] = null; }
                        count = count + 1;
                    }
                    switch (i)
                    {
                        case typeIsOwner:
                            ImportOwner(ref fields);
                            break;
                        case typeIsPark:
                            ImportPark(ref fields);
                            break;
                        case typeIsSpaceAndTenant:
                            ImportSpaceAndTenant(ref fields);
                            break;
                        case typeIsBilling:
                            ImportBilling(ref fields);
                            break;
                        default:
                            break;
                    }
                   }
                string s = "File imported with " + inserted.ToString() + " records! ";
                if (failed > 0)
                {
                    s= s + " and " + failed.ToString() + " records failed!";
                }
                MessageBox.Show(s);
                inputFile.Text = "";
                typeComboBox.Text = "";
                headerComboBox.Text = "";
                //parkLabel.Visible = false;
                //parkList.Visible = false;
            }
        }
Ejemplo n.º 27
0
 static IEnumerable<ProcessMonitorEntry> Parse(string path, bool header = true)
 {
     var parser = new TextFieldParser(path);
     parser.TextFieldType = FieldType.Delimited;
     parser.SetDelimiters(",");
     if (header)
     {
         parser.ReadLine();
     }
     while (!parser.EndOfData)
     {
         var fields = parser.ReadFields();
         yield return new ProcessMonitorEntry
         {
             //TimeOfDay = fields[0],
             //ProcessName = fields[1],
             //PID = fields[2],
             //Operation = fields[3],
             Path = fields[4],
             Result = fields[5],
             //Details = fields[6]
         };
     }
     parser.Close();
 }
Ejemplo n.º 28
0
        private void StartBenchmark(string args, string path)
        {
            StartGame(args, path);

            // Parse the csv file generated by TF2.

            var results = new FileInfo(path + @"\tf\sourcebench.csv");
            if (!results.Exists)
            {
                WorkerThread.ReportProgress(0, "Benchmark results file not found.");
                return;
            }

            using (var parser = new TextFieldParser(results.FullName))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                parser.HasFieldsEnclosedInQuotes = false;
                parser.TrimWhiteSpace = true;

                int lineCount = File.ReadLines(results.FullName).Count();

                // Skip the first header line of the file.
                parser.ReadLine();

                while (parser.PeekChars(1) != null)
                {
                    var li = new ListViewItem();

                    // Read every other line if benchmarking twice per command.
                    if (cb_runtwice.Checked && lineCount > 2 && parser.LineNumber % 2 == 0)
                    {
                        parser.ReadLine();
                        continue;
                    }

                    string[] row = parser.ReadFields();
                    for (int i = 0; i < row.Length; i++)
                    {
                        switch(i)
                        {
                            case 0:
                                li.Text = row[i];
                                break;
                            case 1: case 2: case 3: case 4: case 12: case 23:
                                li.SubItems.Add(row[i]);
                                break;
                        }
                    }

                    // Add the results to the listview.
                    if (lv_results.InvokeRequired)
                    {
                        lv_results.Invoke(new MethodInvoker(delegate
                        {
                            lv_results.Items.Add(li);
                        }));
                    }
                    else
                        lv_results.Items.Add(li);
                }
            }
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Lê o csv e retorna uma lista com as rodadas
        /// </summary>
        /// <param name="instanceFile"></param>
        /// <param name="biblioteca"></param>
        /// <param name="dirGa"></param>
        private static List<RodadaMapper> RecuperarRodadasDoGaNoCsv(FileInfo instanceFile, DirectoryInfo biblioteca, DirectoryInfo dirGa)
        {
            if (!File.Exists(biblioteca.GetFiles().First().FullName))
            {
                Console.WriteLine("Deveria existir o arquivo original da biblioteca aqui. | {0}", dirGa.FullName);
                Environment.Exit(-1);
            }

            //Ler as rodadas
            var rodadas = new List<RodadaMapper>();

            using (var csv = new TextFieldParser(instanceFile.FullName))
            {
                csv.ReadLine();
                csv.ReadLine();

                csv.TextFieldType = FieldType.Delimited;
                csv.SetDelimiters(",");
                csv.HasFieldsEnclosedInQuotes = true;

                //Conta das linhas do original
                var totalLinhas = ContarLinhas(biblioteca.GetFiles().First().FullName);
                //Conta Tokens do Original
                var totalchars = GetNumOfCharsInFile(biblioteca.GetFiles().First().FullName);
                //Texto do Original
                var textoOriginal = File.ReadAllText(biblioteca.GetFiles().First().FullName);

                while (!csv.EndOfData)
                {
                    string[] currentRow = csv.ReadFields();
                    string bestFile = Path.GetFileName(currentRow[1]);
                    int totalLinhasBest = 0;
                    int totalcharsBest = 0;
                    string tempoOriginal = "00:00:00,000";
                    string fitOrignal = "00000";
                    string tempoFinalComUnload = "";
                    string fitFinal = "";
                    DiffPaneModel resultadoComparacao = null;

                    //Tempo e fit originais
                    tempoOriginal = RecuperarTempoMedioeFitOriginal(biblioteca, dirGa, currentRow[0], out fitOrignal);

                    var fileList = dirGa.GetFiles(bestFile, SearchOption.AllDirectories);
                    //Se o arquivo melhorado existe, conta as linhas e os caracteres do mesmo

                    if (fileList.Any())
                    {
                        totalLinhasBest = ContarLinhas(fileList.First().FullName);
                        totalcharsBest = GetNumOfCharsInFile(fileList.First().FullName);
                        tempoFinalComUnload = currentRow[4];
                        fitFinal = currentRow[3];

                        var textoMelhor = File.ReadAllText(fileList.First().FullName);

                        var d = new Differ();
                        var builder = new InlineDiffBuilder(d);
                        resultadoComparacao = builder.BuildDiffModel(textoOriginal, textoMelhor);
                    }
                    else
                    {
                        //Não houve melhor
                        totalLinhasBest = totalLinhas;
                        totalcharsBest = totalchars;
                        tempoFinalComUnload = tempoOriginal;
                        fitFinal = fitOrignal;
                    }

                    rodadas.Add(new RodadaMapper()
                        {
                            Algoritmo = dirGa.Name,
                            Biblioteca = biblioteca.Name,
                            Rodada = currentRow[0],
                            Individuo = currentRow[1],
                            Operacao = currentRow[2],
                            Fitness = fitOrignal,
                            FitnessFinal = fitFinal,
                            TempoOriginalComUnload = tempoOriginal,
                            TempoFinalComUnload = tempoFinalComUnload,
                            Testes = currentRow[5],
                            LocOriginal = totalLinhas,
                            LocFinal = totalLinhasBest,
                            CaracteresOriginal = totalchars,
                            CaracteresFinal = totalcharsBest,
                            Diferencas = resultadoComparacao
                        });
                }
            }

            return rodadas;
        }
Ejemplo n.º 30
0
        private void openButton_Click(object sender, EventArgs e)
        {
            List<string[]> parsedData = new List<string[]>();
            try
            {

                // Detect the path of documents folder and assign it to initial directory path
                String myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFile.InitialDirectory = myDocument;

                if (openFile.ShowDialog() == DialogResult.OK)
                {

                    Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(openFile.FileName);
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.TrimWhiteSpace = true;
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();

                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        parsedData.Add(fields);
                        Trace.WriteLine(parsedData);
                        /*foreach (string field in fields)
                        {
                            parsedData.Add(field.ToString());
                            Trace.WriteLine(field);

                        }
                        */

                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }